Skip to content

Commit

Permalink
improve test case
Browse files Browse the repository at this point in the history
  • Loading branch information
Devin-Yeung committed Nov 11, 2024
1 parent 817eea9 commit 3dca608
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 170 deletions.
20 changes: 11 additions & 9 deletions src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,19 @@ mod test {
#[test]
fn test_typed_generator() {
let text = r#"
<S> ::= <Expr> | <S> <Expr> {3, 5};
<Expr> ::= <E>
| <E: "int"> "+" <E: "int">
| <E: "bool"> "&" <E: "bool"> ;
<E: "int"> ::= "1" | "2" | "3" ;
<E: "bool"> ::= "true" | "false" ;
<S> ::= <Expr> | <S> "\n" <Expr> {10, 20};
<Expr> ::= <E> ;
<E: "int"> ::= "1" | "2" | "3"
| <E: "int"> "+" <E: "int"> {3, } ;
<E: "bool"> ::= "true" | "false"
| <E: "bool"> "&" <E: "bool"> {3, } ;
"#;
let grammar = RawGrammar::parse(text).unwrap().to_checked().unwrap();
let tree_gen = TreeGenerator { grammar };
let gen = Generator { grammar };
let mut seeded_rng = rand::rngs::StdRng::seed_from_u64(42);
let tree = tree_gen.generate("S", &mut seeded_rng);
insta::assert_debug_snapshot!(&tree);
insta::assert_snapshot!(gen.generate("S", &mut seeded_rng));
}
}
174 changes: 13 additions & 161 deletions src/snapshots/bnfgen__generator__test__typed_generator.snap
Original file line number Diff line number Diff line change
@@ -1,164 +1,16 @@
---
source: src/generator.rs
expression: "&tree"
expression: "gen.generate(\"S\", &mut seeded_rng)"
---
Branch {
name: "S",
children: [
Branch {
name: "S",
children: [
Branch {
name: "S",
children: [
Branch {
name: "S",
children: [
Branch {
name: "S",
children: [
Branch {
name: "Expr",
children: [
Branch {
name: "E",
children: [
Leaf(
Terminal(
"1",
),
),
],
},
],
},
],
},
Branch {
name: "Expr",
children: [
Branch {
name: "E",
children: [
Leaf(
Terminal(
"1",
),
),
],
},
Leaf(
Terminal(
"+",
),
),
Branch {
name: "E",
children: [
Leaf(
Terminal(
"2",
),
),
],
},
],
},
],
},
Branch {
name: "Expr",
children: [
Branch {
name: "E",
children: [
Leaf(
Terminal(
"3",
),
),
],
},
Leaf(
Terminal(
"+",
),
),
Branch {
name: "E",
children: [
Leaf(
Terminal(
"2",
),
),
],
},
],
},
],
},
Branch {
name: "Expr",
children: [
Branch {
name: "E",
children: [
Leaf(
Terminal(
"2",
),
),
],
},
Leaf(
Terminal(
"+",
),
),
Branch {
name: "E",
children: [
Leaf(
Terminal(
"3",
),
),
],
},
],
},
],
},
Branch {
name: "Expr",
children: [
Branch {
name: "E",
children: [
Leaf(
Terminal(
"2",
),
),
],
},
Leaf(
Terminal(
"+",
),
),
Branch {
name: "E",
children: [
Leaf(
Terminal(
"2",
),
),
],
},
],
},
],
}
true & false & true & false & false
1 + 3 + 1 + 1 + 3
3
true
true
true
1
2
true
3
true
false

0 comments on commit 3dca608

Please sign in to comment.