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.
12 "golang.org/x/tools/internal/lsp/tests"
15 const internalDefinition = `
31 const message = "Hello World."
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)
41 if want := env.RegexpSearch("const.go", "message"); pos != want {
42 t.Errorf("GoToDefinition: got position %v, want %v", pos, want)
47 const stdlibDefinition = `
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)
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)
72 t.Errorf("GoToDefinition is not idempotent: got %q, want %q", newName, name)
75 t.Errorf("GoToDefinition is not idempotent: got %v, want %v", newPos, pos)
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)`))
86 pos := env.RegexpSearch(name, `:=\s*(newPrinter)\(\)`)
88 // Check that we can find references on a reference
89 refs := env.References(name, pos)
91 t.Errorf("expected 5+ references to newPrinter, found: %#v", refs)
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")
99 // And on the definition too.
100 refs = env.References(name, pos)
102 t.Errorf("expected 5+ references to newPrinter, found: %#v", refs)
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) {
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"))
124 t.Fatalf("nil hover content for Error")
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))