1
0
mirror of https://github.com/golang/go synced 2024-11-05 15:06:09 -07:00
go/internal/lsp/regtest/definition_test.go
Heschi Kreinick c8f3937451 internal/lsp/source: fix unexported references of non-workspace packages
qualifiedObjsAtProtocolPos returned too early. Have it keep looking in
the rest of the candidate packages.

This changes the returned error slightly but AFAICT nobody cares.

Updates golang/go#40809.

Change-Id: Ic8199a484f0abcaa48cb6a3bcdd782195802d670
Reviewed-on: https://go-review.googlesource.com/c/tools/+/249637
Run-TryBot: Heschi Kreinick <heschi@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2020-08-20 18:02:10 +00:00

104 lines
2.6 KiB
Go

// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package regtest
import (
"path"
"strings"
"testing"
)
const internalDefinition = `
-- go.mod --
module mod.com
go 1.12
-- main.go --
package main
import "fmt"
func main() {
fmt.Println(message)
}
-- const.go --
package main
const message = "Hello World."
`
func TestGoToInternalDefinition(t *testing.T) {
runner.Run(t, internalDefinition, func(t *testing.T, env *Env) {
env.OpenFile("main.go")
name, pos := env.GoToDefinition("main.go", env.RegexpSearch("main.go", "message"))
if want := "const.go"; name != want {
t.Errorf("GoToDefinition: got file %q, want %q", name, want)
}
if want := env.RegexpSearch("const.go", "message"); pos != want {
t.Errorf("GoToDefinition: got position %v, want %v", pos, want)
}
})
}
const stdlibDefinition = `
-- go.mod --
module mod.com
-- main.go --
package main
import "fmt"
func main() {
fmt.Printf()
}`
func TestGoToStdlibDefinition_Issue37045(t *testing.T) {
runner.Run(t, stdlibDefinition, func(t *testing.T, env *Env) {
env.OpenFile("main.go")
name, pos := env.GoToDefinition("main.go", env.RegexpSearch("main.go", `fmt.(Printf)`))
if got, want := path.Base(name), "print.go"; got != want {
t.Errorf("GoToDefinition: got file %q, want %q", name, want)
}
// Test that we can jump to definition from outside our workspace.
// See golang.org/issues/37045.
newName, newPos := env.GoToDefinition(name, pos)
if newName != name {
t.Errorf("GoToDefinition is not idempotent: got %q, want %q", newName, name)
}
if newPos != pos {
t.Errorf("GoToDefinition is not idempotent: got %v, want %v", newPos, pos)
}
})
}
func TestUnexportedStdlib_Issue40809(t *testing.T) {
runner.Run(t, stdlibDefinition, func(t *testing.T, env *Env) {
env.OpenFile("main.go")
name, _ := env.GoToDefinition("main.go", env.RegexpSearch("main.go", `fmt.(Printf)`))
env.OpenFile(name)
pos := env.RegexpSearch(name, `:=\s*(newPrinter)\(\)`)
// Check that we can find references on a reference
refs := env.References(name, pos)
if len(refs) < 5 {
t.Errorf("expected 5+ references to newPrinter, found: %#v", refs)
}
name, pos = env.GoToDefinition(name, pos)
content, _ := env.Hover(name, pos)
if !strings.Contains(content.Value, "newPrinter") {
t.Fatal("definition of newPrinter went to the incorrect place")
}
// And on the definition too.
refs = env.References(name, pos)
if len(refs) < 5 {
t.Errorf("expected 5+ references to newPrinter, found: %#v", refs)
}
})
}