Skip to content

Commit 51dfad7

Browse files
committed
gopls: port workspace/symbol marker tests to the new framework
Updates golang/go#54845 Change-Id: I1857786a7034782d545c6b5585c83ec00b4e855c Reviewed-on: https://go-review.googlesource.com/c/tools/+/490875 gopls-CI: kokoro <[email protected]> Run-TryBot: Robert Findley <[email protected]> Reviewed-by: Alan Donovan <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent c6b5780 commit 51dfad7

20 files changed

+304
-342
lines changed

gopls/internal/lsp/lsp_test.go

-29
Original file line numberDiff line numberDiff line change
@@ -956,35 +956,6 @@ func applyTextDocumentEdits(r *runner, edits []protocol.DocumentChanges) (map[sp
956956
return res, nil
957957
}
958958

959-
func (r *runner) WorkspaceSymbols(t *testing.T, uri span.URI, query string, typ tests.WorkspaceSymbolsTestType) {
960-
matcher := tests.WorkspaceSymbolsTestTypeToMatcher(typ)
961-
962-
original := r.server.session.Options()
963-
modified := original
964-
modified.SymbolMatcher = matcher
965-
r.server.session.SetOptions(modified)
966-
defer r.server.session.SetOptions(original)
967-
968-
params := &protocol.WorkspaceSymbolParams{
969-
Query: query,
970-
}
971-
gotSymbols, err := r.server.Symbol(r.ctx, params)
972-
if err != nil {
973-
t.Fatal(err)
974-
}
975-
got, err := tests.WorkspaceSymbolsString(r.ctx, r.data, uri, gotSymbols)
976-
if err != nil {
977-
t.Fatal(err)
978-
}
979-
got = filepath.ToSlash(tests.Normalize(got, r.normalizers))
980-
want := string(r.data.Golden(t, fmt.Sprintf("workspace_symbol-%s-%s", strings.ToLower(string(matcher)), query), uri.Filename(), func() ([]byte, error) {
981-
return []byte(got), nil
982-
}))
983-
if diff := compare.Text(want, got); diff != "" {
984-
t.Error(diff)
985-
}
986-
}
987-
988959
func (r *runner) SignatureHelp(t *testing.T, spn span.Span, want *protocol.SignatureHelp) {
989960
m, err := r.data.Mapper(spn.URI())
990961
if err != nil {

gopls/internal/lsp/regtest/marker.go

+61-13
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,14 @@ var update = flag.Bool("update", false, "if set, update test data during marker
175175
// location information. There is no point to using more than one
176176
// @symbol marker in a given file.
177177
//
178+
// - workspacesymbol(query, golden): makes a workspace/symbol request for the
179+
// given query, formats the response with one symbol per line, and compares
180+
// against the named golden file. As workspace symbols are by definition a
181+
// workspace-wide request, the location of the workspace symbol marker does
182+
// not matter. Each line is of the form:
183+
//
184+
// location name kind
185+
//
178186
// # Argument conversion
179187
//
180188
// Marker arguments are first parsed by the go/expect package, which accepts
@@ -492,18 +500,19 @@ arity:
492500
// Marker funcs should not mutate the test environment (e.g. via opening files
493501
// or applying edits in the editor).
494502
var markerFuncs = map[string]markerFunc{
495-
"complete": makeMarkerFunc(completeMarker),
496-
"def": makeMarkerFunc(defMarker),
497-
"diag": makeMarkerFunc(diagMarker),
498-
"hover": makeMarkerFunc(hoverMarker),
499-
"format": makeMarkerFunc(formatMarker),
500-
"implementation": makeMarkerFunc(implementationMarker),
501-
"loc": makeMarkerFunc(locMarker),
502-
"rename": makeMarkerFunc(renameMarker),
503-
"renameerr": makeMarkerFunc(renameErrMarker),
504-
"suggestedfix": makeMarkerFunc(suggestedfixMarker),
505-
"symbol": makeMarkerFunc(symbolMarker),
506-
"refs": makeMarkerFunc(refsMarker),
503+
"complete": makeMarkerFunc(completeMarker),
504+
"def": makeMarkerFunc(defMarker),
505+
"diag": makeMarkerFunc(diagMarker),
506+
"hover": makeMarkerFunc(hoverMarker),
507+
"format": makeMarkerFunc(formatMarker),
508+
"implementation": makeMarkerFunc(implementationMarker),
509+
"loc": makeMarkerFunc(locMarker),
510+
"rename": makeMarkerFunc(renameMarker),
511+
"renameerr": makeMarkerFunc(renameErrMarker),
512+
"suggestedfix": makeMarkerFunc(suggestedfixMarker),
513+
"symbol": makeMarkerFunc(symbolMarker),
514+
"refs": makeMarkerFunc(refsMarker),
515+
"workspacesymbol": makeMarkerFunc(workspaceSymbolMarker),
507516
}
508517

509518
// markerTest holds all the test data extracted from a test txtar archive.
@@ -863,6 +872,12 @@ func (run *markerTestRun) fmtPos(pos token.Pos) string {
863872
// archive-relative paths for files and including the line number in the full
864873
// archive file.
865874
func (run *markerTestRun) fmtLoc(loc protocol.Location) string {
875+
return run.fmtLocDetails(loc, true)
876+
}
877+
878+
// See fmtLoc. If includeTxtPos is not set, the position in the full archive
879+
// file is omitted.
880+
func (run *markerTestRun) fmtLocDetails(loc protocol.Location, includeTxtPos bool) string {
866881
if loc == (protocol.Location{}) {
867882
return "<missing location>"
868883
}
@@ -904,7 +919,11 @@ func (run *markerTestRun) fmtLoc(loc protocol.Location) string {
904919
}
905920
}
906921

907-
return fmt.Sprintf("%s:%s (%s:%s)", name, innerSpan, run.test.name, outerSpan)
922+
if includeTxtPos {
923+
return fmt.Sprintf("%s:%s (%s:%s)", name, innerSpan, run.test.name, outerSpan)
924+
} else {
925+
return fmt.Sprintf("%s:%s", name, innerSpan)
926+
}
908927
}
909928

910929
// makeMarkerFunc uses reflection to create a markerFunc for the given func value.
@@ -1571,3 +1590,32 @@ func compareLocations(mark marker, got, want []protocol.Location) error {
15711590
}
15721591
return nil
15731592
}
1593+
1594+
func workspaceSymbolMarker(mark marker, query string, golden *Golden) {
1595+
params := &protocol.WorkspaceSymbolParams{
1596+
Query: query,
1597+
}
1598+
1599+
gotSymbols, err := mark.server().Symbol(mark.run.env.Ctx, params)
1600+
if err != nil {
1601+
mark.errorf("Symbol(%q) failed: %v", query, err)
1602+
return
1603+
}
1604+
var got bytes.Buffer
1605+
for _, s := range gotSymbols {
1606+
// Omit the txtar position of the symbol location; otherwise edits to the
1607+
// txtar archive lead to unexpected failures.
1608+
loc := mark.run.fmtLocDetails(s.Location, false)
1609+
fmt.Fprintf(&got, "%s %s %s\n", loc, s.Name, s.Kind)
1610+
}
1611+
1612+
want, ok := golden.Get(mark.run.env.T, "", got.Bytes())
1613+
if !ok {
1614+
mark.errorf("missing golden file @%s", golden.id)
1615+
return
1616+
}
1617+
1618+
if diff := compare.Bytes(want, got.Bytes()); diff != "" {
1619+
mark.errorf("Symbol(%q) mismatch:\n%s", query, diff)
1620+
}
1621+
}

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

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ HighlightsCount = 69
2121
InlayHintsCount = 4
2222
RenamesCount = 41
2323
PrepareRenamesCount = 7
24-
WorkspaceSymbolsCount = 20
2524
SignaturesCount = 33
2625
LinksCount = 7
2726
SelectionRangesCount = 3

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

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ HighlightsCount = 69
2121
InlayHintsCount = 5
2222
RenamesCount = 48
2323
PrepareRenamesCount = 7
24-
WorkspaceSymbolsCount = 20
2524
SignaturesCount = 33
2625
LinksCount = 7
2726
SelectionRangesCount = 3

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

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ HighlightsCount = 69
2121
InlayHintsCount = 5
2222
RenamesCount = 48
2323
PrepareRenamesCount = 7
24-
WorkspaceSymbolsCount = 20
2524
SignaturesCount = 33
2625
LinksCount = 7
2726
SelectionRangesCount = 3

gopls/internal/lsp/testdata/workspacesymbol/a/a.go

-9
This file was deleted.

gopls/internal/lsp/testdata/workspacesymbol/a/a_test.go

-3
This file was deleted.

gopls/internal/lsp/testdata/workspacesymbol/a/a_x_test.go

-3
This file was deleted.

gopls/internal/lsp/testdata/workspacesymbol/b/b.go

-7
This file was deleted.

gopls/internal/lsp/testdata/workspacesymbol/issue44806.go

-10
This file was deleted.

gopls/internal/lsp/testdata/workspacesymbol/main.go

-47
This file was deleted.

gopls/internal/lsp/testdata/workspacesymbol/p/p.go

-3
This file was deleted.

gopls/internal/lsp/testdata/workspacesymbol/query.go

-29
This file was deleted.

gopls/internal/lsp/testdata/workspacesymbol/query.go.golden

-83
This file was deleted.

0 commit comments

Comments
 (0)