Skip to content

Commit 7c5501e

Browse files
committed
test:add test of overload function definition
1 parent e1ec977 commit 7c5501e

File tree

2 files changed

+143
-3
lines changed

2 files changed

+143
-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: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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+
const overloadDefinition2 = `
28+
-- go.mod --
29+
module mod.com
30+
31+
go 1.21.4
32+
-- def.gop --
33+
func mulInt(a, b int) int {
34+
return a * b
35+
}
36+
37+
func mulFloat(a, b float64) float64 {
38+
return a * b
39+
}
40+
41+
func mul = (
42+
mulInt
43+
mulFloat
44+
)
45+
-- test.gop --
46+
println mul(100, 7)
47+
`
48+
49+
const overloadDefinition3 = `
50+
-- go.mod --
51+
module mod.com
52+
53+
go 1.21.4
54+
-- def.gop --
55+
type foo struct {
56+
}
57+
58+
func (a *foo) mulInt(b int) *foo {
59+
return a
60+
}
61+
62+
func (a *foo) mulFoo(b *foo) *foo {
63+
return a
64+
}
65+
66+
func (foo).mul = (
67+
(foo).mulInt
68+
(foo).mulFoo
69+
)
70+
-- test.gop --
71+
var a *foo
72+
var c = a.mul(100)
73+
`
74+
75+
const overloadDefinition4 = `
76+
-- go.mod --
77+
module mod.com
78+
79+
go 1.21.4
80+
-- def.go --
81+
package main
82+
type foo struct {
83+
}
84+
85+
func (f *foo) Broadcast__0(msg string) bool {
86+
return true
87+
}
88+
-- test.gop --
89+
var a *foo
90+
a.Broadcast("hhh")
91+
`
92+
93+
func TestOverloadDefinition(t *testing.T) {
94+
Run(t, overloadDefinition1, func(t *testing.T, env *Env) {
95+
env.OpenFile("test.gop")
96+
loc := env.GoToDefinition(env.RegexpSearch("test.gop", "add"))
97+
name := env.Sandbox.Workdir.URIToPath(loc.URI)
98+
if want := "def.gop"; name != want {
99+
t.Errorf("GoToDefinition: got file %q, want %q", name, want)
100+
}
101+
// goxls : match the 'func' position of the corresponding overloaded function
102+
if want := env.RegexpSearch("def.gop", `(func)\(a, b int\) int`); loc != want {
103+
t.Errorf("GoToDefinition: got location %v, want %v", loc, want)
104+
}
105+
})
106+
Run(t, overloadDefinition2, func(t *testing.T, env *Env) {
107+
env.OpenFile("test.gop")
108+
loc := env.GoToDefinition(env.RegexpSearch("test.gop", `println (mul)\(100, 7\)`))
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 (mulInt)\(a, b int\) int`); loc != want {
115+
t.Errorf("GoToDefinition: got location %v, want %v", loc, want)
116+
}
117+
})
118+
Run(t, overloadDefinition3, func(t *testing.T, env *Env) {
119+
env.OpenFile("test.gop")
120+
loc := env.GoToDefinition(env.RegexpSearch("test.gop", "mul"))
121+
name := env.Sandbox.Workdir.URIToPath(loc.URI)
122+
if want := "def.gop"; name != want {
123+
t.Errorf("GoToDefinition: got file %q, want %q", name, want)
124+
}
125+
// goxls: match mulInt
126+
if want := env.RegexpSearch("def.gop", `func \(a \*foo\) (mulInt)\(b int\) \*foo`); loc != want {
127+
t.Errorf("GoToDefinition: got location %v, want %v", loc, want)
128+
}
129+
})
130+
Run(t, overloadDefinition4, func(t *testing.T, env *Env) {
131+
env.OpenFile("test.gop")
132+
loc := env.GoToDefinition(env.RegexpSearch("test.gop", "Broadcast"))
133+
name := env.Sandbox.Workdir.URIToPath(loc.URI)
134+
if want := "def.go"; name != want {
135+
t.Errorf("GoToDefinition: got file %q, want %q", name, want)
136+
}
137+
if want := env.RegexpSearch("def.go", `Broadcast__0`); loc != want {
138+
t.Errorf("GoToDefinition: got location %v, want %v", loc, want)
139+
}
140+
})
141+
}

0 commit comments

Comments
 (0)