Skip to content

Commit 5be56db

Browse files
committed
test:patch more overload define test
1 parent 473c47f commit 5be56db

File tree

2 files changed

+167
-6
lines changed

2 files changed

+167
-6
lines changed

gopls/internal/lsp/source/definition_gox.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -338,9 +338,9 @@ func IsOverloadAnonymousMember(ctx context.Context, snapshot Snapshot, pkg Packa
338338
if err != nil {
339339
return nil, nil, nil, false
340340
}
341-
varPkg := pkgs[0]
342-
if funcLit, ovObj, ok := inPkg(varPkg); ok {
343-
return varPkg, funcLit, ovObj, true
341+
localPkg := pkgs[0]
342+
if funcLit, ovObj, ok := inPkg(localPkg); ok {
343+
return localPkg, funcLit, ovObj, true
344344
}
345345
}
346346
}

gopls/internal/regtest/misc/definition_gox_test.go

Lines changed: 164 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func add = (
2222
)
2323
-- test.gop --
2424
println add(100, 7)
25+
println add("Hello", "World")
2526
-- gop_autogen.go --
2627
package main
2728
@@ -36,12 +37,14 @@ func add__1(a string, b string) string {
3637
}
3738
func main() {
3839
fmt.Println(add__0(100, 7))
40+
fmt.Println(add__1("Hello", "World"))
3941
}
4042
`
4143

4244
func TestAnonyOverload(t *testing.T) {
4345
testCases := []match{
4446
{`test.gop`, `println (add)\(100, 7\)`, "def.gop", `func\(a, b int\) int`},
47+
{`test.gop`, `println (add)\("Hello", "World"\)`, "def.gop", `func\(a, b string\) string`},
4548
}
4649
runGoToDefinitionTest(t, anonyOverload, testCases)
4750
}
@@ -69,6 +72,8 @@ func mul = (
6972
)
7073
-- test.gop --
7174
println mul(100, 7)
75+
println mul("Hello", "World")
76+
println mul(200.5, 2.3)
7277
-- gop_autogen.go --
7378
package main
7479
@@ -87,12 +92,16 @@ func mulFloat(a float64, b float64) float64 {
8792
}
8893
func main() {
8994
fmt.Println(mulInt(100, 7))
95+
fmt.Println(mul__1("Hello", "World"))
96+
fmt.Println(mulFloat(200.5, 2.3))
9097
}
9198
`
9299

