internal/lsp: normalize interface{} to any in test comparisons

The switch to use any in standard library signatures breaks many of our
tests that match signature strings exactly. Fix this by normalizing
strings to use 'any' in place of interface{}, before comparing.

Updates golang/go#49884

Change-Id: If18ce1035b3206f37d7de6e584cf2c2cae9481c5
Reviewed-on: https://go-review.googlesource.com/c/tools/+/370294
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
This commit is contained in:
Robert Findley 2021-12-08 11:47:09 -05:00 committed by Russ Cox
parent 3fca6a08d7
commit e06c10712b
2 changed files with 13 additions and 4 deletions

View File

@ -9,6 +9,7 @@ import (
"testing"
"golang.org/x/tools/internal/lsp/protocol"
"golang.org/x/tools/internal/lsp/tests"
"golang.org/x/tools/internal/span"
)
@ -27,7 +28,7 @@ func (r *runner) SignatureHelp(t *testing.T, spn span.Span, want *protocol.Signa
expect := string(r.data.Golden(goldenTag, filename, func() ([]byte, error) {
return []byte(got), nil
}))
if expect != got {
if tests.NormalizeAny(expect) != tests.NormalizeAny(got) {
t.Errorf("signature failed for %s expected:\n%q\ngot:\n%q'", filename, expect, got)
}
}

View File

@ -252,7 +252,7 @@ func DiffSignatures(spn span.Span, want, got *protocol.SignatureHelp) (string, e
}
g := got.Signatures[0]
w := want.Signatures[0]
if w.Label != g.Label {
if NormalizeAny(w.Label) != NormalizeAny(g.Label) {
wLabel := w.Label + "\n"
d, err := myers.ComputeEdits("", wLabel, g.Label+"\n")
if err != nil {
@ -271,6 +271,14 @@ func DiffSignatures(spn span.Span, want, got *protocol.SignatureHelp) (string, e
return "", nil
}
// NormalizeAny replaces occurrences of interface{} in input with any.
//
// In Go 1.18, standard library functions were changed to use the 'any'
// alias in place of interface{}, which affects their type string.
func NormalizeAny(input string) string {
return strings.ReplaceAll(input, "interface{}", "any")
}
// DiffCallHierarchyItems returns the diff between expected and actual call locations for incoming/outgoing call hierarchies
func DiffCallHierarchyItems(gotCalls []protocol.CallHierarchyItem, expectedCalls []protocol.CallHierarchyItem) string {
expected := make(map[protocol.Location]bool)
@ -369,7 +377,7 @@ func CheckCompletionOrder(want, got []protocol.CompletionItem, strictScores bool
for _, w := range want {
var found bool
for i, g := range got {
if w.Label == g.Label && w.Detail == g.Detail && w.Kind == g.Kind {
if w.Label == g.Label && NormalizeAny(w.Detail) == NormalizeAny(g.Detail) && w.Kind == g.Kind {
matchedIdxs = append(matchedIdxs, i)
found = true
@ -444,7 +452,7 @@ func DiffCompletionItems(want, got []protocol.CompletionItem) string {
if w.Label != g.Label {
return summarizeCompletionItems(i, want, got, "incorrect Label got %v want %v", g.Label, w.Label)
}
if w.Detail != g.Detail {
if NormalizeAny(w.Detail) != NormalizeAny(g.Detail) {
return summarizeCompletionItems(i, want, got, "incorrect Detail got %v want %v", g.Detail, w.Detail)
}
if w.Documentation != "" && !strings.HasPrefix(w.Documentation, "@") {