aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md2
-rw-r--r--spec/test/parser.go5
-rw-r--r--spec/test/parser_test.go49
-rw-r--r--tester/tester.go2
4 files changed, 55 insertions, 3 deletions
diff --git a/README.md b/README.md
index 222ee62..3718437 100644
--- a/README.md
+++ b/README.md
@@ -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.
diff --git a/spec/test/parser.go b/spec/test/parser.go
index 483e553..f255381 100644
--- a/spec/test/parser.go
+++ b/spec/test/parser.go
@@ -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),
@@ -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...)
}
}
diff --git a/spec/test/parser_test.go b/spec/test/parser_test.go
index 41b7189..979202e 100644
--- a/spec/test/parser_test.go
+++ b/spec/test/parser_test.go
@@ -75,6 +75,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"),
different: true,
@@ -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) {
diff --git a/tester/tester.go b/tester/tester.go
index 70d4800..a39026a 100644
--- a/tester/tester.go
+++ b/tester/tester.go
@@ -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,