Source file
src/path/match_test.go
1
2
3
4
5 package path_test
6
7 import (
8 "fmt"
9 . "path"
10 "testing"
11 )
12
13 type MatchTest struct {
14 pattern, s string
15 match bool
16 err error
17 }
18
19 var matchTests = []MatchTest{
20 {"abc", "abc", true, nil},
21 {"*", "abc", true, nil},
22 {"*c", "abc", true, nil},
23 {"a*", "a", true, nil},
24 {"a*", "abc", true, nil},
25 {"a*", "ab/c", false, nil},
26 {"a*/b", "abc/b", true, nil},
27 {"a*/b", "a/c/b", false, nil},
28 {"a*b*c*d*e*/f", "axbxcxdxe/f", true, nil},
29 {"a*b*c*d*e*/f", "axbxcxdxexxx/f", true, nil},
30 {"a*b*c*d*e*/f", "axbxcxdxe/xxx/f", false, nil},
31 {"a*b*c*d*e*/f", "axbxcxdxexxx/fff", false, nil},
32 {"a*b?c*x", "abxbbxdbxebxczzx", true, nil},
33 {"a*b?c*x", "abxbbxdbxebxczzy", false, nil},
34 {"ab[c]", "abc", true, nil},
35 {"ab[b-d]", "abc", true, nil},
36 {"ab[e-g]", "abc", false, nil},
37 {"ab[^c]", "abc", false, nil},
38 {"ab[^b-d]", "abc", false, nil},
39 {"ab[^e-g]", "abc", true, nil},
40 {"a\\*b", "a*b", true, nil},
41 {"a\\*b", "ab", false, nil},
42 {"a?b", "a☺b", true, nil},
43 {"a[^a]b", "a☺b", true, nil},
44 {"a???b", "a☺b", false, nil},
45 {"a[^a][^a][^a]b", "a☺b", false, nil},
46 {"[a-ζ]*", "α", true, nil},
47 {"*[a-ζ]", "A", false, nil},
48 {"a?b", "a/b", false, nil},
49 {"a*b", "a/b", false, nil},
50 {"[\\]a]", "]", true, nil},
51 {"[\\-]", "-", true, nil},
52 {"[x\\-]", "x", true, nil},
53 {"[x\\-]", "-", true, nil},
54 {"[x\\-]", "z", false, nil},
55 {"[\\-x]", "x", true, nil},
56 {"[\\-x]", "-", true, nil},
57 {"[\\-x]", "a", false, nil},
58 {"[]a]", "]", false, ErrBadPattern},
59 {"[-]", "-", false, ErrBadPattern},
60 {"[x-]", "x", false, ErrBadPattern},
61 {"[x-]", "-", false, ErrBadPattern},
62 {"[x-]", "z", false, ErrBadPattern},
63 {"[-x]", "x", false, ErrBadPattern},
64 {"[-x]", "-", false, ErrBadPattern},
65 {"[-x]", "a", false, ErrBadPattern},
66 {"\\", "a", false, ErrBadPattern},
67 {"[a-b-c]", "a", false, ErrBadPattern},
68 {"[", "a", false, ErrBadPattern},
69 {"[^", "a", false, ErrBadPattern},
70 {"[^bc", "a", false, ErrBadPattern},
71 {"a[", "a", false, ErrBadPattern},
72 {"a[", "ab", false, ErrBadPattern},
73 {"a[", "x", false, ErrBadPattern},
74 {"a/b[", "x", false, ErrBadPattern},
75 {"*x", "xxx", true, nil},
76 }
77
78 func TestMatch(t *testing.T) {
79 for _, tt := range matchTests {
80 ok, err := Match(tt.pattern, tt.s)
81 if ok != tt.match || err != tt.err {
82 t.Errorf("Match(%#q, %#q) = %v, %v want %v, %v", tt.pattern, tt.s, ok, err, tt.match, tt.err)
83 }
84 }
85 }
86
87 func BenchmarkMatch(b *testing.B) {
88 for _, tt := range matchTests {
89 name := fmt.Sprintf("%q %q", tt.pattern, tt.s)
90 b.Run(name, func(b *testing.B) {
91 b.ReportAllocs()
92 for range b.N {
93 bSink, errSink = Match(tt.pattern, tt.s)
94 }
95 })
96 }
97 }
98
99 var (
100 bSink bool
101 errSink error
102 )
103
View as plain text