Skip to content

Commit 25dd65d

Browse files
bors[bot]matklad
andauthored
Merge #11163
11163: internal: start tests for top-level parser entry points r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2 parents d783226 + bebfb83 commit 25dd65d

File tree

4 files changed

+228
-119
lines changed

4 files changed

+228
-119
lines changed

crates/parser/src/tests.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
mod sourcegen_inline_tests;
2-
mod entries;
2+
mod prefix_entries;
3+
mod top_entries;
34

45
use std::{
56
fmt::Write,
@@ -9,7 +10,7 @@ use std::{
910

1011
use expect_test::expect_file;
1112

12-
use crate::LexedStr;
13+
use crate::{LexedStr, TopEntryPoint};
1314

1415
#[test]
1516
fn lex_ok() {
@@ -45,7 +46,7 @@ fn lex(text: &str) -> String {
4546
#[test]
4647
fn parse_ok() {
4748
for case in TestCase::list("parser/ok") {
48-
let (actual, errors) = parse(&case.text);
49+
let (actual, errors) = parse(TopEntryPoint::SourceFile, &case.text);
4950
assert!(!errors, "errors in an OK file {}:\n{}", case.rs.display(), actual);
5051
expect_file![case.txt].assert_eq(&actual);
5152
}
@@ -54,7 +55,7 @@ fn parse_ok() {
5455
#[test]
5556
fn parse_inline_ok() {
5657
for case in TestCase::list("parser/inline/ok") {
57-
let (actual, errors) = parse(&case.text);
58+
let (actual, errors) = parse(TopEntryPoint::SourceFile, &case.text);
5859
assert!(!errors, "errors in an OK file {}:\n{}", case.rs.display(), actual);
5960
expect_file![case.txt].assert_eq(&actual);
6061
}
@@ -63,7 +64,7 @@ fn parse_inline_ok() {
6364
#[test]
6465
fn parse_err() {
6566
for case in TestCase::list("parser/err") {
66-
let (actual, errors) = parse(&case.text);
67+
let (actual, errors) = parse(TopEntryPoint::SourceFile, &case.text);
6768
assert!(errors, "no errors in an ERR file {}:\n{}", case.rs.display(), actual);
6869
expect_file![case.txt].assert_eq(&actual)
6970
}
@@ -72,36 +73,48 @@ fn parse_err() {
7273
#[test]
7374
fn parse_inline_err() {
7475
for case in TestCase::list("parser/inline/err") {
75-
let (actual, errors) = parse(&case.text);
76+
let (actual, errors) = parse(TopEntryPoint::SourceFile, &case.text);
7677
assert!(errors, "no errors in an ERR file {}:\n{}", case.rs.display(), actual);
7778
expect_file![case.txt].assert_eq(&actual)
7879
}
7980
}
8081

81-
fn parse(text: &str) -> (String, bool) {
82+
fn parse(entry: TopEntryPoint, text: &str) -> (String, bool) {
8283
let lexed = LexedStr::new(text);
8384
let input = lexed.to_input();
84-
let output = crate::TopEntryPoint::SourceFile.parse(&input);
85+
let output = entry.parse(&input);
8586

8687
let mut buf = String::new();
8788
let mut errors = Vec::new();
8889
let mut indent = String::new();
90+
let mut depth = 0;
91+
let mut len = 0;
8992
lexed.intersperse_trivia(&output, &mut |step| match step {
9093
crate::StrStep::Token { kind, text } => {
94+
assert!(depth > 0);
95+
len += text.len();
9196
write!(buf, "{}", indent).unwrap();
9297
write!(buf, "{:?} {:?}\n", kind, text).unwrap();
9398
}
9499
crate::StrStep::Enter { kind } => {
100+
assert!(depth > 0 || len == 0);
101+
depth += 1;
95102
write!(buf, "{}", indent).unwrap();
96103
write!(buf, "{:?}\n", kind).unwrap();
97104
indent.push_str(" ");
98105
}
99106
crate::StrStep::Exit => {
107+
assert!(depth > 0);
108+
depth -= 1;
100109
indent.pop();
101110
indent.pop();
102111
}
103-
crate::StrStep::Error { msg, pos } => errors.push(format!("error {}: {}\n", pos, msg)),
112+
crate::StrStep::Error { msg, pos } => {
113+
assert!(depth > 0);
114+
errors.push(format!("error {}: {}\n", pos, msg))
115+
}
104116
});
117+
assert_eq!(len, text.len());
105118

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

crates/parser/src/tests/entries.rs

Lines changed: 0 additions & 110 deletions
This file was deleted.
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
use crate::{LexedStr, PrefixEntryPoint, Step};
2+
3+
#[test]
4+
fn vis() {
5+
check(PrefixEntryPoint::Vis, "pub(crate) fn foo() {}", "pub(crate)");
6+
check(PrefixEntryPoint::Vis, "fn foo() {}", "");
7+
check(PrefixEntryPoint::Vis, "pub(fn foo() {}", "pub");
8+
check(PrefixEntryPoint::Vis, "pub(crate fn foo() {}", "pub(crate");
9+
check(PrefixEntryPoint::Vis, "crate fn foo() {}", "crate");
10+
}
11+
12+
#[test]
13+
fn block() {
14+
check(PrefixEntryPoint::Block, "{}, 92", "{}");
15+
check(PrefixEntryPoint::Block, "{, 92)", "{, 92)");
16+
check(PrefixEntryPoint::Block, "()", "");
17+
}
18+
19+
#[test]
20+
fn stmt() {
21+
check(PrefixEntryPoint::Stmt, "92; fn", "92");
22+
check(PrefixEntryPoint::Stmt, "let _ = 92; 1", "let _ = 92");
23+
check(PrefixEntryPoint::Stmt, "pub fn f() {} = 92", "pub fn f() {}");
24+
check(PrefixEntryPoint::Stmt, "struct S;;", "struct S;");
25+
check(PrefixEntryPoint::Stmt, "fn f() {};", "fn f() {}");
26+
check(PrefixEntryPoint::Stmt, ";;;", ";");
27+
check(PrefixEntryPoint::Stmt, "+", "+");
28+
check(PrefixEntryPoint::Stmt, "@", "@");
29+
check(PrefixEntryPoint::Stmt, "loop {} - 1", "loop {}");
30+
}
31+
32+
#[test]
33+
fn pat() {
34+
check(PrefixEntryPoint::Pat, "x y", "x");
35+
check(PrefixEntryPoint::Pat, "fn f() {}", "fn");
36+
// FIXME: This one is wrong, we should consume only one pattern.
37+
check(PrefixEntryPoint::Pat, ".. ..", ".. ..");
38+
}
39+
40+
#[test]
41+
fn ty() {
42+
check(PrefixEntryPoint::Ty, "fn() foo", "fn()");
43+
check(PrefixEntryPoint::Ty, "Clone + Copy + fn", "Clone + Copy +");
44+
check(PrefixEntryPoint::Ty, "struct f", "struct");
45+
}
46+
47+
#[test]
48+
fn expr() {
49+
check(PrefixEntryPoint::Expr, "92 92", "92");
50+
check(PrefixEntryPoint::Expr, "+1", "+");
51+
check(PrefixEntryPoint::Expr, "-1", "-1");
52+
check(PrefixEntryPoint::Expr, "fn foo() {}", "fn");
53+
check(PrefixEntryPoint::Expr, "#[attr] ()", "#[attr] ()");
54+
}
55+
56+
#[test]
57+
fn path() {
58+
check(PrefixEntryPoint::Path, "foo::bar baz", "foo::bar");
59+
check(PrefixEntryPoint::Path, "foo::<> baz", "foo::<>");
60+
check(PrefixEntryPoint::Path, "foo<> baz", "foo<>");
61+
check(PrefixEntryPoint::Path, "Fn() -> i32?", "Fn() -> i32");
62+
// FIXME: This shouldn't be accepted as path actually.
63+
check(PrefixEntryPoint::Path, "<_>::foo", "<_>::foo");
64+
}
65+
66+
#[test]
67+
fn item() {
68+
// FIXME: This shouldn't consume the semicolon.
69+
check(PrefixEntryPoint::Item, "fn foo() {};", "fn foo() {};");
70+
check(PrefixEntryPoint::Item, "#[attr] pub struct S {} 92", "#[attr] pub struct S {}");
71+
check(PrefixEntryPoint::Item, "item!{}?", "item!{}");
72+
check(PrefixEntryPoint::Item, "????", "?");
73+
}
74+
75+
#[test]
76+
fn meta_item() {
77+
check(PrefixEntryPoint::MetaItem, "attr, ", "attr");
78+
check(PrefixEntryPoint::MetaItem, "attr(some token {stream});", "attr(some token {stream})");
79+
check(PrefixEntryPoint::MetaItem, "path::attr = 2 * 2!", "path::attr = 2 * 2");
80+
}
81+
82+
#[track_caller]
83+
fn check(entry: PrefixEntryPoint, input: &str, prefix: &str) {
84+
let lexed = LexedStr::new(input);
85+
let input = lexed.to_input();
86+
87+
let mut n_tokens = 0;
88+
for step in entry.parse(&input).iter() {
89+
match step {
90+
Step::Token { n_input_tokens, .. } => n_tokens += n_input_tokens as usize,
91+
Step::Enter { .. } | Step::Exit | Step::Error { .. } => (),
92+
}
93+
}
94+
95+
let mut i = 0;
96+
loop {
97+
if n_tokens == 0 {
98+
break;
99+
}
100+
if !lexed.kind(i).is_trivia() {
101+
n_tokens -= 1;
102+
}
103+
i += 1;
104+
}
105+
let buf = &lexed.as_str()[..lexed.text_start(i)];
106+
assert_eq!(buf, prefix);
107+
}

0 commit comments

Comments
 (0)