1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package path
20
21 import (
22 "strings"
23 "testing"
24 )
25
26 type MatchTest struct {
27 pattern, s string
28 match bool
29 err error
30 }
31
32 var matchTests = []MatchTest{
33 {"abc", "abc", true, nil},
34 {"*", "abc", true, nil},
35 {"*c", "abc", true, nil},
36 {"a*", "a", true, nil},
37 {"a*", "abc", true, nil},
38 {"a*", "ab/c", false, nil},
39 {"a*/b", "abc/b", true, nil},
40 {"a*/b", "a/c/b", false, nil},
41 {"a*b*c*d*e*/f", "axbxcxdxe/f", true, nil},
42 {"a*b*c*d*e*/f", "axbxcxdxexxx/f", true, nil},
43 {"a*b*c*d*e*/f", "axbxcxdxe/xxx/f", false, nil},
44 {"a*b*c*d*e*/f", "axbxcxdxexxx/fff", false, nil},
45 {"a*b?c*x", "abxbbxdbxebxczzx", true, nil},
46 {"a*b?c*x", "abxbbxdbxebxczzy", false, nil},
47 {"ab[c]", "abc", true, nil},
48 {"ab[b-d]", "abc", true, nil},
49 {"ab[e-g]", "abc", false, nil},
50 {"ab[^c]", "abc", false, nil},
51 {"ab[^b-d]", "abc", false, nil},
52 {"ab[^e-g]", "abc", true, nil},
53 {"a\\*b", "a*b", true, nil},
54 {"a\\*b", "ab", false, nil},
55 {"a?b", "a☺b", true, nil},
56 {"a[^a]b", "a☺b", true, nil},
57 {"a???b", "a☺b", false, nil},
58 {"a[^a][^a][^a]b", "a☺b", false, nil},
59 {"[a-ζ]*", "α", true, nil},
60 {"*[a-ζ]", "A", false, nil},
61 {"a?b", "a/b", false, nil},
62 {"a*b", "a/b", false, nil},
63 {"[\\]a]", "]", true, nil},
64 {"[\\-]", "-", true, nil},
65 {"[x\\-]", "x", true, nil},
66 {"[x\\-]", "-", true, nil},
67 {"[x\\-]", "z", false, nil},
68 {"[\\-x]", "x", true, nil},
69 {"[\\-x]", "-", true, nil},
70 {"[\\-x]", "a", false, nil},
71 {"[]a]", "]", false, ErrBadPattern},
72 {"[-]", "-", false, ErrBadPattern},
73 {"[x-]", "x", false, ErrBadPattern},
74 {"[x-]", "-", false, ErrBadPattern},
75 {"[x-]", "z", false, ErrBadPattern},
76 {"[-x]", "x", false, ErrBadPattern},
77 {"[-x]", "-", false, ErrBadPattern},
78 {"[-x]", "a", false, ErrBadPattern},
79 {"\\", "a", false, ErrBadPattern},
80 {"[a-b-c]", "a", false, ErrBadPattern},
81 {"[", "a", false, ErrBadPattern},
82 {"[^", "a", false, ErrBadPattern},
83 {"[^bc", "a", false, ErrBadPattern},
84 {"a[", "a", false, ErrBadPattern},
85 {"a[", "ab", false, ErrBadPattern},
86 {"a[", "x", false, ErrBadPattern},
87 {"a/b[", "x", false, ErrBadPattern},
88 {"*x", "xxx", true, nil},
89 }
90
91 func errp(e error) string {
92 if e == nil {
93 return "<nil>"
94 }
95 return e.Error()
96 }
97
98 func TestMatch(t *testing.T) {
99 testEachOS(t, []OS{Unix, Windows, Plan9}, func(t *testing.T, os OS) {
100 for _, tt := range matchTests {
101 pattern := tt.pattern
102 s := tt.s
103 if os == Windows {
104 if strings.Contains(pattern, "\\") {
105
106 continue
107 }
108 pattern = Clean(pattern, os)
109 s = Clean(s, os)
110 }
111 ok, err := Match(pattern, s, os)
112 if ok != tt.match || err != tt.err {
113 t.Errorf("Match(%#q, %#q, %q) = %v, %q want %v, %q",
114 pattern, s, os, ok, errp(err), tt.match, errp(tt.err))
115 }
116 }
117 })
118 }
119
View as plain text