about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAleksey Kladov <aleksey.kladov@gmail.com>2021-12-29 18:23:34 +0300
committerAleksey Kladov <aleksey.kladov@gmail.com>2021-12-29 18:28:11 +0300
commit3f5fc05d6675394f46b7cdeb6803d7c7045e16ab (patch)
tree43388f7c1ac9de63097ae9808765b6c0f857399f
parent355a4bdb883ee9edc1c32b553fcf91c302b3df19 (diff)
downloadrust-3f5fc05d6675394f46b7cdeb6803d7c7045e16ab.tar.gz
rust-3f5fc05d6675394f46b7cdeb6803d7c7045e16ab.zip
internal: add tests for extra parser entry points
-rw-r--r--crates/parser/src/output.rs1
-rw-r--r--crates/parser/src/shortcuts.rs4
-rw-r--r--crates/parser/src/tests.rs1
-rw-r--r--crates/parser/src/tests/entries.rs41
4 files changed, 46 insertions, 1 deletions
diff --git a/crates/parser/src/output.rs b/crates/parser/src/output.rs
index b613df029f8..e9ec9822d68 100644
--- a/crates/parser/src/output.rs
+++ b/crates/parser/src/output.rs
@@ -22,6 +22,7 @@ pub struct Output {
     error: Vec<String>,
 }
 
+#[derive(Debug)]
 pub enum Step<'a> {
     Token { kind: SyntaxKind, n_input_tokens: u8 },
     Enter { kind: SyntaxKind },
diff --git a/crates/parser/src/shortcuts.rs b/crates/parser/src/shortcuts.rs
index 3d28f814c9f..e14526aa733 100644
--- a/crates/parser/src/shortcuts.rs
+++ b/crates/parser/src/shortcuts.rs
@@ -16,6 +16,7 @@ use crate::{
     SyntaxKind::{self, *},
 };
 
+#[derive(Debug)]
 pub enum StrStep<'a> {
     Token { kind: SyntaxKind, text: &'a str },
     Enter { kind: SyntaxKind },
@@ -75,7 +76,8 @@ impl<'a> LexedStr<'a> {
                 builder.eat_trivias();
                 (builder.sink)(StrStep::Exit);
             }
-            State::PendingEnter | State::Normal => unreachable!(),
+            State::PendingEnter => (),
+            State::Normal => unreachable!(),
         }
 
         let is_eof = builder.pos == builder.lexed.len();
diff --git a/crates/parser/src/tests.rs b/crates/parser/src/tests.rs
index 512f7ddb95b..fb4885e98d5 100644
--- a/crates/parser/src/tests.rs
+++ b/crates/parser/src/tests.rs
@@ -1,4 +1,5 @@
 mod sourcegen_inline_tests;
+mod entries;
 
 use std::{
     fmt::Write,
diff --git a/crates/parser/src/tests/entries.rs b/crates/parser/src/tests/entries.rs
new file mode 100644
index 00000000000..93e8136263e
--- /dev/null
+++ b/crates/parser/src/tests/entries.rs
@@ -0,0 +1,41 @@
+use crate::{LexedStr, PrefixEntryPoint, StrStep};
+
+#[test]
+fn vis() {
+    check_prefix(PrefixEntryPoint::Vis, "pub(crate) fn foo() {}", "pub(crate)");
+    check_prefix(PrefixEntryPoint::Vis, "fn foo() {}", "");
+    check_prefix(PrefixEntryPoint::Vis, "pub(fn foo() {}", "pub");
+    check_prefix(PrefixEntryPoint::Vis, "pub(crate fn foo() {}", "pub(crate");
+    check_prefix(PrefixEntryPoint::Vis, "crate fn foo() {}", "crate");
+}
+
+#[test]
+fn block() {
+    check_prefix(PrefixEntryPoint::Block, "{}, 92", "{}");
+    check_prefix(PrefixEntryPoint::Block, "{, 92)", "{, 92)");
+    check_prefix(PrefixEntryPoint::Block, "()", "");
+}
+
+#[test]
+fn stmt() {
+    check_prefix(PrefixEntryPoint::Stmt, "92; fn", "92");
+    check_prefix(PrefixEntryPoint::Stmt, "let _ = 92; 1", "let _ = 92");
+    check_prefix(PrefixEntryPoint::Stmt, "pub fn f() {} = 92", "pub fn f() {}");
+    check_prefix(PrefixEntryPoint::Stmt, ";;;", ";");
+    check_prefix(PrefixEntryPoint::Stmt, "+", "+");
+    check_prefix(PrefixEntryPoint::Stmt, "@", "@");
+    check_prefix(PrefixEntryPoint::Stmt, "loop {} - 1", "loop {}");
+}
+
+fn check_prefix(entry: PrefixEntryPoint, input: &str, prefix: &str) {
+    let lexed = LexedStr::new(input);
+    let input = lexed.to_input();
+    let output = entry.parse(&input);
+
+    let mut buf = String::new();
+    lexed.intersperse_trivia(&output, &mut |step| match step {
+        StrStep::Token { kind: _, text } => buf.push_str(text),
+        _ => (),
+    });
+    assert_eq!(buf.trim(), prefix)
+}