From f2ea7853ee9b2b44464b93a6008ce8e1e39e2127 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 2 Jan 2022 15:15:04 +0300 Subject: [PATCH] start top-level entry point tests --- crates/parser/src/tests.rs | 14 +++++++- crates/parser/src/tests/entries.rs | 58 +++++++++++++++++++++++++++++- 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/crates/parser/src/tests.rs b/crates/parser/src/tests.rs index 157c381f4b2..73bf472f386 100644 --- a/crates/parser/src/tests.rs +++ b/crates/parser/src/tests.rs @@ -86,22 +86,34 @@ fn parse(entry: TopEntryPoint, text: &str) -> (String, bool) { 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); diff --git a/crates/parser/src/tests/entries.rs b/crates/parser/src/tests/entries.rs index d52c6fbb16f..ea8ff415029 100644 --- a/crates/parser/src/tests/entries.rs +++ b/crates/parser/src/tests/entries.rs @@ -1,4 +1,6 @@ -use crate::{LexedStr, PrefixEntryPoint, Step}; +use expect_test::expect; + +use crate::{LexedStr, PrefixEntryPoint, Step, TopEntryPoint}; #[test] fn vis() { @@ -83,6 +85,7 @@ fn meta_item() { check_prefix(PrefixEntryPoint::MetaItem, "path::attr = 2 * 2!", "path::attr = 2 * 2"); } +#[track_caller] fn check_prefix(entry: PrefixEntryPoint, input: &str, prefix: &str) { let lexed = LexedStr::new(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)]; 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) +}