Skip to content

Commit 84a5e0a

Browse files
committed
test:add test of overload function definition
1 parent e1ec977 commit 84a5e0a

File tree

2 files changed

+152
-3
lines changed

2 files changed

+152
-3
lines changed

gopls/internal/lsp/source/definition_gox.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,14 @@ 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
}
8786

8887
anonyOvId := false
8988
if fun, ok := obj.(*types.Func); ok {
90-
for _, ov := range pkg.GopTypesInfo().Implicits {
91-
if v, ok := ov.(*types.Func); ok {
89+
for ov := range pkg.GopTypesInfo().Implicits {
90+
if v, ok := ov.(*ast.FuncLit); ok {
9291
if v.Pos() == fun.Pos() {
9392
anonyOvId = true
9493
break
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
package misc
2+
3+
import (
4+
"testing"
5+
6+
. "golang.org/x/tools/gopls/internal/lsp/regtest"
7+
)
8+
9+
const overloadDefinition1 = `
10+
-- go.mod --
11+
module mod.com
12+
13+
go 1.21.4
14+
-- def.gop --
15+
func add = (
16+
func(a, b int) int {
17+
return a + b
18+
}
19+
func(a, b string) string {
20+
return a + b
21+
}
22+
)
23+
-- test.gop --
24+
println add(100, 7)
25+
`
26+
27+
func TestOverloadDefinition1(t *testing.T) {
28+
Run(t, overloadDefinition1, func(t *testing.T, env *Env) {
29+
env.OpenFile("test.gop")
30+
loc := env.GoToDefinition(env.RegexpSearch("test.gop", "add"))
31+
name := env.Sandbox.Workdir.URIToPath(loc.URI)
32+
if want := "def.gop"; name != want {
33+
t.Errorf("GoToDefinition: got file %q, want %q", name, want)
34+
}
35+
// goxls : match the 'func' position of the corresponding overloaded function
36+
if want := env.RegexpSearch("def.gop", `(func)\(a, b int\) int`); loc != want {
37+
t.Errorf("GoToDefinition: got location %v, want %v", loc, want)
38+
}
39+
})
40+
}
41+
42+
const overloadDefinition2 = `
43+
-- go.mod --
44+
module mod.com
45+
46+
go 1.21.4
47+
-- def.gop --
48+
func mulInt(a, b int) int {
49+
return a * b
50+
}
51+
52+
func mulFloat(a, b float64) float64 {
53+
return a * b
54+
}
55+
56+
func mul = (
57+
mulInt
58+
mulFloat
59+
)
60+
-- test.gop --
61+
println mul(100, 7)
62+
`
63+
64+
func TestOverloadDefinition2(t *testing.T) {
65+
Run(t, overloadDefinition2, func(t *testing.T, env *Env) {
66+
env.OpenFile("test.gop")
67+
loc := env.GoToDefinition(env.RegexpSearch("test.gop", `println (mul)\(100, 7\)`))
68+
name := env.Sandbox.Workdir.URIToPath(loc.URI)
69+
if want := "def.gop"; name != want {
70+
t.Errorf("GoToDefinition: got file %q, want %q", name, want)
71+
}
72+
// goxls: match mulInt
73+
if want := env.RegexpSearch("def.gop", `func (mulInt)\(a, b int\) int`); loc != want {
74+
t.Errorf("GoToDefinition: got location %v, want %v", loc, want)
75+
}
76+
})
77+
}
78+
79+
const overloadDefinition3 = `
80+
-- go.mod --
81+
module mod.com
82+
83+
go 1.21.4
84+
-- def.gop --
85+
type foo struct {
86+
}
87+
88+
func (a *foo) mulInt(b int) *foo {
89+
return a
90+
}
91+
92+
func (a *foo) mulFoo(b *foo) *foo {
93+
return a
94+
}
95+
96+
func (foo).mul = (
97+
(foo).mulInt
98+
(foo).mulFoo
99+
)
100+
-- test.gop --
101+
var a *foo
102+
var c = a.mul(100)
103+
`
104+
105+
func TestOverloadDefinition3(t *testing.T) {
106+
Run(t, overloadDefinition3, func(t *testing.T, env *Env) {
107+
env.OpenFile("test.gop")
108+
loc := env.GoToDefinition(env.RegexpSearch("test.gop", "mul"))
109+
name := env.Sandbox.Workdir.URIToPath(loc.URI)
110+
if want := "def.gop"; name != want {
111+
t.Errorf("GoToDefinition: got file %q, want %q", name, want)
112+
}
113+
// goxls: match mulInt
114+
if want := env.RegexpSearch("def.gop", `func \(a \*foo\) (mulInt)\(b int\) \*foo`); loc != want {
115+
t.Errorf("GoToDefinition: got location %v, want %v", loc, want)
116+
}
117+
})
118+
}
119+
120+
const overloadDefinition4 = `
121+
-- go.mod --
122+
module mod.com
123+
124+
go 1.21.4
125+
-- def.go --
126+
package main
127+
type foo struct {
128+
}
129+
130+
func (f *foo) Broadcast__0(msg string) bool {
131+
return true
132+
}
133+
-- test.gop --
134+
var a *foo
135+
a.Broadcast("hhh")
136+
`
137+
138+
func TestOverloadDefinition4(t *testing.T) {
139+
Run(t, overloadDefinition4, func(t *testing.T, env *Env) {
140+
env.OpenFile("test.gop")
141+
loc := env.GoToDefinition(env.RegexpSearch("test.gop", "Broadcast"))
142+
name := env.Sandbox.Workdir.URIToPath(loc.URI)
143+
if want := "def.go"; name != want {
144+
t.Errorf("GoToDefinition: got file %q, want %q", name, want)
145+
}
146+
if want := env.RegexpSearch("def.go", `Broadcast__0`); loc != want {
147+
t.Errorf("GoToDefinition: got location %v, want %v", loc, want)
148+
}
149+
})
150+
}

0 commit comments

Comments
 (0)