From affba5001cf912ed3781ee1da3f93f1be0246db2 Mon Sep 17 00:00:00 2001 From: Robert Findley Date: Fri, 22 Oct 2021 09:39:50 -0400 Subject: [PATCH] internal/lsp: temporarily strip subscripts from generic hover To enable removing subscripts from go/types without breaking x/tools tests, temporarily normalize test hover strings to strip subscripts. Change-Id: I373122150e9ca889e2dcbc77875fe1b0dbbe3e15 Reviewed-on: https://go-review.googlesource.com/c/tools/+/358014 Trust: Robert Findley Run-TryBot: Robert Findley gopls-CI: kokoro TryBot-Result: Go Bot Reviewed-by: Robert Griesemer --- internal/lsp/lsp_test.go | 6 ++++-- internal/lsp/source/source_test.go | 2 ++ internal/lsp/tests/util.go | 19 +++++++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/internal/lsp/lsp_test.go b/internal/lsp/lsp_test.go index 32cd597acf..8a6673012c 100644 --- a/internal/lsp/lsp_test.go +++ b/internal/lsp/lsp_test.go @@ -722,8 +722,10 @@ func (r *runner) Definition(t *testing.T, spn span.Span, d tests.Definition) { expectHover := string(r.data.Golden(tag, d.Src.URI().Filename(), func() ([]byte, error) { return []byte(hover.Contents.Value), nil })) - if hover.Contents.Value != expectHover { - t.Errorf("%s:\n%s", d.Src, tests.Diff(t, expectHover, hover.Contents.Value)) + got := tests.StripSubscripts(hover.Contents.Value) + expectHover = tests.StripSubscripts(expectHover) + if got != expectHover { + t.Errorf("%s:\n%s", d.Src, tests.Diff(t, expectHover, got)) } } if !d.OnlyHover { diff --git a/internal/lsp/source/source_test.go b/internal/lsp/source/source_test.go index 83ce712c83..d0e6b8a2eb 100644 --- a/internal/lsp/source/source_test.go +++ b/internal/lsp/source/source_test.go @@ -580,6 +580,8 @@ func (r *runner) Definition(t *testing.T, spn span.Span, d tests.Definition) { expectHover := string(r.data.Golden(tag, d.Src.URI().Filename(), func() ([]byte, error) { return []byte(hover), nil })) + hover = tests.StripSubscripts(hover) + expectHover = tests.StripSubscripts(expectHover) if hover != expectHover { t.Errorf("hoverdef for %s failed:\n%s", d.Src, tests.Diff(t, expectHover, hover)) } diff --git a/internal/lsp/tests/util.go b/internal/lsp/tests/util.go index 94c948de97..f75677a0e2 100644 --- a/internal/lsp/tests/util.go +++ b/internal/lsp/tests/util.go @@ -551,3 +551,22 @@ func Diff(t *testing.T, want, got string) string { } return fmt.Sprintf("%q", diff.ToUnified("want", "got", want, d)) } + +// StripSubscripts removes type parameter id subscripts. +// +// TODO(rfindley): remove this function once subscripts are removed from the +// type parameter type string. +func StripSubscripts(s string) string { + var runes []rune + for _, r := range s { + // For debugging/uniqueness purposes, TypeString on a type parameter adds a + // subscript corresponding to the type parameter's unique id. This is going + // to be removed, but in the meantime we skip the subscript runes to get a + // deterministic output. + if '₀' <= r && r < '₀'+10 { + continue // trim type parameter subscripts + } + runes = append(runes, r) + } + return string(runes) +}