2011-03-06 15:33:23 -07:00
|
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
package filepath_test
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"os"
|
2011-04-04 14:09:34 -06:00
|
|
|
|
. "path/filepath"
|
2011-03-06 15:33:23 -07:00
|
|
|
|
"testing"
|
2011-03-16 17:41:23 -06:00
|
|
|
|
"runtime"
|
2011-03-06 15:33:23 -07:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type MatchTest struct {
|
|
|
|
|
pattern, s string
|
|
|
|
|
match bool
|
|
|
|
|
err os.Error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var matchTests = []MatchTest{
|
|
|
|
|
{"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},
|
2011-04-04 14:09:34 -06:00
|
|
|
|
{"[]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},
|
2011-03-06 15:33:23 -07:00
|
|
|
|
{"*x", "xxx", true, nil},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestMatch(t *testing.T) {
|
2011-03-16 17:41:23 -06:00
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
|
// XXX: Don't pass for windows.
|
|
|
|
|
return
|
|
|
|
|
}
|
2011-03-06 15:33:23 -07:00
|
|
|
|
for _, tt := range matchTests {
|
2011-04-04 14:09:34 -06:00
|
|
|
|
ok, err := Match(tt.pattern, tt.s)
|
2011-03-06 15:33:23 -07:00
|
|
|
|
if ok != tt.match || err != tt.err {
|
|
|
|
|
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 {
|
2011-04-04 14:09:34 -06:00
|
|
|
|
s = ToSlash(s)
|
2011-03-06 15:33:23 -07:00
|
|
|
|
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", "../filepath/match.go"},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestGlob(t *testing.T) {
|
2011-03-16 17:41:23 -06:00
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
|
// XXX: Don't pass for windows.
|
|
|
|
|
return
|
|
|
|
|
}
|
2011-03-06 15:33:23 -07:00
|
|
|
|
for _, tt := range globTests {
|
2011-04-04 14:09:34 -06:00
|
|
|
|
matches, err := Glob(tt.pattern)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Errorf("Glob error for %q: %s", tt.pattern, err)
|
|
|
|
|
continue
|
|
|
|
|
}
|
2011-03-06 15:33:23 -07:00
|
|
|
|
if !contains(matches, tt.result) {
|
|
|
|
|
t.Errorf("Glob(%#q) = %#v want %v", tt.pattern, matches, tt.result)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-04-04 14:09:34 -06:00
|
|
|
|
|
|
|
|
|
func TestGlobError(t *testing.T) {
|
|
|
|
|
_, err := Glob("[7]")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Error("expected error for bad pattern; got none")
|
|
|
|
|
}
|
|
|
|
|
}
|