Debian Codegraph @

gopls/internal/golang/workspace_symbol_test.go

raw symbols SCIP
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.
4
5 package golang
6
7 import (
8 "testing"
9
10 "golang.org/x/tools/gopls/internal/cache"
11 )
12
13 func TestParseQuery(t *testing.T) {
14 tests := []struct {
15 query, s string
16 wantMatch bool
17 }{
18 {"", "anything", false},
19 {"any", "anything", true},
20 {"any$", "anything", false},
21 {"ing$", "anything", true},
22 {"ing$", "anythinG", true},
23 {"inG$", "anything", false},
24 {"^any", "anything", true},
25 {"^any", "Anything", true},
26 {"^Any", "anything", false},
27 {"at", "anything", true},
28 // TODO: this appears to be a bug in the fuzzy matching algorithm. 'At'
29 // should cause a case-sensitive match.
30 // {"At", "anything", false},
31 {"At", "Anything", true},
32 {"'yth", "Anything", true},
33 {"'yti", "Anything", false},
34 {"'any 'thing", "Anything", true},
35 {"anythn nythg", "Anything", true},
36 {"ntx", "Anything", false},
37 {"anythn", "anything", true},
38 {"ing", "anything", true},
39 {"anythn nythgx", "anything", false},
40 }
41
42 for _, test := range tests {
43 matcher := parseQuery(test.query, newFuzzyMatcher)
44 if _, score := matcher([]string{test.s}); score > 0 != test.wantMatch {
45 t.Errorf("parseQuery(%q) match for %q: %.2g, want match: %t", test.query, test.s, score, test.wantMatch)
46 }
47 }
48 }
49
50 func TestPathIncludeFunc(t *testing.T) {
51 tests := []struct {
52 filters []string
53 included []string
54 excluded []string
55 }{
56 {
57 []string{"+**/c.go"},
58 []string{"a/c.go", "a/b/c.go"},
59 []string{},
60 },
61 {
62 []string{"+a/**/c.go"},
63 []string{"a/b/c.go", "a/b/d/c.go", "a/c.go"},
64 []string{},
65 },
66 {
67 []string{"-a/c.go", "+a/**"},
68 []string{"a/c.go"},
69 []string{},
70 },
71 {
72 []string{"+a/**/c.go", "-**/c.go"},
73 []string{},
74 []string{"a/b/c.go"},
75 },
76 {
77 []string{"+a/**/c.go", "-a/**"},
78 []string{},
79 []string{"a/b/c.go"},
80 },
81 {
82 []string{"+**/c.go", "-a/**/c.go"},
83 []string{},
84 []string{"a/b/c.go"},
85 },
86 {
87 []string{"+foobar", "-foo"},
88 []string{"foobar", "foobar/a"},
89 []string{"foo", "foo/a"},
90 },
91 {
92 []string{"+", "-"},
93 []string{},
94 []string{"foobar", "foobar/a", "foo", "foo/a"},
95 },
96 {
97 []string{"-", "+"},
98 []string{"foobar", "foobar/a", "foo", "foo/a"},
99 []string{},
100 },
101 {
102 []string{"-a/**/b/**/c.go"},
103 []string{},
104 []string{"a/x/y/z/b/f/g/h/c.go"},
105 },
106 // tests for unsupported glob operators
107 {
108 []string{"+**/c.go", "-a/*/c.go"},
109 []string{"a/b/c.go"},
110 []string{},
111 },
112 {
113 []string{"+**/c.go", "-a/?/c.go"},
114 []string{"a/b/c.go"},
115 []string{},
116 },
117 {
118 []string{"-b"}, // should only filter paths prefixed with the "b" directory
119 []string{"a/b/c.go", "bb"},
120 []string{"b/c/d.go", "b"},
121 },
122 // golang/vscode-go#3692
123 {
124 []string{"-**/foo", "+**/bar"},
125 []string{"bar/a.go", "a/bar/b.go"},
126 []string{"foo/a.go", "a/foo/b.go"},
127 },
128 }
129
130 for _, test := range tests {
131 pathIncluded := cache.PathIncludeFunc(test.filters)
132 for _, inc := range test.included {
133 if !pathIncluded(inc) {
134 t.Errorf("Filters %v excluded %v, wanted included", test.filters, inc)
135 }
136 }
137
138 for _, exc := range test.excluded {
139 if pathIncluded(exc) {
140 t.Errorf("Filters %v included %v, wanted excluded", test.filters, exc)
141 }
142 }
143 }
144 }