Skip to content

Commit cc511f4

Browse files
committed
gopls/internal/lsp/regtest: move @implementation to new framework
This change is a simple migration of the old @implementation tests into the new marker framework. The tests themselves are logically unchanged. Change-Id: I431e13814612180a5500dba18b26ce8c66dbbe8b Reviewed-on: https://go-review.googlesource.com/c/tools/+/479295 gopls-CI: kokoro <[email protected]> Reviewed-by: Robert Findley <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Alan Donovan <[email protected]>
1 parent e0ecd1b commit cc511f4

File tree

13 files changed

+196
-253
lines changed

13 files changed

+196
-253
lines changed

gopls/internal/lsp/lsp_test.go

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"golang.org/x/tools/gopls/internal/lsp/tests/compare"
2626
"golang.org/x/tools/gopls/internal/span"
2727
"golang.org/x/tools/internal/bug"
28-
"golang.org/x/tools/internal/diff"
2928
"golang.org/x/tools/internal/event"
3029
"golang.org/x/tools/internal/testenv"
3130
)
@@ -769,35 +768,6 @@ func (r *runner) Definition(t *testing.T, _ span.Span, d tests.Definition) {
769768
}
770769
}
771770

772-
func (r *runner) Implementation(t *testing.T, spn span.Span, wantSpans []span.Span) {
773-
sm, err := r.data.Mapper(spn.URI())
774-
if err != nil {
775-
t.Fatal(err)
776-
}
777-
loc, err := sm.SpanLocation(spn)
778-
if err != nil {
779-
t.Fatal(err)
780-
}
781-
gotImpls, err := r.server.Implementation(r.ctx, &protocol.ImplementationParams{
782-
TextDocumentPositionParams: protocol.LocationTextDocumentPositionParams(loc),
783-
})
784-
if err != nil {
785-
t.Fatalf("Server.Implementation(%s): %v", spn, err)
786-
}
787-
gotLocs, err := tests.LocationsToSpans(r.data, gotImpls)
788-
if err != nil {
789-
t.Fatal(err)
790-
}
791-
sanitize := func(s string) string {
792-
return strings.ReplaceAll(s, r.data.Config.Dir, "gopls/internal/lsp/testdata")
793-
}
794-
want := sanitize(tests.SortAndFormatSpans(wantSpans))
795-
got := sanitize(tests.SortAndFormatSpans(gotLocs))
796-
if got != want {
797-
t.Errorf("implementations(%s):\n%s", sanitize(fmt.Sprint(spn)), diff.Unified("want", "got", want, got))
798-
}
799-
}
800-
801771
func (r *runner) Highlight(t *testing.T, src span.Span, spans []span.Span) {
802772
m, err := r.data.Mapper(src.URI())
803773
if err != nil {

gopls/internal/lsp/regtest/marker.go

Lines changed: 58 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import (
3434
"golang.org/x/tools/gopls/internal/lsp/source"
3535
"golang.org/x/tools/gopls/internal/lsp/tests"
3636
"golang.org/x/tools/gopls/internal/lsp/tests/compare"
37-
"golang.org/x/tools/internal/diff"
3837
"golang.org/x/tools/internal/jsonrpc2"
3938
"golang.org/x/tools/internal/jsonrpc2/servertest"
4039
"golang.org/x/tools/internal/testenv"
@@ -130,6 +129,10 @@ var update = flag.Bool("update", false, "if set, update test data during marker
130129
// src location, and checks that the result is the dst location, with hover
131130
// content matching "hover.md" in the golden data g.
132131
//
132+
// - implementations(src location, want ...location): makes a
133+
// textDocument/implementation query at the src location and
134+
// checks that the resulting set of locations matches want.
135+
//
133136
// - loc(name, location): specifies the name for a location in the source. These
134137
// locations may be referenced by other markers.
135138
//
@@ -258,7 +261,6 @@ var update = flag.Bool("update", false, "if set, update test data during marker
258261
// - FunctionExtractions
259262
// - MethodExtractions
260263
// - Definitions
261-
// - Implementations
262264
// - Highlights
263265
// - Renames
264266
// - PrepareRenames
@@ -428,9 +430,7 @@ func (mark marker) execute() {
428430
if i < len(fn.converters) {
429431
convert = fn.converters[i]
430432
} else if !fn.variadic {
431-
mark.errorf("got %d arguments to %s, expect %d",
432-
len(mark.note.Args), mark.note.Name, len(fn.converters))
433-
return
433+
goto arity // too many args
434434
}
435435

436436
// Special handling for the blank identifier: treat it as the zero value.
@@ -447,8 +447,16 @@ func (mark marker) execute() {
447447
}
448448
args = append(args, reflect.ValueOf(out))
449449
}
450+
if len(args) < len(fn.converters) {
451+
goto arity // too few args
452+
}
450453

451454
fn.fn.Call(args)
455+
return
456+
457+
arity:
458+
mark.errorf("got %d arguments to %s, want %d",
459+
len(mark.note.Args), mark.note.Name, len(fn.converters))
452460
}
453461

454462
// Supported marker functions.
@@ -459,15 +467,16 @@ func (mark marker) execute() {
459467
// Marker funcs should not mutate the test environment (e.g. via opening files
460468
// or applying edits in the editor).
461469
var markerFuncs = map[string]markerFunc{
462-
"complete": makeMarkerFunc(completeMarker),
463-
"def": makeMarkerFunc(defMarker),
464-
"diag": makeMarkerFunc(diagMarker),
465-
"hover": makeMarkerFunc(hoverMarker),
466-
"loc": makeMarkerFunc(locMarker),
467-
"rename": makeMarkerFunc(renameMarker),
468-
"renameerr": makeMarkerFunc(renameErrMarker),
469-
"suggestedfix": makeMarkerFunc(suggestedfixMarker),
470-
"refs": makeMarkerFunc(refsMarker),
470+
"complete": makeMarkerFunc(completeMarker),
471+
"def": makeMarkerFunc(defMarker),
472+
"diag": makeMarkerFunc(diagMarker),
473+
"hover": makeMarkerFunc(hoverMarker),
474+
"implementation": makeMarkerFunc(implementationMarker),
475+
"loc": makeMarkerFunc(locMarker),
476+
"rename": makeMarkerFunc(renameMarker),
477+
"renameerr": makeMarkerFunc(renameErrMarker),
478+
"suggestedfix": makeMarkerFunc(suggestedfixMarker),
479+
"refs": makeMarkerFunc(refsMarker),
471480
}
472481

473482
// markerTest holds all the test data extracted from a test txtar archive.
@@ -1354,37 +1363,17 @@ func suggestedfix(env *Env, loc protocol.Location, diag protocol.Diagnostic, act
13541363
// refsMarker implements the @refs marker.
13551364
func refsMarker(mark marker, src protocol.Location, want ...protocol.Location) {
13561365
refs := func(includeDeclaration bool, want []protocol.Location) error {
1357-
params := &protocol.ReferenceParams{
1366+
got, err := mark.run.env.Editor.Server.References(mark.run.env.Ctx, &protocol.ReferenceParams{
13581367
TextDocumentPositionParams: protocol.LocationTextDocumentPositionParams(src),
13591368
Context: protocol.ReferenceContext{
13601369
IncludeDeclaration: includeDeclaration,
13611370
},
1362-
}
1363-
1364-
got, err := mark.run.env.Editor.Server.References(mark.run.env.Ctx, params)
1371+
})
13651372
if err != nil {
13661373
return err
13671374
}
13681375

1369-
// Compare the sets of locations.
1370-
toString := func(locs []protocol.Location) string {
1371-
// TODO(adonovan): use generic JoinValues(locs, fmtLoc).
1372-
strs := make([]string, len(locs))
1373-
for i, loc := range locs {
1374-
strs[i] = mark.run.fmtLoc(loc)
1375-
}
1376-
sort.Strings(strs)
1377-
return strings.Join(strs, "\n")
1378-
}
1379-
gotStr := toString(got)
1380-
wantStr := toString(want)
1381-
if gotStr != wantStr {
1382-
return fmt.Errorf("incorrect references (got %d, want %d) at %s:\n%s",
1383-
len(got), len(want),
1384-
mark.run.fmtLoc(src),
1385-
diff.Unified("want", "got", wantStr, gotStr))
1386-
}
1387-
return nil
1376+
return compareLocations(mark, got, want)
13881377
}
13891378

13901379
for _, includeDeclaration := range []bool{false, true} {
@@ -1401,3 +1390,35 @@ func refsMarker(mark marker, src protocol.Location, want ...protocol.Location) {
14011390
}
14021391
}
14031392
}
1393+
1394+
// implementationMarker implements the @implementation marker.
1395+
func implementationMarker(mark marker, src protocol.Location, want ...protocol.Location) {
1396+
got, err := mark.run.env.Editor.Server.Implementation(mark.run.env.Ctx, &protocol.ImplementationParams{
1397+
TextDocumentPositionParams: protocol.LocationTextDocumentPositionParams(src),
1398+
})
1399+
if err != nil {
1400+
mark.errorf("implementation at %s failed: %v", src, err)
1401+
return
1402+
}
1403+
if err := compareLocations(mark, got, want); err != nil {
1404+
mark.errorf("implementation: %v", err)
1405+
}
1406+
}
1407+
1408+
// compareLocations returns an error message if got and want are not
1409+
// the same set of locations. The marker is used only for fmtLoc.
1410+
func compareLocations(mark marker, got, want []protocol.Location) error {
1411+
toStrings := func(locs []protocol.Location) []string {
1412+
strs := make([]string, len(locs))
1413+
for i, loc := range locs {
1414+
strs[i] = mark.run.fmtLoc(loc)
1415+
}
1416+
sort.Strings(strs)
1417+
return strs
1418+
}
1419+
if diff := cmp.Diff(toStrings(want), toStrings(got)); diff != "" {
1420+
return fmt.Errorf("incorrect result locations: (got %d, want %d):\n%s",
1421+
len(got), len(want), diff)
1422+
}
1423+
return nil
1424+
}

gopls/internal/lsp/testdata/implementation/implementation.go

Lines changed: 0 additions & 37 deletions
This file was deleted.

gopls/internal/lsp/testdata/implementation/implementation_generics.go

Lines changed: 0 additions & 16 deletions
This file was deleted.

gopls/internal/lsp/testdata/implementation/other/other.go

Lines changed: 0 additions & 27 deletions
This file was deleted.

gopls/internal/lsp/testdata/implementation/other/other_generics.go

Lines changed: 0 additions & 16 deletions
This file was deleted.

gopls/internal/lsp/testdata/implementation/other/other_test.go

Lines changed: 0 additions & 10 deletions
This file was deleted.

gopls/internal/lsp/testdata/summary.txt.golden

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,5 @@ SymbolsCount = 1
2626
WorkspaceSymbolsCount = 20
2727
SignaturesCount = 33
2828
LinksCount = 7
29-
ImplementationsCount = 16
3029
SelectionRangesCount = 3
3130

gopls/internal/lsp/testdata/summary_go1.18.txt.golden

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,5 @@ SymbolsCount = 2
2626
WorkspaceSymbolsCount = 20
2727
SignaturesCount = 33
2828
LinksCount = 7
29-
ImplementationsCount = 26
3029
SelectionRangesCount = 3
3130

0 commit comments

Comments
 (0)