...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package load
16
17 import (
18 "strings"
19 "testing"
20 )
21
22 var matchPatternTests = `
23 pattern ...
24 match foo
25
26 pattern net
27 match net
28 not net/http
29
30 pattern net/http
31 match net/http
32 not net
33
34 pattern net...
35 match net net/http netchan
36 not not/http not/net/http
37
38 # Special cases. Quoting docs:
39
40 # First, /... at the end of the pattern can match an empty string,
41 # so that net/... matches both net and packages in its subdirectories, like net/http.
42 pattern net/...
43 match net net/http
44 not not/http not/net/http netchan
45
46 # Second, any slash-separted pattern element containing a wildcard never
47 # participates in a match of the "vendor" element in the path of a vendored
48 # package, so that ./... does not match packages in subdirectories of
49 # ./vendor or ./mycode/vendor, but ./vendor/... and ./mycode/vendor/... do.
50 # Note, however, that a directory named vendor that itself contains code
51 # is not a vendored package: cmd/vendor would be a command named vendor,
52 # and the pattern cmd/... matches it.
53 pattern ./...
54 match ./vendor ./mycode/vendor
55 not ./vendor/foo ./mycode/vendor/foo
56
57 pattern ./vendor/...
58 match ./vendor/foo ./vendor/foo/vendor
59 not ./vendor/foo/vendor/bar
60
61 pattern mycode/vendor/...
62 match mycode/vendor mycode/vendor/foo mycode/vendor/foo/vendor
63 not mycode/vendor/foo/vendor/bar
64
65 pattern x/vendor/y
66 match x/vendor/y
67 not x/vendor
68
69 pattern x/vendor/y/...
70 match x/vendor/y x/vendor/y/z x/vendor/y/vendor x/vendor/y/z/vendor
71 not x/vendor/y/vendor/z
72
73 pattern .../vendor/...
74 match x/vendor/y x/vendor/y/z x/vendor/y/vendor x/vendor/y/z/vendor
75 `
76
77 func testPatterns(t *testing.T, name, tests string, fn func(string, string) bool) {
78 var patterns []string
79 for _, line := range strings.Split(tests, "\n") {
80 if i := strings.Index(line, "#"); i >= 0 {
81 line = line[:i]
82 }
83 f := strings.Fields(line)
84 if len(f) == 0 {
85 continue
86 }
87 switch f[0] {
88 default:
89 t.Fatalf("unknown directive %q", f[0])
90 case "pattern":
91 patterns = f[1:]
92 case "match", "not":
93 want := f[0] == "match"
94 for _, pattern := range patterns {
95 for _, in := range f[1:] {
96 if fn(pattern, in) != want {
97 t.Errorf("%s(%q, %q) = %v, want %v", name, pattern, in, !want, want)
98 }
99 }
100 }
101 }
102 }
103 }
104
View as plain text