1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
12 type MatchTest struct {
18 var matchTests = []MatchTest{
19 {"abc", "abc", true, nil},
20 {"*", "abc", true, nil},
21 {"*c", "abc", true, nil},
22 {"a*", "a", true, nil},
23 {"a*", "abc", true, nil},
24 {"a*", "ab/c", false, nil},
25 {"a*/b", "abc/b", true, nil},
26 {"a*/b", "a/c/b", false, nil},
27 {"a*b*c*d*e*/f", "axbxcxdxe/f", true, nil},
28 {"a*b*c*d*e*/f", "axbxcxdxexxx/f", true, nil},
29 {"a*b*c*d*e*/f", "axbxcxdxe/xxx/f", false, nil},
30 {"a*b*c*d*e*/f", "axbxcxdxexxx/fff", false, nil},
31 {"a*b?c*x", "abxbbxdbxebxczzx", true, nil},
32 {"a*b?c*x", "abxbbxdbxebxczzy", false, nil},
33 {"ab[c]", "abc", true, nil},
34 {"ab[b-d]", "abc", true, nil},
35 {"ab[e-g]", "abc", false, nil},
36 {"ab[^c]", "abc", false, nil},
37 {"ab[^b-d]", "abc", false, nil},
38 {"ab[^e-g]", "abc", true, nil},
39 {"a\\*b", "a*b", true, nil},
40 {"a\\*b", "ab", false, nil},
41 {"a?b", "a☺b", true, nil},
42 {"a[^a]b", "a☺b", true, nil},
43 {"a???b", "a☺b", false, nil},
44 {"a[^a][^a][^a]b", "a☺b", false, nil},
45 {"[a-ζ]*", "α", true, nil},
46 {"*[a-ζ]", "A", false, nil},
47 {"a?b", "a/b", false, nil},
48 {"a*b", "a/b", false, nil},
49 {"[\\]a]", "]", true, nil},
50 {"[\\-]", "-", true, nil},
51 {"[x\\-]", "x", true, nil},
52 {"[x\\-]", "-", true, nil},
53 {"[x\\-]", "z", false, nil},
54 {"[\\-x]", "x", true, nil},
55 {"[\\-x]", "-", true, nil},
56 {"[\\-x]", "a", false, nil},
57 {"[]a]", "]", false, ErrBadPattern},
58 {"[-]", "-", false, ErrBadPattern},
59 {"[x-]", "x", false, ErrBadPattern},
60 {"[x-]", "-", false, ErrBadPattern},
61 {"[x-]", "z", false, ErrBadPattern},
62 {"[-x]", "x", false, ErrBadPattern},
63 {"[-x]", "-", false, ErrBadPattern},
64 {"[-x]", "a", false, ErrBadPattern},
65 {"\\", "a", false, ErrBadPattern},
66 {"[a-b-c]", "a", false, ErrBadPattern},
67 {"*x", "xxx", true, nil},
70 func TestMatch(t *testing.T) {
71 for _, tt := range matchTests {
72 ok, err := Match(tt.pattern, tt.s)
73 if ok != tt.match || err != tt.err {
74 t.Errorf("Match(%#q, %#q) = %v, %v want %v, nil", tt.pattern, tt.s, ok, err, tt.match)
79 // contains returns true if vector contains the string s.
80 func contains(vector []string, s string) bool {
81 for _, elem := range vector {
89 var globTests = []struct {
90 pattern, result string
92 {"match.go", "match.go"},
93 {"mat?h.go", "match.go"},
95 // Fails in the gccgo test environment.
96 // {"../*/match.go", "../path/match.go"},
99 func TestGlob(t *testing.T) {
100 for _, tt := range globTests {
101 matches := Glob(tt.pattern)
102 if !contains(matches, tt.result) {
103 t.Errorf("Glob(%#q) = %#v want %v", tt.pattern, matches, tt.result)