1 // Copyright 2020 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
12 func TestParseQuery(t *testing.T) {
17 {"", "anything", false},
18 {"any", "anything", true},
19 {"any$", "anything", false},
20 {"ing$", "anything", true},
21 {"ing$", "anythinG", true},
22 {"inG$", "anything", false},
23 {"^any", "anything", true},
24 {"^any", "Anything", true},
25 {"^Any", "anything", false},
26 {"at", "anything", true},
27 // TODO: this appears to be a bug in the fuzzy matching algorithm. 'At'
28 // should cause a case-sensitive match.
29 // {"At", "anything", false},
30 {"At", "Anything", true},
31 {"'yth", "Anything", true},
32 {"'yti", "Anything", false},
33 {"'any 'thing", "Anything", true},
34 {"anythn nythg", "Anything", true},
35 {"ntx", "Anything", false},
36 {"anythn", "anything", true},
37 {"ing", "anything", true},
38 {"anythn nythgx", "anything", false},
41 for _, test := range tests {
42 matcher := parseQuery(test.query)
43 if score := matcher(test.s); score > 0 != test.wantMatch {
44 t.Errorf("parseQuery(%q) match for %q: %.2g, want match: %t", test.query, test.s, score, test.wantMatch)
49 func TestBestMatch(t *testing.T) {
58 desc: "shortest match",
59 symbol: "foo/bar/baz.quux",
60 matcher: func(string) float64 { return 1.0 },
65 desc: "partial match",
66 symbol: "foo/bar/baz.quux",
67 matcher: func(s string) float64 {
68 if strings.HasPrefix(s, "bar") {
73 wantMatch: "bar/baz.quux",
77 desc: "longest match",
78 symbol: "foo/bar/baz.quux",
79 matcher: func(s string) float64 {
80 parts := strings.Split(s, "/")
81 return float64(len(parts))
83 wantMatch: "foo/bar/baz.quux",
88 for _, test := range tests {
90 t.Run(test.desc, func(t *testing.T) {
91 gotMatch, gotScore := bestMatch(test.symbol, test.matcher)
92 if gotMatch != test.wantMatch || gotScore != test.wantScore {
93 t.Errorf("bestMatch(%q, matcher) = (%q, %.2g), want (%q, %.2g)", test.symbol, gotMatch, gotScore, test.wantMatch, test.wantScore)