github.com/cockroachdb/tools@v0.0.0-20230222021103-a6d27438930d/internal/fuzzy/symbol_test.go (about) 1 // Copyright 2021 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. 4 5 package fuzzy_test 6 7 import ( 8 "testing" 9 10 . "golang.org/x/tools/internal/fuzzy" 11 ) 12 13 func TestSymbolMatchIndex(t *testing.T) { 14 tests := []struct { 15 pattern, input string 16 want int 17 }{ 18 {"test", "foo.TestFoo", 4}, 19 {"test", "test", 0}, 20 {"test", "Test", 0}, 21 {"test", "est", -1}, 22 {"t", "shortest", 7}, 23 {"", "foo", -1}, 24 {"", string([]rune{0}), -1}, // verify that we don't default to an empty pattern. 25 {"anything", "", -1}, 26 } 27 28 for _, test := range tests { 29 matcher := NewSymbolMatcher(test.pattern) 30 if got, _ := matcher.Match([]string{test.input}); got != test.want { 31 t.Errorf("NewSymbolMatcher(%q).Match(%q) = %v, _, want %v, _", test.pattern, test.input, got, test.want) 32 } 33 } 34 } 35 36 func TestSymbolRanking(t *testing.T) { 37 matcher := NewSymbolMatcher("test") 38 39 // symbols to match, in ascending order of ranking. 40 symbols := []string{ 41 "this.is.better.than.most", 42 "test.foo.bar", 43 "atest", 44 "thebest", 45 "test.foo", 46 "test.foo", 47 "tTest", 48 "testage", 49 "foo.test", 50 "test", 51 } 52 prev := 0.0 53 for _, sym := range symbols { 54 _, score := matcher.Match([]string{sym}) 55 t.Logf("Match(%q) = %v", sym, score) 56 if score < prev { 57 t.Errorf("Match(%q) = _, %v, want > %v", sym, score, prev) 58 } 59 prev = score 60 } 61 } 62 63 func TestChunkedMatch(t *testing.T) { 64 matcher := NewSymbolMatcher("test") 65 66 chunked := [][]string{ 67 {"test"}, 68 {"", "test"}, 69 {"test", ""}, 70 {"te", "st"}, 71 } 72 73 for _, chunks := range chunked { 74 offset, score := matcher.Match(chunks) 75 if offset != 0 || score != 1.0 { 76 t.Errorf("Match(%v) = %v, %v, want 0, 1.0", chunks, offset, score) 77 } 78 } 79 }