Skip to content

Commit

Permalink
Support the underscore symbol matching any symbols in vartan-test com…
Browse files Browse the repository at this point in the history
…mand
  • Loading branch information
nihei9 committed Jun 11, 2022
1 parent 7403c18 commit 52ad315
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ Passed test.txt

When you specify a directory as the 2nd argument of `vartan test` command, it will run all test cases in the directory.

The underscore `_` allows you to match any symbols. Thus `(expr (expr (id)) (_) (expr (id)))` matches `a + b`, `a - b`, and so on.

### 5. Generate a parser

Using `vartan-go` command, you can generate a source code of a parser to recognize your grammar.
Expand Down
5 changes: 3 additions & 2 deletions spec/test/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ func DiffTree(expected, actual *Tree) []*TreeDiff {
if expected == nil && actual == nil {
return nil
}
if actual.Kind != expected.Kind {
// _ matches any symbols.
if expected.Kind != "_" && actual.Kind != expected.Kind {
msg := fmt.Sprintf("unexpected kind: expected '%v' but got '%v'", expected.Kind, actual.Kind)
return []*TreeDiff{
newTreeDiff(expected, actual, msg),
Expand All @@ -103,7 +104,7 @@ func DiffTree(expected, actual *Tree) []*TreeDiff {
}
var diffs []*TreeDiff
for i, exp := range expected.Children {
if ds := DiffTree(actual.Children[i], exp); len(ds) > 0 {
if ds := DiffTree(exp, actual.Children[i]); len(ds) > 0 {
diffs = append(diffs, ds...)
}
}
Expand Down
49 changes: 49 additions & 0 deletions spec/test/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,26 @@ func TestDiffTree(t *testing.T) {
),
),
},
{
t1: NewTree("_"),
t2: NewTree("a"),
},
{
t1: NewTree("a",
NewTree("_"),
),
t2: NewTree("a",
NewTree("b"),
),
},
{
t1: NewTree("_",
NewTree("b"),
),
t2: NewTree("a",
NewTree("b"),
),
},
{
t1: NewTree("a"),
t2: NewTree("b"),
Expand Down Expand Up @@ -139,6 +159,35 @@ func TestDiffTree(t *testing.T) {
),
different: true,
},
{
t1: NewTree("a",
NewTree("_"),
NewTree("c"),
),
t2: NewTree("a",
NewTree("b"),
NewTree("x"),
),
different: true,
},
{
t1: NewTree("_"),
t2: NewTree("a",
NewTree("b"),
),
different: true,
},
{
t1: NewTree("a",
NewTree("_"),
),
t2: NewTree("a",
NewTree("b",
NewTree("c"),
),
),
different: true,
},
}
for i, tt := range tests {
t.Run(fmt.Sprintf("#%v", i), func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion tester/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func runTest(g *gspec.CompiledGrammar, c *TestCaseWithMetadata) *TestResult {
}

// When a parse tree exists, the test continues regardless of whether or not syntax errors occurred.
diffs := tspec.DiffTree(ConvertSyntaxTreeToTestableTree(tb.Tree()).Fill(), c.TestCase.Output)
diffs := tspec.DiffTree(c.TestCase.Output, ConvertSyntaxTreeToTestableTree(tb.Tree()).Fill())
if len(diffs) > 0 {
return &TestResult{
TestCasePath: c.FilePath,
Expand Down

0 comments on commit 52ad315

Please sign in to comment.