Skip to content

Commit

Permalink
test:add test of overload function definition
Browse files Browse the repository at this point in the history
  • Loading branch information
luoliwoshang committed Apr 25, 2024
1 parent 69624ac commit da60091
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 1 deletion.
1 change: 0 additions & 1 deletion gopls/internal/lsp/source/definition_gox.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ func GopDefinition(ctx context.Context, snapshot Snapshot, fh FileHandle, positi

// The general case: the cursor is on an identifier.
_, obj, _ := gopReferencedObject(pkg, pgf, pos)

if obj == nil {
return nil, nil
}
Expand Down
105 changes: 105 additions & 0 deletions gopls/internal/regtest/misc/definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,111 @@ import (
"golang.org/x/tools/gopls/internal/lsp/tests/compare"
)

const overloadDefinition1 = `
-- go.mod --
module mod.com
go 1.21.4
-- def.gop --
func add = (
func(a, b int) int {
return a + b
}
func(a, b string) string {
return a + b
}
)
-- test.gop --
println add(100, 7)
`

const overloadDefinition2 = `
-- go.mod --
module mod.com
go 1.21.4
-- def.gop --
func mulInt(a, b int) int {
return a * b
}
func mulFloat(a, b float64) float64 {
return a * b
}
func mul = (
mulInt
mulFloat
)
-- test.gop --
println mul(100, 7)
`

const overloadDefinition3 = `
-- go.mod --
module mod.com
go 1.21.4
-- def.gop --
type foo struct {
}
func (a *foo) mulInt(b int) *foo {
return a
}
func (a *foo) mulFoo(b *foo) *foo {
return a
}
func (foo).mul = (
(foo).mulInt
(foo).mulFoo
)
-- test.gop --
var a *foo
var c = a.mul(100)
`

func TestOverloadDefinition(t *testing.T) {
Run(t, overloadDefinition1, func(t *testing.T, env *Env) {
env.OpenFile("test.gop")
loc := env.GoToDefinition(env.RegexpSearch("test.gop", "add"))
name := env.Sandbox.Workdir.URIToPath(loc.URI)
if want := "def.gop"; name != want {
t.Errorf("GoToDefinition: got file %q, want %q", name, want)
}
// goxls : match the 'func' position of the corresponding overloaded function
if want := env.RegexpSearch("def.gop", `(func)\(a, b int\) int`); loc != want {
t.Errorf("GoToDefinition: got location %v, want %v", loc, want)
}
})
Run(t, overloadDefinition2, func(t *testing.T, env *Env) {
env.OpenFile("test.gop")
loc := env.GoToDefinition(env.RegexpSearch("test.gop", "mul"))
name := env.Sandbox.Workdir.URIToPath(loc.URI)
if want := "def.gop"; name != want {
t.Errorf("GoToDefinition: got file %q, want %q", name, want)
}
// goxls: match mulInt
if want := env.RegexpSearch("def.gop", `func (mulInt)\(a, b int\) int`); loc != want {
t.Errorf("GoToDefinition: got location %v, want %v", loc, want)
}
})
Run(t, overloadDefinition3, func(t *testing.T, env *Env) {
env.OpenFile("test.gop")
loc := env.GoToDefinition(env.RegexpSearch("test.gop", "mul"))
name := env.Sandbox.Workdir.URIToPath(loc.URI)
if want := "def.gop"; name != want {
t.Errorf("GoToDefinition: got file %q, want %q", name, want)
}
// goxls: match mulInt
if want := env.RegexpSearch("def.gop", `func \(a \*foo\) (mulInt)\(b int\) \*foo`); loc != want {
t.Errorf("GoToDefinition: got location %v, want %v", loc, want)
}
})
}

const internalDefinition = `
-- go.mod --
module mod.com
Expand Down

0 comments on commit da60091

Please sign in to comment.