| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package fuzzy_test |
| 9 |
|
| 10 |
import ( |
| 11 |
"bytes" |
| 12 |
"fmt" |
| 13 |
"math" |
| 14 |
"testing" |
| 15 |
|
| 16 |
"golang.org/x/tools/gopls/internal/fuzzy" |
| 17 |
) |
| 18 |
|
| 19 |
type comparator struct { |
| 20 |
f func(val, ref float32) bool |
| 21 |
descr string |
| 22 |
} |
| 23 |
|
| 24 |
var ( |
| 25 |
eq = comparator{ |
| 26 |
f: func(val, ref float32) bool { |
| 27 |
return val == ref |
| 28 |
}, |
| 29 |
descr: "==", |
| 30 |
} |
| 31 |
ge = comparator{ |
| 32 |
f: func(val, ref float32) bool { |
| 33 |
return val >= ref |
| 34 |
}, |
| 35 |
descr: ">=", |
| 36 |
} |
| 37 |
gt = comparator{ |
| 38 |
f: func(val, ref float32) bool { |
| 39 |
return val > ref |
| 40 |
}, |
| 41 |
descr: ">", |
| 42 |
} |
| 43 |
) |
| 44 |
|
| 45 |
func (c comparator) eval(val, ref float32) bool { |
| 46 |
return c.f(val, ref) |
| 47 |
} |
| 48 |
|
| 49 |
func (c comparator) String() string { |
| 50 |
return c.descr |
| 51 |
} |
| 52 |
|
| 53 |
type scoreTest struct { |
| 54 |
candidate string |
| 55 |
comparator |
| 56 |
ref float32 |
| 57 |
} |
| 58 |
|
| 59 |
var matcherTests = []struct { |
| 60 |
pattern string |
| 61 |
tests []scoreTest |
| 62 |
}{ |
| 63 |
{ |
| 64 |
pattern: "", |
| 65 |
tests: []scoreTest{ |
| 66 |
{"def", eq, 1}, |
| 67 |
{"Ab stuff c", eq, 1}, |
| 68 |
}, |
| 69 |
}, |
| 70 |
{ |
| 71 |
pattern: "abc", |
| 72 |
tests: []scoreTest{ |
| 73 |
{"def", eq, 0}, |
| 74 |
{"abd", eq, 0}, |
| 75 |
{"abc", ge, 0}, |
| 76 |
{"Abc", ge, 0}, |
| 77 |
{"Ab stuff c", ge, 0}, |
| 78 |
}, |
| 79 |
}, |
| 80 |
{ |
| 81 |
pattern: "Abc", |
| 82 |
tests: []scoreTest{ |
| 83 |
{"def", eq, 0}, |
| 84 |
{"abd", eq, 0}, |
| 85 |
{"abc", ge, 0}, |
| 86 |
{"Abc", ge, 0}, |
| 87 |
{"Ab stuff c", ge, 0}, |
| 88 |
}, |
| 89 |
}, |
| 90 |
{ |
| 91 |
pattern: "U", |
| 92 |
tests: []scoreTest{ |
| 93 |
{"ErrUnexpectedEOF", gt, 0}, |
| 94 |
{"ErrUnexpectedEOF.Error", eq, 0}, |
| 95 |
}, |
| 96 |
}, |
| 97 |
} |
| 98 |
|
| 99 |
func TestScore(t *testing.T) { |
| 100 |
for _, tc := range matcherTests { |
| 101 |
m := fuzzy.NewMatcher(tc.pattern) |
| 102 |
for _, sct := range tc.tests { |
| 103 |
score := m.Score(sct.candidate) |
| 104 |
if !sct.comparator.eval(score, sct.ref) { |
| 105 |
t.Errorf("m.Score(%q) = %.2g, want %s %v", sct.candidate, score, sct.comparator, sct.ref) |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
var compareCandidatesTestCases = []struct { |
| 112 |
pattern string |
| 113 |
|
| 114 |
|
| 115 |
orderedCandidates [][]string |
| 116 |
}{ |
| 117 |
{ |
| 118 |
pattern: "Foo", |
| 119 |
orderedCandidates: [][]string{ |
| 120 |
{"Barfoo"}, |
| 121 |
{"Faoo"}, |
| 122 |
{"F_o_o"}, |
| 123 |
{"FaoFooa", "BarFoo"}, |
| 124 |
{"F__oo", "F_oo"}, |
| 125 |
{"FooA", "FooBar", "Foo"}, |
| 126 |
}, |
| 127 |
}, |
| 128 |
{ |
| 129 |
pattern: "U", |
| 130 |
orderedCandidates: [][]string{ |
| 131 |
{"ErrUnexpectedEOF.Error"}, |
| 132 |
{"ErrUnexpectedEOF"}, |
| 133 |
}, |
| 134 |
}, |
| 135 |
{ |
| 136 |
pattern: "N", |
| 137 |
orderedCandidates: [][]string{ |
| 138 |
{"name"}, |
| 139 |
{"Name"}, |
| 140 |
}, |
| 141 |
}, |
| 142 |
} |
| 143 |
|
| 144 |
func TestCompareCandidateScores(t *testing.T) { |
| 145 |
for _, tc := range compareCandidatesTestCases { |
| 146 |
m := fuzzy.NewMatcher(tc.pattern) |
| 147 |
|
| 148 |
var prevScore float32 |
| 149 |
var prevCandGroup []string |
| 150 |
for i, candGroup := range tc.orderedCandidates { |
| 151 |
var groupScore float32 |
| 152 |
for j, cand := range candGroup { |
| 153 |
score := m.Score(cand) |
| 154 |
if j > 0 && score != groupScore { |
| 155 |
t.Fatalf("score %f of %q different than group", score, cand) |
| 156 |
} |
| 157 |
groupScore = score |
| 158 |
} |
| 159 |
|
| 160 |
if i > 0 && prevScore >= groupScore { |
| 161 |
t.Errorf("%s[=%v] is not scored higher than %s[=%v]", candGroup, groupScore, prevCandGroup, prevScore) |
| 162 |
} |
| 163 |
if groupScore < 0 || groupScore > 1 { |
| 164 |
t.Errorf("%s score is %v; want value between [0, 1]", candGroup, groupScore) |
| 165 |
} |
| 166 |
prevScore = groupScore |
| 167 |
prevCandGroup = candGroup |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
var fuzzyMatcherTestCases = []struct { |
| 173 |
p string |
| 174 |
str string |
| 175 |
want string |
| 176 |
}{ |
| 177 |
{p: "foo", str: "abc::foo", want: "abc::[foo]"}, |
| 178 |
{p: "foo", str: "foo.foo", want: "foo.[foo]"}, |
| 179 |
{p: "foo", str: "fo_oo.o_oo", want: "[fo]_oo.[o]_oo"}, |
| 180 |
{p: "foo", str: "fo_oo.fo_oo", want: "fo_oo.[fo]_[o]o"}, |
| 181 |
{p: "fo_o", str: "fo_oo.o_oo", want: "[f]o_oo.[o_o]o"}, |
| 182 |
{p: "fOO", str: "fo_oo.o_oo", want: "[f]o_oo.[o]_[o]o"}, |
| 183 |
{p: "tedit", str: "foo.TextEdit", want: "foo.[T]ext[Edit]"}, |
| 184 |
{p: "TEdit", str: "foo.TextEdit", want: "foo.[T]ext[Edit]"}, |
| 185 |
{p: "Tedit", str: "foo.TextEdit", want: "foo.[T]ext[Edit]"}, |
| 186 |
{p: "Tedit", str: "foo.Textedit", want: "foo.[Te]xte[dit]"}, |
| 187 |
{p: "TEdit", str: "foo.Textedit", want: ""}, |
| 188 |
{p: "te", str: "foo.Textedit", want: "foo.[Te]xtedit"}, |
| 189 |
{p: "ee", str: "foo.Textedit", want: ""}, |
| 190 |
{p: "ex", str: "foo.Textedit", want: "foo.T[ex]tedit"}, |
| 191 |
{p: "exdi", str: "foo.Textedit", want: ""}, |
| 192 |
{p: "exdit", str: "foo.Textedit", want: ""}, |
| 193 |
{p: "extdit", str: "foo.Textedit", want: "foo.T[ext]e[dit]"}, |
| 194 |
{p: "e", str: "foo.Textedit", want: "foo.T[e]xtedit"}, |
| 195 |
{p: "E", str: "foo.Textedit", want: "foo.T[e]xtedit"}, |
| 196 |
{p: "ed", str: "foo.Textedit", want: "foo.Text[ed]it"}, |
| 197 |
{p: "edt", str: "foo.Textedit", want: ""}, |
| 198 |
{p: "edit", str: "foo.Textedit", want: "foo.Text[edit]"}, |
| 199 |
{p: "edin", str: "foo.TexteditNum", want: "foo.Text[edi]t[N]um"}, |
| 200 |
{p: "n", str: "node.GoNodeMax", want: "[n]ode.GoNodeMax"}, |
| 201 |
{p: "N", str: "node.GoNodeMax", want: "[n]ode.GoNodeMax"}, |
| 202 |
{p: "completio", str: "completion", want: "[completio]n"}, |
| 203 |
{p: "completio", str: "completion.None", want: "[completio]n.None"}, |
| 204 |
} |
| 205 |
|
| 206 |
func TestFuzzyMatcherRanges(t *testing.T) { |
| 207 |
for _, tc := range fuzzyMatcherTestCases { |
| 208 |
matcher := fuzzy.NewMatcher(tc.p) |
| 209 |
score := matcher.Score(tc.str) |
| 210 |
if tc.want == "" { |
| 211 |
if score > 0 { |
| 212 |
t.Errorf("Score(%s, %s) = %v; want: <= 0", tc.p, tc.str, score) |
| 213 |
} |
| 214 |
continue |
| 215 |
} |
| 216 |
if score < 0 { |
| 217 |
t.Errorf("Score(%s, %s) = %v, want: > 0", tc.p, tc.str, score) |
| 218 |
continue |
| 219 |
} |
| 220 |
got := highlightMatches(tc.str, matcher) |
| 221 |
if tc.want != got { |
| 222 |
t.Errorf("highlightMatches(%s, %s) = %v, want: %v", tc.p, tc.str, got, tc.want) |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
var scoreTestCases = []struct { |
| 228 |
p string |
| 229 |
str string |
| 230 |
want float64 |
| 231 |
}{ |
| 232 |
|
| 233 |
|
| 234 |
{p: "abc", str: "abc", want: 1}, |
| 235 |
{p: "abc", str: "Abc", want: 1}, |
| 236 |
{p: "abc", str: "Abcdef", want: 1}, |
| 237 |
{p: "strc", str: "StrCat", want: 1}, |
| 238 |
{p: "abc_def", str: "abc_def_xyz", want: 1}, |
| 239 |
{p: "abcdef", str: "abc_def_xyz", want: 0.91667}, |
| 240 |
{p: "abcxyz", str: "abc_def_xyz", want: 0.91667}, |
| 241 |
{p: "sc", str: "StrCat", want: 0.75}, |
| 242 |
{p: "abc", str: "AbstrBasicCtor", want: 0.83333}, |
| 243 |
{p: "foo", str: "abc::foo", want: 0.91667}, |
| 244 |
{p: "afoo", str: "abc::foo", want: 0.9375}, |
| 245 |
{p: "abr", str: "abc::bar", want: 0.5}, |
| 246 |
{p: "br", str: "abc::bar", want: 0.25}, |
| 247 |
{p: "aar", str: "abc::bar", want: 0.41667}, |
| 248 |
{p: "edin", str: "foo.TexteditNum", want: 0.125}, |
| 249 |
{p: "ediu", str: "foo.TexteditNum", want: 0}, |
| 250 |
|
| 251 |
{p: "up", str: "unique_ptr", want: 0.75}, |
| 252 |
{p: "up", str: "upper_bound", want: 1}, |
| 253 |
} |
| 254 |
|
| 255 |
func TestScores(t *testing.T) { |
| 256 |
for _, tc := range scoreTestCases { |
| 257 |
matcher := fuzzy.NewMatcher(tc.p) |
| 258 |
got := math.Round(float64(matcher.Score(tc.str))*1e5) / 1e5 |
| 259 |
if got != tc.want { |
| 260 |
t.Errorf("Score(%s, %s) = %v, want: %v", tc.p, tc.str, got, tc.want) |
| 261 |
} |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
func highlightMatches(str string, matcher *fuzzy.Matcher) string { |
| 266 |
matches := matcher.MatchedRanges() |
| 267 |
|
| 268 |
var buf bytes.Buffer |
| 269 |
index := 0 |
| 270 |
for i := 0; i < len(matches)-1; i += 2 { |
| 271 |
s, e := matches[i], matches[i+1] |
| 272 |
fmt.Fprintf(&buf, "%s[%s]", str[index:s], str[s:e]) |
| 273 |
index = e |
| 274 |
} |
| 275 |
buf.WriteString(str[index:]) |
| 276 |
return buf.String() |
| 277 |
} |
| 278 |
|
| 279 |
func BenchmarkMatcher(b *testing.B) { |
| 280 |
pattern := "Foo" |
| 281 |
candidates := []string{ |
| 282 |
"F_o_o", |
| 283 |
"Barfoo", |
| 284 |
"Faoo", |
| 285 |
"F__oo", |
| 286 |
"F_oo", |
| 287 |
"FaoFooa", |
| 288 |
"BarFoo", |
| 289 |
"FooA", |
| 290 |
"FooBar", |
| 291 |
"Foo", |
| 292 |
} |
| 293 |
|
| 294 |
matcher := fuzzy.NewMatcher(pattern) |
| 295 |
|
| 296 |
for b.Loop() { |
| 297 |
for _, c := range candidates { |
| 298 |
matcher.Score(c) |
| 299 |
} |
| 300 |
} |
| 301 |
var numBytes int |
| 302 |
for _, c := range candidates { |
| 303 |
numBytes += len(c) |
| 304 |
} |
| 305 |
b.SetBytes(int64(numBytes)) |
| 306 |
} |