Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
gopls/internal/lsp: add testSignatureHelpOverload
  • Loading branch information
visualfc committed Apr 2, 2025
commit 59d1f96b417089e24a7c138f4a912c57b7da7cdc
11 changes: 11 additions & 0 deletions gopls/internal/goxls/testdata/overload/signature.gop
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
func _() {
Demo(100) //@signature_overload("100", "Demo__1(n int) int", 1, 0)
Demo(100, 200) //@signature_overload("100", "Demo__2(n1 int, n2 int)", 2, 0)
Demo(100, 200) //@signature_overload("200", "Demo__2(n1 int, n2 int)", 2, 1)
}

func _() {
Demo 100 //@signature_overload("100", "Demo__1(n int) int", 1, 0)
Demo 100, 200 //@signature_overload("100", "Demo__2(n1 int, n2 int)", 2, 0)
Demo 100, 200 //@signature_overload("200", "Demo__2(n1 int, n2 int)", 2, 1)
}
102 changes: 102 additions & 0 deletions gopls/internal/lsp/goxls_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package lsp

import (
"fmt"
"path/filepath"
"strings"
"testing"

"golang.org/x/tools/gopls/internal/lsp/cache"
"golang.org/x/tools/gopls/internal/lsp/debug"
"golang.org/x/tools/gopls/internal/lsp/protocol"
"golang.org/x/tools/gopls/internal/lsp/source"
"golang.org/x/tools/gopls/internal/lsp/tests"
"golang.org/x/tools/gopls/internal/lsp/tests/compare"
"golang.org/x/tools/gopls/internal/span"
"golang.org/x/tools/internal/testenv"
)
Expand Down Expand Up @@ -98,4 +102,102 @@ func testXLS(t *testing.T, datum *tests.Data) {
}
r.server = NewServer(session, testClient{runner: r})
tests.Run(t, r, datum)
// test signature_overload
testSignatureHelpOverload(t, r, datum)
}

func testSignatureHelpOverload(t *testing.T, r *runner, datum *tests.Data) {
// Collect names for the entries that require golden files.
signatures := make(map[span.Span]*protocol.SignatureHelp)
collectSignatures := func(spn span.Span, signature string, activeSignature int64, activeParam int64) {
signatures[spn] = &protocol.SignatureHelp{
Signatures: []protocol.SignatureInformation{
{
Label: signature,
},
},
ActiveSignature: uint32(activeSignature),
ActiveParameter: uint32(activeParam),
}
// Hardcode special case to test the lack of a signature.
if signature == "" && activeParam == 0 {
signatures[spn] = nil
}
}

if err := datum.Exported.Expect(map[string]interface{}{
"signature_overload": collectSignatures,
}); err != nil {
t.Fatal(err)
}

t.Run("SignatureHelpOverload", func(t *testing.T) {
t.Helper()
for spn, expectedSignature := range signatures {
t.Run(tests.SpanName(spn), func(t *testing.T) {
t.Helper()
r.SignatureHelpOverload(t, spn, expectedSignature)
})
}
})
}

func (r *runner) SignatureHelpOverload(t *testing.T, spn span.Span, want *protocol.SignatureHelp) {
m, err := r.data.Mapper(spn.URI())
if err != nil {
t.Fatal(err)
}
loc, err := m.SpanLocation(spn)
if err != nil {
t.Fatalf("failed for %v: %v", loc, err)
}
params := &protocol.SignatureHelpParams{
TextDocumentPositionParams: protocol.LocationTextDocumentPositionParams(loc),
}
got, err := r.server.SignatureHelp(r.ctx, params)
if err != nil {
// Only fail if we got an error we did not expect.
if want != nil {
t.Fatal(err)
}
return
}
if want == nil {
if got != nil {
t.Errorf("expected no signature, got %v", got)
}
return
}
if got == nil {
t.Fatalf("expected %v, got nil", want)
}
if diff := DiffSignaturesOverload(spn, want, got); diff != "" {
t.Error(diff)
}
}

func DiffSignaturesOverload(spn span.Span, want, got *protocol.SignatureHelp) string {
decorate := func(f string, args ...interface{}) string {
return fmt.Sprintf("invalid signature at %s: %s", spn, fmt.Sprintf(f, args...))
}
if want.ActiveSignature != got.ActiveSignature {
return decorate("wanted active signature of %d, got %d", want.ActiveSignature, int(got.ActiveSignature))
}
if want.ActiveParameter != got.ActiveParameter {
return decorate("wanted active parameter of %d, got %d", want.ActiveParameter, int(got.ActiveParameter))
}
g := got.Signatures[got.ActiveSignature]
w := want.Signatures[0]
if diff := compare.Text(tests.NormalizeAny(w.Label), tests.NormalizeAny(g.Label)); diff != "" {
return decorate("mismatched labels:\n%s", diff)
}
var paramParts []string
for _, p := range g.Parameters {
paramParts = append(paramParts, p.Label)
}
paramsStr := strings.Join(paramParts, ", ")
if !strings.Contains(g.Label, paramsStr) {
return decorate("expected signature %q to contain params %q", g.Label, paramsStr)
}
return ""
}