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 / definition_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         "path"
9         "strings"
10         "testing"
11
12         "golang.org/x/tools/internal/lsp/tests"
13 )
14
15 const internalDefinition = `
16 -- go.mod --
17 module mod.com
18
19 go 1.12
20 -- main.go --
21 package main
22
23 import "fmt"
24
25 func main() {
26         fmt.Println(message)
27 }
28 -- const.go --
29 package main
30
31 const message = "Hello World."
32 `
33
34 func TestGoToInternalDefinition(t *testing.T) {
35         runner.Run(t, internalDefinition, func(t *testing.T, env *Env) {
36                 env.OpenFile("main.go")
37                 name, pos := env.GoToDefinition("main.go", env.RegexpSearch("main.go", "message"))
38                 if want := "const.go"; name != want {
39                         t.Errorf("GoToDefinition: got file %q, want %q", name, want)
40                 }
41                 if want := env.RegexpSearch("const.go", "message"); pos != want {
42                         t.Errorf("GoToDefinition: got position %v, want %v", pos, want)
43                 }
44         })
45 }
46
47 const stdlibDefinition = `
48 -- go.mod --
49 module mod.com
50
51 -- main.go --
52 package main
53
54 import "fmt"
55
56 func main() {
57         fmt.Printf()
58 }`
59
60 func TestGoToStdlibDefinition_Issue37045(t *testing.T) {
61         runner.Run(t, stdlibDefinition, func(t *testing.T, env *Env) {
62                 env.OpenFile("main.go")
63                 name, pos := env.GoToDefinition("main.go", env.RegexpSearch("main.go", `fmt.(Printf)`))
64                 if got, want := path.Base(name), "print.go"; got != want {
65                         t.Errorf("GoToDefinition: got file %q, want %q", name, want)
66                 }
67
68                 // Test that we can jump to definition from outside our workspace.
69                 // See golang.org/issues/37045.
70                 newName, newPos := env.GoToDefinition(name, pos)
71                 if newName != name {
72                         t.Errorf("GoToDefinition is not idempotent: got %q, want %q", newName, name)
73                 }
74                 if newPos != pos {
75                         t.Errorf("GoToDefinition is not idempotent: got %v, want %v", newPos, pos)
76                 }
77         })
78 }
79
80 func TestUnexportedStdlib_Issue40809(t *testing.T) {
81         runner.Run(t, stdlibDefinition, func(t *testing.T, env *Env) {
82                 env.OpenFile("main.go")
83                 name, _ := env.GoToDefinition("main.go", env.RegexpSearch("main.go", `fmt.(Printf)`))
84                 env.OpenFile(name)
85
86                 pos := env.RegexpSearch(name, `:=\s*(newPrinter)\(\)`)
87
88                 // Check that we can find references on a reference
89                 refs := env.References(name, pos)
90                 if len(refs) < 5 {
91                         t.Errorf("expected 5+ references to newPrinter, found: %#v", refs)
92                 }
93
94                 name, pos = env.GoToDefinition(name, pos)
95                 content, _ := env.Hover(name, pos)
96                 if !strings.Contains(content.Value, "newPrinter") {
97                         t.Fatal("definition of newPrinter went to the incorrect place")
98                 }
99                 // And on the definition too.
100                 refs = env.References(name, pos)
101                 if len(refs) < 5 {
102                         t.Errorf("expected 5+ references to newPrinter, found: %#v", refs)
103                 }
104         })
105 }
106
107 // Test the hover on an error's Error function.
108 // This can't be done via the marker tests because Error is a builtin.
109 func TestHoverOnError(t *testing.T) {
110         const mod = `
111 -- go.mod --
112 module mod.com
113 -- main.go --
114 package main
115
116 func main() {
117         var err error
118         err.Error()
119 }`
120         run(t, mod, func(t *testing.T, env *Env) {
121                 env.OpenFile("main.go")
122                 content, _ := env.Hover("main.go", env.RegexpSearch("main.go", "Error"))
123                 if content == nil {
124                         t.Fatalf("nil hover content for Error")
125                 }
126                 want := "```go\nfunc (error).Error() string\n```"
127                 if content.Value != want {
128                         t.Fatalf("hover failed:\n%s", tests.Diff(want, content.Value))
129                 }
130         })
131 }