Commit 8295dbda authored by Russ Cox's avatar Russ Cox

cmd/go: make pattern matching tests less repetitive

Change-Id: I25db1d637dd461cec67ba70659d523b46895c113
Reviewed-on: https://go-review.googlesource.com/38744Reviewed-by: default avatarAlan Donovan <adonovan@google.com>
parent f0a3018b
...@@ -4,60 +4,75 @@ ...@@ -4,60 +4,75 @@
package load package load
import "testing" import (
"strings"
"testing"
)
var matchPatternTests = []stringPairTest{ var matchPatternTests = `
{"...", "foo", true}, pattern ...
{"net", "net", true}, match foo
{"net", "net/http", false},
{"net/http", "net", false}, pattern net
{"net/http", "net/http", true}, match net
{"net...", "netchan", true}, not net/http
{"net...", "net", true},
{"net...", "net/http", true}, pattern net/http
{"net...", "not/http", false}, match net/http
{"net/...", "netchan", false}, not net
{"net/...", "net", true},
{"net/...", "net/http", true}, pattern net...
{"net/...", "not/http", false}, 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) { 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) return matchPattern(pattern)(name)
}) })
} }
var treeCanMatchPatternTests = []stringPairTest{ var treeCanMatchPatternTests = `
{"...", "foo", true}, pattern ...
{"net", "net", true}, match foo
{"net", "net/http", false},
{"net/http", "net", true}, pattern net
{"net/http", "net/http", true}, match net
{"net...", "netchan", true}, not net/http
{"net...", "net", true},
{"net...", "net/http", true}, pattern net/http
{"net...", "not/http", false}, match net net/http
{"net/...", "netchan", false},
{"net/...", "net", true}, pattern net...
{"net/...", "net/http", true}, match net netchan net/http
{"net/...", "not/http", false}, not not/http not/net/http
{"abc.../def", "abcxyz", true},
{"abc.../def", "xyxabc", false}, pattern net/...
{"x/y/z/...", "x", true}, match net net/http
{"x/y/z/...", "x/y", true}, not not/http netchan
{"x/y/z/...", "x/y/z", true},
{"x/y/z/...", "x/y/z/w", true}, pattern abc.../def
{"x/y/z", "x", true}, match abcxyz
{"x/y/z", "x/y", true}, not xyzabc
{"x/y/z", "x/y/z", true},
{"x/y/z", "x/y/z/w", false}, pattern x/y/z/...
{"x/.../y/z", "x/a/b/c", true}, match x x/y x/y/z x/y/z/w
{"x/.../y/z", "y/x/a/b/c", false},
} 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) { func TestTreeCanMatchPattern(t *testing.T) {
testStringPairs(t, "treeCanMatchPattern", treeCanMatchPatternTests, func(pattern, name string) bool { testPatterns(t, "treeCanMatchPattern", treeCanMatchPatternTests, func(pattern, name string) bool {
return treeCanMatchPattern(pattern)(name) return treeCanMatchPattern(pattern)(name)
}) })
} }
...@@ -86,3 +101,31 @@ func testStringPairs(t *testing.T, name string, tests []stringPairTest, f func(s ...@@ -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)
}
}
}
}
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment