-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathaddtest.go
796 lines (722 loc) · 23.4 KB
/
addtest.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
// 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 golang
// This file defines the behavior of the "Add test for FUNC" command.
import (
"bytes"
"context"
"errors"
"fmt"
"go/ast"
"go/format"
"go/token"
"go/types"
"os"
"path/filepath"
"strconv"
"strings"
"text/template"
"unicode"
"golang.org/x/tools/go/ast/astutil"
"golang.org/x/tools/gopls/internal/cache"
"golang.org/x/tools/gopls/internal/cache/metadata"
"golang.org/x/tools/gopls/internal/cache/parsego"
"golang.org/x/tools/gopls/internal/protocol"
goplsastutil "golang.org/x/tools/gopls/internal/util/astutil"
"golang.org/x/tools/gopls/internal/util/moremaps"
"golang.org/x/tools/internal/imports"
"golang.org/x/tools/internal/typesinternal"
)
const testTmplString = `
func {{.TestFuncName}}(t *{{.TestingPackageName}}.T) {
{{- /* Test cases struct declaration and empty initialization. */}}
tests := []struct {
name string // description of this test case
{{- $commentPrinted := false }}
{{- if and .Receiver .Receiver.Constructor}}
{{- range .Receiver.Constructor.Args}}
{{- if .Name}}
{{- if not $commentPrinted}}
// Named input parameters for receiver constructor.
{{- $commentPrinted = true }}
{{- end}}
{{.Name}} {{.Type}}
{{- end}}
{{- end}}
{{- end}}
{{- $commentPrinted := false }}
{{- range .Func.Args}}
{{- if .Name}}
{{- if not $commentPrinted}}
// Named input parameters for target function.
{{- $commentPrinted = true }}
{{- end}}
{{.Name}} {{.Type}}
{{- end}}
{{- end}}
{{- range $index, $res := .Func.Results}}
{{- if eq $res.Name "gotErr"}}
wantErr bool
{{- else if eq $index 0}}
want {{$res.Type}}
{{- else}}
want{{add $index 1}} {{$res.Type}}
{{- end}}
{{- end}}
}{
// TODO: Add test cases.
}
{{- /* Loop over all the test cases. */}}
for _, tt := range tests {
t.Run(tt.name, func(t *{{.TestingPackageName}}.T) {
{{- /* Constructor or empty initialization. */}}
{{- if .Receiver}}
{{- if .Receiver.Constructor}}
{{- /* Receiver variable by calling constructor. */}}
{{fieldNames .Receiver.Constructor.Results ""}} := {{if .PackageName}}{{.PackageName}}.{{end}}
{{- .Receiver.Constructor.Name}}
{{- /* Constructor input parameters. */ -}}
(
{{- range $index, $arg := .Receiver.Constructor.Args}}
{{- if ne $index 0}}, {{end}}
{{- if .Name}}tt.{{.Name}}{{else}}{{.Value}}{{end}}
{{- end -}}
)
{{- /* Handles the error return from constructor. */}}
{{- $last := last .Receiver.Constructor.Results}}
{{- if eq $last.Type "error"}}
if err != nil {
t.Fatalf("could not construct receiver type: %v", err)
}
{{- end}}
{{- else}}
{{- /* Receiver variable declaration. */}}
// TODO: construct the receiver type.
var {{.Receiver.Var.Name}} {{.Receiver.Var.Type}}
{{- end}}
{{- end}}
{{- /* Got variables. */}}
{{if .Func.Results}}{{fieldNames .Func.Results ""}} := {{end}}
{{- /* Call expression. */}}
{{- if .Receiver}}{{/* Call method by VAR.METHOD. */}}
{{- .Receiver.Var.Name}}.
{{- else if .PackageName}}{{/* Call function by PACKAGE.FUNC. */}}
{{- .PackageName}}.
{{- end}}{{.Func.Name}}
{{- /* Input parameters. */ -}}
(
{{- range $index, $arg := .Func.Args}}
{{- if ne $index 0}}, {{end}}
{{- if .Name}}tt.{{.Name}}{{else}}{{.Value}}{{end}}
{{- end -}}
)
{{- /* Handles the returned error before the rest of return value. */}}
{{- $last := last .Func.Results}}
{{- if eq $last.Type "error"}}
if gotErr != nil {
if !tt.wantErr {
t.Errorf("{{$.Func.Name}}() failed: %v", gotErr)
}
return
}
if tt.wantErr {
t.Fatal("{{$.Func.Name}}() succeeded unexpectedly")
}
{{- end}}
{{- /* Compare the returned values except for the last returned error. */}}
{{- if or (and .Func.Results (ne $last.Type "error")) (and (gt (len .Func.Results) 1) (eq $last.Type "error"))}}
// TODO: update the condition below to compare got with tt.want.
{{- range $index, $res := .Func.Results}}
{{- if ne $res.Name "gotErr"}}
if true {
t.Errorf("{{$.Func.Name}}() = %v, want %v", {{.Name}}, tt.{{if eq $index 0}}want{{else}}want{{add $index 1}}{{end}})
}
{{- end}}
{{- end}}
{{- end}}
})
}
}
`
// Name is the name of the field this input parameter should reference.
// Value is the expression this input parameter should accept.
//
// Exactly one of Name or Value must be set.
type field struct {
Name, Type, Value string
}
type function struct {
Name string
Args []field
Results []field
}
type receiver struct {
// Var is the name and type of the receiver variable.
Var field
// Constructor holds information about the constructor for the receiver type.
// If no qualified constructor is found, this field will be nil.
Constructor *function
}
type testInfo struct {
// TestingPackageName is the package name should be used when referencing
// package "testing"
TestingPackageName string
// PackageName is the package name the target function/method is delcared from.
PackageName string
TestFuncName string
// Func holds information about the function or method being tested.
Func function
// Receiver holds information about the receiver of the function or method
// being tested.
// This field is nil for functions and non-nil for methods.
Receiver *receiver
}
var testTmpl = template.Must(template.New("test").Funcs(template.FuncMap{
"add": func(a, b int) int { return a + b },
"last": func(slice []field) field {
if len(slice) == 0 {
return field{}
}
return slice[len(slice)-1]
},
"fieldNames": func(fields []field, qualifier string) (res string) {
var names []string
for _, f := range fields {
names = append(names, qualifier+f.Name)
}
return strings.Join(names, ", ")
},
}).Parse(testTmplString))
// AddTestForFunc adds a test for the function enclosing the given input range.
// It creates a _test.go file if one does not already exist.
func AddTestForFunc(ctx context.Context, snapshot *cache.Snapshot, loc protocol.Location) (changes []protocol.DocumentChange, _ error) {
pkg, pgf, err := NarrowestPackageForFile(ctx, snapshot, loc.URI)
if err != nil {
return nil, err
}
if metadata.IsCommandLineArguments(pkg.Metadata().ID) {
return nil, fmt.Errorf("current file in command-line-arguments package")
}
if errors := pkg.ParseErrors(); len(errors) > 0 {
return nil, fmt.Errorf("package has parse errors: %v", errors[0])
}
if errors := pkg.TypeErrors(); len(errors) > 0 {
return nil, fmt.Errorf("package has type errors: %v", errors[0])
}
// All three maps map the path of an imported package to
// the local name if explicit or "" otherwise.
var (
fileImports map[string]string // imports in foo.go file
testImports map[string]string // imports in foo_test.go file
extraImports = make(map[string]string) // imports to add to test file
)
var collectImports = func(file *ast.File) (map[string]string, error) {
imps := make(map[string]string)
for _, spec := range file.Imports {
// TODO(hxjiang): support dot imports.
if spec.Name != nil && spec.Name.Name == "." {
return nil, fmt.Errorf("\"add test for func\" does not support files containing dot imports")
}
path, err := strconv.Unquote(spec.Path.Value)
if err != nil {
return nil, err
}
if spec.Name != nil {
if spec.Name.Name == "_" {
continue
}
imps[path] = spec.Name.Name
} else {
imps[path] = ""
}
}
return imps, nil
}
// Collect all the imports from the x.go, keep track of the local package name.
if fileImports, err = collectImports(pgf.File); err != nil {
return nil, err
}
testBase := strings.TrimSuffix(filepath.Base(loc.URI.Path()), ".go") + "_test.go"
goTestFileURI := protocol.URIFromPath(filepath.Join(loc.URI.DirPath(), testBase))
testFH, err := snapshot.ReadFile(ctx, goTestFileURI)
if err != nil {
return nil, err
}
// TODO(hxjiang): use a fresh name if the same test function name already
// exist.
var (
eofRange protocol.Range // empty selection at end of new file
// edits contains all the text edits to be applied to the test file.
edits []protocol.TextEdit
// xtest indicates whether the test file use package x or x_test.
// TODO(hxjiang): We can discuss the option to interpret the user's
// intention by which function they are selecting. Have one file for
// x_test package testing, one file for x package testing.
xtest = true
)
start, end, err := pgf.RangePos(loc.Range)
if err != nil {
return nil, err
}
path, _ := astutil.PathEnclosingInterval(pgf.File, start, end)
if len(path) < 2 {
return nil, fmt.Errorf("no enclosing function")
}
decl, ok := path[len(path)-2].(*ast.FuncDecl)
if !ok {
return nil, fmt.Errorf("no enclosing function")
}
fn := pkg.TypesInfo().Defs[decl.Name].(*types.Func)
sig := fn.Signature()
testPGF, err := snapshot.ParseGo(ctx, testFH, parsego.Header)
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
return nil, err
}
changes = append(changes, protocol.DocumentChangeCreate(goTestFileURI))
// header is the buffer containing the text to add to the beginning of the file.
var header bytes.Buffer
// If this test file was created by the gopls, add a copyright header and
// package decl based on the originating file.
// Search for something that looks like a copyright header, to replicate
// in the new file.
if c := CopyrightComment(pgf.File); c != nil {
start, end, err := pgf.NodeOffsets(c)
if err != nil {
return nil, err
}
header.Write(pgf.Src[start:end])
// One empty line between copyright header and following.
header.WriteString("\n\n")
}
// If this test file was created by gopls, add build constraints
// matching the non-test file.
if c := buildConstraintComment(pgf.File); c != nil {
start, end, err := pgf.NodeOffsets(c)
if err != nil {
return nil, err
}
header.Write(pgf.Src[start:end])
// One empty line between build constraint and following.
header.WriteString("\n\n")
}
// Determine if a new test file should use in-package test (package x)
// or external test (package x_test). If any of the function parameters
// reference an unexported object, we cannot write out test cases from
// an x_test package.
externalTestOK := func() bool {
if !fn.Exported() {
return false
}
if fn.Signature().Recv() != nil {
if _, ident, _ := goplsastutil.UnpackRecv(decl.Recv.List[0].Type); ident == nil || !ident.IsExported() {
return false
}
}
refsUnexported := false
ast.Inspect(decl, func(n ast.Node) bool {
// The original function refs to an unexported object from the
// same package, so further inspection is unnecessary.
if refsUnexported {
return false
}
switch t := n.(type) {
case *ast.BlockStmt:
// Avoid inspect the function body.
return false
case *ast.Ident:
// Use test variant (package foo) if the function signature
// references any unexported objects (like types or
// constants) from the same package.
// Note: types.PkgName is excluded from this check as it's
// always defined in the same package.
if obj, ok := pkg.TypesInfo().Uses[t]; ok && !obj.Exported() && obj.Pkg() == pkg.Types() && !is[*types.PkgName](obj) {
refsUnexported = true
}
return false
default:
return true
}
})
return !refsUnexported
}
xtest = externalTestOK()
if xtest {
fmt.Fprintf(&header, "package %s_test\n", pkg.Types().Name())
} else {
fmt.Fprintf(&header, "package %s\n", pkg.Types().Name())
}
// Write the copyright and package decl to the beginning of the file.
edits = append(edits, protocol.TextEdit{
Range: protocol.Range{},
NewText: header.String(),
})
} else { // existing _test.go file.
if testPGF.File.Name == nil || testPGF.File.Name.NamePos == token.NoPos {
return nil, fmt.Errorf("missing package declaration")
}
switch testPGF.File.Name.Name {
case pgf.File.Name.Name:
xtest = false
case pgf.File.Name.Name + "_test":
xtest = true
default:
return nil, fmt.Errorf("invalid package declaration %q in test file %q", testPGF.File.Name, testPGF)
}
eofRange, err = testPGF.PosRange(testPGF.File.FileEnd, testPGF.File.FileEnd)
if err != nil {
return nil, err
}
// Collect all the imports from the foo_test.go.
if testImports, err = collectImports(testPGF.File); err != nil {
return nil, err
}
}
// qual qualifier determines the correct package name to use for a type in
// foo_test.go. It does this by:
// - Consult imports map from test file foo_test.go.
// - If not found, consult imports map from original file foo.go.
// If the package is not imported in test file foo_test.go, it is added to
// extraImports map.
qual := func(p *types.Package) string {
// References from an in-package test should not be qualified.
if !xtest && p == pkg.Types() {
return ""
}
// Prefer using the package name if already defined in foo_test.go
if local, ok := testImports[p.Path()]; ok {
if local != "" {
return local
} else {
return p.Name()
}
}
// TODO(hxjiang): we should consult the scope of the test package to
// ensure these new imports do not shadow any package-level names.
// Prefer the local import name (if any) used in the package under test.
if local, ok := fileImports[p.Path()]; ok && local != "" {
extraImports[p.Path()] = local
return local
}
// Fall back to the package name since there is no renaming.
extraImports[p.Path()] = ""
return p.Name()
}
if xtest {
// Reject if function/method is unexported.
if !fn.Exported() {
return nil, fmt.Errorf("cannot add test of unexported function %s to external test package %s_test", decl.Name, pgf.File.Name)
}
// Reject if receiver is unexported.
if sig.Recv() != nil {
if _, ident, _ := goplsastutil.UnpackRecv(decl.Recv.List[0].Type); ident == nil || !ident.IsExported() {
return nil, fmt.Errorf("cannot add external test for method %s.%s as receiver type is not exported", ident.Name, decl.Name)
}
}
// TODO(hxjiang): reject if the any input parameter type is unexported.
// TODO(hxjiang): reject if any return value type is unexported. Explore
// the option to drop the return value if the type is unexported.
}
testName, err := testName(fn)
if err != nil {
return nil, err
}
data := testInfo{
TestingPackageName: qual(types.NewPackage("testing", "testing")),
PackageName: qual(pkg.Types()),
TestFuncName: testName,
Func: function{
Name: fn.Name(),
},
}
var isContextType = func(t types.Type) bool {
named, ok := t.(*types.Named)
if !ok {
return false
}
return named.Obj().Pkg().Path() == "context" && named.Obj().Name() == "Context"
}
for i := range sig.Params().Len() {
param := sig.Params().At(i)
name, typ := param.Name(), param.Type()
f := field{Type: types.TypeString(typ, qual)}
if i == 0 && isContextType(typ) {
f.Value = qual(types.NewPackage("context", "context")) + ".Background()"
} else if name == "" || name == "_" {
f.Value, _ = typesinternal.ZeroString(typ, qual)
} else {
f.Name = name
}
data.Func.Args = append(data.Func.Args, f)
}
for i := range sig.Results().Len() {
typ := sig.Results().At(i).Type()
var name string
if i == sig.Results().Len()-1 && types.Identical(typ, errorType) {
name = "gotErr"
} else if i == 0 {
name = "got"
} else {
name = fmt.Sprintf("got%d", i+1)
}
data.Func.Results = append(data.Func.Results, field{
Name: name,
Type: types.TypeString(typ, qual),
})
}
if sig.Recv() != nil {
// Find the preferred type for the receiver. We don't use
// typesinternal.ReceiverNamed here as we want to preserve aliases.
recvType := sig.Recv().Type()
if ptr, ok := recvType.(*types.Pointer); ok {
recvType = ptr.Elem()
}
t, ok := recvType.(typesinternal.NamedOrAlias)
if !ok {
return nil, fmt.Errorf("the receiver type is neither named type nor alias type")
}
var varName string
{
var possibleNames []string // list of candidates, preferring earlier entries.
if len(sig.Recv().Name()) > 0 {
possibleNames = append(possibleNames,
sig.Recv().Name(), // receiver name.
string(sig.Recv().Name()[0]), // first character of receiver name.
)
}
possibleNames = append(possibleNames,
string(t.Obj().Name()[0]), // first character of receiver type name.
)
if len(t.Obj().Name()) >= 2 {
possibleNames = append(possibleNames,
string(t.Obj().Name()[:2]), // first two character of receiver type name.
)
}
var camelCase []rune
for i, s := range t.Obj().Name() {
if i == 0 || unicode.IsUpper(s) {
camelCase = append(camelCase, s)
}
}
possibleNames = append(possibleNames,
string(camelCase), // captalized initials.
)
for _, name := range possibleNames {
name = strings.ToLower(name)
if name == "" || name == "t" || name == "tt" {
continue
}
varName = name
break
}
if varName == "" {
varName = "r" // default as "r" for "receiver".
}
}
data.Receiver = &receiver{
Var: field{
Name: varName,
Type: types.TypeString(recvType, qual),
},
}
// constructor is the selected constructor for type T.
var constructor *types.Func
// When finding the qualified constructor, the function should return the
// any type whose named type is the same type as T's named type.
_, wantType := typesinternal.ReceiverNamed(sig.Recv())
for _, name := range pkg.Types().Scope().Names() {
f, ok := pkg.Types().Scope().Lookup(name).(*types.Func)
if !ok {
continue
}
if f.Signature().Recv() != nil {
continue
}
// Unexported constructor is not visible in x_test package.
if xtest && !f.Exported() {
continue
}
// Only allow constructors returning T, T, (T, error), or (T, error).
if f.Signature().Results().Len() > 2 || f.Signature().Results().Len() == 0 {
continue
}
_, gotType := typesinternal.ReceiverNamed(f.Signature().Results().At(0))
if gotType == nil || !types.Identical(gotType, wantType) {
continue
}
if f.Signature().Results().Len() == 2 && !types.Identical(f.Signature().Results().At(1).Type(), errorType) {
continue
}
if constructor == nil {
constructor = f
}
// Functions named NewType are prioritized as constructors over other
// functions that match only the signature criteria.
if strings.EqualFold(strings.ToLower(f.Name()), strings.ToLower("new"+t.Obj().Name())) {
constructor = f
}
}
if constructor != nil {
data.Receiver.Constructor = &function{Name: constructor.Name()}
for i := range constructor.Signature().Params().Len() {
param := constructor.Signature().Params().At(i)
name, typ := param.Name(), param.Type()
f := field{Type: types.TypeString(typ, qual)}
if i == 0 && isContextType(typ) {
f.Value = qual(types.NewPackage("context", "context")) + ".Background()"
} else if name == "" || name == "_" {
f.Value, _ = typesinternal.ZeroString(typ, qual)
} else {
f.Name = name
}
data.Receiver.Constructor.Args = append(data.Receiver.Constructor.Args, f)
}
for i := range constructor.Signature().Results().Len() {
typ := constructor.Signature().Results().At(i).Type()
var name string
if i == 0 {
// The first return value must be of type T, *T, or a type whose named
// type is the same as named type of T.
name = varName
} else if i == constructor.Signature().Results().Len()-1 && types.Identical(typ, errorType) {
name = "err"
} else {
// Drop any return values beyond the first and the last.
// e.g., "f, _, _, err := NewFoo()".
name = "_"
}
data.Receiver.Constructor.Results = append(data.Receiver.Constructor.Results, field{
Name: name,
Type: types.TypeString(typ, qual),
})
}
}
}
// Resolves duplicate parameter names between the function and its
// receiver's constructor. It adds prefix to the constructor's parameters
// until no conflicts remain.
if data.Receiver != nil && data.Receiver.Constructor != nil {
seen := map[string]bool{}
for _, f := range data.Func.Args {
if f.Name == "" {
continue
}
seen[f.Name] = true
}
// "" for no change, "c" for constructor, "i" for input.
for _, prefix := range []string{"", "c", "c_", "i", "i_"} {
conflict := false
for _, f := range data.Receiver.Constructor.Args {
if f.Name == "" {
continue
}
if seen[prefix+f.Name] {
conflict = true
break
}
}
if !conflict {
for i, f := range data.Receiver.Constructor.Args {
if f.Name == "" {
continue
}
data.Receiver.Constructor.Args[i].Name = prefix + data.Receiver.Constructor.Args[i].Name
}
break
}
}
}
// Compute edits to update imports.
//
// If we're adding to an existing test file, we need to adjust existing
// imports. Otherwise, we can simply write out the imports to the new file.
if testPGF != nil {
var importFixes []*imports.ImportFix
for path, name := range extraImports {
importFixes = append(importFixes, &imports.ImportFix{
StmtInfo: imports.ImportInfo{
ImportPath: path,
Name: name,
},
FixType: imports.AddImport,
})
}
importEdits, err := ComputeImportFixEdits(snapshot.Options().Local, testPGF.Src, importFixes...)
if err != nil {
return nil, fmt.Errorf("could not compute the import fix edits: %w", err)
}
edits = append(edits, importEdits...)
} else {
var importsBuffer bytes.Buffer
if len(extraImports) == 1 {
importsBuffer.WriteString("\nimport ")
for path, name := range extraImports {
if name != "" {
importsBuffer.WriteString(name + " ")
}
importsBuffer.WriteString(fmt.Sprintf("\"%s\"\n", path))
}
} else {
importsBuffer.WriteString("\nimport(")
// Sort for determinism.
for path, name := range moremaps.Sorted(extraImports) {
importsBuffer.WriteString("\n\t")
if name != "" {
importsBuffer.WriteString(name + " ")
}
importsBuffer.WriteString(fmt.Sprintf("\"%s\"", path))
}
importsBuffer.WriteString("\n)\n")
}
edits = append(edits, protocol.TextEdit{
Range: protocol.Range{},
NewText: importsBuffer.String(),
})
}
var test bytes.Buffer
if err := testTmpl.Execute(&test, data); err != nil {
return nil, err
}
formatted, err := format.Source(test.Bytes())
if err != nil {
return nil, err
}
edits = append(edits,
protocol.TextEdit{
Range: eofRange,
NewText: string(formatted),
})
return append(changes, protocol.DocumentChangeEdit(testFH, edits)), nil
}
// testName returns the name of the function to use for the new function that
// tests fn.
// Returns empty string if the fn is ill typed or nil.
func testName(fn *types.Func) (string, error) {
if fn == nil {
return "", fmt.Errorf("input nil function")
}
testName := "Test"
if recv := fn.Signature().Recv(); recv != nil { // method declaration.
// Retrieve the unpointered receiver type to ensure the test name is based
// on the topmost alias or named type, not the alias' RHS type (potentially
// unexported) type.
// For example:
// type Foo = foo // Foo is an exported alias for the unexported type foo
recvType := recv.Type()
if ptr, ok := recv.Type().(*types.Pointer); ok {
recvType = ptr.Elem()
}
t, ok := recvType.(typesinternal.NamedOrAlias)
if !ok {
return "", fmt.Errorf("receiver type is not named type or alias type")
}
if !t.Obj().Exported() {
testName += "_"
}
testName += t.Obj().Name() + "_"
} else if !fn.Exported() { // unexported function declaration.
testName += "_"
}
return testName + fn.Name(), nil
}