diff options
author | Ryo Nihei <nihei.dev@gmail.com> | 2022-06-11 21:40:38 +0900 |
---|---|---|
committer | Ryo Nihei <nihei.dev@gmail.com> | 2022-06-11 22:27:52 +0900 |
commit | 52ad315a0df8a346953e570e8be472709b81cc6a (patch) | |
tree | 5221784eacd9efcc63d83297d8e2b60f8acbc1bd | |
parent | Remove the kind field from a node corresponding to an anonymous terminal symbol (diff) | |
download | urubu-52ad315a0df8a346953e570e8be472709b81cc6a.tar.gz urubu-52ad315a0df8a346953e570e8be472709b81cc6a.tar.xz |
Support the underscore symbol matching any symbols in vartan-test command
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | spec/test/parser.go | 5 | ||||
-rw-r--r-- | spec/test/parser_test.go | 49 | ||||
-rw-r--r-- | tester/tester.go | 2 |
4 files changed, 55 insertions, 3 deletions
@@ -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, |