@@ -12,6 +12,7 @@ import (
12
12
"errors"
13
13
"fmt"
14
14
"go/ast"
15
+ "go/format"
15
16
"go/token"
16
17
"go/types"
17
18
"os"
@@ -34,124 +35,124 @@ import (
34
35
35
36
const testTmplString = `
36
37
func {{.TestFuncName}}(t *{{.TestingPackageName}}.T) {
37
- {{- /* Test cases struct declaration and empty initialization. */}}
38
- tests := []struct {
39
- name string // description of this test case
40
-
41
- {{- $commentPrinted := false }}
42
- {{- if and .Receiver .Receiver.Constructor}}
43
- {{- range .Receiver.Constructor.Args}}
44
- {{- if .Name}}
45
- {{- if not $commentPrinted}}
46
- // Named input parameters for receiver constructor.
47
- {{- $commentPrinted = true }}
48
- {{- end}}
49
- {{.Name}} {{.Type}}
50
- {{- end}}
51
- {{- end}}
52
- {{- end}}
53
-
54
- {{- $commentPrinted := false }}
55
- {{- range .Func.Args}}
56
- {{- if .Name}}
57
- {{- if not $commentPrinted}}
58
- // Named input parameters for target function.
59
- {{- $commentPrinted = true }}
60
- {{- end}}
61
- {{.Name}} {{.Type}}
62
- {{- end}}
63
- {{- end}}
64
-
65
- {{- range $index, $res := .Func.Results}}
66
- {{- if eq $res.Name "gotErr"}}
67
- wantErr bool
68
- {{- else if eq $index 0}}
69
- want {{$res.Type}}
70
- {{- else}}
71
- want{{add $index 1}} {{$res.Type}}
72
- {{- end}}
73
- {{- end}}
74
- }{
75
- // TODO: Add test cases.
76
- }
77
-
78
- {{- /* Loop over all the test cases. */}}
79
- for _, tt := range tests {
80
- t.Run(tt.name, func(t *{{.TestingPackageName}}.T) {
81
- {{- /* Constructor or empty initialization. */}}
82
- {{- if .Receiver}}
83
- {{- if .Receiver.Constructor}}
84
- {{- /* Receiver variable by calling constructor. */}}
85
- {{fieldNames .Receiver.Constructor.Results ""}} := {{if .PackageName}}{{.PackageName}}.{{end}}
86
- {{- .Receiver.Constructor.Name}}
87
-
88
- {{- /* Constructor input parameters. */ -}}
89
- (
90
- {{- range $index, $arg := .Receiver.Constructor.Args}}
91
- {{- if ne $index 0}}, {{end}}
92
- {{- if .Name}}tt.{{.Name}}{{else}}{{.Value}}{{end}}
93
- {{- end -}}
94
- )
95
-
96
- {{- /* Handles the error return from constructor. */}}
97
- {{- $last := last .Receiver.Constructor.Results}}
98
- {{- if eq $last.Type "error"}}
99
- if err != nil {
100
- t.Fatalf("could not construct receiver type: %v", err)
101
- }
102
- {{- end}}
103
- {{- else}}
104
- {{- /* Receiver variable declaration. */}}
105
- // TODO: construct the receiver type.
106
- var {{.Receiver.Var.Name}} {{.Receiver.Var.Type}}
107
- {{- end}}
108
- {{- end}}
109
-
110
- {{- /* Got variables. */}}
111
- {{if .Func.Results}}{{fieldNames .Func.Results ""}} := {{end}}
112
-
113
- {{- /* Call expression. */}}
114
- {{- if .Receiver}}{{/* Call method by VAR.METHOD. */}}
115
- {{- .Receiver.Var.Name}}.
116
- {{- else if .PackageName}}{{/* Call function by PACKAGE.FUNC. */}}
117
- {{- .PackageName}}.
118
- {{- end}}{{.Func.Name}}
119
-
120
- {{- /* Input parameters. */ -}}
121
- (
122
- {{- range $index, $arg := .Func.Args}}
123
- {{- if ne $index 0}}, {{end}}
124
- {{- if .Name}}tt.{{.Name}}{{else}}{{.Value}}{{end}}
125
- {{- end -}}
126
- )
127
-
128
- {{- /* Handles the returned error before the rest of return value. */}}
129
- {{- $last := last .Func.Results}}
130
- {{- if eq $last.Type "error"}}
131
- if gotErr != nil {
132
- if !tt.wantErr {
133
- t.Errorf("{{$.Func.Name}}() failed: %v", gotErr)
134
- }
135
- return
136
- }
137
- if tt.wantErr {
138
- t.Fatal("{{$.Func.Name}}() succeeded unexpectedly")
139
- }
140
- {{- end}}
141
-
142
- {{- /* Compare the returned values except for the last returned error. */}}
143
- {{- if or (and .Func.Results (ne $last.Type "error")) (and (gt (len .Func.Results) 1) (eq $last.Type "error"))}}
144
- // TODO: update the condition below to compare got with tt.want.
145
- {{- range $index, $res := .Func.Results}}
146
- {{- if ne $res.Name "gotErr"}}
147
- if true {
148
- t.Errorf("{{$.Func.Name}}() = %v, want %v", {{.Name}}, tt.{{if eq $index 0}}want{{else}}want{{add $index 1}}{{end}})
149
- }
150
- {{- end}}
151
- {{- end}}
152
- {{- end}}
153
- })
154
- }
38
+ {{- /* Test cases struct declaration and empty initialization. */}}
39
+ tests := []struct {
40
+ name string // description of this test case
41
+
42
+ {{- $commentPrinted := false }}
43
+ {{- if and .Receiver .Receiver.Constructor}}
44
+ {{- range .Receiver.Constructor.Args}}
45
+ {{- if .Name}}
46
+ {{- if not $commentPrinted}}
47
+ // Named input parameters for receiver constructor.
48
+ {{- $commentPrinted = true }}
49
+ {{- end}}
50
+ {{.Name}} {{.Type}}
51
+ {{- end}}
52
+ {{- end}}
53
+ {{- end}}
54
+
55
+ {{- $commentPrinted := false }}
56
+ {{- range .Func.Args}}
57
+ {{- if .Name}}
58
+ {{- if not $commentPrinted}}
59
+ // Named input parameters for target function.
60
+ {{- $commentPrinted = true }}
61
+ {{- end}}
62
+ {{.Name}} {{.Type}}
63
+ {{- end}}
64
+ {{- end}}
65
+
66
+ {{- range $index, $res := .Func.Results}}
67
+ {{- if eq $res.Name "gotErr"}}
68
+ wantErr bool
69
+ {{- else if eq $index 0}}
70
+ want {{$res.Type}}
71
+ {{- else}}
72
+ want{{add $index 1}} {{$res.Type}}
73
+ {{- end}}
74
+ {{- end}}
75
+ }{
76
+ // TODO: Add test cases.
77
+ }
78
+
79
+ {{- /* Loop over all the test cases. */}}
80
+ for _, tt := range tests {
81
+ t.Run(tt.name, func(t *{{.TestingPackageName}}.T) {
82
+ {{- /* Constructor or empty initialization. */}}
83
+ {{- if .Receiver}}
84
+ {{- if .Receiver.Constructor}}
85
+ {{- /* Receiver variable by calling constructor. */}}
86
+ {{fieldNames .Receiver.Constructor.Results ""}} := {{if .PackageName}}{{.PackageName}}.{{end}}
87
+ {{- .Receiver.Constructor.Name}}
88
+
89
+ {{- /* Constructor input parameters. */ -}}
90
+ (
91
+ {{- range $index, $arg := .Receiver.Constructor.Args}}
92
+ {{- if ne $index 0}}, {{end}}
93
+ {{- if .Name}}tt.{{.Name}}{{else}}{{.Value}}{{end}}
94
+ {{- end -}}
95
+ )
96
+
97
+ {{- /* Handles the error return from constructor. */}}
98
+ {{- $last := last .Receiver.Constructor.Results}}
99
+ {{- if eq $last.Type "error"}}
100
+ if err != nil {
101
+ t.Fatalf("could not construct receiver type: %v", err)
102
+ }
103
+ {{- end}}
104
+ {{- else}}
105
+ {{- /* Receiver variable declaration. */}}
106
+ // TODO: construct the receiver type.
107
+ var {{.Receiver.Var.Name}} {{.Receiver.Var.Type}}
108
+ {{- end}}
109
+ {{- end}}
110
+
111
+ {{- /* Got variables. */}}
112
+ {{if .Func.Results}}{{fieldNames .Func.Results ""}} := {{end}}
113
+
114
+ {{- /* Call expression. */}}
115
+ {{- if .Receiver}}{{/* Call method by VAR.METHOD. */}}
116
+ {{- .Receiver.Var.Name}}.
117
+ {{- else if .PackageName}}{{/* Call function by PACKAGE.FUNC. */}}
118
+ {{- .PackageName}}.
119
+ {{- end}}{{.Func.Name}}
120
+
121
+ {{- /* Input parameters. */ -}}
122
+ (
123
+ {{- range $index, $arg := .Func.Args}}
124
+ {{- if ne $index 0}}, {{end}}
125
+ {{- if .Name}}tt.{{.Name}}{{else}}{{.Value}}{{end}}
126
+ {{- end -}}
127
+ )
128
+
129
+ {{- /* Handles the returned error before the rest of return value. */}}
130
+ {{- $last := last .Func.Results}}
131
+ {{- if eq $last.Type "error"}}
132
+ if gotErr != nil {
133
+ if !tt.wantErr {
134
+ t.Errorf("{{$.Func.Name}}() failed: %v", gotErr)
135
+ }
136
+ return
137
+ }
138
+ if tt.wantErr {
139
+ t.Fatal("{{$.Func.Name}}() succeeded unexpectedly")
140
+ }
141
+ {{- end}}
142
+
143
+ {{- /* Compare the returned values except for the last returned error. */}}
144
+ {{- if or (and .Func.Results (ne $last.Type "error")) (and (gt (len .Func.Results) 1) (eq $last.Type "error"))}}
145
+ // TODO: update the condition below to compare got with tt.want.
146
+ {{- range $index, $res := .Func.Results}}
147
+ {{- if ne $res.Name "gotErr"}}
148
+ if true {
149
+ t.Errorf("{{$.Func.Name}}() = %v, want %v", {{.Name}}, tt.{{if eq $index 0}}want{{else}}want{{add $index 1}}{{end}})
150
+ }
151
+ {{- end}}
152
+ {{- end}}
153
+ {{- end}}
154
+ })
155
+ }
155
156
}
156
157
`
157
158
@@ -250,7 +251,7 @@ func AddTestForFunc(ctx context.Context, snapshot *cache.Snapshot, loc protocol.
250
251
for _ , spec := range file .Imports {
251
252
// TODO(hxjiang): support dot imports.
252
253
if spec .Name != nil && spec .Name .Name == "." {
253
- return nil , fmt .Errorf ("\" add a test for func\" does not support files containing dot imports" )
254
+ return nil , fmt .Errorf ("\" add test for func\" does not support files containing dot imports" )
254
255
}
255
256
path , err := strconv .Unquote (spec .Path .Value )
256
257
if err != nil {
@@ -783,10 +784,15 @@ func AddTestForFunc(ctx context.Context, snapshot *cache.Snapshot, loc protocol.
783
784
return nil , err
784
785
}
785
786
787
+ formatted , err := format .Source (test .Bytes ())
788
+ if err != nil {
789
+ return nil , err
790
+ }
791
+
786
792
edits = append (edits ,
787
793
protocol.TextEdit {
788
794
Range : eofRange ,
789
- NewText : test . String ( ),
795
+ NewText : string ( formatted ),
790
796
})
791
797
792
798
return append (changes , protocol .DocumentChangeEdit (testFH , edits )), nil
0 commit comments