This repository was archived by the owner on Jun 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 609
/
Copy pathmockgen_test.go
450 lines (419 loc) · 11.1 KB
/
mockgen_test.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
package main
import (
"fmt"
"os"
"path/filepath"
"reflect"
"regexp"
"strings"
"testing"
"github.com/golang/mock/mockgen/model"
)
func TestMakeArgString(t *testing.T) {
testCases := []struct {
argNames []string
argTypes []string
argString string
}{
{
argNames: nil,
argTypes: nil,
argString: "",
},
{
argNames: []string{"arg0"},
argTypes: []string{"int"},
argString: "arg0 int",
},
{
argNames: []string{"arg0", "arg1"},
argTypes: []string{"int", "bool"},
argString: "arg0 int, arg1 bool",
},
{
argNames: []string{"arg0", "arg1"},
argTypes: []string{"int", "int"},
argString: "arg0, arg1 int",
},
{
argNames: []string{"arg0", "arg1", "arg2"},
argTypes: []string{"bool", "int", "int"},
argString: "arg0 bool, arg1, arg2 int",
},
{
argNames: []string{"arg0", "arg1", "arg2"},
argTypes: []string{"int", "bool", "int"},
argString: "arg0 int, arg1 bool, arg2 int",
},
{
argNames: []string{"arg0", "arg1", "arg2"},
argTypes: []string{"int", "int", "bool"},
argString: "arg0, arg1 int, arg2 bool",
},
{
argNames: []string{"arg0", "arg1", "arg2"},
argTypes: []string{"int", "int", "int"},
argString: "arg0, arg1, arg2 int",
},
{
argNames: []string{"arg0", "arg1", "arg2", "arg3"},
argTypes: []string{"bool", "int", "int", "int"},
argString: "arg0 bool, arg1, arg2, arg3 int",
},
{
argNames: []string{"arg0", "arg1", "arg2", "arg3"},
argTypes: []string{"int", "bool", "int", "int"},
argString: "arg0 int, arg1 bool, arg2, arg3 int",
},
{
argNames: []string{"arg0", "arg1", "arg2", "arg3"},
argTypes: []string{"int", "int", "bool", "int"},
argString: "arg0, arg1 int, arg2 bool, arg3 int",
},
{
argNames: []string{"arg0", "arg1", "arg2", "arg3"},
argTypes: []string{"int", "int", "int", "bool"},
argString: "arg0, arg1, arg2 int, arg3 bool",
},
{
argNames: []string{"arg0", "arg1", "arg2", "arg3", "arg4"},
argTypes: []string{"bool", "int", "int", "int", "bool"},
argString: "arg0 bool, arg1, arg2, arg3 int, arg4 bool",
},
{
argNames: []string{"arg0", "arg1", "arg2", "arg3", "arg4"},
argTypes: []string{"int", "bool", "int", "int", "bool"},
argString: "arg0 int, arg1 bool, arg2, arg3 int, arg4 bool",
},
{
argNames: []string{"arg0", "arg1", "arg2", "arg3", "arg4"},
argTypes: []string{"int", "int", "bool", "int", "bool"},
argString: "arg0, arg1 int, arg2 bool, arg3 int, arg4 bool",
},
{
argNames: []string{"arg0", "arg1", "arg2", "arg3", "arg4"},
argTypes: []string{"int", "int", "int", "bool", "bool"},
argString: "arg0, arg1, arg2 int, arg3, arg4 bool",
},
{
argNames: []string{"arg0", "arg1", "arg2", "arg3", "arg4"},
argTypes: []string{"int", "int", "bool", "bool", "int"},
argString: "arg0, arg1 int, arg2, arg3 bool, arg4 int",
},
}
for i, tc := range testCases {
t.Run(fmt.Sprintf("#%d", i), func(t *testing.T) {
s := makeArgString(tc.argNames, tc.argTypes)
if s != tc.argString {
t.Errorf("result == %q, want %q", s, tc.argString)
}
})
}
}
func TestNewIdentifierAllocator(t *testing.T) {
a := newIdentifierAllocator([]string{"taken1", "taken2"})
if len(a) != 2 {
t.Fatalf("expected 2 items, got %v", len(a))
}
_, ok := a["taken1"]
if !ok {
t.Errorf("allocator doesn't contain 'taken1': %#v", a)
}
_, ok = a["taken2"]
if !ok {
t.Errorf("allocator doesn't contain 'taken2': %#v", a)
}
}
func allocatorContainsIdentifiers(a identifierAllocator, ids []string) bool {
if len(a) != len(ids) {
return false
}
for _, id := range ids {
_, ok := a[id]
if !ok {
return false
}
}
return true
}
func TestIdentifierAllocator_allocateIdentifier(t *testing.T) {
a := newIdentifierAllocator([]string{"taken"})
t2 := a.allocateIdentifier("taken_2")
if t2 != "taken_2" {
t.Fatalf("expected 'taken_2', got %q", t2)
}
expected := []string{"taken", "taken_2"}
if !allocatorContainsIdentifiers(a, expected) {
t.Fatalf("allocator doesn't contain the expected items - allocator: %#v, expected items: %#v", a, expected)
}
t3 := a.allocateIdentifier("taken")
if t3 != "taken_3" {
t.Fatalf("expected 'taken_3', got %q", t3)
}
expected = []string{"taken", "taken_2", "taken_3"}
if !allocatorContainsIdentifiers(a, expected) {
t.Fatalf("allocator doesn't contain the expected items - allocator: %#v, expected items: %#v", a, expected)
}
t4 := a.allocateIdentifier("taken")
if t4 != "taken_4" {
t.Fatalf("expected 'taken_4', got %q", t4)
}
expected = []string{"taken", "taken_2", "taken_3", "taken_4"}
if !allocatorContainsIdentifiers(a, expected) {
t.Fatalf("allocator doesn't contain the expected items - allocator: %#v, expected items: %#v", a, expected)
}
id := a.allocateIdentifier("id")
if id != "id" {
t.Fatalf("expected 'id', got %q", id)
}
expected = []string{"taken", "taken_2", "taken_3", "taken_4", "id"}
if !allocatorContainsIdentifiers(a, expected) {
t.Fatalf("allocator doesn't contain the expected items - allocator: %#v, expected items: %#v", a, expected)
}
}
func TestGenerateMockInterface_Helper(t *testing.T) {
for _, test := range []struct {
Name string
Identifier string
HelperLine string
Methods []*model.Method
}{
{Name: "mock", Identifier: "MockSomename", HelperLine: "m.ctrl.T.Helper()"},
{Name: "recorder", Identifier: "MockSomenameMockRecorder", HelperLine: "mr.mock.ctrl.T.Helper()"},
{
Name: "mock identifier conflict",
Identifier: "MockSomename",
HelperLine: "m_2.ctrl.T.Helper()",
Methods: []*model.Method{
{
Name: "MethodA",
In: []*model.Parameter{
{
Name: "m",
Type: &model.NamedType{Type: "int"},
},
},
},
},
},
{
Name: "recorder identifier conflict",
Identifier: "MockSomenameMockRecorder",
HelperLine: "mr_2.mock.ctrl.T.Helper()",
Methods: []*model.Method{
{
Name: "MethodA",
In: []*model.Parameter{
{
Name: "mr",
Type: &model.NamedType{Type: "int"},
},
},
},
},
},
} {
t.Run(test.Name, func(t *testing.T) {
g := generator{}
if len(test.Methods) == 0 {
test.Methods = []*model.Method{
{Name: "MethodA"},
{Name: "MethodB"},
}
}
intf := &model.Interface{Name: "Somename"}
for _, m := range test.Methods {
intf.AddMethod(m)
}
if err := g.GenerateMockInterface(intf, "somepackage"); err != nil {
t.Fatal(err)
}
lines := strings.Split(g.buf.String(), "\n")
// T.Helper() should be the first line
for _, method := range test.Methods {
if strings.TrimSpace(lines[findMethod(t, test.Identifier, method.Name, lines)+1]) != test.HelperLine {
t.Fatalf("method %s.%s did not declare itself a Helper method", test.Identifier, method.Name)
}
}
})
}
}
func findMethod(t *testing.T, identifier, methodName string, lines []string) int {
t.Helper()
r := regexp.MustCompile(fmt.Sprintf(`func\s+\(.+%s\)\s*%s`, identifier, methodName))
for i, line := range lines {
if r.MatchString(line) {
return i
}
}
t.Fatalf("unable to find 'func (m %s) %s'", identifier, methodName)
panic("unreachable")
}
func TestGetArgNames(t *testing.T) {
for _, testCase := range []struct {
name string
method *model.Method
expected []string
}{
{
name: "NamedArg",
method: &model.Method{
In: []*model.Parameter{
{
Name: "firstArg",
Type: &model.NamedType{Type: "int"},
},
{
Name: "secondArg",
Type: &model.NamedType{Type: "string"},
},
},
},
expected: []string{"firstArg", "secondArg"},
},
{
name: "NotNamedArg",
method: &model.Method{
In: []*model.Parameter{
{
Name: "",
Type: &model.NamedType{Type: "int"},
},
{
Name: "",
Type: &model.NamedType{Type: "string"},
},
},
},
expected: []string{"arg0", "arg1"},
},
{
name: "MixedNameArg",
method: &model.Method{
In: []*model.Parameter{
{
Name: "firstArg",
Type: &model.NamedType{Type: "int"},
},
{
Name: "_",
Type: &model.NamedType{Type: "string"},
},
},
},
expected: []string{"firstArg", "arg1"},
},
} {
t.Run(testCase.name, func(t *testing.T) {
g := generator{}
result := g.getArgNames(testCase.method)
if !reflect.DeepEqual(result, testCase.expected) {
t.Fatalf("expected %s, got %s", result, testCase.expected)
}
})
}
}
func Test_createPackageMap(t *testing.T) {
tests := []struct {
name string
importPath string
wantPackageName string
wantOK bool
}{
{"golang package", "context", "context", true},
{"third party", "golang.org/x/tools/present", "present", true},
}
var importPaths []string
for _, t := range tests {
importPaths = append(importPaths, t.importPath)
}
packages := createPackageMap(importPaths)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotPackageName, gotOk := packages[tt.importPath]
if gotPackageName != tt.wantPackageName {
t.Errorf("createPackageMap() gotPackageName = %v, wantPackageName = %v", gotPackageName, tt.wantPackageName)
}
if gotOk != tt.wantOK {
t.Errorf("createPackageMap() gotOk = %v, wantOK = %v", gotOk, tt.wantOK)
}
})
}
}
func TestParsePackageImport_FallbackGoPath(t *testing.T) {
goPath, err := os.MkdirTemp("", "gopath")
if err != nil {
t.Error(err)
}
defer func() {
if err = os.RemoveAll(goPath); err != nil {
t.Error(err)
}
}()
srcDir := filepath.Join(goPath, "src/example.com/foo")
err = os.MkdirAll(srcDir, 0755)
if err != nil {
t.Error(err)
}
key := "GOPATH"
value := goPath
if err := os.Setenv(key, value); err != nil {
t.Fatalf("unable to set environment variable %q to %q: %v", key, value, err)
}
key = "GO111MODULE"
value = "on"
if err := os.Setenv(key, value); err != nil {
t.Fatalf("unable to set environment variable %q to %q: %v", key, value, err)
}
pkgPath, err := parsePackageImport(srcDir)
expected := "example.com/foo"
if pkgPath != expected {
t.Errorf("expect %s, got %s", expected, pkgPath)
}
}
func TestParsePackageImport_FallbackMultiGoPath(t *testing.T) {
var goPathList []string
// first gopath
goPath, err := os.MkdirTemp("", "gopath1")
if err != nil {
t.Error(err)
}
goPathList = append(goPathList, goPath)
defer func() {
if err = os.RemoveAll(goPath); err != nil {
t.Error(err)
}
}()
srcDir := filepath.Join(goPath, "src/example.com/foo")
err = os.MkdirAll(srcDir, 0755)
if err != nil {
t.Error(err)
}
// second gopath
goPath, err = os.MkdirTemp("", "gopath2")
if err != nil {
t.Error(err)
}
goPathList = append(goPathList, goPath)
defer func() {
if err = os.RemoveAll(goPath); err != nil {
t.Error(err)
}
}()
goPaths := strings.Join(goPathList, string(os.PathListSeparator))
key := "GOPATH"
value := goPaths
if err := os.Setenv(key, value); err != nil {
t.Fatalf("unable to set environment variable %q to %q: %v", key, value, err)
}
key = "GO111MODULE"
value = "on"
if err := os.Setenv(key, value); err != nil {
t.Fatalf("unable to set environment variable %q to %q: %v", key, value, err)
}
pkgPath, err := parsePackageImport(srcDir)
expected := "example.com/foo"
if pkgPath != expected {
t.Errorf("expect %s, got %s", expected, pkgPath)
}
}