1
0
mirror of https://github.com/golang/go synced 2024-10-04 22:21:22 -06:00
go/src/pkg/path/match_test.go
Kevin Ballard 20834d644f path: Fix bug in Match with non-greedy stars
path.Match() errors out when testing "*x" against "xxx"
because it matches the star non-greedily. Ensure that
the last chunk consumes the rest of the name.

R=r, rsc
CC=golang-dev
https://golang.org/cl/223050
2010-02-25 09:15:52 -08:00

78 lines
2.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 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 path
import (
"os"
"testing"
)
type MatchTest struct {
pattern, s string
match bool
err os.Error
}
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},
}
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)
}
}
}