Skip to content

Commit 3494d81

Browse files
bors[bot]matklad
andauthored
Merge #11120
11120: internal: move parser tests to parser crate r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents f2f5748 + 8e9734e commit 3494d81

File tree

987 files changed

+24847
-94
lines changed

Some content is hidden

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

987 files changed

+24847
-94
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/parser/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ limit = { path = "../limit", version = "0.0.0" }
1616

1717
[dev-dependencies]
1818
expect-test = "1.2"
19+
sourcegen = { path = "../sourcegen" }
20+

crates/parser/src/grammar/expressions.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -463,13 +463,6 @@ fn method_call_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker {
463463
// x.0.bar;
464464
// x.0();
465465
// }
466-
467-
// test_err bad_tuple_index_expr
468-
// fn foo() {
469-
// x.0.;
470-
// x.1i32;
471-
// x.0x01;
472-
// }
473466
fn field_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker {
474467
assert!(p.at(T![.]));
475468
let m = lhs.precede(p);

crates/parser/src/tests.rs

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
mod sourcegen_inline_tests;
2+
13
use std::{
24
fmt::Write,
35
fs,
@@ -9,15 +11,15 @@ use expect_test::expect_file;
911
use crate::LexedStr;
1012

1113
#[test]
12-
fn valid_lexes_input() {
14+
fn lex_ok() {
1315
for case in TestCase::list("lexer/ok") {
1416
let actual = lex(&case.text);
1517
expect_file![case.txt].assert_eq(&actual)
1618
}
1719
}
1820

1921
#[test]
20-
fn invalid_lexes_input() {
22+
fn lex_err() {
2123
for case in TestCase::list("lexer/err") {
2224
let actual = lex(&case.text);
2325
expect_file![case.txt].assert_eq(&actual)
@@ -39,6 +41,79 @@ fn lex(text: &str) -> String {
3941
res
4042
}
4143

44+
#[test]
45+
fn parse_ok() {
46+
for case in TestCase::list("parser/ok") {
47+
let (actual, errors) = parse(&case.text);
48+
assert!(!errors, "errors in an OK file {}:\n{}", case.rs.display(), actual);
49+
expect_file![case.txt].assert_eq(&actual);
50+
}
51+
}
52+
53+
#[test]
54+
fn parse_inline_ok() {
55+
for case in TestCase::list("parser/inline/ok") {
56+
let (actual, errors) = parse(&case.text);
57+
assert!(!errors, "errors in an OK file {}:\n{}", case.rs.display(), actual);
58+
expect_file![case.txt].assert_eq(&actual);
59+
}
60+
}
61+
62+
#[test]
63+
fn parse_err() {
64+
for case in TestCase::list("parser/err") {
65+
let (actual, errors) = parse(&case.text);
66+
assert!(errors, "no errors in an ERR file {}:\n{}", case.rs.display(), actual);
67+
expect_file![case.txt].assert_eq(&actual)
68+
}
69+
}
70+
71+
#[test]
72+
fn parse_inline_err() {
73+
for case in TestCase::list("parser/inline/err") {
74+
let (actual, errors) = parse(&case.text);
75+
assert!(errors, "no errors in an ERR file {}:\n{}", case.rs.display(), actual);
76+
expect_file![case.txt].assert_eq(&actual)
77+
}
78+
}
79+
80+
fn parse(text: &str) -> (String, bool) {
81+
let lexed = LexedStr::new(text);
82+
let input = lexed.to_input();
83+
let output = crate::parse_source_file(&input);
84+
85+
let mut buf = String::new();
86+
let mut errors = Vec::new();
87+
let mut indent = String::new();
88+
lexed.intersperse_trivia(&output, false, &mut |step| match step {
89+
crate::StrStep::Token { kind, text } => {
90+
write!(buf, "{}", indent).unwrap();
91+
write!(buf, "{:?} {:?}\n", kind, text).unwrap();
92+
}
93+
crate::StrStep::Enter { kind } => {
94+
write!(buf, "{}", indent).unwrap();
95+
write!(buf, "{:?}\n", kind).unwrap();
96+
indent.push_str(" ");
97+
}
98+
crate::StrStep::Exit => {
99+
indent.pop();
100+
indent.pop();
101+
}
102+
crate::StrStep::Error { msg, pos } => errors.push(format!("error {}: {}\n", pos, msg)),
103+
});
104+
105+
for (token, msg) in lexed.errors() {
106+
let pos = lexed.text_start(token);
107+
errors.push(format!("error {}: {}\n", pos, msg));
108+
}
109+
110+
let has_errors = !errors.is_empty();
111+
for e in errors {
112+
buf.push_str(&e);
113+
}
114+
(buf, has_errors)
115+
}
116+
42117
#[derive(PartialEq, Eq, PartialOrd, Ord)]
43118
struct TestCase {
44119
rs: PathBuf,

crates/syntax/src/tests/sourcegen_tests.rs renamed to crates/parser/src/tests/sourcegen_inline_tests.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,20 @@
22
//! them into tests.
33
44
use std::{
5+
collections::HashMap,
56
fs, iter,
67
path::{Path, PathBuf},
78
};
89

9-
use rustc_hash::FxHashMap;
10-
1110
#[test]
1211
fn sourcegen_parser_tests() {
1312
let grammar_dir = sourcegen::project_root().join(Path::new("crates/parser/src/grammar"));
1413
let tests = tests_from_dir(&grammar_dir);
1514

16-
install_tests(&tests.ok, "crates/syntax/test_data/parser/inline/ok");
17-
install_tests(&tests.err, "crates/syntax/test_data/parser/inline/err");
15+
install_tests(&tests.ok, "crates/parser/test_data/parser/inline/ok");
16+
install_tests(&tests.err, "crates/parser/test_data/parser/inline/err");
1817

19-
fn install_tests(tests: &FxHashMap<String, Test>, into: &str) {
18+
fn install_tests(tests: &HashMap<String, Test>, into: &str) {
2019
let tests_dir = sourcegen::project_root().join(into);
2120
if !tests_dir.is_dir() {
2221
fs::create_dir_all(&tests_dir).unwrap();
@@ -51,8 +50,8 @@ struct Test {
5150

5251
#[derive(Default, Debug)]
5352
struct Tests {
54-
ok: FxHashMap<String, Test>,
55-
err: FxHashMap<String, Test>,
53+
ok: HashMap<String, Test>,
54+
err: HashMap<String, Test>,
5655
}
5756

5857
fn collect_tests(s: &str) -> Vec<Test> {
@@ -102,8 +101,8 @@ fn tests_from_dir(dir: &Path) -> Tests {
102101
}
103102
}
104103

105-
fn existing_tests(dir: &Path, ok: bool) -> FxHashMap<String, (PathBuf, Test)> {
106-
let mut res = FxHashMap::default();
104+
fn existing_tests(dir: &Path, ok: bool) -> HashMap<String, (PathBuf, Test)> {
105+
let mut res = HashMap::default();
107106
for file in fs::read_dir(dir).unwrap() {
108107
let file = file.unwrap();
109108
let path = file.path();
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

0 commit comments

Comments
 (0)