Skip to content

Commit f2ea785

Browse files
committed
start top-level entry point tests
1 parent e366b3c commit f2ea785

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

crates/parser/src/tests.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,22 +86,34 @@ fn parse(entry: TopEntryPoint, text: &str) -> (String, bool) {
8686
let mut buf = String::new();
8787
let mut errors = Vec::new();
8888
let mut indent = String::new();
89+
let mut depth = 0;
90+
let mut len = 0;
8991
lexed.intersperse_trivia(&output, &mut |step| match step {
9092
crate::StrStep::Token { kind, text } => {
93+
assert!(depth > 0);
94+
len += text.len();
9195
write!(buf, "{}", indent).unwrap();
9296
write!(buf, "{:?} {:?}\n", kind, text).unwrap();
9397
}
9498
crate::StrStep::Enter { kind } => {
99+
assert!(depth > 0 || len == 0);
100+
depth += 1;
95101
write!(buf, "{}", indent).unwrap();
96102
write!(buf, "{:?}\n", kind).unwrap();
97103
indent.push_str(" ");
98104
}
99105
crate::StrStep::Exit => {
106+
assert!(depth > 0);
107+
depth -= 1;
100108
indent.pop();
101109
indent.pop();
102110
}
103-
crate::StrStep::Error { msg, pos } => errors.push(format!("error {}: {}\n", pos, msg)),
111+
crate::StrStep::Error { msg, pos } => {
112+
assert!(depth > 0);
113+
errors.push(format!("error {}: {}\n", pos, msg))
114+
}
104115
});
116+
assert_eq!(len, text.len());
105117

106118
for (token, msg) in lexed.errors() {
107119
let pos = lexed.text_start(token);

crates/parser/src/tests/entries.rs

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use crate::{LexedStr, PrefixEntryPoint, Step};
1+
use expect_test::expect;
2+
3+
use crate::{LexedStr, PrefixEntryPoint, Step, TopEntryPoint};
24

35
#[test]
46
fn vis() {
@@ -83,6 +85,7 @@ fn meta_item() {
8385
check_prefix(PrefixEntryPoint::MetaItem, "path::attr = 2 * 2!", "path::attr = 2 * 2");
8486
}
8587

88+
#[track_caller]
8689
fn check_prefix(entry: PrefixEntryPoint, input: &str, prefix: &str) {
8790
let lexed = LexedStr::new(input);
8891
let input = lexed.to_input();
@@ -108,3 +111,56 @@ fn check_prefix(entry: PrefixEntryPoint, input: &str, prefix: &str) {
108111
let buf = &lexed.as_str()[..lexed.text_start(i)];
109112
assert_eq!(buf, prefix);
110113
}
114+
115+
#[test]
116+
fn source_file() {
117+
check_top(
118+
TopEntryPoint::SourceFile,
119+
"",
120+
expect![[r#"
121+
SOURCE_FILE
122+
"#]],
123+
);
124+
125+
check_top(
126+
TopEntryPoint::SourceFile,
127+
"struct S;",
128+
expect![[r#"
129+
SOURCE_FILE
130+
STRUCT
131+
STRUCT_KW "struct"
132+
WHITESPACE " "
133+
NAME
134+
IDENT "S"
135+
SEMICOLON ";"
136+
"#]],
137+
);
138+
139+
check_top(
140+
TopEntryPoint::SourceFile,
141+
"@error@",
142+
expect![[r#"
143+
SOURCE_FILE
144+
ERROR
145+
AT "@"
146+
MACRO_CALL
147+
PATH
148+
PATH_SEGMENT
149+
NAME_REF
150+
IDENT "error"
151+
ERROR
152+
AT "@"
153+
error 0: expected an item
154+
error 6: expected BANG
155+
error 6: expected `{`, `[`, `(`
156+
error 6: expected SEMICOLON
157+
error 6: expected an item
158+
"#]],
159+
);
160+
}
161+
162+
#[track_caller]
163+
fn check_top(entry: TopEntryPoint, input: &str, expect: expect_test::Expect) {
164+
let (parsed, _errors) = super::parse(entry, input);
165+
expect.assert_eq(&parsed)
166+
}

0 commit comments

Comments
 (0)