1 // Copyright 2019 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 "golang.org/x/tools/internal/lsp/fuzzy"
15 var rolesTests = []struct {
19 {str: "abc::def::goo", want: "Ccc//Ccc//Ccc"},
20 {str: "proto::Message", want: "Ccccc//Ccccccc"},
21 {str: "AbstractSWTFactory", want: "CcccccccCuuCcccccc"},
22 {str: "Abs012", want: "Cccccc"},
23 {str: "/", want: " "},
24 {str: "fOO", want: "CCu"},
25 {str: "fo_oo.o_oo", want: "Cc Cc/C Cc"},
28 func rolesString(roles []fuzzy.RuneRole) string {
30 for _, r := range roles {
31 buf.WriteByte(" /cuC"[int(r)])
36 func TestRoles(t *testing.T) {
37 for _, tc := range rolesTests {
38 gotRoles := make([]fuzzy.RuneRole, len(tc.str))
39 fuzzy.RuneRoles(tc.str, gotRoles)
40 got := rolesString(gotRoles)
42 t.Errorf("roles(%s) = %v; want %v", tc.str, got, tc.want)
47 var wordSplitTests = []struct {
53 want: []string{"foo", "bar", "baz"},
57 want: []string{"foo", "Bar", "Baz"},
61 want: []string{"FOO", "Bar", "BAZ"},
64 input: "foo123_bar2Baz3",
65 want: []string{"foo123", "bar2", "Baz3"},
69 func TestWordSplit(t *testing.T) {
70 for _, tc := range wordSplitTests {
71 roles := fuzzy.RuneRoles(tc.input, nil)
74 consumer := func(i, j int) {
75 got = append(got, tc.input[i:j])
77 fuzzy.Words(roles, consumer)
79 if eq := diffStringLists(tc.want, got); !eq {
80 t.Errorf("input %v: (want %v -> got %v)", tc.input, tc.want, got)
85 func diffStringLists(a, b []string) bool {
99 var lastSegmentSplitTests = []struct {
112 str: "first::second",
116 str: "foo.bar.FOOBar_buz123_test",
117 want: "FOOBar_buz123_test",
121 func TestLastSegment(t *testing.T) {
122 for _, tc := range lastSegmentSplitTests {
123 roles := fuzzy.RuneRoles(tc.str, nil)
125 got := fuzzy.LastSegment(tc.str, roles)
128 t.Errorf("str %v: want %v; got %v", tc.str, tc.want, got)
133 func BenchmarkRoles(b *testing.B) {
134 str := "AbstractSWTFactory"
135 out := make([]fuzzy.RuneRole, len(str))
137 for i := 0; i < b.N; i++ {
138 fuzzy.RuneRoles(str, out)
140 b.SetBytes(int64(len(str)))