Skip to content

Commit

Permalink
internal/lsp: normalize interface{} to any in test comparisons
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
findleyr authored and rsc committed Dec 9, 2021
1 parent 3fca6a0 commit e06c107
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
3 changes: 2 additions & 1 deletion internal/lsp/cmd/test/signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)
}
}
14 changes: 11 additions & 3 deletions internal/lsp/tests/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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, "@") {
Expand Down

0 comments on commit e06c107

Please sign in to comment.