Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / tools / gopls@v0.5.2 / internal / regtest / symbol_test.go
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 regtest
6
7 import (
8         "testing"
9
10         "golang.org/x/tools/internal/lsp/protocol"
11 )
12
13 const symbolSetup = `
14 -- go.mod --
15 module mod.com
16
17 go 1.12
18 -- main.go --
19 package main
20
21 import (
22         "encoding/json"
23         "fmt"
24 )
25
26 func main() { // function
27         fmt.Println("Hello")
28 }
29
30 var myvar int // variable
31
32 type myType string // basic type
33
34 type myDecoder json.Decoder // to use the encoding/json import
35
36 func (m *myType) Blahblah() {} // method
37
38 type myStruct struct { // struct type
39         myStructField int // struct field
40 }
41
42 type myInterface interface { // interface
43         DoSomeCoolStuff() string // interface method
44 }
45
46 type embed struct {
47         myStruct
48
49         nestedStruct struct {
50                 nestedField int
51
52                 nestedStruct2 struct {
53                         int
54                 }
55         }
56
57         nestedInterface interface {
58                 myInterface
59                 nestedMethod()
60         }
61 }
62 -- p/p.go --
63 package p
64
65 const Message = "Hello World." // constant
66 `
67
68 var caseSensitiveSymbolChecks = map[string]*expSymbolInformation{
69         "main": {
70                 Name: pString("main.main"),
71                 Kind: pKind(protocol.Function),
72                 Location: &expLocation{
73                         Path: pString("main.go"),
74                         Range: &expRange{
75                                 Start: &expPos{
76                                         Line:   pInt(7),
77                                         Column: pInt(5),
78                                 },
79                         },
80                 },
81         },
82         "Message": {
83                 Name: pString("p.Message"),
84                 Kind: pKind(protocol.Constant),
85                 Location: &expLocation{
86                         Path: pString("p/p.go"),
87                         Range: &expRange{
88                                 Start: &expPos{
89                                         Line:   pInt(2),
90                                         Column: pInt(6),
91                                 },
92                         },
93                 },
94         },
95         "myvar": {
96                 Name: pString("main.myvar"),
97                 Kind: pKind(protocol.Variable),
98                 Location: &expLocation{
99                         Path: pString("main.go"),
100                         Range: &expRange{
101                                 Start: &expPos{
102                                         Line:   pInt(11),
103                                         Column: pInt(4),
104                                 },
105                         },
106                 },
107         },
108         "myType": {
109                 Name: pString("main.myType"),
110                 Kind: pKind(protocol.String),
111                 Location: &expLocation{
112                         Path: pString("main.go"),
113                         Range: &expRange{
114                                 Start: &expPos{
115                                         Line:   pInt(13),
116                                         Column: pInt(5),
117                                 },
118                         },
119                 },
120         },
121         "Blahblah": {
122                 Name: pString("main.myType.Blahblah"),
123                 Kind: pKind(protocol.Method),
124                 Location: &expLocation{
125                         Path: pString("main.go"),
126                         Range: &expRange{
127                                 Start: &expPos{
128                                         Line:   pInt(17),
129                                         Column: pInt(17),
130                                 },
131                         },
132                 },
133         },
134         "NewEncoder": {
135                 Name: pString("json.NewEncoder"),
136                 Kind: pKind(protocol.Function),
137         },
138         "myStruct": {
139                 Name: pString("main.myStruct"),
140                 Kind: pKind(protocol.Struct),
141                 Location: &expLocation{
142                         Path: pString("main.go"),
143                         Range: &expRange{
144                                 Start: &expPos{
145                                         Line:   pInt(19),
146                                         Column: pInt(5),
147                                 },
148                         },
149                 },
150         },
151         // TODO: not sure we should be returning struct fields
152         "myStructField": {
153                 Name: pString("main.myStruct.myStructField"),
154                 Kind: pKind(protocol.Field),
155                 Location: &expLocation{
156                         Path: pString("main.go"),
157                         Range: &expRange{
158                                 Start: &expPos{
159                                         Line:   pInt(20),
160                                         Column: pInt(1),
161                                 },
162                         },
163                 },
164         },
165         "myInterface": {
166                 Name: pString("main.myInterface"),
167                 Kind: pKind(protocol.Interface),
168                 Location: &expLocation{
169                         Path: pString("main.go"),
170                         Range: &expRange{
171                                 Start: &expPos{
172                                         Line:   pInt(23),
173                                         Column: pInt(5),
174                                 },
175                         },
176                 },
177         },
178         // TODO: not sure we should be returning interface methods
179         "DoSomeCoolStuff": {
180                 Name: pString("main.myInterface.DoSomeCoolStuff"),
181                 Kind: pKind(protocol.Method),
182                 Location: &expLocation{
183                         Path: pString("main.go"),
184                         Range: &expRange{
185                                 Start: &expPos{
186                                         Line:   pInt(24),
187                                         Column: pInt(1),
188                                 },
189                         },
190                 },
191         },
192
193         "embed.myStruct": {
194                 Name: pString("main.embed.myStruct"),
195                 Kind: pKind(protocol.Field),
196                 Location: &expLocation{
197                         Path: pString("main.go"),
198                         Range: &expRange{
199                                 Start: &expPos{
200                                         Line:   pInt(28),
201                                         Column: pInt(1),
202                                 },
203                         },
204                 },
205         },
206
207         "nestedStruct2.int": {
208                 Name: pString("main.embed.nestedStruct.nestedStruct2.int"),
209                 Kind: pKind(protocol.Field),
210                 Location: &expLocation{
211                         Path: pString("main.go"),
212                         Range: &expRange{
213                                 Start: &expPos{
214                                         Line:   pInt(34),
215                                         Column: pInt(3),
216                                 },
217                         },
218                 },
219         },
220
221         "nestedInterface.myInterface": {
222                 Name: pString("main.embed.nestedInterface.myInterface"),
223                 Kind: pKind(protocol.Interface),
224                 Location: &expLocation{
225                         Path: pString("main.go"),
226                         Range: &expRange{
227                                 Start: &expPos{
228                                         Line:   pInt(39),
229                                         Column: pInt(2),
230                                 },
231                         },
232                 },
233         },
234
235         "nestedInterface.nestedMethod": {
236                 Name: pString("main.embed.nestedInterface.nestedMethod"),
237                 Kind: pKind(protocol.Method),
238                 Location: &expLocation{
239                         Path: pString("main.go"),
240                         Range: &expRange{
241                                 Start: &expPos{
242                                         Line:   pInt(40),
243                                         Column: pInt(2),
244                                 },
245                         },
246                 },
247         },
248 }
249
250 var caseInsensitiveSymbolChecks = map[string]*expSymbolInformation{
251         "Main": caseSensitiveSymbolChecks["main"],
252 }
253
254 var fuzzySymbolChecks = map[string]*expSymbolInformation{
255         "Mn": caseSensitiveSymbolChecks["main"],
256 }
257
258 // TestSymbolPos tests that, at a basic level, we get the correct position
259 // information for symbols matches that are returned.
260 func TestSymbolPos(t *testing.T) {
261         checkChecks(t, "caseSensitive", caseSensitiveSymbolChecks)
262         checkChecks(t, "caseInsensitive", caseInsensitiveSymbolChecks)
263         checkChecks(t, "fuzzy", fuzzySymbolChecks)
264 }
265
266 func checkChecks(t *testing.T, matcher string, checks map[string]*expSymbolInformation) {
267         t.Helper()
268         withOptions(
269                 EditorConfig{SymbolMatcher: &matcher},
270         ).run(t, symbolSetup, func(t *testing.T, env *Env) {
271                 t.Run(matcher, func(t *testing.T) {
272                         for query, exp := range checks {
273                                 t.Run(query, func(t *testing.T) {
274                                         res := env.Symbol(query)
275                                         if !exp.matchAgainst(res) {
276                                                 t.Fatalf("failed to find a match against query %q for %v,\ngot: %v", query, exp, res)
277                                         }
278                                 })
279                         }
280                 })
281         })
282 }