Skip to content

Commit da60091

Browse files
committed
test:add test of overload function definition
1 parent 69624ac commit da60091

2 files changed

Lines changed: 105 additions & 1 deletion

File tree

gopls/internal/lsp/source/definition_gox.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ func GopDefinition(ctx context.Context, snapshot Snapshot, fh FileHandle, positi
8080

8181
// The general case: the cursor is on an identifier.
8282
_, obj, _ := gopReferencedObject(pkg, pgf, pos)
83-
8483
if obj == nil {
8584
return nil, nil
8685
}

gopls/internal/regtest/misc/definition_test.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,111 @@ import (
1616
"golang.org/x/tools/gopls/internal/lsp/tests/compare"
1717
)
1818

19+
const overloadDefinition1 = `
20+
-- go.mod --
21+
module mod.com
22+
23+
go 1.21.4
24+
-- def.gop --
25+
func add = (
26+
func(a, b int) int {
27+
return a + b
28+
}
29+
func(a, b string) string {
30+
return a + b
31+
}
32+
)
33+
-- test.gop --
34+
println add(100, 7)
35+
`
36+
37+
const overloadDefinition2 = `
38+
-- go.mod --
39+
module mod.com
40+
41+
go 1.21.4
42+
-- def.gop --
43+
func mulInt(a, b int) int {
44+
return a * b
45+
}
46+
47+
func mulFloat(a, b float64) float64 {
48+
return a * b
49+
}
50+
51+
func mul = (
52+
mulInt
53+
mulFloat
54+
)
55+
-- test.gop --
56+
println mul(100, 7)
57+
`
58+
59+
const overloadDefinition3 = `
60+
-- go.mod --
61+
module mod.com
62+
63+
go 1.21.4
64+
-- def.gop --
65+
type foo struct {
66+
}
67+
68+
func (a *foo) mulInt(b int) *foo {
69+
return a
70+
}
71+
72+
func (a *foo) mulFoo(b *foo) *foo {
73+
return a
74+
}
75+
76+
func (foo).mul = (
77+
(foo).mulInt
78+
(foo).mulFoo
79+
)
80+
-- test.gop --
81+
var a *foo
82+
var c = a.mul(100)
83+
`
84+
85+
func TestOverloadDefinition(t *testing.T) {
86+
Run(t, overloadDefinition1, func(t *testing.T, env *Env) {
87+
env.OpenFile("test.gop")
88+
loc := env.GoToDefinition(env.RegexpSearch("test.gop", "add"))
89+
name := env.Sandbox.Workdir.URIToPath(loc.URI)
90+
if want := "def.gop"; name != want {
91+
t.Errorf("GoToDefinition: got file %q, want %q", name, want)
92+
}
93+
// goxls : match the 'func' position of the corresponding overloaded function
94+
if want := env.RegexpSearch("def.gop", `(func)\(a, b int\) int`); loc != want {
95+
t.Errorf("GoToDefinition: got location %v, want %v", loc, want)
96+
}
97+
})
98+
Run(t, overloadDefinition2, func(t *testing.T, env *Env) {
99+
env.OpenFile("test.gop")
100+
loc := env.GoToDefinition(env.RegexpSearch("test.gop", "mul"))
101+
name := env.Sandbox.Workdir.URIToPath(loc.URI)
102+
if want := "def.gop"; name != want {
103+
t.Errorf("GoToDefinition: got file %q, want %q", name, want)
104+
}
105+
// goxls: match mulInt
106+
if want := env.RegexpSearch("def.gop", `func (mulInt)\(a, b int\) int`); loc != want {
107+
t.Errorf("GoToDefinition: got location %v, want %v", loc, want)
108+
}
109+
})
110+
Run(t, overloadDefinition3, func(t *testing.T, env *Env) {
111+
env.OpenFile("test.gop")
112+
loc := env.GoToDefinition(env.RegexpSearch("test.gop", "mul"))
113+
name := env.Sandbox.Workdir.URIToPath(loc.URI)
114+
if want := "def.gop"; name != want {
115+
t.Errorf("GoToDefinition: got file %q, want %q", name, want)
116+
}
117+
// goxls: match mulInt
118+
if want := env.RegexpSearch("def.gop", `func \(a \*foo\) (mulInt)\(b int\) \*foo`); loc != want {
119+
t.Errorf("GoToDefinition: got location %v, want %v", loc, want)
120+
}
121+
})
122+
}
123+
19124
const internalDefinition = `
20125
-- go.mod --
21126
module mod.com

0 commit comments

Comments
 (0)