Skip to content

Commit 0f74758

Browse files
committed
internal: move outlined parser tests
1 parent f4cb0ff commit 0f74758

File tree

353 files changed

+13703
-10
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

353 files changed

+13703
-10
lines changed

crates/parser/src/tests.rs

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ use expect_test::expect_file;
99
use crate::LexedStr;
1010

1111
#[test]
12-
fn valid_lexes_input() {
12+
fn lex_valid() {
1313
for case in TestCase::list("lexer/ok") {
1414
let actual = lex(&case.text);
1515
expect_file![case.txt].assert_eq(&actual)
1616
}
1717
}
1818

1919
#[test]
20-
fn invalid_lexes_input() {
20+
fn lex_invalid() {
2121
for case in TestCase::list("lexer/err") {
2222
let actual = lex(&case.text);
2323
expect_file![case.txt].assert_eq(&actual)
@@ -39,6 +39,61 @@ fn lex(text: &str) -> String {
3939
res
4040
}
4141

42+
#[test]
43+
fn parse_valid() {
44+
for case in TestCase::list("parser/ok") {
45+
let (actual, errors) = parse(&case.text);
46+
assert!(!errors, "errors in an OK file {}:\n{}", case.rs.display(), actual);
47+
expect_file![case.txt].assert_eq(&actual);
48+
}
49+
}
50+
51+
#[test]
52+
fn parse_invalid() {
53+
for case in TestCase::list("parser/err") {
54+
let (actual, errors) = parse(&case.text);
55+
assert!(errors, "no errors in an ERR file {}:\n{}", case.rs.display(), actual);
56+
expect_file![case.txt].assert_eq(&actual)
57+
}
58+
}
59+
60+
fn parse(text: &str) -> (String, bool) {
61+
let lexed = LexedStr::new(text);
62+
let input = lexed.to_input();
63+
let output = crate::parse_source_file(&input);
64+
65+
let mut buf = String::new();
66+
let mut errors = Vec::new();
67+
let mut indent = String::new();
68+
lexed.intersperse_trivia(&output, false, &mut |step| match step {
69+
crate::StrStep::Token { kind, text } => {
70+
write!(buf, "{}", indent).unwrap();
71+
write!(buf, "{:?} {:?}\n", kind, text).unwrap();
72+
}
73+
crate::StrStep::Enter { kind } => {
74+
write!(buf, "{}", indent).unwrap();
75+
write!(buf, "{:?}\n", kind).unwrap();
76+
indent.push_str(" ");
77+
}
78+
crate::StrStep::Exit => {
79+
indent.pop();
80+
indent.pop();
81+
}
82+
crate::StrStep::Error { msg, pos } => errors.push(format!("error {}: {}\n", pos, msg)),
83+
});
84+
85+
for (token, msg) in lexed.errors() {
86+
let pos = lexed.text_start(token);
87+
errors.push(format!("error {}: {}\n", pos, msg));
88+
}
89+
90+
let has_errors = !errors.is_empty();
91+
for e in errors {
92+
buf.push_str(&e);
93+
}
94+
(buf, has_errors)
95+
}
96+
4297
#[derive(PartialEq, Eq, PartialOrd, Ord)]
4398
struct TestCase {
4499
rs: PathBuf,
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
SOURCE_FILE
2+
STRUCT
3+
STRUCT_KW "struct"
4+
WHITESPACE " "
5+
NAME
6+
IDENT "S"
7+
WHITESPACE " "
8+
RECORD_FIELD_LIST
9+
L_CURLY "{"
10+
WHITESPACE "\n "
11+
RECORD_FIELD
12+
NAME
13+
IDENT "a"
14+
COLON ":"
15+
WHITESPACE " "
16+
PATH_TYPE
17+
PATH
18+
PATH_SEGMENT
19+
NAME_REF
20+
IDENT "u32"
21+
WHITESPACE "\n "
22+
RECORD_FIELD
23+
NAME
24+
IDENT "b"
25+
COLON ":"
26+
WHITESPACE " "
27+
PATH_TYPE
28+
PATH
29+
PATH_SEGMENT
30+
NAME_REF
31+
IDENT "u32"
32+
WHITESPACE "\n"
33+
R_CURLY "}"
34+
error 21: expected COMMA
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
SOURCE_FILE
2+
ERROR
3+
IF_KW "if"
4+
WHITESPACE " "
5+
ERROR
6+
MATCH_KW "match"
7+
WHITESPACE "\n\n"
8+
STRUCT
9+
STRUCT_KW "struct"
10+
WHITESPACE " "
11+
NAME
12+
IDENT "S"
13+
WHITESPACE " "
14+
RECORD_FIELD_LIST
15+
L_CURLY "{"
16+
R_CURLY "}"
17+
error 0: expected an item
18+
error 3: expected an item
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
SOURCE_FILE
2+
SHEBANG "#!/use/bin/env rusti"
3+
WHITESPACE "\n"
4+
ATTR
5+
POUND "#"
6+
BANG "!"
7+
ERROR
8+
SLASH "/"
9+
USE
10+
USE_KW "use"
11+
ERROR
12+
SLASH "/"
13+
MACRO_CALL
14+
PATH
15+
PATH_SEGMENT
16+
NAME_REF
17+
IDENT "bin"
18+
ERROR
19+
SLASH "/"
20+
MACRO_CALL
21+
PATH
22+
PATH_SEGMENT
23+
NAME_REF
24+
IDENT "env"
25+
WHITESPACE " "
26+
MACRO_CALL
27+
PATH
28+
PATH_SEGMENT
29+
NAME_REF
30+
IDENT "rusti"
31+
WHITESPACE "\n"
32+
error 23: expected `[`
33+
error 23: expected an item
34+
error 27: expected one of `*`, `::`, `{`, `self`, `super` or an identifier
35+
error 28: expected SEMICOLON
36+
error 31: expected BANG
37+
error 31: expected `{`, `[`, `(`
38+
error 31: expected SEMICOLON
39+
error 31: expected an item
40+
error 35: expected BANG
41+
error 35: expected `{`, `[`, `(`
42+
error 35: expected SEMICOLON
43+
error 41: expected BANG
44+
error 41: expected `{`, `[`, `(`
45+
error 41: expected SEMICOLON

0 commit comments

Comments
 (0)