Skip to content

Commit 259af5f

Browse files
muirdmstamblerre
authored andcommitted
internal/lsp: tweak implementation tests
- Add test count to golden file so test count gets checked. - Make @implementation note take a list of marks similar to completion tests. - Get rid of unnecessary intermediate test data type. Change-Id: I741eb14b77b0b8ed08e86c634ed39457116e8718 Reviewed-on: https://go-review.googlesource.com/c/tools/+/210278 Run-TryBot: Muir Manders <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Rebecca Stambler <[email protected]>
1 parent bc36936 commit 259af5f

File tree

6 files changed

+39
-47
lines changed

6 files changed

+39
-47
lines changed

internal/lsp/cmd/test/implementation.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@ import (
99
"sort"
1010
"testing"
1111

12-
"golang.org/x/tools/internal/lsp/tests"
1312
"golang.org/x/tools/internal/span"
1413
)
1514

16-
func (r *runner) Implementation(t *testing.T, spn span.Span, imp tests.Implementations) {
15+
func (r *runner) Implementation(t *testing.T, spn span.Span, imps []span.Span) {
1716
var itemStrings []string
18-
for _, i := range imp.Implementations {
17+
for _, i := range imps {
1918
itemStrings = append(itemStrings, fmt.Sprint(i))
2019
}
2120
sort.Strings(itemStrings)

internal/lsp/lsp_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -436,14 +436,14 @@ func (r *runner) Definition(t *testing.T, spn span.Span, d tests.Definition) {
436436
}
437437
}
438438

439-
func (r *runner) Implementation(t *testing.T, spn span.Span, m tests.Implementations) {
440-
sm, err := r.data.Mapper(m.Src.URI())
439+
func (r *runner) Implementation(t *testing.T, spn span.Span, impls []span.Span) {
440+
sm, err := r.data.Mapper(spn.URI())
441441
if err != nil {
442442
t.Fatal(err)
443443
}
444-
loc, err := sm.Location(m.Src)
444+
loc, err := sm.Location(spn)
445445
if err != nil {
446-
t.Fatalf("failed for %v: %v", m.Src, err)
446+
t.Fatalf("failed for %v: %v", spn, err)
447447
}
448448
tdpp := protocol.TextDocumentPositionParams{
449449
TextDocument: protocol.TextDocumentIdentifier{URI: loc.URI},
@@ -455,10 +455,10 @@ func (r *runner) Implementation(t *testing.T, spn span.Span, m tests.Implementat
455455
}
456456
locs, err = r.server.Implementation(r.ctx, params)
457457
if err != nil {
458-
t.Fatalf("failed for %v: %v", m.Src, err)
458+
t.Fatalf("failed for %v: %v", spn, err)
459459
}
460-
if len(locs) != len(m.Implementations) {
461-
t.Fatalf("got %d locations for implementation, expected %d", len(locs), len(m.Implementations))
460+
if len(locs) != len(impls) {
461+
t.Fatalf("got %d locations for implementation, expected %d", len(locs), len(impls))
462462
}
463463

464464
var results []span.Span
@@ -478,12 +478,12 @@ func (r *runner) Implementation(t *testing.T, spn span.Span, m tests.Implementat
478478
sort.SliceStable(results, func(i, j int) bool {
479479
return span.Compare(results[i], results[j]) == -1
480480
})
481-
sort.SliceStable(m.Implementations, func(i, j int) bool {
482-
return span.Compare(m.Implementations[i], m.Implementations[j]) == -1
481+
sort.SliceStable(impls, func(i, j int) bool {
482+
return span.Compare(impls[i], impls[j]) == -1
483483
})
484484
for i := range results {
485-
if results[i] != m.Implementations[i] {
486-
t.Errorf("for %dth implementation of %v got %v want %v", i, m.Src, results[i], m.Implementations[i])
485+
if results[i] != impls[i] {
486+
t.Errorf("for %dth implementation of %v got %v want %v", i, spn, results[i], impls[i])
487487
}
488488
}
489489
}

internal/lsp/source/source_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -548,31 +548,31 @@ func (r *runner) Definition(t *testing.T, spn span.Span, d tests.Definition) {
548548
}
549549
}
550550

551-
func (r *runner) Implementation(t *testing.T, spn span.Span, m tests.Implementations) {
551+
func (r *runner) Implementation(t *testing.T, spn span.Span, impls []span.Span) {
552552
ctx := r.ctx
553-
f, err := r.view.GetFile(ctx, m.Src.URI())
553+
f, err := r.view.GetFile(ctx, spn.URI())
554554
if err != nil {
555-
t.Fatalf("failed for %v: %v", m.Src, err)
555+
t.Fatalf("failed for %v: %v", spn, err)
556556
}
557-
sm, err := r.data.Mapper(m.Src.URI())
557+
sm, err := r.data.Mapper(spn.URI())
558558
if err != nil {
559559
t.Fatal(err)
560560
}
561-
loc, err := sm.Location(m.Src)
561+
loc, err := sm.Location(spn)
562562
if err != nil {
563-
t.Fatalf("failed for %v: %v", m.Src, err)
563+
t.Fatalf("failed for %v: %v", spn, err)
564564
}
565565
ident, err := source.Identifier(ctx, r.view.Snapshot(), f, loc.Range.Start)
566566
if err != nil {
567-
t.Fatalf("failed for %v: %v", m.Src, err)
567+
t.Fatalf("failed for %v: %v", spn, err)
568568
}
569569
var locs []protocol.Location
570570
locs, err = ident.Implementation(r.ctx)
571571
if err != nil {
572-
t.Fatalf("failed for %v: %v", m.Src, err)
572+
t.Fatalf("failed for %v: %v", spn, err)
573573
}
574-
if len(locs) != len(m.Implementations) {
575-
t.Fatalf("got %d locations for implementation, expected %d", len(locs), len(m.Implementations))
574+
if len(locs) != len(impls) {
575+
t.Fatalf("got %d locations for implementation, expected %d", len(locs), len(impls))
576576
}
577577
var results []span.Span
578578
for i := range locs {
@@ -591,12 +591,12 @@ func (r *runner) Implementation(t *testing.T, spn span.Span, m tests.Implementat
591591
sort.SliceStable(results, func(i, j int) bool {
592592
return span.Compare(results[i], results[j]) == -1
593593
})
594-
sort.SliceStable(m.Implementations, func(i, j int) bool {
595-
return span.Compare(m.Implementations[i], m.Implementations[j]) == -1
594+
sort.SliceStable(impls, func(i, j int) bool {
595+
return span.Compare(impls[i], impls[j]) == -1
596596
})
597597
for i := range results {
598-
if results[i] != m.Implementations[i] {
599-
t.Errorf("for %dth implementation of %v got %v want %v", i, m.Src, results[i], m.Implementations[i])
598+
if results[i] != impls[i] {
599+
t.Errorf("for %dth implementation of %v got %v want %v", i, spn, results[i], impls[i])
600600
}
601601
}
602602
}

internal/lsp/testdata/implementation/implementation.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ func (ImpS) Laugh() { //@mark(LaughS, "Laugh")
1313
}
1414

1515
type ImpI interface { //@ImpI
16-
Laugh() //@mark(LaughI, "Laugh"),implementations("augh", LaughP),implementations("augh", OtherLaughP),implementations("augh", LaughS),implementations("augh", LaughL),implementations("augh", OtherLaughI),implementations("augh", OtherLaughS)
16+
Laugh() //@mark(LaughI, "Laugh"),implementations("Laugh", LaughP, OtherLaughP, LaughS, LaughL, OtherLaughI, OtherLaughS)
1717
}
1818

19-
type Laugher interface { //@Laugher,implementations("augher", ImpP),implementations("augher", OtherImpP),implementations("augher", ImpI),implementations("augher", ImpS),implementations("augher", OtherImpI),implementations("augher", OtherImpS),
20-
Laugh() //@mark(LaughL, "Laugh"),implementations("augh", LaughP),implementations("augh", OtherLaughP),implementations("augh", LaughI),implementations("augh", LaughS),implementations("augh", OtherLaughI),implementations("augh", OtherLaughS)
19+
type Laugher interface { //@implementations("Laugher", ImpP, OtherImpP, ImpI, ImpS, OtherImpI, OtherImpS)
20+
Laugh() //@mark(LaughL, "Laugh"),implementations("Laugh", LaughP, OtherLaughP, LaughI, LaughS, OtherLaughI, OtherLaughS)
2121
}
2222

2323
type Foo struct {

internal/lsp/testdata/summary.txt.golden

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ PrepareRenamesCount = 8
2020
SymbolsCount = 1
2121
SignaturesCount = 22
2222
LinksCount = 6
23+
ImplementationsCount = 4
2324

internal/lsp/tests/tests.go

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ type Formats []span.Span
5353
type Imports []span.Span
5454
type SuggestedFixes []span.Span
5555
type Definitions map[span.Span]Definition
56-
type Implementationses map[span.Span]Implementations
56+
type Implementations map[span.Span][]span.Span
5757
type Highlights map[span.Span][]span.Span
5858
type References map[span.Span][]span.Span
5959
type Renames map[span.Span]string
@@ -80,7 +80,7 @@ type Data struct {
8080
Imports Imports
8181
SuggestedFixes SuggestedFixes
8282
Definitions Definitions
83-
Implementationses Implementationses
83+
Implementations Implementations
8484
Highlights Highlights
8585
References References
8686
Renames Renames
@@ -113,7 +113,7 @@ type Tests interface {
113113
Import(*testing.T, span.Span)
114114
SuggestedFix(*testing.T, span.Span)
115115
Definition(*testing.T, span.Span, Definition)
116-
Implementation(*testing.T, span.Span, Implementations)
116+
Implementation(*testing.T, span.Span, []span.Span)
117117
Highlight(*testing.T, span.Span, []span.Span)
118118
References(*testing.T, span.Span, []span.Span)
119119
Rename(*testing.T, span.Span, string)
@@ -130,11 +130,6 @@ type Definition struct {
130130
Src, Def span.Span
131131
}
132132

133-
type Implementations struct {
134-
Src span.Span
135-
Implementations []span.Span
136-
}
137-
138133
type CompletionTestType int
139134

140135
const (
@@ -214,7 +209,7 @@ func Load(t testing.TB, exporter packagestest.Exporter, dir string) *Data {
214209
RankCompletions: make(RankCompletions),
215210
CaseSensitiveCompletions: make(CaseSensitiveCompletions),
216211
Definitions: make(Definitions),
217-
Implementationses: make(Implementationses),
212+
Implementations: make(Implementations),
218213
Highlights: make(Highlights),
219214
References: make(References),
220215
Renames: make(Renames),
@@ -478,7 +473,7 @@ func Run(t *testing.T, tests Tests, data *Data) {
478473

479474
t.Run("Implementation", func(t *testing.T) {
480475
t.Helper()
481-
for spn, m := range data.Implementationses {
476+
for spn, m := range data.Implementations {
482477
t.Run(spanName(spn), func(t *testing.T) {
483478
t.Helper()
484479
tests.Implementation(t, spn, m)
@@ -624,6 +619,7 @@ func checkData(t *testing.T, data *Data) {
624619
fmt.Fprintf(buf, "SymbolsCount = %v\n", len(data.Symbols))
625620
fmt.Fprintf(buf, "SignaturesCount = %v\n", len(data.Signatures))
626621
fmt.Fprintf(buf, "LinksCount = %v\n", linksCount)
622+
fmt.Fprintf(buf, "ImplementationsCount = %v\n", len(data.Implementations))
627623

628624
want := string(data.Golden("summary", "summary.txt", func() ([]byte, error) {
629625
return buf.Bytes(), nil
@@ -804,12 +800,8 @@ func (data *Data) collectDefinitions(src, target span.Span) {
804800
}
805801
}
806802

807-
func (data *Data) collectImplementations(src, target span.Span) {
808-
// Add target to the list of expected implementations for src
809-
imps := data.Implementationses[src]
810-
imps.Src = src // Src is already set if imps already exists, but then we're setting it to the same thing.
811-
imps.Implementations = append(imps.Implementations, target)
812-
data.Implementationses[src] = imps
803+
func (data *Data) collectImplementations(src span.Span, targets []span.Span) {
804+
data.Implementations[src] = targets
813805
}
814806

815807
func (data *Data) collectHoverDefinitions(src, target span.Span) {

0 commit comments

Comments
 (0)