-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuzzy_test.go
355 lines (310 loc) · 9.78 KB
/
fuzzy_test.go
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
package fuzzy
import (
"testing"
"github.com/stretchr/testify/assert"
)
var haystack = []string{}
type matchesTestCase struct {
str string
score int
matches []int
matched bool
}
func TestSimpleMatches(t *testing.T) {
testCases := []matchesTestCase{
matchesTestCase{
str: "abcABC",
score: firstRuneBonus + 2*sequentialBonus + 3*unmatchedRunePenalty,
matches: []int{0, 1, 2},
matched: true,
},
matchesTestCase{
str: "ABCabc",
score: firstRuneBonus + 2*sequentialBonus + 3*unmatchedRunePenalty,
matches: []int{0, 1, 2},
matched: true,
},
matchesTestCase{
str: "aXbXcXXX",
score: firstRuneBonus + 5*unmatchedRunePenalty,
matches: []int{0, 2, 4},
matched: true,
},
matchesTestCase{
str: "XaXbcX",
score: leadingRunePenalty + sequentialBonus + 3*unmatchedRunePenalty,
matches: []int{1, 3, 4},
matched: true,
},
matchesTestCase{
str: "XXXXabc",
score: max(4*leadingRunePenalty, maxLeadingRunePenalty) + 2*sequentialBonus + 4*unmatchedRunePenalty,
matches: []int{4, 5, 6},
matched: true,
},
matchesTestCase{
str: "aabbccXabcXabcX",
score: firstRuneBonus + sequentialBonus + 12*unmatchedRunePenalty,
matches: []int{0, 3, 4},
matched: true,
},
matchesTestCase{
str: "Xaabc",
score: 2*leadingRunePenalty + 2*sequentialBonus + 2*unmatchedRunePenalty,
matches: []int{2, 3, 4},
matched: true,
},
matchesTestCase{
str: "\u0061bc",
score: firstRuneBonus + 2*sequentialBonus,
matches: []int{0, 1, 2},
matched: true,
},
// non-matching:
matchesTestCase{
str: "ab",
},
matchesTestCase{
str: "bc",
},
matchesTestCase{
str: "ac",
},
matchesTestCase{
str: "acb",
},
matchesTestCase{
str: "bca",
},
matchesTestCase{
str: "äbc",
},
}
patterns := []string{"abc", "ABC", "Abc", "aBC"}
for _, pattern := range patterns {
for _, testCase := range testCases {
validateMatchesTestCase(t, pattern, testCase)
}
}
}
func TestSpecialCharacters(t *testing.T) {
testCases := []matchesTestCase{
matchesTestCase{
str: "äöüß", // ß is both an uppercase and lowercase character, but we don't want a camelCase bonus for it
score: firstRuneBonus + 3*sequentialBonus,
matches: []int{0, 1, 2, 3},
matched: true,
},
matchesTestCase{
str: "ÄÖÜß",
score: firstRuneBonus + 3*sequentialBonus,
matches: []int{0, 1, 2, 3},
matched: true,
},
}
patterns := []string{"äöüß", "ÄÖÜß", "Äöüß", "äÖÜß"}
for _, pattern := range patterns {
for _, testCase := range testCases {
validateMatchesTestCase(t, pattern, testCase)
}
}
}
func TestLeadingAndTrailingSpecialCharacters(t *testing.T) {
testCase := matchesTestCase{
str: "äöüßXäöüß",
score: max(maxLeadingRunePenalty, 4*leadingRunePenalty) + 8*unmatchedRunePenalty,
matches: []int{4},
matched: true,
}
patterns := []string{"x", "X"}
for _, pattern := range patterns {
validateMatchesTestCase(t, pattern, testCase)
}
}
func TestManyMatchesCauseRecursionLimit(t *testing.T) {
testCase := matchesTestCase{
str: "XXababababababababab" + // 10x2
"abababababababababab" + // 10x2
"abababababababababaB", //10x2
// --> there would be a CamelCase bonus at the end
// but due to the recursion limit, we shouldn't reach it
score: 2*leadingRunePenalty + sequentialBonus + 58*unmatchedRunePenalty,
matches: []int{2, 3},
matched: true,
}
validateMatchesTestCase(t, "ab", testCase)
}
func TestCamelCaseBonus(t *testing.T) {
testCase := matchesTestCase{
str: "XyababaBab", // Xy is used to avoid leading-letter penalities
score: firstRuneBonus + sequentialBonus + camelCaseBonus + 7*unmatchedRunePenalty,
matches: []int{0, 6, 7}, // use the SequentialBonus and CamelCase bonus - don't take the first match
matched: true,
}
validateMatchesTestCase(t, "Xab", testCase)
testCase = matchesTestCase{
str: "thisIsACamelCaseString",
score: max(maxLeadingRunePenalty, 4*leadingRunePenalty) + 4*camelCaseBonus + sequentialBonus + 17*unmatchedRunePenalty,
matches: []int{4, 6, 7, 12, 16},
matched: true,
}
validateMatchesTestCase(t, "IaCCS", testCase)
// testCase = matchesTestCase{
// str: "AbcdaAbcdaAbcda",
// score: firstRuneBonus + 2*camelCaseBonus + sequentialBonus + 11*unmatchedRunePenalty,
// // In this test case, there are two possible results with the exact same score.
// // matches: []int{0, 5, 9, 10},
// matches: []int{0, 4, 5, 10},
// matched: true,
// }
// validateMatchesTestCase(t, "AAAA", testCase)
}
func TestPickEarliestMatchOnTie(t *testing.T) {
testCase := matchesTestCase{
str: "xAxBxBxAxBxBxAxAxBxAxB",
score: leadingRunePenalty + 2*camelCaseBonus + 20*unmatchedRunePenalty,
// In this test case, there are multiple possible results with the exact same score.
// In such cases, we prefer results that match as early as possible.
matches: []int{1, 3},
matched: true,
}
validateMatchesTestCase(t, "AB", testCase)
}
func TestSeparatorBonus(t *testing.T) {
separators := []string{"/", "-", "_", " ", ".", ",", "\\", "\t"}
for _, sep := range separators {
testCase := matchesTestCase{
str: "Xaxbaxba" + sep + "baxb",
score: firstRuneBonus + sequentialBonus + separatorBonus + 10*unmatchedRunePenalty,
matches: []int{0, 1, 9},
matched: true,
}
validateMatchesTestCase(t, "Xab", testCase)
testCase = matchesTestCase{
str: "Xaxbaxb" + sep + sep + "a" + sep + sep + "baxb",
score: firstRuneBonus + 2*separatorBonus + 13*unmatchedRunePenalty,
matches: []int{0, 9, 12},
matched: true,
}
validateMatchesTestCase(t, "Xab", testCase)
}
}
func TestUnicodeLetterCasing(t *testing.T) {
var cyrillicSmallLetterBe = '\u0431'
var cyrillicCapitalLetterBe = '\u0411'
validateLetterCasing(t, cyrillicSmallLetterBe, cyrillicCapitalLetterBe)
var greekSmallLetterOmegaWithPsiliAndPerispomeniAndYpogegrammeni = '\u1FA6'
var greekCapitalLetterOmegaWithPsiliAndPerispomeniAndProsgegrammeni = '\u1FAE'
validateLetterCasing(t, greekSmallLetterOmegaWithPsiliAndPerispomeniAndYpogegrammeni, greekCapitalLetterOmegaWithPsiliAndPerispomeniAndProsgegrammeni)
}
func validateLetterCasing(t *testing.T, smallLetter, captialLetter rune) {
smallString := string([]rune{'x', smallLetter, 'x', smallLetter, 'x'})
capitalString := string([]rune{'X', captialLetter, 'X', captialLetter, 'X'})
testCases := []matchesTestCase{
matchesTestCase{
str: smallString,
score: leadingRunePenalty + 3*unmatchedRunePenalty,
matches: []int{1, 3},
matched: true,
},
matchesTestCase{
str: capitalString,
score: leadingRunePenalty + 3*unmatchedRunePenalty,
matches: []int{1, 3},
matched: true,
},
}
patterns := []string{
string([]rune{smallLetter, smallLetter}),
string([]rune{smallLetter, captialLetter}),
string([]rune{captialLetter, smallLetter}),
string([]rune{captialLetter, captialLetter}),
}
for _, pattern := range patterns {
for _, testCase := range testCases {
validateMatchesTestCase(t, pattern, testCase)
}
}
}
func TestMatchEmptyStrings(t *testing.T) {
testCase := matchesTestCase{
str: "",
score: 0,
matches: []int{},
matched: true,
}
validateMatchesTestCase(t, "", testCase)
}
func TestMatchWithEmptyPattern(t *testing.T) {
testCase := matchesTestCase{
str: "abc",
score: 3 * unmatchedRunePenalty, // No leading-character penality
matches: []int{},
matched: true,
}
validateMatchesTestCase(t, "", testCase)
}
func TestMatchAgainstEmptyString(t *testing.T) {
testCase := matchesTestCase{
str: "",
matched: false,
}
validateMatchesTestCase(t, "abc", testCase)
}
func TestRankDiscardsMismatchs(t *testing.T) {
haystack := []string{"b", "a", "b", "aa", "bb", "aaa", "a", "c"}
expectedRanking := []string{"a", "a", "aa", "aaa"}
matches := Rank("a", haystack)
assert.Len(t, matches, len(expectedRanking))
for i, expected := range expectedRanking {
assert.Equal(t, expected, matches[i].Str)
}
}
func TestRankIsStable(t *testing.T) {
haystack := []string{"b", "a", "a", "b", "aaa", "aa", "bb", "aaa", "a", "c"}
expectedIndices := []int{1, 2, 8, 5, 4, 7}
matches := Rank("a", haystack)
assert.Len(t, matches, len(expectedIndices))
for i, expected := range expectedIndices {
assert.Equal(t, expected, matches[i].Index)
}
}
func TestRankReversedOrder(t *testing.T) {
haystack := []string{"aaa", "aa", "a"}
expectedRanking := []string{"a", "aa", "aaa"}
matches := Rank("a", haystack)
assert.Len(t, matches, len(expectedRanking))
for i, expected := range expectedRanking {
assert.Equal(t, expected, matches[i].Str)
}
}
func TestRankPopulatesAllFields(t *testing.T) {
haystack := []string{"hay", "stack"}
matches := Rank("a", haystack)
assert.Len(t, matches, 2)
assert.Equal(t, "hay", matches[0].Str)
assert.Equal(t, 0, matches[0].Index)
assert.Equal(t, leadingRunePenalty+2*unmatchedRunePenalty, matches[0].Score)
assert.Equal(t, []int{1}, matches[0].MatchedIndexes)
assert.Equal(t, "stack", matches[1].Str)
assert.Equal(t, 1, matches[1].Index)
assert.Equal(t, 2*leadingRunePenalty+4*unmatchedRunePenalty, matches[1].Score)
assert.Equal(t, []int{2}, matches[1].MatchedIndexes)
}
func TestRankMatchesNothing(t *testing.T) {
haystack := []string{"x", "xx"}
matches := Rank("a", haystack)
assert.Len(t, matches, 0)
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
func validateMatchesTestCase(t *testing.T, pattern string, testCase matchesTestCase) {
score, matches, matched := Matches(pattern, testCase.str)
assert.Equal(t, testCase.matched, matched, "Pattern %q, string %q: 'matched' assertion failed", pattern, testCase.str)
assert.EqualValues(t, testCase.matches, matches, "Pattern %q, string %q: 'matches' assertion failed", pattern, testCase.str)
assert.Equal(t, testCase.score, score, "Pattern %q, string %q: 'score' assertion failed", pattern, testCase.str)
}