Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/parser/src/tests.rs')
| -rw-r--r-- | crates/parser/src/tests.rs | 31 |
1 files changed, 22 insertions, 9 deletions
diff --git a/crates/parser/src/tests.rs b/crates/parser/src/tests.rs index fb4885e98d..52388dacc6 100644 --- a/crates/parser/src/tests.rs +++ b/crates/parser/src/tests.rs @@ -1,5 +1,6 @@ mod sourcegen_inline_tests; -mod entries; +mod prefix_entries; +mod top_entries; use std::{ fmt::Write, @@ -9,7 +10,7 @@ use std::{ use expect_test::expect_file; -use crate::LexedStr; +use crate::{LexedStr, TopEntryPoint}; #[test] fn lex_ok() { @@ -45,7 +46,7 @@ fn lex(text: &str) -> String { #[test] fn parse_ok() { for case in TestCase::list("parser/ok") { - let (actual, errors) = parse(&case.text); + let (actual, errors) = parse(TopEntryPoint::SourceFile, &case.text); assert!(!errors, "errors in an OK file {}:\n{}", case.rs.display(), actual); expect_file![case.txt].assert_eq(&actual); } @@ -54,7 +55,7 @@ fn parse_ok() { #[test] fn parse_inline_ok() { for case in TestCase::list("parser/inline/ok") { - let (actual, errors) = parse(&case.text); + let (actual, errors) = parse(TopEntryPoint::SourceFile, &case.text); assert!(!errors, "errors in an OK file {}:\n{}", case.rs.display(), actual); expect_file![case.txt].assert_eq(&actual); } @@ -63,7 +64,7 @@ fn parse_inline_ok() { #[test] fn parse_err() { for case in TestCase::list("parser/err") { - let (actual, errors) = parse(&case.text); + let (actual, errors) = parse(TopEntryPoint::SourceFile, &case.text); assert!(errors, "no errors in an ERR file {}:\n{}", case.rs.display(), actual); expect_file![case.txt].assert_eq(&actual) } @@ -72,36 +73,48 @@ fn parse_err() { #[test] fn parse_inline_err() { for case in TestCase::list("parser/inline/err") { - let (actual, errors) = parse(&case.text); + let (actual, errors) = parse(TopEntryPoint::SourceFile, &case.text); assert!(errors, "no errors in an ERR file {}:\n{}", case.rs.display(), actual); expect_file![case.txt].assert_eq(&actual) } } -fn parse(text: &str) -> (String, bool) { +fn parse(entry: TopEntryPoint, text: &str) -> (String, bool) { let lexed = LexedStr::new(text); let input = lexed.to_input(); - let output = crate::TopEntryPoint::SourceFile.parse(&input); + let output = entry.parse(&input); let mut buf = String::new(); let mut errors = Vec::new(); let mut indent = String::new(); + let mut depth = 0; + let mut len = 0; lexed.intersperse_trivia(&output, &mut |step| match step { crate::StrStep::Token { kind, text } => { + assert!(depth > 0); + len += text.len(); write!(buf, "{}", indent).unwrap(); write!(buf, "{:?} {:?}\n", kind, text).unwrap(); } crate::StrStep::Enter { kind } => { + assert!(depth > 0 || len == 0); + depth += 1; write!(buf, "{}", indent).unwrap(); write!(buf, "{:?}\n", kind).unwrap(); indent.push_str(" "); } crate::StrStep::Exit => { + assert!(depth > 0); + depth -= 1; 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() { let pos = lexed.text_start(token); |