summaryrefslogtreecommitdiff
path: root/src/testing/match_test.go
blob: d31efbc95e6803742aaca39e3a825936791e6b5a (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// Copyright 2015 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 testing

import (
	"fmt"
	"reflect"
	"regexp"
	"strings"
	"unicode"
)

func init() {
	testingTesting = true
}

// Verify that our IsSpace agrees with unicode.IsSpace.
func TestIsSpace(t *T) {
	n := 0
	for r := rune(0); r <= unicode.MaxRune; r++ {
		if isSpace(r) != unicode.IsSpace(r) {
			t.Errorf("IsSpace(%U)=%t incorrect", r, isSpace(r))
			n++
			if n > 10 {
				return
			}
		}
	}
}

func TestSplitRegexp(t *T) {
	res := func(s ...string) filterMatch { return simpleMatch(s) }
	alt := func(m ...filterMatch) filterMatch { return alternationMatch(m) }
	testCases := []struct {
		pattern string
		result  filterMatch
	}{
		// Correct patterns
		// If a regexp pattern is correct, all split regexps need to be correct
		// as well.
		{"", res("")},
		{"/", res("", "")},
		{"//", res("", "", "")},
		{"A", res("A")},
		{"A/B", res("A", "B")},
		{"A/B/", res("A", "B", "")},
		{"/A/B/", res("", "A", "B", "")},
		{"[A]/(B)", res("[A]", "(B)")},
		{"[/]/[/]", res("[/]", "[/]")},
		{"[/]/[:/]", res("[/]", "[:/]")},
		{"/]", res("", "]")},
		{"]/", res("]", "")},
		{"]/[/]", res("]", "[/]")},
		{`([)/][(])`, res(`([)/][(])`)},
		{"[(]/[)]", res("[(]", "[)]")},

		{"A/B|C/D", alt(res("A", "B"), res("C", "D"))},

		// Faulty patterns
		// Errors in original should produce at least one faulty regexp in results.
		{")/", res(")/")},
		{")/(/)", res(")/(", ")")},
		{"a[/)b", res("a[/)b")},
		{"(/]", res("(/]")},
		{"(/", res("(/")},
		{"[/]/[/", res("[/]", "[/")},
		{`\p{/}`, res(`\p{`, "}")},
		{`\p/`, res(`\p`, "")},
		{`[[:/:]]`, res(`[[:/:]]`)},
	}
	for _, tc := range testCases {
		a := splitRegexp(tc.pattern)
		if !reflect.DeepEqual(a, tc.result) {
			t.Errorf("splitRegexp(%q) = %#v; want %#v", tc.pattern, a, tc.result)
		}

		// If there is any error in the pattern, one of the returned subpatterns
		// needs to have an error as well.
		if _, err := regexp.Compile(tc.pattern); err != nil {
			ok := true
			if err := a.verify("", regexp.MatchString); err != nil {
				ok = false
			}
			if ok {
				t.Errorf("%s: expected error in any of %q", tc.pattern, a)
			}
		}
	}
}

func TestMatcher(t *T) {
	testCases := []struct {
		pattern     string
		skip        string
		parent, sub string
		ok          bool
		partial     bool
	}{
		// Behavior without subtests.
		{"", "", "", "TestFoo", true, false},
		{"TestFoo", "", "", "TestFoo", true, false},
		{"TestFoo/", "", "", "TestFoo", true, true},
		{"TestFoo/bar/baz", "", "", "TestFoo", true, true},
		{"TestFoo", "", "", "TestBar", false, false},
		{"TestFoo/", "", "", "TestBar", false, false},
		{"TestFoo/bar/baz", "", "", "TestBar/bar/baz", false, false},
		{"", "TestBar", "", "TestFoo", true, false},
		{"", "TestBar", "", "TestBar", false, false},

		// Skipping a non-existent test doesn't change anything.
		{"", "TestFoo/skipped", "", "TestFoo", true, false},
		{"TestFoo", "TestFoo/skipped", "", "TestFoo", true, false},
		{"TestFoo/", "TestFoo/skipped", "", "TestFoo", true, true},
		{"TestFoo/bar/baz", "TestFoo/skipped", "", "TestFoo", true, true},
		{"TestFoo", "TestFoo/skipped", "", "TestBar", false, false},
		{"TestFoo/", "TestFoo/skipped", "", "TestBar", false, false},
		{"TestFoo/bar/baz", "TestFoo/skipped", "", "TestBar/bar/baz", false, false},

		// with subtests
		{"", "", "TestFoo", "x", true, false},
		{"TestFoo", "", "TestFoo", "x", true, false},
		{"TestFoo/", "", "TestFoo", "x", true, false},
		{"TestFoo/bar/baz", "", "TestFoo", "bar", true, true},

		{"", "TestFoo/skipped", "TestFoo", "x", true, false},
		{"TestFoo", "TestFoo/skipped", "TestFoo", "x", true, false},
		{"TestFoo", "TestFoo/skipped", "TestFoo", "skipped", false, false},
		{"TestFoo/", "TestFoo/skipped", "TestFoo", "x", true, false},
		{"TestFoo/bar/baz", "TestFoo/skipped", "TestFoo", "bar", true, true},

		// Subtest with a '/' in its name still allows for copy and pasted names
		// to match.
		{"TestFoo/bar/baz", "", "TestFoo", "bar/baz", true, false},
		{"TestFoo/bar/baz", "TestFoo/bar/baz", "TestFoo", "bar/baz", false, false},
		{"TestFoo/bar/baz", "TestFoo/bar/baz/skip", "TestFoo", "bar/baz", true, false},
		{"TestFoo/bar/baz", "", "TestFoo/bar", "baz", true, false},
		{"TestFoo/bar/baz", "", "TestFoo", "x", false, false},
		{"TestFoo", "", "TestBar", "x", false, false},
		{"TestFoo/", "", "TestBar", "x", false, false},
		{"TestFoo/bar/baz", "", "TestBar", "x/bar/baz", false, false},

		{"A/B|C/D", "", "TestA", "B", true, false},
		{"A/B|C/D", "", "TestC", "D", true, false},
		{"A/B|C/D", "", "TestA", "C", false, false},

		// subtests only
		{"", "", "TestFoo", "x", true, false},
		{"/", "", "TestFoo", "x", true, false},
		{"./", "", "TestFoo", "x", true, false},
		{"./.", "", "TestFoo", "x", true, false},
		{"/bar/baz", "", "TestFoo", "bar", true, true},
		{"/bar/baz", "", "TestFoo", "bar/baz", true, false},
		{"//baz", "", "TestFoo", "bar/baz", true, false},
		{"//", "", "TestFoo", "bar/baz", true, false},
		{"/bar/baz", "", "TestFoo/bar", "baz", true, false},
		{"//foo", "", "TestFoo", "bar/baz", false, false},
		{"/bar/baz", "", "TestFoo", "x", false, false},
		{"/bar/baz", "", "TestBar", "x/bar/baz", false, false},
	}

	for _, tc := range testCases {
		m := newMatcher(regexp.MatchString, tc.pattern, "-test.run", tc.skip)

		parent := &common{name: tc.parent}
		if tc.parent != "" {
			parent.level = 1
		}
		if n, ok, partial := m.fullName(parent, tc.sub); ok != tc.ok || partial != tc.partial {
			t.Errorf("for pattern %q, fullName(parent=%q, sub=%q) = %q, ok %v partial %v; want ok %v partial %v",
				tc.pattern, tc.parent, tc.sub, n, ok, partial, tc.ok, tc.partial)
		}
	}
}

var namingTestCases = []struct{ name, want string }{
	// Uniqueness
	{"", "x/#00"},
	{"", "x/#01"},
	{"#0", "x/#0"},      // Doesn't conflict with #00 because the number of digits differs.
	{"#00", "x/#00#01"}, // Conflicts with implicit #00 (used above), so add a suffix.
	{"#", "x/#"},
	{"#", "x/##01"},

	{"t", "x/t"},
	{"t", "x/t#01"},
	{"t", "x/t#02"},
	{"t#00", "x/t#00"}, // Explicit "#00" doesn't conflict with the unsuffixed first subtest.

	{"a#01", "x/a#01"},    // user has subtest with this name.
	{"a", "x/a"},          // doesn't conflict with this name.
	{"a", "x/a#02"},       // This string is claimed now, so resume
	{"a", "x/a#03"},       // with counting.
	{"a#02", "x/a#02#01"}, // We already used a#02 once, so add a suffix.

	{"b#00", "x/b#00"},
	{"b", "x/b"}, // Implicit 0 doesn't conflict with explicit "#00".
	{"b", "x/b#01"},
	{"b#9223372036854775807", "x/b#9223372036854775807"}, // MaxInt64
	{"b", "x/b#02"},
	{"b", "x/b#03"},

	// Sanitizing
	{"A:1 B:2", "x/A:1_B:2"},
	{"s\t\r\u00a0", "x/s___"},
	{"\x01", `x/\x01`},
	{"\U0010ffff", `x/\U0010ffff`},
}

func TestNaming(t *T) {
	m := newMatcher(regexp.MatchString, "", "", "")
	parent := &common{name: "x", level: 1} // top-level test.

	for i, tc := range namingTestCases {
		if got, _, _ := m.fullName(parent, tc.name); got != tc.want {
			t.Errorf("%d:%s: got %q; want %q", i, tc.name, got, tc.want)
		}
	}
}

func FuzzNaming(f *F) {
	for _, tc := range namingTestCases {
		f.Add(tc.name)
	}
	parent := &common{name: "x", level: 1}
	var m *matcher
	var seen map[string]string
	reset := func() {
		m = allMatcher()
		seen = make(map[string]string)
	}
	reset()

	f.Fuzz(func(t *T, subname string) {
		if len(subname) > 10 {
			// Long names attract the OOM killer.
			t.Skip()
		}
		name := m.unique(parent.name, subname)
		if !strings.Contains(name, "/"+subname) {
			t.Errorf("name %q does not contain subname %q", name, subname)
		}
		if prev, ok := seen[name]; ok {
			t.Errorf("name %q generated by both %q and %q", name, prev, subname)
		}
		if len(seen) > 1e6 {
			// Free up memory.
			reset()
		}
		seen[name] = subname
	})
}

// GoString returns a string that is more readable than the default, which makes
// it easier to read test errors.
func (m alternationMatch) GoString() string {
	s := make([]string, len(m))
	for i, m := range m {
		s[i] = fmt.Sprintf("%#v", m)
	}
	return fmt.Sprintf("(%s)", strings.Join(s, " | "))
}