]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: make pattern matching tests less repetitive
authorRuss Cox <rsc@golang.org>
Tue, 28 Mar 2017 18:59:13 +0000 (14:59 -0400)
committerRuss Cox <rsc@golang.org>
Wed, 29 Mar 2017 18:05:13 +0000 (18:05 +0000)
Change-Id: I25db1d637dd461cec67ba70659d523b46895c113
Reviewed-on: https://go-review.googlesource.com/38744
Reviewed-by: Alan Donovan <adonovan@google.com>
src/cmd/go/internal/load/match_test.go

index dc05cbb17e4220f5c92cd18130c683cd60aa5e39..41924a2d9683094a7f4bc86273a7902783c1d294 100644 (file)
@@ -4,60 +4,75 @@
 
 package load
 
-import "testing"
+import (
+       "strings"
+       "testing"
+)
 
-var matchPatternTests = []stringPairTest{
-       {"...", "foo", true},
-       {"net", "net", true},
-       {"net", "net/http", false},
-       {"net/http", "net", false},
-       {"net/http", "net/http", true},
-       {"net...", "netchan", true},
-       {"net...", "net", true},
-       {"net...", "net/http", true},
-       {"net...", "not/http", false},
-       {"net/...", "netchan", false},
-       {"net/...", "net", true},
-       {"net/...", "net/http", true},
-       {"net/...", "not/http", false},
-}
+var matchPatternTests = `
+       pattern ...
+       match foo
+       
+       pattern net
+       match net
+       not net/http
+       
+       pattern net/http
+       match net/http
+       not net
+       
+       pattern net...
+       match net net/http netchan
+       not not/http not/net/http
+       
+       pattern net/...
+       match net net/http
+       not not/http not/net/http netchan
+`
 
 func TestMatchPattern(t *testing.T) {
-       testStringPairs(t, "matchPattern", matchPatternTests, func(pattern, name string) bool {
+       testPatterns(t, "matchPattern", matchPatternTests, func(pattern, name string) bool {
                return matchPattern(pattern)(name)
        })
 }
 
-var treeCanMatchPatternTests = []stringPairTest{
-       {"...", "foo", true},
-       {"net", "net", true},
-       {"net", "net/http", false},
-       {"net/http", "net", true},
-       {"net/http", "net/http", true},
-       {"net...", "netchan", true},
-       {"net...", "net", true},
-       {"net...", "net/http", true},
-       {"net...", "not/http", false},
-       {"net/...", "netchan", false},
-       {"net/...", "net", true},
-       {"net/...", "net/http", true},
-       {"net/...", "not/http", false},
-       {"abc.../def", "abcxyz", true},
-       {"abc.../def", "xyxabc", false},
-       {"x/y/z/...", "x", true},
-       {"x/y/z/...", "x/y", true},
-       {"x/y/z/...", "x/y/z", true},
-       {"x/y/z/...", "x/y/z/w", true},
-       {"x/y/z", "x", true},
-       {"x/y/z", "x/y", true},
-       {"x/y/z", "x/y/z", true},
-       {"x/y/z", "x/y/z/w", false},
-       {"x/.../y/z", "x/a/b/c", true},
-       {"x/.../y/z", "y/x/a/b/c", false},
-}
+var treeCanMatchPatternTests = `
+       pattern ...
+       match foo
+       
+       pattern net
+       match net
+       not net/http
+       
+       pattern net/http
+       match net net/http
+       
+       pattern net...
+       match net netchan net/http
+       not not/http not/net/http
+
+       pattern net/...
+       match net net/http
+       not not/http netchan
+       
+       pattern abc.../def
+       match abcxyz
+       not xyzabc
+       
+       pattern x/y/z/...
+       match x x/y x/y/z x/y/z/w
+       
+       pattern x/y/z
+       match x x/y x/y/z
+       not x/y/z/w
+       
+       pattern x/.../y/z
+       match x/a/b/c
+       not y/x/a/b/c
+`
 
-func TestChildrenCanMatchPattern(t *testing.T) {
-       testStringPairs(t, "treeCanMatchPattern", treeCanMatchPatternTests, func(pattern, name string) bool {
+func TestTreeCanMatchPattern(t *testing.T) {
+       testPatterns(t, "treeCanMatchPattern", treeCanMatchPatternTests, func(pattern, name string) bool {
                return treeCanMatchPattern(pattern)(name)
        })
 }
@@ -86,3 +101,31 @@ func testStringPairs(t *testing.T, name string, tests []stringPairTest, f func(s
                }
        }
 }
+
+func testPatterns(t *testing.T, name, tests string, fn func(string, string) bool) {
+       var patterns []string
+       for _, line := range strings.Split(tests, "\n") {
+               if i := strings.Index(line, "#"); i >= 0 {
+                       line = line[:i]
+               }
+               f := strings.Fields(line)
+               if len(f) == 0 {
+                       continue
+               }
+               switch f[0] {
+               default:
+                       t.Fatalf("unknown directive %q", f[0])
+               case "pattern":
+                       patterns = f[1:]
+               case "match", "not":
+                       want := f[0] == "match"
+                       for _, pattern := range patterns {
+                               for _, in := range f[1:] {
+                                       if fn(pattern, in) != want {
+                                               t.Errorf("%s(%q, %q) = %v, want %v", name, pattern, in, !want, want)
+                                       }
+                               }
+                       }
+               }
+       }
+}