summaryrefslogtreecommitdiff
path: root/vendor/golang.org/x/text/search/pattern_test.go
blob: 931fa65628951fae5014d9643f1537732d73a29f (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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// 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 search

import (
	"reflect"
	"strings"
	"testing"

	"golang.org/x/text/language"
)

func TestCompile(t *testing.T) {
	for i, tc := range []struct {
		desc    string
		pattern string
		options []Option
		n       int
	}{{
		desc:    "empty",
		pattern: "",
		n:       0,
	}, {
		desc:    "single",
		pattern: "a",
		n:       1,
	}, {
		desc:    "keep modifier",
		pattern: "a\u0300", // U+0300: COMBINING GRAVE ACCENT
		n:       2,
	}, {
		desc:    "remove modifier",
		pattern: "a\u0300", // U+0300: COMBINING GRAVE ACCENT
		options: []Option{IgnoreDiacritics},
		n:       1,
	}, {
		desc:    "single with double collation element",
		pattern: "ä",
		n:       2,
	}, {
		desc:    "leading variable",
		pattern: " a",
		n:       2,
	}, {
		desc:    "trailing variable",
		pattern: "aa ",
		n:       3,
	}, {
		desc:    "leading and trailing variable",
		pattern: " äb ",
		n:       5,
	}, {
		desc:    "keep interior variable",
		pattern: " ä b ",
		n:       6,
	}, {
		desc:    "keep interior variables",
		pattern: " b  ä ",
		n:       7,
	}, {
		desc:    "remove ignoreables (zero-weights across the board)",
		pattern: "\u009Db\u009Dä\u009D", // U+009D: OPERATING SYSTEM COMMAND
		n:       3,
	}} {
		m := New(language.Und, tc.options...)
		p := m.CompileString(tc.pattern)
		if len(p.ce) != tc.n {
			t.Errorf("%d:%s: Compile(%+q): got %d; want %d", i, tc.desc, tc.pattern, len(p.ce), tc.n)
		}
	}
}

func TestNorm(t *testing.T) {
	// U+0300: COMBINING GRAVE ACCENT (CCC=230)
	// U+031B: COMBINING HORN (CCC=216)
	for _, tc := range []struct {
		desc string
		a    string
		b    string
		want bool // a and b compile into the same pattern?
	}{{
		"simple",
		"eee\u0300\u031b",
		"eee\u031b\u0300",
		true,
	}, {
		"large number of modifiers in pattern",
		strings.Repeat("\u0300", 29) + "\u0318",
		"\u0318" + strings.Repeat("\u0300", 29),
		true,
	}, {
		"modifier overflow in pattern",
		strings.Repeat("\u0300", 30) + "\u0318",
		"\u0318" + strings.Repeat("\u0300", 30),
		false,
	}} {
		m := New(language.Und)
		a := m.CompileString(tc.a)
		b := m.CompileString(tc.b)
		if got := reflect.DeepEqual(a, b); got != tc.want {
			t.Errorf("Compile(a) == Compile(b) == %v; want %v", got, tc.want)
		}
	}
}

func TestForwardSearch(t *testing.T) {
	for i, tc := range []struct {
		desc    string
		tag     string
		options []Option
		pattern string
		text    string
		want    []int
	}{{
		// The semantics of an empty search is to match nothing.
		// TODO: change this to be in line with strings.Index? It is quite a
		// different beast, so not sure yet.

		desc:    "empty pattern and text",
		tag:     "und",
		pattern: "",
		text:    "",
		want:    nil, // TODO: consider: []int{0, 0},
	}, {
		desc:    "non-empty pattern and empty text",
		tag:     "und",
		pattern: " ",
		text:    "",
		want:    nil,
	}, {
		desc:    "empty pattern and non-empty text",
		tag:     "und",
		pattern: "",
		text:    "abc",
		want:    nil, // TODO: consider: []int{0, 0, 1, 1, 2, 2, 3, 3},
	}, {
		// Variable-only patterns. We don't support variables at the moment,
		// but verify that, given this, the behavior is indeed as expected.

		desc:    "exact match of variable",
		tag:     "und",
		pattern: " ",
		text:    " ",
		want:    []int{0, 1},
	}, {
		desc:    "variables not handled by default",
		tag:     "und",
		pattern: "- ",
		text:    " -",
		want:    nil, // Would be (1, 2) for a median match with variable}.
	}, {
		desc:    "multiple subsequent identical variables",
		tag:     "und",
		pattern: " ",
		text:    "    ",
		want:    []int{0, 1, 1, 2, 2, 3, 3, 4},
	}, {
		desc:    "text with variables",
		tag:     "und",
		options: []Option{IgnoreDiacritics},
		pattern: "abc",
		text:    "3 abc 3",
		want:    []int{2, 5},
	}, {
		desc:    "pattern with interior variables",
		tag:     "und",
		options: []Option{IgnoreDiacritics},
		pattern: "a b c",
		text:    "3 a b c abc a  b  c 3",
		want:    []int{2, 7}, // Would have 3 matches using variable.

		// TODO: Different variable handling settings.
	}, {
		// Options.

		desc:    "match all levels",
		tag:     "und",
		pattern: "Abc",
		text:    "abcAbcABCÁbcábc",
		want:    []int{3, 6},
	}, {
		desc:    "ignore diacritics in text",
		tag:     "und",
		options: []Option{IgnoreDiacritics},
		pattern: "Abc",
		text:    "Ábc",
		want:    []int{0, 4},
	}, {
		desc:    "ignore diacritics in pattern",
		tag:     "und",
		options: []Option{IgnoreDiacritics},
		pattern: "Ábc",
		text:    "Abc",
		want:    []int{0, 3},
	}, {
		desc:    "ignore diacritics",
		tag:     "und",
		options: []Option{IgnoreDiacritics},
		pattern: "Abc",
		text:    "abcAbcABCÁbcábc",
		want:    []int{3, 6, 9, 13},
	}, {
		desc:    "ignore case",
		tag:     "und",
		options: []Option{IgnoreCase},
		pattern: "Abc",
		text:    "abcAbcABCÁbcábc",
		want:    []int{0, 3, 3, 6, 6, 9},
	}, {
		desc:    "ignore case and diacritics",
		tag:     "und",
		options: []Option{IgnoreCase, IgnoreDiacritics},
		pattern: "Abc",
		text:    "abcAbcABCÁbcábc",
		want:    []int{0, 3, 3, 6, 6, 9, 9, 13, 13, 17},
	}, {
		desc:    "ignore width to fullwidth",
		tag:     "und",
		options: []Option{IgnoreWidth},
		pattern: "abc",
		text:    "123 \uFF41\uFF42\uFF43 123", // U+FF41-3: FULLWIDTH LATIN SMALL LETTER A-C
		want:    []int{4, 13},
	}, {
		// TODO: distinguish between case and width.
		desc:    "don't ignore width to fullwidth, ignoring only case",
		tag:     "und",
		options: []Option{IgnoreCase},
		pattern: "abc",
		text:    "123 \uFF41\uFF42\uFF43 123", // U+FF41-3: FULLWIDTH LATIN SMALL LETTER A-C
		want:    []int{4, 13},
	}, {
		desc:    "ignore width to fullwidth and diacritics",
		tag:     "und",
		options: []Option{IgnoreWidth, IgnoreDiacritics},
		pattern: "abc",
		text:    "123 \uFF41\uFF42\uFF43 123", // U+FF41-3: FULLWIDTH LATIN SMALL LETTER A-C
		want:    []int{4, 13},
	}, {
		desc:    "whole grapheme, single rune",
		tag:     "und",
		pattern: "eee",
		text:    "123 eeé 123",
		want:    nil,
	}, {
		// Note: rules on when to apply contractions may, for certain languages,
		// differ between search and collation. For example, "ch" is not
		// considered a contraction for the purpose of searching in Spanish.
		// Therefore, be careful picking this test.
		desc:    "whole grapheme, contractions",
		tag:     "da",
		pattern: "aba",
		// Fails at the primary level, because "aa" is a contraction.
		text: "123 abaa 123",
		want: []int{},
	}, {
		desc:    "whole grapheme, trailing modifier",
		tag:     "und",
		pattern: "eee",
		text:    "123 eee\u0300 123", // U+0300: COMBINING GRAVE ACCENT
		want:    nil,
	}, {
		// Language-specific matching.

		desc:    "",
		tag:     "da",
		options: []Option{IgnoreCase},
		pattern: "Århus",
		text:    "AarhusÅrhus  Århus  ",
		want:    []int{0, 6, 6, 12, 14, 20},
	}, {
		desc:    "",
		tag:     "da",
		options: []Option{IgnoreCase},
		pattern: "Aarhus",
		text:    "Århus Aarhus",
		want:    []int{0, 6, 7, 13},
	}, {
		desc:    "",
		tag:     "en", // Å does not match A for English.
		options: []Option{IgnoreCase},
		pattern: "Aarhus",
		text:    "Århus",
		want:    nil,
	}, {
		desc:    "ignore modifier in text",
		options: []Option{IgnoreDiacritics},
		tag:     "und",
		pattern: "eee",
		text:    "123 eee\u0300 123", // U+0300: COMBINING GRAVE ACCENT
		want:    []int{4, 9},         // Matches on grapheme boundary.
	}, {
		desc:    "ignore multiple modifiers in text",
		options: []Option{IgnoreDiacritics},
		tag:     "und",
		pattern: "eee",
		text:    "123 eee\u0300\u0300 123", // U+0300: COMBINING GRAVE ACCENT
		want:    []int{4, 11},              // Matches on grapheme boundary.
	}, {
		desc:    "ignore modifier in pattern",
		options: []Option{IgnoreDiacritics},
		tag:     "und",
		pattern: "eee\u0300", // U+0300: COMBINING GRAVE ACCENT
		text:    "123 eee 123",
		want:    []int{4, 7},
	}, {
		desc:    "ignore multiple modifiers in pattern",
		options: []Option{IgnoreDiacritics},
		tag:     "und",
		pattern: "eee\u0300\u0300", // U+0300: COMBINING GRAVE ACCENT
		text:    "123 eee 123",
		want:    []int{4, 7},
	}, {
		desc: "match non-normalized pattern",
		tag:  "und",
		// U+0300: COMBINING GRAVE ACCENT (CCC=230)
		// U+031B: COMBINING HORN (CCC=216)
		pattern: "eee\u0300\u031b",
		text:    "123 eee\u031b\u0300 123",
		want:    []int{4, 11},
	}, {
		desc: "match non-normalized text",
		tag:  "und",
		// U+0300: COMBINING GRAVE ACCENT (CCC=230)
		// U+031B: COMBINING HORN (CCC=216)
		pattern: "eee\u031b\u0300",
		text:    "123 eee\u0300\u031b 123",
		want:    []int{4, 11},
	}} {
		m := New(language.MustParse(tc.tag), tc.options...)
		p := m.CompileString(tc.pattern)
		for j := 0; j < len(tc.text); {
			start, end := p.IndexString(tc.text[j:])
			if start == -1 && end == -1 {
				j++
				continue
			}
			start += j
			end += j
			j = end
			if len(tc.want) == 0 {
				t.Errorf("%d:%s: found unexpected result [%d %d]", i, tc.desc, start, end)
				break
			}
			if tc.want[0] != start || tc.want[1] != end {
				t.Errorf("%d:%s: got [%d %d]; want %v", i, tc.desc, start, end, tc.want[:2])
				tc.want = tc.want[2:]
				break
			}
			tc.want = tc.want[2:]
		}
		if len(tc.want) != 0 {
			t.Errorf("%d:%s: %d extra results", i, tc.desc, len(tc.want)/2)
		}
	}
}