summaryrefslogtreecommitdiff
path: root/src/pkg/path/match_test.go
blob: c02384f9274687fa75da1a99b663f6180a9a1717 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// 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)
		}
	}
}