Skip to content

Commit

Permalink
snapshot test bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
CrockAgile committed Feb 11, 2023
1 parent 692ac13 commit ea1c1d4
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 57 deletions.
75 changes: 18 additions & 57 deletions tests/parse_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ fn empty() {

let input = "";

let parses = grammar.parse_input(input);
assert_eq!(parses.count(), 1);
let parses: Vec<_> = grammar.parse_input(input).map(|a| a.to_string()).collect();
assert_snapshot!(parses.join("\n"));
}

#[test]
Expand Down Expand Up @@ -362,32 +362,31 @@ fn swap_left_right_recursion() {
<whitespace> ::= ' '
<ws> ::= ' ' | ' ' <ws>
";
assert!(left_recursive
let parses: Vec<_> = left_recursive
.parse::<Grammar>()
.unwrap()
.parse_input(input)
.next()
.is_some());
.map(|a| a.to_string())
.collect();
assert_snapshot!(parses.join("\n"));

let right_recursive = left_recursive.replace(
// rewrite production from left- to right- recursive
"<string> ::= <char_null> | <string> <char_null>",
"<string> ::= <char_null> | <char_null> <string>",
);
assert!(
right_recursive
.parse::<Grammar>()
.unwrap()
.parse_input(input)
.next()
.is_some(),
"right-recursive grammar failed to parse: {input}"
);
let parses: Vec<_> = right_recursive
.parse::<Grammar>()
.unwrap()
.parse_input(input)
.map(|a| a.to_string())
.collect();
assert_snapshot!(parses.join("\n"));
}

#[test]
fn shared_nullable_nonterminal() {
let fails: &str = "
let grammar: &str = "
<disjunction> ::= <predicate> | <disjunction> <or> <predicate>
<predicate> ::= <char_null_one> | <special-string> '.'
Expand All @@ -406,51 +405,13 @@ fn shared_nullable_nonterminal() {

let input = "a or a";

let passes_1 = fails.replace(
// skip nullable production <char_null_two>
"<char_null_one> ::= <char_null_two>",
"<char_null_one> ::= <char_null_three>",
);
assert!(passes_1
.parse::<Grammar>()
.unwrap()
.parse_input(input)
.next()
.is_some());

let passes_2 = fails.replace(
// replace <whitespace> with its terminal ' '
"<ws> ::= <whitespace> | ' ' <ws>",
"<ws> ::= ' ' | ' ' <ws>",
);
assert!(passes_2
.parse::<Grammar>()
.unwrap()
.parse_input(input)
.next()
.is_some());

let passes_3 = fails.replace(
// again, replace <whitespace> with its terminal ' '
"<special-char> ::= <char> | <whitespace>",
"<special-char> ::= <char> | ' '",
);
assert!(passes_3
let parses: Vec<_> = grammar
.parse::<Grammar>()
.unwrap()
.parse_input(input)
.next()
.is_some());

assert!(
fails
.parse::<Grammar>()
.unwrap()
.parse_input(input)
.next()
.is_some(),
"all grammars except last one parsed: {input}"
);
.map(|a| a.to_string())
.collect();
assert_snapshot!(parses.join("\n"));
}

#[test]
Expand Down
7 changes: 7 additions & 0 deletions tests/snapshots/parse_input__empty.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
source: tests/parse_input.rs
expression: "parses.join(\"\\n\")"
---
<start> ::= ""
└── ""

27 changes: 27 additions & 0 deletions tests/snapshots/parse_input__shared_nullable_nonterminal.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
source: tests/parse_input.rs
expression: "parses.join(\"\\n\")"
---
<disjunction> ::= <disjunction> <or> <predicate>
├── <disjunction> ::= <predicate>
│ └── <predicate> ::= <char_null_one>
│ └── <char_null_one> ::= <char_null_two>
│ └── <char_null_two> ::= <char_null_three>
│ └── <char_null_three> ::= <char>
│ └── <char> ::= "a"
│ └── "a"
├── <or> ::= <ws> "or" <ws>
│ ├── <ws> ::= <whitespace>
│ │ └── <whitespace> ::= " "
│ │ └── " "
│ ├── "or"
│ └── <ws> ::= <whitespace>
│ └── <whitespace> ::= " "
│ └── " "
└── <predicate> ::= <char_null_one>
└── <char_null_one> ::= <char_null_two>
└── <char_null_two> ::= <char_null_three>
└── <char_null_three> ::= <char>
└── <char> ::= "a"
└── "a"

29 changes: 29 additions & 0 deletions tests/snapshots/parse_input__swap_left_right_recursion.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
source: tests/parse_input.rs
expression: "parses.join(\"\\n\")"
---
<conjunction> ::= <conjunction> <ws> <predicate>
├── <conjunction> ::= <predicate>
│ └── <predicate> ::= <string_null_one>
│ └── <string_null_one> ::= <string_null_two>
│ └── <string_null_two> ::= <string_null_three>
│ └── <string_null_three> ::= <string>
│ └── <string> ::= <string> <char_null>
│ ├── <string> ::= <char_null>
│ │ └── <char_null> ::= <char>
│ │ └── <char> ::= "a"
│ │ └── "a"
│ └── <char_null> ::= <char>
│ └── <char> ::= "a"
│ └── "a"
├── <ws> ::= " "
│ └── " "
└── <predicate> ::= <string_null_one>
└── <string_null_one> ::= <string_null_two>
└── <string_null_two> ::= <string_null_three>
└── <string_null_three> ::= <string>
└── <string> ::= <char_null>
└── <char_null> ::= <char>
└── <char> ::= "a"
└── "a"

0 comments on commit ea1c1d4

Please sign in to comment.