From 330cdc78b8c944a28acfa02e5c93f657dae01762 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Thu, 25 Apr 2024 14:42:44 +0800 Subject: [PATCH] test:add test of overload function definition --- gopls/internal/lsp/source/definition_gox.go | 1 - .../internal/regtest/misc/definition_test.go | 173 ++++++++++++++++++ 2 files changed, 173 insertions(+), 1 deletion(-) diff --git a/gopls/internal/lsp/source/definition_gox.go b/gopls/internal/lsp/source/definition_gox.go index ace53e6c7a1..e8eaf76fa12 100644 --- a/gopls/internal/lsp/source/definition_gox.go +++ b/gopls/internal/lsp/source/definition_gox.go @@ -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 } diff --git a/gopls/internal/regtest/misc/definition_test.go b/gopls/internal/regtest/misc/definition_test.go index 0a36336b567..8496bbb7b52 100644 --- a/gopls/internal/regtest/misc/definition_test.go +++ b/gopls/internal/regtest/misc/definition_test.go @@ -16,6 +16,179 @@ 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) +` +const overloadDefinition4 = ` +-- go.mod -- +module mod.com + +go 1.21.4 +-- def.gop -- +type Foo struct { +} + +func (a *Foo) DivisionInt(b int) *Foo { + println "DivisionInt" + return a +} + +func (a *Foo) DivisionFoo(b *Foo) *Foo { + println "DivisionFoo" + return a +} + +func (Foo).Division = ( + (Foo).DivisionInt + (Foo).DivisionFoo +) +-- test.gop -- +var b *Foo + +b.Division(1) +` + +const overloadDefinition5 = ` +-- go.mod -- +module mod.com + +go 1.21.4 +-- def.go -- +package main +type foo struct { +} + +func (f *foo) Broadcast__0(msg string) bool { + return true +} +-- test.gop -- +var a *foo +a.Broadcast("hhh") +` + +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", `println (mul)\(100, 7\)`)) + 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) + } + }) + Run(t, overloadDefinition4, func(t *testing.T, env *Env) { + env.OpenFile("test.gop") + loc := env.GoToDefinition(env.RegexpSearch("test.gop", "Division")) + name := env.Sandbox.Workdir.URIToPath(loc.URI) + if want := "def.gop"; name != want { + t.Errorf("GoToDefinition: got file %q, want %q", name, want) + } + if want := env.RegexpSearch("def.gop", `func \(a \*Foo\) (DivisionInt)\(b int\) \*Foo`); loc != want { + t.Errorf("GoToDefinition: got location %v, want %v", loc, want) + } + }) + Run(t, overloadDefinition5, func(t *testing.T, env *Env) { + env.OpenFile("test.gop") + loc := env.GoToDefinition(env.RegexpSearch("test.gop", "Broadcast")) + name := env.Sandbox.Workdir.URIToPath(loc.URI) + if want := "def.go"; name != want { + t.Errorf("GoToDefinition: got file %q, want %q", name, want) + } + if want := env.RegexpSearch("def.go", `Broadcast__0`); loc != want { + t.Errorf("GoToDefinition: got location %v, want %v", loc, want) + } + }) +} + const internalDefinition = ` -- go.mod -- module mod.com