Source file
src/testing/match_test.go
Documentation: testing
1
2
3
4
5 package testing
6
7 import (
8 "reflect"
9 "regexp"
10 "unicode"
11 )
12
13
14 func TestIsSpace(t *T) {
15 n := 0
16 for r := rune(0); r <= unicode.MaxRune; r++ {
17 if isSpace(r) != unicode.IsSpace(r) {
18 t.Errorf("IsSpace(%U)=%t incorrect", r, isSpace(r))
19 n++
20 if n > 10 {
21 return
22 }
23 }
24 }
25 }
26
27 func TestSplitRegexp(t *T) {
28 res := func(s ...string) []string { return s }
29 testCases := []struct {
30 pattern string
31 result []string
32 }{
33
34
35
36 {"", res("")},
37 {"/", res("", "")},
38 {"//", res("", "", "")},
39 {"A", res("A")},
40 {"A/B", res("A", "B")},
41 {"A/B/", res("A", "B", "")},
42 {"/A/B/", res("", "A", "B", "")},
43 {"[A]/(B)", res("[A]", "(B)")},
44 {"[/]/[/]", res("[/]", "[/]")},
45 {"[/]/[:/]", res("[/]", "[:/]")},
46 {"/]", res("", "]")},
47 {"]/", res("]", "")},
48 {"]/[/]", res("]", "[/]")},
49 {`([)/][(])`, res(`([)/][(])`)},
50 {"[(]/[)]", res("[(]", "[)]")},
51
52
53
54 {")/", res(")/")},
55 {")/(/)", res(")/(", ")")},
56 {"a[/)b", res("a[/)b")},
57 {"(/]", res("(/]")},
58 {"(/", res("(/")},
59 {"[/]/[/", res("[/]", "[/")},
60 {`\p{/}`, res(`\p{`, "}")},
61 {`\p/`, res(`\p`, "")},
62 {`[[:/:]]`, res(`[[:/:]]`)},
63 }
64 for _, tc := range testCases {
65 a := splitRegexp(tc.pattern)
66 if !reflect.DeepEqual(a, tc.result) {
67 t.Errorf("splitRegexp(%q) = %#v; want %#v", tc.pattern, a, tc.result)
68 }
69
70
71
72 if _, err := regexp.Compile(tc.pattern); err != nil {
73 ok := true
74 for _, re := range a {
75 if _, err := regexp.Compile(re); err != nil {
76 ok = false
77 }
78 }
79 if ok {
80 t.Errorf("%s: expected error in any of %q", tc.pattern, a)
81 }
82 }
83 }
84 }
85
86 func TestMatcher(t *T) {
87 testCases := []struct {
88 pattern string
89 parent, sub string
90 ok bool
91 partial bool
92 }{
93
94 {"", "", "TestFoo", true, false},
95 {"TestFoo", "", "TestFoo", true, false},
96 {"TestFoo/", "", "TestFoo", true, true},
97 {"TestFoo/bar/baz", "", "TestFoo", true, true},
98 {"TestFoo", "", "TestBar", false, false},
99 {"TestFoo/", "", "TestBar", false, false},
100 {"TestFoo/bar/baz", "", "TestBar/bar/baz", false, false},
101
102
103 {"", "TestFoo", "x", true, false},
104 {"TestFoo", "TestFoo", "x", true, false},
105 {"TestFoo/", "TestFoo", "x", true, false},
106 {"TestFoo/bar/baz", "TestFoo", "bar", true, true},
107
108
109 {"TestFoo/bar/baz", "TestFoo", "bar/baz", true, false},
110 {"TestFoo/bar/baz", "TestFoo/bar", "baz", true, false},
111 {"TestFoo/bar/baz", "TestFoo", "x", false, false},
112 {"TestFoo", "TestBar", "x", false, false},
113 {"TestFoo/", "TestBar", "x", false, false},
114 {"TestFoo/bar/baz", "TestBar", "x/bar/baz", false, false},
115
116
117 {"", "TestFoo", "x", true, false},
118 {"/", "TestFoo", "x", true, false},
119 {"./", "TestFoo", "x", true, false},
120 {"./.", "TestFoo", "x", true, false},
121 {"/bar/baz", "TestFoo", "bar", true, true},
122 {"/bar/baz", "TestFoo", "bar/baz", true, false},
123 {"//baz", "TestFoo", "bar/baz", true, false},
124 {"//", "TestFoo", "bar/baz", true, false},
125 {"/bar/baz", "TestFoo/bar", "baz", true, false},
126 {"//foo", "TestFoo", "bar/baz", false, false},
127 {"/bar/baz", "TestFoo", "x", false, false},
128 {"/bar/baz", "TestBar", "x/bar/baz", false, false},
129 }
130
131 for _, tc := range testCases {
132 m := newMatcher(regexp.MatchString, tc.pattern, "-test.run")
133
134 parent := &common{name: tc.parent}
135 if tc.parent != "" {
136 parent.level = 1
137 }
138 if n, ok, partial := m.fullName(parent, tc.sub); ok != tc.ok || partial != tc.partial {
139 t.Errorf("for pattern %q, fullName(parent=%q, sub=%q) = %q, ok %v partial %v; want ok %v partial %v",
140 tc.pattern, tc.parent, tc.sub, n, ok, partial, tc.ok, tc.partial)
141 }
142 }
143 }
144
145 func TestNaming(t *T) {
146 m := newMatcher(regexp.MatchString, "", "")
147
148 parent := &common{name: "x", level: 1}
149
150
151 m.subNames["x/b"] = 1000
152
153 testCases := []struct {
154 name, want string
155 }{
156
157 {"", "x/#00"},
158 {"", "x/#01"},
159
160 {"t", "x/t"},
161 {"t", "x/t#01"},
162 {"t", "x/t#02"},
163
164 {"a#01", "x/a#01"},
165 {"a", "x/a"},
166 {"a", "x/a#01#01"},
167 {"a", "x/a#02"},
168 {"a", "x/a#03"},
169 {"a#02", "x/a#02#01"},
170
171 {"b", "x/b#1000"},
172 {"b", "x/b#1001"},
173
174
175 {"A:1 B:2", "x/A:1_B:2"},
176 {"s\t\r\u00a0", "x/s___"},
177 {"\x01", `x/\x01`},
178 {"\U0010ffff", `x/\U0010ffff`},
179 }
180
181 for i, tc := range testCases {
182 if got, _, _ := m.fullName(parent, tc.name); got != tc.want {
183 t.Errorf("%d:%s: got %q; want %q", i, tc.name, got, tc.want)
184 }
185 }
186 }
187
View as plain text