| 1 |
This test was ported from 'godef' in the old marker tests. |
| 2 |
It tests various hover and definition requests. |
| 3 |
|
| 4 |
-- go.mod -- |
| 5 |
module godef.test |
| 6 |
|
| 7 |
go 1.18 |
| 8 |
|
| 9 |
-- a/a_x_test.go -- |
| 10 |
package a_test |
| 11 |
|
| 12 |
import ( |
| 13 |
"testing" |
| 14 |
) |
| 15 |
|
| 16 |
func TestA2(t *testing.T) { //@hover("TestA2", "TestA2", TestA2) |
| 17 |
Nonexistant() //@diag("Nonexistant", re"(undeclared name|undefined): Nonexistant") |
| 18 |
} |
| 19 |
|
| 20 |
-- @TestA2 -- |
| 21 |
```go |
| 22 |
func TestA2(t *testing.T) |
| 23 |
``` |
| 24 |
-- @ember -- |
| 25 |
```go |
| 26 |
field Member string |
| 27 |
``` |
| 28 |
|
| 29 |
--- |
| 30 |
|
| 31 |
@loc(Member, "Member") |
| 32 |
|
| 33 |
|
| 34 |
--- |
| 35 |
|
| 36 |
[`(a.Thing).Member` on pkg.go.dev](https://pkg.go.dev/godef.test/a#Thing.Member) |
| 37 |
-- a/d.go -- |
| 38 |
package a //@hover("a", _, a) |
| 39 |
|
| 40 |
import "fmt" |
| 41 |
|
| 42 |
type Thing struct { //@loc(Thing, "Thing") |
| 43 |
Member string //@loc(Member, "Member") |
| 44 |
} |
| 45 |
|
| 46 |
var Other Thing //@loc(Other, "Other") |
| 47 |
|
| 48 |
func Things(val []string) []Thing { //@loc(Things, "Things") |
| 49 |
return nil |
| 50 |
} |
| 51 |
|
| 52 |
func (t Thing) Method(i int) string { //@loc(Method, "Method") |
| 53 |
return t.Member |
| 54 |
} |
| 55 |
|
| 56 |
func (t Thing) Method3() { |
| 57 |
} |
| 58 |
|
| 59 |
func (t *Thing) Method2(i int, j int) (error, string) { |
| 60 |
return nil, t.Member |
| 61 |
} |
| 62 |
|
| 63 |
func (t *Thing) private() { |
| 64 |
} |
| 65 |
|
| 66 |
func useThings() { |
| 67 |
t := Thing{ //@hover("ing", "Thing", ing) |
| 68 |
Member: "string", //@hover("ember", "Member", ember), def("ember", Member) |
| 69 |
} |
| 70 |
fmt.Print(t.Member) //@hover("ember", "Member", ember), def("ember", Member) |
| 71 |
fmt.Print(Other) //@hover("ther", "Other", ther), def("ther", Other) |
| 72 |
Things(nil) //@hover("ings", "Things", ings), def("ings", Things) |
| 73 |
t.Method(0) //@hover("eth", "Method", eth), def("eth", Method) |
| 74 |
} |
| 75 |
|
| 76 |
type NextThing struct { //@loc(NextThing, "NextThing") |
| 77 |
Thing |
| 78 |
Value int |
| 79 |
} |
| 80 |
|
| 81 |
func (n NextThing) another() string { |
| 82 |
return n.Member |
| 83 |
} |
| 84 |
|
| 85 |
// Shadows Thing.Method3 |
| 86 |
func (n *NextThing) Method3() int { |
| 87 |
return n.Value |
| 88 |
} |
| 89 |
|
| 90 |
var nextThing NextThing //@hover("NextThing", "NextThing", NextThing), def("NextThing", NextThing) |
| 91 |
|
| 92 |
-- @ings -- |
| 93 |
```go |
| 94 |
func Things(val []string) []Thing |
| 95 |
``` |
| 96 |
|
| 97 |
--- |
| 98 |
|
| 99 |
[`a.Things` on pkg.go.dev](https://pkg.go.dev/godef.test/a#Things) |
| 100 |
-- @ther -- |
| 101 |
```go |
| 102 |
var Other Thing |
| 103 |
``` |
| 104 |
|
| 105 |
--- |
| 106 |
|
| 107 |
@loc(Other, "Other") |
| 108 |
|
| 109 |
|
| 110 |
--- |
| 111 |
|
| 112 |
[`a.Other` on pkg.go.dev](https://pkg.go.dev/godef.test/a#Other) |
| 113 |
-- @a -- |
| 114 |
-- @ing -- |
| 115 |
```go |
| 116 |
type Thing struct { |
| 117 |
Member string //@loc(Member, "Member") |
| 118 |
} |
| 119 |
``` |
| 120 |
|
| 121 |
--- |
| 122 |
|
| 123 |
```go |
| 124 |
func (t Thing) Method(i int) string |
| 125 |
func (t *Thing) Method2(i int, j int) (error, string) |
| 126 |
func (t Thing) Method3() |
| 127 |
func (t *Thing) private() |
| 128 |
``` |
| 129 |
|
| 130 |
--- |
| 131 |
|
| 132 |
[`a.Thing` on pkg.go.dev](https://pkg.go.dev/godef.test/a#Thing) |
| 133 |
-- @NextThing -- |
| 134 |
```go |
| 135 |
type NextThing struct { |
| 136 |
Thing |
| 137 |
Value int |
| 138 |
} |
| 139 |
``` |
| 140 |
|
| 141 |
--- |
| 142 |
|
| 143 |
```go |
| 144 |
// Embedded fields: |
| 145 |
Member string // through Thing |
| 146 |
``` |
| 147 |
|
| 148 |
```go |
| 149 |
func (t Thing) Method(i int) string |
| 150 |
func (t *Thing) Method2(i int, j int) (error, string) |
| 151 |
func (n *NextThing) Method3() int |
| 152 |
func (n NextThing) another() string |
| 153 |
func (t *Thing) private() |
| 154 |
``` |
| 155 |
|
| 156 |
--- |
| 157 |
|
| 158 |
[`a.NextThing` on pkg.go.dev](https://pkg.go.dev/godef.test/a#NextThing) |
| 159 |
-- @eth -- |
| 160 |
```go |
| 161 |
func (t Thing) Method(i int) string |
| 162 |
``` |
| 163 |
|
| 164 |
--- |
| 165 |
|
| 166 |
[`(a.Thing).Method` on pkg.go.dev](https://pkg.go.dev/godef.test/a#Thing.Method) |
| 167 |
-- a/f.go -- |
| 168 |
// Package a is a package for testing go to definition. |
| 169 |
package a |
| 170 |
|
| 171 |
import "fmt" |
| 172 |
|
| 173 |
func TypeStuff() { |
| 174 |
var x string |
| 175 |
|
| 176 |
switch y := any(x).(type) { //@loc(y, "y"), hover("y", "y", y) , def("y", y) |
| 177 |
case int: //@loc(intY, "int") |
| 178 |
fmt.Printf("%v", y) //@hover("y", "y", inty), def("y", y) |
| 179 |
case string: //@loc(stringY, "string") |
| 180 |
fmt.Printf("%v", y) //@hover("y", "y", stringy), def("y", y) |
| 181 |
} |
| 182 |
|
| 183 |
} |
| 184 |
-- @inty -- |
| 185 |
```go |
| 186 |
var y int |
| 187 |
``` |
| 188 |
-- @stringy -- |
| 189 |
```go |
| 190 |
var y string |
| 191 |
``` |
| 192 |
-- @y -- |
| 193 |
```go |
| 194 |
var y any |
| 195 |
``` |
| 196 |
-- a/h.go -- |
| 197 |
package a |
| 198 |
|
| 199 |
func _() { |
| 200 |
type s struct { |
| 201 |
nested struct { |
| 202 |
// nested number |
| 203 |
number int64 //@loc(nestedNumber, "number") |
| 204 |
} |
| 205 |
nested2 []struct { |
| 206 |
// nested string |
| 207 |
str string //@loc(nestedString, "str") |
| 208 |
} |
| 209 |
x struct { |
| 210 |
x struct { |
| 211 |
x struct { |
| 212 |
x struct { |
| 213 |
x struct { |
| 214 |
// nested map |
| 215 |
m map[string]float64 //@loc(nestedMap, "m") |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
var t s |
| 224 |
_ = t.nested.number //@hover("number", "number", nestedNumber), def("number", nestedNumber) |
| 225 |
_ = t.nested2[0].str //@hover("str", "str", nestedString), def("str", nestedString) |
| 226 |
_ = t.x.x.x.x.x.m //@hover("m", "m", nestedMap), def("m", nestedMap) |
| 227 |
} |
| 228 |
|
| 229 |
func _() { |
| 230 |
var s struct { |
| 231 |
// a field |
| 232 |
a int //@loc(structA, "a") |
| 233 |
// b nested struct |
| 234 |
b struct { //@loc(structB, "b") |
| 235 |
// c field of nested struct |
| 236 |
c int //@loc(structC, "c") |
| 237 |
} |
| 238 |
} |
| 239 |
_ = s.a //@def("a", structA) |
| 240 |
_ = s.b //@def("b", structB) |
| 241 |
_ = s.b.c //@def("c", structC) |
| 242 |
|
| 243 |
var arr []struct { |
| 244 |
// d field |
| 245 |
d int //@loc(arrD, "d") |
| 246 |
// e nested struct |
| 247 |
e struct { //@loc(arrE, "e") |
| 248 |
// f field of nested struct |
| 249 |
f int //@loc(arrF, "f") |
| 250 |
} |
| 251 |
} |
| 252 |
_ = arr[0].d //@def("d", arrD) |
| 253 |
_ = arr[0].e //@def("e", arrE) |
| 254 |
_ = arr[0].e.f //@def("f", arrF) |
| 255 |
|
| 256 |
var complex []struct { |
| 257 |
c <-chan map[string][]struct { |
| 258 |
// h field |
| 259 |
h int //@loc(complexH, "h") |
| 260 |
// i nested struct |
| 261 |
i struct { //@loc(complexI, "i") |
| 262 |
// j field of nested struct |
| 263 |
j int //@loc(complexJ, "j") |
| 264 |
} |
| 265 |
} |
| 266 |
} |
| 267 |
_ = (<-complex[0].c)["0"][0].h //@def("h", complexH) |
| 268 |
_ = (<-complex[0].c)["0"][0].i //@def("i", complexI) |
| 269 |
_ = (<-complex[0].c)["0"][0].i.j //@def("j", complexJ) |
| 270 |
|
| 271 |
var mapWithStructKey map[struct { //@diag("struct", re"invalid map key") |
| 272 |
// X key field |
| 273 |
x []string //@loc(mapStructKeyX, "x") |
| 274 |
}]int |
| 275 |
for k := range mapWithStructKey { |
| 276 |
_ = k.x //@def("x", mapStructKeyX) |
| 277 |
} |
| 278 |
|
| 279 |
var mapWithStructKeyAndValue map[struct { |
| 280 |
// Y key field |
| 281 |
y string //@loc(mapStructKeyY, "y") |
| 282 |
}]struct { |
| 283 |
// X value field |
| 284 |
x string //@loc(mapStructValueX, "x") |
| 285 |
} |
| 286 |
for k, v := range mapWithStructKeyAndValue { |
| 287 |
// TODO: we don't show docs for y field because both map key and value |
| 288 |
// are structs. And in this case, we parse only map value |
| 289 |
_ = k.y //@hover("y", "y", hoverStructKeyY), def("y", mapStructKeyY) |
| 290 |
_ = v.x //@hover("x", "x", hoverStructKeyX), def("x", mapStructValueX) |
| 291 |
} |
| 292 |
|
| 293 |
var i []map[string]interface { |
| 294 |
// open method comment |
| 295 |
open() error //@loc(openMethod, "open") |
| 296 |
} |
| 297 |
i[0]["1"].open() //@hover("pen","open", openMethod), def("open", openMethod) |
| 298 |
} |
| 299 |
|
| 300 |
func _() { |
| 301 |
test := struct { |
| 302 |
// test description |
| 303 |
desc string //@loc(testDescription, "desc") |
| 304 |
}{} |
| 305 |
_ = test.desc //@def("desc", testDescription) |
| 306 |
|
| 307 |
for _, tt := range []struct { |
| 308 |
// test input |
| 309 |
in map[string][]struct { //@loc(testInput, "in") |
| 310 |
// test key |
| 311 |
key string //@loc(testInputKey, "key") |
| 312 |
// test value |
| 313 |
value any //@loc(testInputValue, "value") |
| 314 |
} |
| 315 |
result struct { |
| 316 |
v <-chan struct { |
| 317 |
// expected test value |
| 318 |
value int //@loc(testResultValue, "value") |
| 319 |
} |
| 320 |
} |
| 321 |
}{} { |
| 322 |
_ = tt.in //@def("in", testInput) |
| 323 |
_ = tt.in["0"][0].key //@def("key", testInputKey) |
| 324 |
_ = tt.in["0"][0].value //@def("value", testInputValue) |
| 325 |
|
| 326 |
_ = (<-tt.result.v).value //@def("value", testResultValue) |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
func _() { |
| 331 |
getPoints := func() []struct { |
| 332 |
// X coord |
| 333 |
x int //@loc(returnX, "x") |
| 334 |
// Y coord |
| 335 |
y int //@loc(returnY, "y") |
| 336 |
} { |
| 337 |
return nil |
| 338 |
} |
| 339 |
|
| 340 |
r := getPoints() |
| 341 |
_ = r[0].x //@def("x", returnX) |
| 342 |
_ = r[0].y //@def("y", returnY) |
| 343 |
} |
| 344 |
-- @hoverStructKeyX -- |
| 345 |
```go |
| 346 |
field x string |
| 347 |
``` |
| 348 |
|
| 349 |
--- |
| 350 |
|
| 351 |
X value field |
| 352 |
-- @hoverStructKeyY -- |
| 353 |
```go |
| 354 |
field y string |
| 355 |
``` |
| 356 |
|
| 357 |
--- |
| 358 |
|
| 359 |
Y key field |
| 360 |
-- @nestedNumber -- |
| 361 |
```go |
| 362 |
field number int64 |
| 363 |
``` |
| 364 |
|
| 365 |
--- |
| 366 |
|
| 367 |
nested number |
| 368 |
-- @nestedString -- |
| 369 |
```go |
| 370 |
field str string |
| 371 |
``` |
| 372 |
|
| 373 |
--- |
| 374 |
|
| 375 |
nested string |
| 376 |
-- @openMethod -- |
| 377 |
```go |
| 378 |
func (interface) open() error |
| 379 |
``` |
| 380 |
|
| 381 |
--- |
| 382 |
|
| 383 |
open method comment |
| 384 |
-- @nestedMap -- |
| 385 |
```go |
| 386 |
field m map[string]float64 |
| 387 |
``` |
| 388 |
|
| 389 |
--- |
| 390 |
|
| 391 |
nested map |
| 392 |
-- b/e.go -- |
| 393 |
package b |
| 394 |
|
| 395 |
import ( |
| 396 |
"fmt" |
| 397 |
|
| 398 |
"godef.test/a" |
| 399 |
) |
| 400 |
|
| 401 |
func useThings() { |
| 402 |
t := a.Thing{} //@loc(bStructType, "ing") |
| 403 |
fmt.Print(t.Member) //@loc(bMember, "ember") |
| 404 |
fmt.Print(a.Other) //@loc(bVar, "ther") |
| 405 |
a.Things(nil) //@loc(bFunc, "ings") |
| 406 |
} |
| 407 |
|
| 408 |
/*@ |
| 409 |
def(bStructType, Thing) |
| 410 |
def(bMember, Member) |
| 411 |
def(bVar, Other) |
| 412 |
def(bFunc, Things) |
| 413 |
*/ |
| 414 |
|
| 415 |
func _() { |
| 416 |
var x any |
| 417 |
switch x := x.(type) { //@hover("x", "x", xInterface) |
| 418 |
case string: //@loc(eString, "string") |
| 419 |
fmt.Println(x) //@hover("x", "x", xString) |
| 420 |
case int: //@loc(eInt, "int") |
| 421 |
fmt.Println(x) //@hover("x", "x", xInt) |
| 422 |
} |
| 423 |
} |
| 424 |
-- @xInt -- |
| 425 |
```go |
| 426 |
var x int |
| 427 |
``` |
| 428 |
-- @xInterface -- |
| 429 |
```go |
| 430 |
var x any |
| 431 |
``` |
| 432 |
-- @xString -- |
| 433 |
```go |
| 434 |
var x string |
| 435 |
``` |
| 436 |
-- broken/unclosedIf.go -- |
| 437 |
package broken |
| 438 |
|
| 439 |
import "fmt" |
| 440 |
|
| 441 |
func unclosedIf() { |
| 442 |
if false { |
| 443 |
var myUnclosedIf string //@loc(myUnclosedIf, "myUnclosedIf") |
| 444 |
fmt.Printf("s = %v\n", myUnclosedIf) //@def("my", myUnclosedIf) |
| 445 |
} |
| 446 |
|
| 447 |
func _() {} //@diag("_", re"expected") |