-
Notifications
You must be signed in to change notification settings - Fork 39
/
tests.go
1421 lines (1291 loc) · 40.5 KB
/
tests.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package tests exports functionality to be used across a variety of gopls tests.
package tests
import (
"bytes"
"context"
"flag"
"fmt"
"go/ast"
"go/token"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"sync"
"testing"
"time"
"github.com/system-pclub/GCatch/GCatch/tools/go/expect"
"github.com/system-pclub/GCatch/GCatch/tools/go/packages"
"github.com/system-pclub/GCatch/GCatch/tools/go/packages/packagestest"
"github.com/system-pclub/GCatch/GCatch/tools/internal/lsp/command"
"github.com/system-pclub/GCatch/GCatch/tools/internal/lsp/protocol"
"github.com/system-pclub/GCatch/GCatch/tools/internal/lsp/source"
"github.com/system-pclub/GCatch/GCatch/tools/internal/lsp/source/completion"
"github.com/system-pclub/GCatch/GCatch/tools/internal/span"
"github.com/system-pclub/GCatch/GCatch/tools/internal/testenv"
"github.com/system-pclub/GCatch/GCatch/tools/internal/typeparams"
"github.com/system-pclub/GCatch/GCatch/tools/txtar"
)
const (
overlayFileSuffix = ".overlay"
goldenFileSuffix = ".golden"
inFileSuffix = ".in"
testModule = "github.com/system-pclub/GCatch/GCatch/tools/internal/lsp"
)
var summaryFile = "summary.txt"
func init() {
if typeparams.Enabled {
summaryFile = "summary_go1.18.txt"
}
}
var UpdateGolden = flag.Bool("golden", false, "Update golden files")
type CallHierarchy map[span.Span]*CallHierarchyResult
type CodeLens map[span.URI][]protocol.CodeLens
type Diagnostics map[span.URI][]*source.Diagnostic
type CompletionItems map[token.Pos]*completion.CompletionItem
type Completions map[span.Span][]Completion
type CompletionSnippets map[span.Span][]CompletionSnippet
type UnimportedCompletions map[span.Span][]Completion
type DeepCompletions map[span.Span][]Completion
type FuzzyCompletions map[span.Span][]Completion
type CaseSensitiveCompletions map[span.Span][]Completion
type RankCompletions map[span.Span][]Completion
type FoldingRanges []span.Span
type Formats []span.Span
type Imports []span.Span
type SemanticTokens []span.Span
type SuggestedFixes map[span.Span][]string
type FunctionExtractions map[span.Span]span.Span
type MethodExtractions map[span.Span]span.Span
type Definitions map[span.Span]Definition
type Implementations map[span.Span][]span.Span
type Highlights map[span.Span][]span.Span
type References map[span.Span][]span.Span
type Renames map[span.Span]string
type PrepareRenames map[span.Span]*source.PrepareItem
type Symbols map[span.URI][]protocol.DocumentSymbol
type SymbolsChildren map[string][]protocol.DocumentSymbol
type SymbolInformation map[span.Span]protocol.SymbolInformation
type WorkspaceSymbols map[WorkspaceSymbolsTestType]map[span.URI][]string
type Signatures map[span.Span]*protocol.SignatureHelp
type Links map[span.URI][]Link
type AddImport map[span.URI]string
type Hovers map[span.Span]string
type Data struct {
Config packages.Config
Exported *packagestest.Exported
CallHierarchy CallHierarchy
CodeLens CodeLens
Diagnostics Diagnostics
CompletionItems CompletionItems
Completions Completions
CompletionSnippets CompletionSnippets
UnimportedCompletions UnimportedCompletions
DeepCompletions DeepCompletions
FuzzyCompletions FuzzyCompletions
CaseSensitiveCompletions CaseSensitiveCompletions
RankCompletions RankCompletions
FoldingRanges FoldingRanges
Formats Formats
Imports Imports
SemanticTokens SemanticTokens
SuggestedFixes SuggestedFixes
FunctionExtractions FunctionExtractions
MethodExtractions MethodExtractions
Definitions Definitions
Implementations Implementations
Highlights Highlights
References References
Renames Renames
PrepareRenames PrepareRenames
Symbols Symbols
symbolsChildren SymbolsChildren
symbolInformation SymbolInformation
WorkspaceSymbols WorkspaceSymbols
Signatures Signatures
Links Links
AddImport AddImport
Hovers Hovers
t testing.TB
fragments map[string]string
dir string
golden map[string]*Golden
mode string
ModfileFlagAvailable bool
mappersMu sync.Mutex
mappers map[span.URI]*protocol.ColumnMapper
}
type Tests interface {
CallHierarchy(*testing.T, span.Span, *CallHierarchyResult)
CodeLens(*testing.T, span.URI, []protocol.CodeLens)
Diagnostics(*testing.T, span.URI, []*source.Diagnostic)
Completion(*testing.T, span.Span, Completion, CompletionItems)
CompletionSnippet(*testing.T, span.Span, CompletionSnippet, bool, CompletionItems)
UnimportedCompletion(*testing.T, span.Span, Completion, CompletionItems)
DeepCompletion(*testing.T, span.Span, Completion, CompletionItems)
FuzzyCompletion(*testing.T, span.Span, Completion, CompletionItems)
CaseSensitiveCompletion(*testing.T, span.Span, Completion, CompletionItems)
RankCompletion(*testing.T, span.Span, Completion, CompletionItems)
FoldingRanges(*testing.T, span.Span)
Format(*testing.T, span.Span)
Import(*testing.T, span.Span)
SemanticTokens(*testing.T, span.Span)
SuggestedFix(*testing.T, span.Span, []string, int)
FunctionExtraction(*testing.T, span.Span, span.Span)
MethodExtraction(*testing.T, span.Span, span.Span)
Definition(*testing.T, span.Span, Definition)
Implementation(*testing.T, span.Span, []span.Span)
Highlight(*testing.T, span.Span, []span.Span)
References(*testing.T, span.Span, []span.Span)
Rename(*testing.T, span.Span, string)
PrepareRename(*testing.T, span.Span, *source.PrepareItem)
Symbols(*testing.T, span.URI, []protocol.DocumentSymbol)
WorkspaceSymbols(*testing.T, span.URI, string, WorkspaceSymbolsTestType)
SignatureHelp(*testing.T, span.Span, *protocol.SignatureHelp)
Link(*testing.T, span.URI, []Link)
AddImport(*testing.T, span.URI, string)
Hover(*testing.T, span.Span, string)
}
type Definition struct {
Name string
IsType bool
OnlyHover bool
Src, Def span.Span
}
type CompletionTestType int
const (
// Default runs the standard completion tests.
CompletionDefault = CompletionTestType(iota)
// Unimported tests the autocompletion of unimported packages.
CompletionUnimported
// Deep tests deep completion.
CompletionDeep
// Fuzzy tests deep completion and fuzzy matching.
CompletionFuzzy
// CaseSensitive tests case sensitive completion.
CompletionCaseSensitive
// CompletionRank candidates in test must be valid and in the right relative order.
CompletionRank
)
type WorkspaceSymbolsTestType int
const (
// Default runs the standard workspace symbols tests.
WorkspaceSymbolsDefault = WorkspaceSymbolsTestType(iota)
// Fuzzy tests workspace symbols with fuzzy matching.
WorkspaceSymbolsFuzzy
// CaseSensitive tests workspace symbols with case sensitive.
WorkspaceSymbolsCaseSensitive
)
type Completion struct {
CompletionItems []token.Pos
}
type CompletionSnippet struct {
CompletionItem token.Pos
PlainSnippet string
PlaceholderSnippet string
}
type CallHierarchyResult struct {
IncomingCalls, OutgoingCalls []protocol.CallHierarchyItem
}
type Link struct {
Src span.Span
Target string
NotePosition token.Position
}
type Golden struct {
Filename string
Archive *txtar.Archive
Modified bool
}
func Context(t testing.TB) context.Context {
return context.Background()
}
func DefaultOptions(o *source.Options) {
o.SupportedCodeActions = map[source.FileKind]map[protocol.CodeActionKind]bool{
source.Go: {
protocol.SourceOrganizeImports: true,
protocol.QuickFix: true,
protocol.RefactorRewrite: true,
protocol.RefactorExtract: true,
protocol.SourceFixAll: true,
},
source.Mod: {
protocol.SourceOrganizeImports: true,
},
source.Sum: {},
source.Tmpl: {},
}
o.UserOptions.Codelenses[string(command.Test)] = true
o.HoverKind = source.SynopsisDocumentation
o.InsertTextFormat = protocol.SnippetTextFormat
o.CompletionBudget = time.Minute
o.HierarchicalDocumentSymbolSupport = true
o.ExperimentalWorkspaceModule = true
o.SemanticTokens = true
}
func RunTests(t *testing.T, dataDir string, includeMultiModule bool, f func(*testing.T, *Data)) {
t.Helper()
modes := []string{"Modules", "GOPATH"}
if includeMultiModule {
modes = append(modes, "MultiModule")
}
for _, mode := range modes {
t.Run(mode, func(t *testing.T) {
t.Helper()
if mode == "MultiModule" {
// Some bug in 1.12 breaks reading markers, and it's not worth figuring out.
testenv.NeedsGo1Point(t, 13)
}
datum := load(t, mode, dataDir)
f(t, datum)
})
}
}
func load(t testing.TB, mode string, dir string) *Data {
t.Helper()
datum := &Data{
CallHierarchy: make(CallHierarchy),
CodeLens: make(CodeLens),
Diagnostics: make(Diagnostics),
CompletionItems: make(CompletionItems),
Completions: make(Completions),
CompletionSnippets: make(CompletionSnippets),
UnimportedCompletions: make(UnimportedCompletions),
DeepCompletions: make(DeepCompletions),
FuzzyCompletions: make(FuzzyCompletions),
RankCompletions: make(RankCompletions),
CaseSensitiveCompletions: make(CaseSensitiveCompletions),
Definitions: make(Definitions),
Implementations: make(Implementations),
Highlights: make(Highlights),
References: make(References),
Renames: make(Renames),
PrepareRenames: make(PrepareRenames),
SuggestedFixes: make(SuggestedFixes),
FunctionExtractions: make(FunctionExtractions),
MethodExtractions: make(MethodExtractions),
Symbols: make(Symbols),
symbolsChildren: make(SymbolsChildren),
symbolInformation: make(SymbolInformation),
WorkspaceSymbols: make(WorkspaceSymbols),
Signatures: make(Signatures),
Links: make(Links),
AddImport: make(AddImport),
Hovers: make(Hovers),
t: t,
dir: dir,
fragments: map[string]string{},
golden: map[string]*Golden{},
mode: mode,
mappers: map[span.URI]*protocol.ColumnMapper{},
}
if !*UpdateGolden {
summary := filepath.Join(filepath.FromSlash(dir), summaryFile+goldenFileSuffix)
if _, err := os.Stat(summary); os.IsNotExist(err) {
t.Fatalf("could not find golden file summary.txt in %#v", dir)
}
archive, err := txtar.ParseFile(summary)
if err != nil {
t.Fatalf("could not read golden file %v/%v: %v", dir, summary, err)
}
datum.golden[summaryFile] = &Golden{
Filename: summary,
Archive: archive,
}
}
files := packagestest.MustCopyFileTree(dir)
// Prune test cases that exercise generics.
if !typeparams.Enabled {
for name := range files {
if strings.Contains(name, "_generics") {
delete(files, name)
}
}
}
overlays := map[string][]byte{}
for fragment, operation := range files {
if trimmed := strings.TrimSuffix(fragment, goldenFileSuffix); trimmed != fragment {
delete(files, fragment)
goldFile := filepath.Join(dir, fragment)
archive, err := txtar.ParseFile(goldFile)
if err != nil {
t.Fatalf("could not read golden file %v: %v", fragment, err)
}
datum.golden[trimmed] = &Golden{
Filename: goldFile,
Archive: archive,
}
} else if trimmed := strings.TrimSuffix(fragment, inFileSuffix); trimmed != fragment {
delete(files, fragment)
files[trimmed] = operation
} else if index := strings.Index(fragment, overlayFileSuffix); index >= 0 {
delete(files, fragment)
partial := fragment[:index] + fragment[index+len(overlayFileSuffix):]
contents, err := ioutil.ReadFile(filepath.Join(dir, fragment))
if err != nil {
t.Fatal(err)
}
overlays[partial] = contents
}
}
modules := []packagestest.Module{
{
Name: testModule,
Files: files,
Overlay: overlays,
},
}
switch mode {
case "Modules":
datum.Exported = packagestest.Export(t, packagestest.Modules, modules)
case "GOPATH":
datum.Exported = packagestest.Export(t, packagestest.GOPATH, modules)
case "MultiModule":
files := map[string]interface{}{}
for k, v := range modules[0].Files {
files[filepath.Join("testmodule", k)] = v
}
modules[0].Files = files
overlays := map[string][]byte{}
for k, v := range modules[0].Overlay {
overlays[filepath.Join("testmodule", k)] = v
}
modules[0].Overlay = overlays
golden := map[string]*Golden{}
for k, v := range datum.golden {
if k == summaryFile {
golden[k] = v
} else {
golden[filepath.Join("testmodule", k)] = v
}
}
datum.golden = golden
datum.Exported = packagestest.Export(t, packagestest.Modules, modules)
default:
panic("unknown mode " + mode)
}
for _, m := range modules {
for fragment := range m.Files {
filename := datum.Exported.File(m.Name, fragment)
datum.fragments[filename] = fragment
}
}
// Turn off go/packages debug logging.
datum.Exported.Config.Logf = nil
datum.Config.Logf = nil
// Merge the exported.Config with the view.Config.
datum.Config = *datum.Exported.Config
datum.Config.Fset = token.NewFileSet()
datum.Config.Context = Context(nil)
datum.Config.ParseFile = func(fset *token.FileSet, filename string, src []byte) (*ast.File, error) {
panic("ParseFile should not be called")
}
// Do a first pass to collect special markers for completion and workspace symbols.
if err := datum.Exported.Expect(map[string]interface{}{
"item": func(name string, r packagestest.Range, _ []string) {
datum.Exported.Mark(name, r)
},
"symbol": func(name string, r packagestest.Range, _ []string) {
datum.Exported.Mark(name, r)
},
}); err != nil {
t.Fatal(err)
}
// Collect any data that needs to be used by subsequent tests.
if err := datum.Exported.Expect(map[string]interface{}{
"codelens": datum.collectCodeLens,
"diag": datum.collectDiagnostics,
"item": datum.collectCompletionItems,
"complete": datum.collectCompletions(CompletionDefault),
"unimported": datum.collectCompletions(CompletionUnimported),
"deep": datum.collectCompletions(CompletionDeep),
"fuzzy": datum.collectCompletions(CompletionFuzzy),
"casesensitive": datum.collectCompletions(CompletionCaseSensitive),
"rank": datum.collectCompletions(CompletionRank),
"snippet": datum.collectCompletionSnippets,
"fold": datum.collectFoldingRanges,
"format": datum.collectFormats,
"import": datum.collectImports,
"semantic": datum.collectSemanticTokens,
"godef": datum.collectDefinitions,
"implementations": datum.collectImplementations,
"typdef": datum.collectTypeDefinitions,
"hoverdef": datum.collectHoverDefinitions,
"hover": datum.collectHovers,
"highlight": datum.collectHighlights,
"refs": datum.collectReferences,
"rename": datum.collectRenames,
"prepare": datum.collectPrepareRenames,
"symbol": datum.collectSymbols,
"signature": datum.collectSignatures,
"link": datum.collectLinks,
"suggestedfix": datum.collectSuggestedFixes,
"extractfunc": datum.collectFunctionExtractions,
"extractmethod": datum.collectMethodExtractions,
"incomingcalls": datum.collectIncomingCalls,
"outgoingcalls": datum.collectOutgoingCalls,
"addimport": datum.collectAddImports,
}); err != nil {
t.Fatal(err)
}
for _, symbols := range datum.Symbols {
for i := range symbols {
children := datum.symbolsChildren[symbols[i].Name]
symbols[i].Children = children
}
}
// Collect names for the entries that require golden files.
if err := datum.Exported.Expect(map[string]interface{}{
"godef": datum.collectDefinitionNames,
"hoverdef": datum.collectDefinitionNames,
"workspacesymbol": datum.collectWorkspaceSymbols(WorkspaceSymbolsDefault),
"workspacesymbolfuzzy": datum.collectWorkspaceSymbols(WorkspaceSymbolsFuzzy),
"workspacesymbolcasesensitive": datum.collectWorkspaceSymbols(WorkspaceSymbolsCaseSensitive),
}); err != nil {
t.Fatal(err)
}
if mode == "MultiModule" {
if err := os.Rename(filepath.Join(datum.Config.Dir, "go.mod"), filepath.Join(datum.Config.Dir, "testmodule/go.mod")); err != nil {
t.Fatal(err)
}
}
return datum
}
func Run(t *testing.T, tests Tests, data *Data) {
t.Helper()
checkData(t, data)
eachCompletion := func(t *testing.T, cases map[span.Span][]Completion, test func(*testing.T, span.Span, Completion, CompletionItems)) {
t.Helper()
for src, exp := range cases {
for i, e := range exp {
t.Run(SpanName(src)+"_"+strconv.Itoa(i), func(t *testing.T) {
t.Helper()
if strings.Contains(t.Name(), "cgo") {
testenv.NeedsTool(t, "cgo")
}
if strings.Contains(t.Name(), "declarecgo") {
testenv.NeedsGo1Point(t, 15)
}
test(t, src, e, data.CompletionItems)
})
}
}
}
t.Run("CallHierarchy", func(t *testing.T) {
t.Helper()
for spn, callHierarchyResult := range data.CallHierarchy {
t.Run(SpanName(spn), func(t *testing.T) {
t.Helper()
tests.CallHierarchy(t, spn, callHierarchyResult)
})
}
})
t.Run("Completion", func(t *testing.T) {
t.Helper()
eachCompletion(t, data.Completions, tests.Completion)
})
t.Run("CompletionSnippets", func(t *testing.T) {
t.Helper()
for _, placeholders := range []bool{true, false} {
for src, expecteds := range data.CompletionSnippets {
for i, expected := range expecteds {
name := SpanName(src) + "_" + strconv.Itoa(i+1)
if placeholders {
name += "_placeholders"
}
t.Run(name, func(t *testing.T) {
t.Helper()
tests.CompletionSnippet(t, src, expected, placeholders, data.CompletionItems)
})
}
}
}
})
t.Run("UnimportedCompletion", func(t *testing.T) {
t.Helper()
eachCompletion(t, data.UnimportedCompletions, tests.UnimportedCompletion)
})
t.Run("DeepCompletion", func(t *testing.T) {
t.Helper()
eachCompletion(t, data.DeepCompletions, tests.DeepCompletion)
})
t.Run("FuzzyCompletion", func(t *testing.T) {
t.Helper()
eachCompletion(t, data.FuzzyCompletions, tests.FuzzyCompletion)
})
t.Run("CaseSensitiveCompletion", func(t *testing.T) {
t.Helper()
eachCompletion(t, data.CaseSensitiveCompletions, tests.CaseSensitiveCompletion)
})
t.Run("RankCompletions", func(t *testing.T) {
t.Helper()
eachCompletion(t, data.RankCompletions, tests.RankCompletion)
})
t.Run("CodeLens", func(t *testing.T) {
t.Helper()
for uri, want := range data.CodeLens {
// Check if we should skip this URI if the -modfile flag is not available.
if shouldSkip(data, uri) {
continue
}
t.Run(uriName(uri), func(t *testing.T) {
t.Helper()
tests.CodeLens(t, uri, want)
})
}
})
t.Run("Diagnostics", func(t *testing.T) {
t.Helper()
for uri, want := range data.Diagnostics {
// Check if we should skip this URI if the -modfile flag is not available.
if shouldSkip(data, uri) {
continue
}
t.Run(uriName(uri), func(t *testing.T) {
t.Helper()
tests.Diagnostics(t, uri, want)
})
}
})
t.Run("FoldingRange", func(t *testing.T) {
t.Helper()
for _, spn := range data.FoldingRanges {
t.Run(uriName(spn.URI()), func(t *testing.T) {
t.Helper()
tests.FoldingRanges(t, spn)
})
}
})
t.Run("Format", func(t *testing.T) {
t.Helper()
for _, spn := range data.Formats {
t.Run(uriName(spn.URI()), func(t *testing.T) {
t.Helper()
tests.Format(t, spn)
})
}
})
t.Run("Import", func(t *testing.T) {
t.Helper()
for _, spn := range data.Imports {
t.Run(uriName(spn.URI()), func(t *testing.T) {
t.Helper()
tests.Import(t, spn)
})
}
})
t.Run("SemanticTokens", func(t *testing.T) {
t.Helper()
for _, spn := range data.SemanticTokens {
t.Run(uriName(spn.URI()), func(t *testing.T) {
t.Helper()
tests.SemanticTokens(t, spn)
})
}
})
t.Run("SuggestedFix", func(t *testing.T) {
t.Helper()
for spn, actionKinds := range data.SuggestedFixes {
// Check if we should skip this spn if the -modfile flag is not available.
if shouldSkip(data, spn.URI()) {
continue
}
t.Run(SpanName(spn), func(t *testing.T) {
t.Helper()
tests.SuggestedFix(t, spn, actionKinds, 1)
})
}
})
t.Run("FunctionExtraction", func(t *testing.T) {
t.Helper()
for start, end := range data.FunctionExtractions {
// Check if we should skip this spn if the -modfile flag is not available.
if shouldSkip(data, start.URI()) {
continue
}
t.Run(SpanName(start), func(t *testing.T) {
t.Helper()
tests.FunctionExtraction(t, start, end)
})
}
})
t.Run("MethodExtraction", func(t *testing.T) {
t.Helper()
for start, end := range data.MethodExtractions {
// Check if we should skip this spn if the -modfile flag is not available.
if shouldSkip(data, start.URI()) {
continue
}
t.Run(SpanName(start), func(t *testing.T) {
t.Helper()
tests.MethodExtraction(t, start, end)
})
}
})
t.Run("Definition", func(t *testing.T) {
t.Helper()
for spn, d := range data.Definitions {
t.Run(SpanName(spn), func(t *testing.T) {
t.Helper()
if strings.Contains(t.Name(), "cgo") {
testenv.NeedsTool(t, "cgo")
}
if strings.Contains(t.Name(), "declarecgo") {
testenv.NeedsGo1Point(t, 15)
}
tests.Definition(t, spn, d)
})
}
})
t.Run("Implementation", func(t *testing.T) {
t.Helper()
for spn, m := range data.Implementations {
t.Run(SpanName(spn), func(t *testing.T) {
t.Helper()
tests.Implementation(t, spn, m)
})
}
})
t.Run("Highlight", func(t *testing.T) {
t.Helper()
for pos, locations := range data.Highlights {
t.Run(SpanName(pos), func(t *testing.T) {
t.Helper()
tests.Highlight(t, pos, locations)
})
}
})
t.Run("Hover", func(t *testing.T) {
t.Helper()
for pos, info := range data.Hovers {
t.Run(SpanName(pos), func(t *testing.T) {
t.Helper()
tests.Hover(t, pos, info)
})
}
})
t.Run("References", func(t *testing.T) {
t.Helper()
for src, itemList := range data.References {
t.Run(SpanName(src), func(t *testing.T) {
t.Helper()
tests.References(t, src, itemList)
})
}
})
t.Run("Renames", func(t *testing.T) {
t.Helper()
for spn, newText := range data.Renames {
t.Run(uriName(spn.URI())+"_"+newText, func(t *testing.T) {
t.Helper()
tests.Rename(t, spn, newText)
})
}
})
t.Run("PrepareRenames", func(t *testing.T) {
t.Helper()
for src, want := range data.PrepareRenames {
t.Run(SpanName(src), func(t *testing.T) {
t.Helper()
tests.PrepareRename(t, src, want)
})
}
})
t.Run("Symbols", func(t *testing.T) {
t.Helper()
for uri, expectedSymbols := range data.Symbols {
t.Run(uriName(uri), func(t *testing.T) {
t.Helper()
tests.Symbols(t, uri, expectedSymbols)
})
}
})
t.Run("WorkspaceSymbols", func(t *testing.T) {
t.Helper()
for _, typ := range []WorkspaceSymbolsTestType{
WorkspaceSymbolsDefault,
WorkspaceSymbolsCaseSensitive,
WorkspaceSymbolsFuzzy,
} {
for uri, cases := range data.WorkspaceSymbols[typ] {
for _, query := range cases {
name := query
if name == "" {
name = "EmptyQuery"
}
t.Run(name, func(t *testing.T) {
t.Helper()
tests.WorkspaceSymbols(t, uri, query, typ)
})
}
}
}
})
t.Run("SignatureHelp", func(t *testing.T) {
t.Helper()
for spn, expectedSignature := range data.Signatures {
t.Run(SpanName(spn), func(t *testing.T) {
t.Helper()
tests.SignatureHelp(t, spn, expectedSignature)
})
}
})
t.Run("Link", func(t *testing.T) {
t.Helper()
for uri, wantLinks := range data.Links {
// If we are testing GOPATH, then we do not want links with the versions
// attached (pkg.go.dev/repoa/moda@v1.1.0/pkg), unless the file is a
// go.mod, then we can skip it altogether.
if data.Exported.Exporter == packagestest.GOPATH {
if strings.HasSuffix(uri.Filename(), ".mod") {
continue
}
re := regexp.MustCompile(`@v\d+\.\d+\.[\w-]+`)
for i, link := range wantLinks {
wantLinks[i].Target = re.ReplaceAllString(link.Target, "")
}
}
t.Run(uriName(uri), func(t *testing.T) {
t.Helper()
tests.Link(t, uri, wantLinks)
})
}
})
t.Run("AddImport", func(t *testing.T) {
t.Helper()
for uri, exp := range data.AddImport {
t.Run(uriName(uri), func(t *testing.T) {
tests.AddImport(t, uri, exp)
})
}
})
if *UpdateGolden {
for _, golden := range data.golden {
if !golden.Modified {
continue
}
sort.Slice(golden.Archive.Files, func(i, j int) bool {
return golden.Archive.Files[i].Name < golden.Archive.Files[j].Name
})
if err := ioutil.WriteFile(golden.Filename, txtar.Format(golden.Archive), 0666); err != nil {
t.Fatal(err)
}
}
}
}
func checkData(t *testing.T, data *Data) {
buf := &bytes.Buffer{}
diagnosticsCount := 0
for _, want := range data.Diagnostics {
diagnosticsCount += len(want)
}
linksCount := 0
for _, want := range data.Links {
linksCount += len(want)
}
definitionCount := 0
typeDefinitionCount := 0
for _, d := range data.Definitions {
if d.IsType {
typeDefinitionCount++
} else {
definitionCount++
}
}
snippetCount := 0
for _, want := range data.CompletionSnippets {
snippetCount += len(want)
}
countCompletions := func(c map[span.Span][]Completion) (count int) {
for _, want := range c {
count += len(want)
}
return count
}
countCodeLens := func(c map[span.URI][]protocol.CodeLens) (count int) {
for _, want := range c {
count += len(want)
}
return count
}
countWorkspaceSymbols := func(c map[WorkspaceSymbolsTestType]map[span.URI][]string) (count int) {
for _, typs := range c {
for _, queries := range typs {
count += len(queries)
}
}
return count
}
fmt.Fprintf(buf, "CallHierarchyCount = %v\n", len(data.CallHierarchy))
fmt.Fprintf(buf, "CodeLensCount = %v\n", countCodeLens(data.CodeLens))
fmt.Fprintf(buf, "CompletionsCount = %v\n", countCompletions(data.Completions))
fmt.Fprintf(buf, "CompletionSnippetCount = %v\n", snippetCount)
fmt.Fprintf(buf, "UnimportedCompletionsCount = %v\n", countCompletions(data.UnimportedCompletions))
fmt.Fprintf(buf, "DeepCompletionsCount = %v\n", countCompletions(data.DeepCompletions))
fmt.Fprintf(buf, "FuzzyCompletionsCount = %v\n", countCompletions(data.FuzzyCompletions))
fmt.Fprintf(buf, "RankedCompletionsCount = %v\n", countCompletions(data.RankCompletions))
fmt.Fprintf(buf, "CaseSensitiveCompletionsCount = %v\n", countCompletions(data.CaseSensitiveCompletions))
fmt.Fprintf(buf, "DiagnosticsCount = %v\n", diagnosticsCount)
fmt.Fprintf(buf, "FoldingRangesCount = %v\n", len(data.FoldingRanges))
fmt.Fprintf(buf, "FormatCount = %v\n", len(data.Formats))
fmt.Fprintf(buf, "ImportCount = %v\n", len(data.Imports))
fmt.Fprintf(buf, "SemanticTokenCount = %v\n", len(data.SemanticTokens))
fmt.Fprintf(buf, "SuggestedFixCount = %v\n", len(data.SuggestedFixes))
fmt.Fprintf(buf, "FunctionExtractionCount = %v\n", len(data.FunctionExtractions))
fmt.Fprintf(buf, "MethodExtractionCount = %v\n", len(data.MethodExtractions))
fmt.Fprintf(buf, "DefinitionsCount = %v\n", definitionCount)
fmt.Fprintf(buf, "TypeDefinitionsCount = %v\n", typeDefinitionCount)
fmt.Fprintf(buf, "HighlightsCount = %v\n", len(data.Highlights))
fmt.Fprintf(buf, "ReferencesCount = %v\n", len(data.References))
fmt.Fprintf(buf, "RenamesCount = %v\n", len(data.Renames))
fmt.Fprintf(buf, "PrepareRenamesCount = %v\n", len(data.PrepareRenames))
fmt.Fprintf(buf, "SymbolsCount = %v\n", len(data.Symbols))
fmt.Fprintf(buf, "WorkspaceSymbolsCount = %v\n", countWorkspaceSymbols(data.WorkspaceSymbols))
fmt.Fprintf(buf, "SignaturesCount = %v\n", len(data.Signatures))
fmt.Fprintf(buf, "LinksCount = %v\n", linksCount)
fmt.Fprintf(buf, "ImplementationsCount = %v\n", len(data.Implementations))
want := string(data.Golden("summary", summaryFile, func() ([]byte, error) {
return buf.Bytes(), nil
}))
got := buf.String()
if want != got {
t.Errorf("test summary does not match:\n%s", Diff(t, want, got))
}
}
func (data *Data) Mapper(uri span.URI) (*protocol.ColumnMapper, error) {
data.mappersMu.Lock()
defer data.mappersMu.Unlock()
if _, ok := data.mappers[uri]; !ok {
content, err := data.Exported.FileContents(uri.Filename())
if err != nil {
return nil, err
}
converter := span.NewContentConverter(uri.Filename(), content)
data.mappers[uri] = &protocol.ColumnMapper{
URI: uri,
Converter: converter,
Content: content,
}
}
return data.mappers[uri], nil
}
func (data *Data) Golden(tag string, target string, update func() ([]byte, error)) []byte {
data.t.Helper()
fragment, found := data.fragments[target]
if !found {
if filepath.IsAbs(target) {
data.t.Fatalf("invalid golden file fragment %v", target)
}
fragment = target
}
golden := data.golden[fragment]
if golden == nil {
if !*UpdateGolden {
data.t.Fatalf("could not find golden file %v: %v", fragment, tag)
}
golden = &Golden{
Filename: filepath.Join(data.dir, fragment+goldenFileSuffix),
Archive: &txtar.Archive{},
Modified: true,
}
data.golden[fragment] = golden
}
var file *txtar.File
for i := range golden.Archive.Files {
f := &golden.Archive.Files[i]
if f.Name == tag {
file = f
break
}