Debian Codegraph @

gopls/internal/fuzzy/input_test.go

raw symbols SCIP
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.
4
5 package fuzzy_test
6
7 import (
8 "bytes"
9 "slices"
10 "sort"
11 "testing"
12
13 "golang.org/x/tools/gopls/internal/fuzzy"
14 )
15
16 var rolesTests = []struct {
17 str string
18 want string
19 }{
20 {str: "abc::def::goo", want: "Ccc//Ccc//Ccc"},
21 {str: "proto::Message", want: "Ccccc//Ccccccc"},
22 {str: "AbstractSWTFactory", want: "CcccccccCuuCcccccc"},
23 {str: "Abs012", want: "Cccccc"},
24 {str: "/", want: " "},
25 {str: "fOO", want: "CCu"},
26 {str: "fo_oo.o_oo", want: "Cc Cc/C Cc"},
27 }
28
29 func rolesString(roles []fuzzy.RuneRole) string {
30 var buf bytes.Buffer
31 for _, r := range roles {
32 buf.WriteByte(" /cuC"[int(r)])
33 }
34 return buf.String()
35 }
36
37 func TestRoles(t *testing.T) {
38 for _, tc := range rolesTests {
39 gotRoles := make([]fuzzy.RuneRole, len(tc.str))
40 fuzzy.RuneRoles([]byte(tc.str), gotRoles)
41 got := rolesString(gotRoles)
42 if got != tc.want {
43 t.Errorf("roles(%s) = %v; want %v", tc.str, got, tc.want)
44 }
45 }
46 }
47
48 var wordSplitTests = []struct {
49 input string
50 want []string
51 }{
52 {
53 input: "foo bar baz",
54 want: []string{"foo", "bar", "baz"},
55 },
56 {
57 input: "fooBarBaz",
58 want: []string{"foo", "Bar", "Baz"},
59 },
60 {
61 input: "FOOBarBAZ",
62 want: []string{"FOO", "Bar", "BAZ"},
63 },
64 {
65 input: "foo123_bar2Baz3",
66 want: []string{"foo123", "bar2", "Baz3"},
67 },
68 }
69
70 func TestWordSplit(t *testing.T) {
71 for _, tc := range wordSplitTests {
72 roles := fuzzy.RuneRoles([]byte(tc.input), nil)
73
74 var got []string
75 consumer := func(i, j int) {
76 got = append(got, tc.input[i:j])
77 }
78 fuzzy.Words(roles, consumer)
79
80 if eq := diffStringLists(tc.want, got); !eq {
81 t.Errorf("input %v: (want %v -> got %v)", tc.input, tc.want, got)
82 }
83 }
84 }
85
86 func diffStringLists(a, b []string) bool {
87 sort.Strings(a)
88 sort.Strings(b)
89 return slices.Equal(a, b)
90 }
91
92 var lastSegmentSplitTests = []struct {
93 str string
94 want string
95 }{
96 {
97 str: "identifier",
98 want: "identifier",
99 },
100 {
101 str: "two_words",
102 want: "two_words",
103 },
104 {
105 str: "first::second",
106 want: "second",
107 },
108 {
109 str: "foo.bar.FOOBar_buz123_test",
110 want: "FOOBar_buz123_test",
111 },
112 }
113
114 func TestLastSegment(t *testing.T) {
115 for _, tc := range lastSegmentSplitTests {
116 roles := fuzzy.RuneRoles([]byte(tc.str), nil)
117
118 got := fuzzy.LastSegment(tc.str, roles)
119
120 if got != tc.want {
121 t.Errorf("str %v: want %v; got %v", tc.str, tc.want, got)
122 }
123 }
124 }
125
126 func BenchmarkRoles(b *testing.B) {
127 str := "AbstractSWTFactory"
128 out := make([]fuzzy.RuneRole, len(str))
129
130 for b.Loop() {
131 fuzzy.RuneRoles([]byte(str), out)
132 }
133 b.SetBytes(int64(len(str)))
134 }