93100
func TestOverloadMixAnonyAndNamed(t *testing.T) {
94101
testCases := []match{
95102
{`test.gop`, `println (mul)\(100, 7\)`, "def.gop", `func (mulInt)\(a, b int\) int`},
103+
{`test.gop`, `println (mul)\("Hello", "World"\)`, "def.gop", `func\(a, b string\) string`},
104+
{`test.gop`, `println (mul)\(200.5, 2.3\)`, "def.gop", `func (mulFloat)\(a, b float64\) float64`},
96105
}
97106
runGoToDefinitionTest(t, overloadMixAnonyAndNamed, testCases)
98107
}
@@ -121,6 +130,7 @@ func (foo).mul = (
121130
-- test.gop --
122131
var a *foo
123132
var c = a.mul(100)
133+
var d = a.mul(&foo{})
124134
-- gop_autogen.go --
125135
package main
126136
@@ -139,14 +149,20 @@ func (a *foo) mulFoo(b *foo) *foo {
139149
140150
var a *foo
141151
var c = a.mulInt(100)
152+
var d = a.mulFoo(&foo{})
142153
143154
func main() {
144155
}
145156
`
146157

147158
func TestOverloadMethod(t *testing.T) {
159+
mulIntLocReg := `func \(a \*foo\) (mulInt)\(b int\) \*foo`
160+
mulFooLocReg := `func \(a \*foo\) (mulFoo)\(b \*foo\) \*foo`
148161
testCases := []match{
149-
{`test.gop`, `var c = a.(mul)\(100\)`, "def.gop", `func \(a \*foo\) (mulInt)\(b int\) \*foo`},
162+
{`test.gop`, `var c = a.(mul)\(100\)`, "def.gop", mulIntLocReg},
163+
{`test.gop`, `var d = a.(mul)\(&foo\{\}\)`, "def.gop", mulFooLocReg},
164+
{`def.gop`, `\(foo\)\.(mulInt)`, "def.gop", mulIntLocReg},
165+
{`def.gop`, `\(foo\)\.(mulFoo)`, "def.gop", mulFooLocReg},
150166
}
151167
runGoToDefinitionTest(t, overloadMethod, testCases)
152168
}
@@ -181,6 +197,14 @@ n := &N{}
181197
n.onKey("hello", func() {
182198
println("hello world")
183199
})
200+
201+
n.onKey("hello", func(key string) {
202+
println("hello world", key)
203+
})
204+
205+
n.onKey([]string{"hello", "world"}, func() {
206+
println("hello world")
207+
})
184208
-- gop_autogen.go --
185209
package main
186210
@@ -192,12 +216,20 @@ func main() {
192216
n.OnKey__0("hello", func() {
193217
fmt.Println("hello world")
194218
})
219+
n.OnKey__1("hello", func(key string) {
220+
fmt.Println("hello world", key)
221+
})
222+
n.OnKey__2([]string{"hello", "world"}, func() {
223+
fmt.Println("hello world")
224+
})
195225
}
196226
`
197227

198228
func TestOverloadFromGo(t *testing.T) {
199229
testCases := []match{
200-
{`test.gop`, `onKey`, "def.go", `OnKey__0`},
230+
{`test.gop`, `n.(onKey)\("hello", func\(\)`, "def.go", `OnKey__0`},
231+
{`test.gop`, `n.(onKey)\("hello", func\(key string\)`, "def.go", `OnKey__1`},
232+
{`test.gop`, `n.(onKey)\(\[\]string\{"hello", "world"\}, func\(\)`, "def.go", `OnKey__2`},
201233
}
202234
runGoToDefinitionTest(t, overloadFromGo, testCases)
203235
}
@@ -219,46 +251,175 @@ func Add = (
219251
}
220252
)
221253
254+
func MulInt(a, b int) int {
255+
return a * b
256+
}
257+
258+
func MulFloat(a, b float64) float64 {
259+
return a * b
260+
}
261+
262+
func Mul = (
263+
MulInt
264+
func(x, y string) string {
265+
return x + y
266+
}
267+
MulFloat
268+
)
269+
270+
type Foo struct {
271+
}
272+
273+
func (a *Foo) MulInt(b int) *Foo {
274+
return a
275+
}
276+
277+
func (a *Foo) MulFoo(b *Foo) *Foo {
278+
return a
279+
}
280+
281+
func (Foo).Mul = (
282+
(Foo).MulInt
283+
(Foo).MulFoo
284+
)
222285
-- lib/gop_autogen.go --
223286
package lib
224287
225288
const GopPackage = true
226289
const _ = true
290+
const Gopo_Mul = "MulInt,,MulFloat"
291+
292+
type Foo struct {
293+
}
294+
295+
const Gopo_Foo_Mul = ".MulInt,.MulFoo"
227296
func Add__0(a int, b int) int {
228297
return a + b
229298
}
230299
func Add__1(a string, b string) string {
231300
return a + b
232301
}
302+
func MulInt(a int, b int) int {
303+
return a * b
304+
}
305+
func Mul__1(x string, y string) string {
306+
return x + y
307+
}
308+
func MulFloat(a float64, b float64) float64 {
309+
return a * b
310+
}
311+
func (a *Foo) MulInt(b int) *Foo {
312+
return a
313+
}
314+
func (a *Foo) MulFoo(b *Foo) *Foo {
315+
return a
316+
}
317+
-- lib2/lib2.go --
318+
package lib2
319+
320+
const GopPackage = true
321+
322+
type N struct {
323+
}
324+
325+
func (m *N) OnKey__0(a string, fn func()) {
326+
fn()
327+
}
233328
329+
func (m *N) OnKey__1(a string, fn func(key string)) {
330+
fn(a)
331+
}
332+
333+
func (m *N) OnKey__2(a []string, fn func()) {
334+
fn()
335+
}
234336
-- main.gop --
235337
import (
236338
"mod.com/lib"
339+
"mod.com/lib2"
237340
)
238341
239342
println lib.Add(100, 7)
240343
println lib.Add("Hello", "World")
241344
345+
println lib.Mul(100, 7)
346+
println lib.Mul("Hello", "World")
347+
println lib.Mul(200.5, 2.3)
348+
349+
var a *lib.Foo
350+
var c = a.Mul(100)
351+
var d = a.Mul(&lib.Foo{})
352+
353+
_ = c
354+
_ = d
355+
356+
n := &lib2.N{}
357+
358+
n.OnKey("hello", func() {
359+
println("hello world")
360+
})
361+
362+
n.OnKey("hello", func(key string) {
363+
println("hello world", key)
364+
})
365+
366+
n.OnKey([]string{"hello", "world"}, func() {
367+
println("hello world")
368+
})
242369
-- gop_autogen.go --
243370
package main
244371
245372
import (
246373
"fmt"
247374
"mod.com/lib"
375+
"mod.com/lib2"
248376
)
249377
250378
const _ = true
251379
func main() {
252380
fmt.Println(lib.Add__0(100, 7))
253381
fmt.Println(lib.Add__1("Hello", "World"))
382+
fmt.Println(lib.MulInt(100, 7))
383+
fmt.Println(lib.Mul__1("Hello", "World"))
384+
fmt.Println(lib.MulFloat(200.5, 2.3))
385+
var a *lib.Foo
386+
var c = a.MulInt(100)
387+
var d = a.MulFoo(&lib.Foo{})
388+
_ = c
389+
_ = d
390+
n := &lib2.N{}
391+
n.OnKey__0("hello", func() {
392+
fmt.Println("hello world")
393+
})
394+
n.OnKey__1("hello", func(key string) {
395+
fmt.Println("hello world", key)
396+
})
397+
n.OnKey__2([]string{"hello", "world"}, func() {
398+
fmt.Println("hello world")
399+
})
254400
}
255401
`
256402

257403
// Test cross package 's overload definition
258404
func TestOverloadCrossPkg(t *testing.T) {
259405
testCases := []match{
406+
// anony overload
260407
{`main.gop`, `println lib.(Add)\(100, 7\)`, "lib/lib.gop", `func\(a, b int\) int`},
261408
{`main.gop`, `println lib.(Add)\("Hello", "World"\)`, "lib/lib.gop", `func\(a, b string\) string`},
409+
410+
// named & anony overload
411+
{`main.gop`, `println lib.(Mul)\(100, 7\)`, "lib/lib.gop", `func (MulInt)\(a, b int\) int`},
412+
{`main.gop`, `println lib.(Mul)\("Hello", "World"\)`, "lib/lib.gop", `func\(x, y string\) string`},
413+
{`main.gop`, `println lib.(Mul)\(200.5, 2.3\)`, "lib/lib.gop", `func (MulFloat)\(a, b float64\) float64`},
414+
415+
// method
416+
{`main.gop`, `var c = a.(Mul)\(100\)`, "lib/lib.gop", `func \(a \*Foo\) (MulInt)\(b int\) \*Foo`},
417+
{`main.gop`, `var d = a.(Mul)\(&lib\.Foo\{\}\)`, "lib/lib.gop", `func \(a \*Foo\) (MulFoo)\(b \*Foo\) \*Foo`},
418+
419+
// from go
420+
{`main.gop`, `n.(OnKey)\("hello", func\(\)`, "lib2/lib2.go", `OnKey__0`},
421+
{`main.gop`, `n.(OnKey)\("hello", func\(key string\)`, "lib2/lib2.go", `OnKey__1`},
422+
{`main.gop`, `n.(OnKey)\(\[\]string\{"hello", "world"\}, func\(\)`, "lib2/lib2.go", `OnKey__2`},
262423
}
263424
runGoToDefinitionTest(t, overloadCrossPkg, testCases)
264425
}
@@ -280,7 +441,7 @@ func runGoToDefinitionTest(t *testing.T, files string, testCases []match) {
280441
t.Errorf("GoToDefinition: got file %q, want %q", name, test.wantFile)
281442
}
282443
if want := env.RegexpSearch(test.wantFile, test.wantLocReg); loc != want {
283-
t.Errorf("GoToDefinition: got location %v, want %v", loc, want)
444+
t.Errorf("GoToDefinition:%v got location %v, want %v", test.findLocReg, loc, want)
284445
}
285446
}
286447
})

0 commit comments

Comments
 (0)