github.com/varialus/godfly@v0.0.0-20130904042352-1934f9f095ab/src/pkg/path/filepath/match_test.go (about) 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. 4 5 package filepath 6 7 import ( 8 "runtime" 9 "strings" 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, nil}, 72 {"a[", "ab", false, ErrBadPattern}, 73 {"*x", "xxx", true, nil}, 74 } 75 76 func errp(e error) string { 77 if e == nil { 78 return "<nil>" 79 } 80 return e.Error() 81 } 82 83 func TestMatch(t *testing.T) { 84 for _, tt := range matchTests { 85 pattern := tt.pattern 86 s := tt.s 87 if runtime.GOOS == "windows" { 88 if strings.Index(pattern, "\\") >= 0 { 89 // no escape allowed on windows. 90 continue 91 } 92 pattern = Clean(pattern) 93 s = Clean(s) 94 } 95 ok, err := Match(pattern, s) 96 if ok != tt.match || err != tt.err { 97 t.Errorf("Match(%#q, %#q) = %v, %q want %v, %q", pattern, s, ok, errp(err), tt.match, errp(tt.err)) 98 } 99 } 100 } 101 102 // contains returns true if vector contains the string s. 103 func contains(vector []string, s string) bool { 104 for _, elem := range vector { 105 if elem == s { 106 return true 107 } 108 } 109 return false 110 } 111 112 var globTests = []struct { 113 pattern, result string 114 }{ 115 {"match.go", "match.go"}, 116 {"mat?h.go", "match.go"}, 117 {"*", "match.go"}, 118 {"../*/match.go", "../filepath/match.go"}, 119 } 120 121 func TestGlob(t *testing.T) { 122 for _, tt := range globTests { 123 pattern := tt.pattern 124 result := tt.result 125 if runtime.GOOS == "windows" { 126 pattern = Clean(pattern) 127 result = Clean(result) 128 } 129 matches, err := Glob(pattern) 130 if err != nil { 131 t.Errorf("Glob error for %q: %s", pattern, err) 132 continue 133 } 134 if !contains(matches, result) { 135 t.Errorf("Glob(%#q) = %#v want %v", pattern, matches, result) 136 } 137 } 138 for _, pattern := range []string{"no_match", "../*/no_match"} { 139 matches, err := Glob(pattern) 140 if err != nil { 141 t.Errorf("Glob error for %q: %s", pattern, err) 142 continue 143 } 144 if len(matches) != 0 { 145 t.Errorf("Glob(%#q) = %#v want []", pattern, matches) 146 } 147 } 148 } 149 150 func TestGlobError(t *testing.T) { 151 _, err := Glob("[7]") 152 if err != nil { 153 t.Error("expected error for bad pattern; got none") 154 } 155 }