summaryrefslogtreecommitdiff
path: root/src/pkg/path/match_test.go
diff options
context:
space:
mode:
authorOndřej Surý <ondrej@sury.org>2011-01-17 12:40:45 +0100
committerOndřej Surý <ondrej@sury.org>2011-01-17 12:40:45 +0100
commit3e45412327a2654a77944249962b3652e6142299 (patch)
treebc3bf69452afa055423cbe0c5cfa8ca357df6ccf /src/pkg/path/match_test.go
parentc533680039762cacbc37db8dc7eed074c3e497be (diff)
downloadgolang-upstream/2011.01.12.tar.gz
Imported Upstream version 2011.01.12upstream/2011.01.12
Diffstat (limited to 'src/pkg/path/match_test.go')
-rw-r--r--src/pkg/path/match_test.go128
1 files changed, 78 insertions, 50 deletions
diff --git a/src/pkg/path/match_test.go b/src/pkg/path/match_test.go
index c02384f92..a1bf508e3 100644
--- a/src/pkg/path/match_test.go
+++ b/src/pkg/path/match_test.go
@@ -16,62 +16,90 @@ type MatchTest struct {
}
var matchTests = []MatchTest{
- MatchTest{"abc", "abc", true, nil},
- MatchTest{"*", "abc", true, nil},
- MatchTest{"*c", "abc", true, nil},
- MatchTest{"a*", "a", true, nil},
- MatchTest{"a*", "abc", true, nil},
- MatchTest{"a*", "ab/c", false, nil},
- MatchTest{"a*/b", "abc/b", true, nil},
- MatchTest{"a*/b", "a/c/b", false, nil},
- MatchTest{"a*b*c*d*e*/f", "axbxcxdxe/f", true, nil},
- MatchTest{"a*b*c*d*e*/f", "axbxcxdxexxx/f", true, nil},
- MatchTest{"a*b*c*d*e*/f", "axbxcxdxe/xxx/f", false, nil},
- MatchTest{"a*b*c*d*e*/f", "axbxcxdxexxx/fff", false, nil},
- MatchTest{"a*b?c*x", "abxbbxdbxebxczzx", true, nil},
- MatchTest{"a*b?c*x", "abxbbxdbxebxczzy", false, nil},
- MatchTest{"ab[c]", "abc", true, nil},
- MatchTest{"ab[b-d]", "abc", true, nil},
- MatchTest{"ab[e-g]", "abc", false, nil},
- MatchTest{"ab[^c]", "abc", false, nil},
- MatchTest{"ab[^b-d]", "abc", false, nil},
- MatchTest{"ab[^e-g]", "abc", true, nil},
- MatchTest{"a\\*b", "a*b", true, nil},
- MatchTest{"a\\*b", "ab", false, nil},
- MatchTest{"a?b", "a☺b", true, nil},
- MatchTest{"a[^a]b", "a☺b", true, nil},
- MatchTest{"a???b", "a☺b", false, nil},
- MatchTest{"a[^a][^a][^a]b", "a☺b", false, nil},
- MatchTest{"[a-ζ]*", "α", true, nil},
- MatchTest{"*[a-ζ]", "A", false, nil},
- MatchTest{"a?b", "a/b", false, nil},
- MatchTest{"a*b", "a/b", false, nil},
- MatchTest{"[\\]a]", "]", true, nil},
- MatchTest{"[\\-]", "-", true, nil},
- MatchTest{"[x\\-]", "x", true, nil},
- MatchTest{"[x\\-]", "-", true, nil},
- MatchTest{"[x\\-]", "z", false, nil},
- MatchTest{"[\\-x]", "x", true, nil},
- MatchTest{"[\\-x]", "-", true, nil},
- MatchTest{"[\\-x]", "a", false, nil},
- MatchTest{"[]a]", "]", false, ErrBadPattern},
- MatchTest{"[-]", "-", false, ErrBadPattern},
- MatchTest{"[x-]", "x", false, ErrBadPattern},
- MatchTest{"[x-]", "-", false, ErrBadPattern},
- MatchTest{"[x-]", "z", false, ErrBadPattern},
- MatchTest{"[-x]", "x", false, ErrBadPattern},
- MatchTest{"[-x]", "-", false, ErrBadPattern},
- MatchTest{"[-x]", "a", false, ErrBadPattern},
- MatchTest{"\\", "a", false, ErrBadPattern},
- MatchTest{"[a-b-c]", "a", false, ErrBadPattern},
- MatchTest{"*x", "xxx", true, nil},
+ {"abc", "abc", true, nil},
+ {"*", "abc", true, nil},
+ {"*c", "abc", true, nil},
+ {"a*", "a", true, nil},
+ {"a*", "abc", true, nil},
+ {"a*", "ab/c", false, nil},
+ {"a*/b", "abc/b", true, nil},
+ {"a*/b", "a/c/b", false, nil},
+ {"a*b*c*d*e*/f", "axbxcxdxe/f", true, nil},
+ {"a*b*c*d*e*/f", "axbxcxdxexxx/f", true, nil},
+ {"a*b*c*d*e*/f", "axbxcxdxe/xxx/f", false, nil},
+ {"a*b*c*d*e*/f", "axbxcxdxexxx/fff", false, nil},
+ {"a*b?c*x", "abxbbxdbxebxczzx", true, nil},
+ {"a*b?c*x", "abxbbxdbxebxczzy", false, nil},
+ {"ab[c]", "abc", true, nil},
+ {"ab[b-d]", "abc", true, nil},
+ {"ab[e-g]", "abc", false, nil},
+ {"ab[^c]", "abc", false, nil},
+ {"ab[^b-d]", "abc", false, nil},
+ {"ab[^e-g]", "abc", true, nil},
+ {"a\\*b", "a*b", true, nil},
+ {"a\\*b", "ab", false, nil},
+ {"a?b", "a☺b", true, nil},
+ {"a[^a]b", "a☺b", true, nil},
+ {"a???b", "a☺b", false, nil},
+ {"a[^a][^a][^a]b", "a☺b", false, nil},
+ {"[a-ζ]*", "α", true, nil},
+ {"*[a-ζ]", "A", false, nil},
+ {"a?b", "a/b", false, nil},
+ {"a*b", "a/b", false, nil},
+ {"[\\]a]", "]", true, nil},
+ {"[\\-]", "-", true, nil},
+ {"[x\\-]", "x", true, nil},
+ {"[x\\-]", "-", true, nil},
+ {"[x\\-]", "z", false, nil},
+ {"[\\-x]", "x", true, nil},
+ {"[\\-x]", "-", true, nil},
+ {"[\\-x]", "a", false, nil},
+ {"[]a]", "]", false, ErrBadPattern},
+ {"[-]", "-", false, ErrBadPattern},
+ {"[x-]", "x", false, ErrBadPattern},
+ {"[x-]", "-", false, ErrBadPattern},
+ {"[x-]", "z", false, ErrBadPattern},
+ {"[-x]", "x", false, ErrBadPattern},
+ {"[-x]", "-", false, ErrBadPattern},
+ {"[-x]", "a", false, ErrBadPattern},
+ {"\\", "a", false, ErrBadPattern},
+ {"[a-b-c]", "a", false, ErrBadPattern},
+ {"*x", "xxx", true, nil},
}
func TestMatch(t *testing.T) {
for _, tt := range matchTests {
ok, err := Match(tt.pattern, tt.s)
if ok != tt.match || err != tt.err {
- t.Errorf("Match(%#q, %#q) = %v, %v want %v, nil\n", tt.pattern, tt.s, ok, err, tt.match)
+ t.Errorf("Match(%#q, %#q) = %v, %v want %v, nil", tt.pattern, tt.s, ok, err, tt.match)
+ }
+ }
+}
+
+// contains returns true if vector contains the string s.
+func contains(vector []string, s string) bool {
+ for _, elem := range vector {
+ if elem == s {
+ return true
+ }
+ }
+ return false
+}
+
+var globTests = []struct {
+ pattern, result string
+}{
+ {"match.go", "match.go"},
+ {"mat?h.go", "match.go"},
+ {"*", "match.go"},
+ {"../*/match.go", "../path/match.go"},
+}
+
+func TestGlob(t *testing.T) {
+ for _, tt := range globTests {
+ matches := Glob(tt.pattern)
+ if !contains(matches, tt.result) {
+ t.Errorf("Glob(%#q) = %#v want %v", tt.pattern, matches, tt.result)
}
}
}