start top-level entry point tests

This commit is contained in:
Aleksey Kladov 2022-01-02 15:15:04 +03:00
parent e366b3c730
commit f2ea7853ee
2 changed files with 70 additions and 2 deletions

View file

@ -86,22 +86,34 @@ fn parse(entry: TopEntryPoint, text: &str) -> (String, bool) {
let mut buf = String::new(); let mut buf = String::new();
let mut errors = Vec::new(); let mut errors = Vec::new();
let mut indent = String::new(); let mut indent = String::new();
let mut depth = 0;
let mut len = 0;
lexed.intersperse_trivia(&output, &mut |step| match step { lexed.intersperse_trivia(&output, &mut |step| match step {
crate::StrStep::Token { kind, text } => { crate::StrStep::Token { kind, text } => {
assert!(depth > 0);
len += text.len();
write!(buf, "{}", indent).unwrap(); write!(buf, "{}", indent).unwrap();
write!(buf, "{:?} {:?}\n", kind, text).unwrap(); write!(buf, "{:?} {:?}\n", kind, text).unwrap();
} }
crate::StrStep::Enter { kind } => { crate::StrStep::Enter { kind } => {
assert!(depth > 0 || len == 0);
depth += 1;
write!(buf, "{}", indent).unwrap(); write!(buf, "{}", indent).unwrap();
write!(buf, "{:?}\n", kind).unwrap(); write!(buf, "{:?}\n", kind).unwrap();
indent.push_str(" "); indent.push_str(" ");
} }
crate::StrStep::Exit => { crate::StrStep::Exit => {
assert!(depth > 0);
depth -= 1;
indent.pop(); indent.pop();
indent.pop(); indent.pop();
} }
crate::StrStep::Error { msg, pos } => errors.push(format!("error {}: {}\n", pos, msg)), crate::StrStep::Error { msg, pos } => {
assert!(depth > 0);
errors.push(format!("error {}: {}\n", pos, msg))
}
}); });
assert_eq!(len, text.len());
for (token, msg) in lexed.errors() { for (token, msg) in lexed.errors() {
let pos = lexed.text_start(token); let pos = lexed.text_start(token);

View file

@ -1,4 +1,6 @@
use crate::{LexedStr, PrefixEntryPoint, Step}; use expect_test::expect;
use crate::{LexedStr, PrefixEntryPoint, Step, TopEntryPoint};
#[test] #[test]
fn vis() { fn vis() {
@ -83,6 +85,7 @@ fn meta_item() {
check_prefix(PrefixEntryPoint::MetaItem, "path::attr = 2 * 2!", "path::attr = 2 * 2"); check_prefix(PrefixEntryPoint::MetaItem, "path::attr = 2 * 2!", "path::attr = 2 * 2");
} }
#[track_caller]
fn check_prefix(entry: PrefixEntryPoint, input: &str, prefix: &str) { fn check_prefix(entry: PrefixEntryPoint, input: &str, prefix: &str) {
let lexed = LexedStr::new(input); let lexed = LexedStr::new(input);
let input = lexed.to_input(); let input = lexed.to_input();
@ -108,3 +111,56 @@ fn check_prefix(entry: PrefixEntryPoint, input: &str, prefix: &str) {
let buf = &lexed.as_str()[..lexed.text_start(i)]; let buf = &lexed.as_str()[..lexed.text_start(i)];
assert_eq!(buf, prefix); assert_eq!(buf, prefix);
} }
#[test]
fn source_file() {
check_top(
TopEntryPoint::SourceFile,
"",
expect![[r#"
SOURCE_FILE
"#]],
);
check_top(
TopEntryPoint::SourceFile,
"struct S;",
expect![[r#"
SOURCE_FILE
STRUCT
STRUCT_KW "struct"
WHITESPACE " "
NAME
IDENT "S"
SEMICOLON ";"
"#]],
);
check_top(
TopEntryPoint::SourceFile,
"@error@",
expect![[r#"
SOURCE_FILE
ERROR
AT "@"
MACRO_CALL
PATH
PATH_SEGMENT
NAME_REF
IDENT "error"
ERROR
AT "@"
error 0: expected an item
error 6: expected BANG
error 6: expected `{`, `[`, `(`
error 6: expected SEMICOLON
error 6: expected an item
"#]],
);
}
#[track_caller]
fn check_top(entry: TopEntryPoint, input: &str, expect: expect_test::Expect) {
let (parsed, _errors) = super::parse(entry, input);
expect.assert_eq(&parsed)
}