forked from cweill/gotests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gotests_test.go
711 lines (701 loc) · 22.4 KB
/
gotests_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
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
package gotests
import (
"errors"
"go/types"
"io/ioutil"
"path"
"regexp"
"runtime"
"strings"
"testing"
"unicode"
"golang.org/x/tools/imports"
)
func TestGenerateTests(t *testing.T) {
type args struct {
srcPath string
only *regexp.Regexp
excl *regexp.Regexp
exported bool
printInputs bool
subtests bool
importer types.Importer
templateDir string
}
tests := []struct {
name string
args args
want string
wantNoTests bool
wantMultipleTests bool
wantErr bool
}{
{
name: "Blank Go file",
args: args{
srcPath: `testdata/blankfile/blank.go`,
},
wantNoTests: true,
wantErr: true,
}, {
name: "Blank Go file in directory",
args: args{
srcPath: `testdata/blankfile/notblank.go`,
},
wantNoTests: true,
wantErr: true,
}, {
name: "Test file with garbage data",
args: args{
srcPath: `testdata/invalidtest/invalid.go`,
},
wantNoTests: true,
wantErr: true,
}, {
name: "Hidden file",
args: args{
srcPath: `testdata/.hidden.go`,
},
wantNoTests: true,
wantErr: true,
}, {
name: "Nonexistant file",
args: args{
srcPath: `testdata/nonexistant.go`,
},
wantNoTests: true,
wantErr: true,
}, {
name: "Target test file",
args: args{
srcPath: `testdata/test103_test.go`,
only: regexp.MustCompile("wrapToString"),
subtests: true,
},
wantNoTests: false,
wantErr: false,
want: mustReadAndFormatGoFile(t, `testdata/goldens/target_test_file.go`),
}, {
name: "Target test file without only flag",
args: args{
srcPath: `testdata/test103_test.go`,
subtests: true,
},
wantNoTests: false,
wantErr: false,
want: mustReadAndFormatGoFile(t, `testdata/goldens/target_test_file.go`),
}, {
name: "No funcs",
args: args{
srcPath: `testdata/test000.go`,
},
wantNoTests: true,
}, {
name: "Function with neither receiver, parameters, nor results",
args: args{
srcPath: `testdata/test001.go`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/function_with_neither_receiver_parameters_nor_results.go"),
}, {
name: "Function with anonymous arguments",
args: args{
srcPath: `testdata/test002.go`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/function_with_anonymous_arguments.go"),
}, {
name: "Function with named argument",
args: args{
srcPath: `testdata/test003.go`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/function_with_named_argument.go"),
}, {
name: "Function with return value",
args: args{
srcPath: `testdata/test004.go`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/function_with_return_value.go"),
}, {
name: "Function returning an error",
args: args{
srcPath: `testdata/test005.go`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/function_returning_an_error.go"),
}, {
name: "Function with multiple arguments",
args: args{
srcPath: `testdata/test006.go`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/function_with_multiple_arguments.go"),
}, {
name: "Print inputs with multiple arguments",
args: args{
srcPath: `testdata/test006.go`,
printInputs: true,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/print_inputs_with_multiple_arguments.go"),
}, {
name: "Method on a struct pointer",
args: args{
srcPath: `testdata/test007.go`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/method_on_a_struct_pointer.go"),
}, {
name: "Print inputs with single argument",
args: args{
srcPath: `testdata/test007.go`,
printInputs: true,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/print_inputs_with_single_argument.go"),
}, {
name: "Function with struct pointer argument and return type",
args: args{
srcPath: `testdata/test008.go`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/function_with_struct_pointer_argument_and_return_type.go"),
}, {
name: "Struct value method with struct value return type",
args: args{
srcPath: `testdata/test009.go`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/struct_value_method_with_struct_value_return_type.go"),
}, {
name: "Function with map argument and return type",
args: args{
srcPath: `testdata/test010.go`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/function_with_map_argument_and_return_type.go"),
}, {
name: "Function with slice argument and return type",
args: args{
srcPath: `testdata/test011.go`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/function_with_slice_argument_and_return_type.go"),
}, {
name: "Function returning only an error",
args: args{
srcPath: `testdata/test012.go`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/function_returning_only_an_error.go"),
}, {
name: "Function with a function parameter",
args: args{
srcPath: `testdata/test013.go`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/function_with_a_function_parameter.go"),
}, {
name: "Function with a function parameter with its own parameters and result",
args: args{
srcPath: `testdata/test014.go`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/function_with_a_function_parameter_with_its_own_parameters_and_result.go"),
}, {
name: "Function with a function parameter that returns two results",
args: args{
srcPath: `testdata/test015.go`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/function_with_a_function_parameter_that_returns_two_results.go"),
}, {
name: "Function with defined interface type parameter and result",
args: args{
srcPath: `testdata/test016.go`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/function_with_defined_interface_type_parameter_and_result.go"),
}, {
name: "Function with imported interface receiver, parameter, and result",
args: args{
srcPath: `testdata/test017.go`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/function_with_imported_interface_receiver_parameter_and_result.go"),
}, {
name: "Function with imported struct receiver, parameter, and result",
args: args{
srcPath: `testdata/test018.go`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/function_with_imported_struct_receiver_parameter_and_result.go"),
}, {
name: "Function with multiple parameters of the same type",
args: args{
srcPath: `testdata/test019.go`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/function_with_multiple_parameters_of_the_same_type.go"),
}, {
name: "Function with a variadic parameter",
args: args{
srcPath: `testdata/test020.go`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/function_with_a_variadic_parameter.go"),
}, {
name: "Function with interface{} parameter and result",
args: args{
srcPath: `testdata/test021.go`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/function_with_interface_parameter_and_result.go"),
}, {
name: "Function with named imports",
args: args{
srcPath: `testdata/test022.go`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/function_with_named_imports.go"),
}, {
name: "Function with channel parameter and result",
args: args{
srcPath: `testdata/test023.go`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/function_with_channel_parameter_and_result.go"),
}, {
name: "File with multiple imports",
args: args{
srcPath: `testdata/test024.go`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/file_with_multiple_imports.go"),
}, {
name: "Function returning two results and an error",
args: args{
srcPath: `testdata/test025.go`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/function_returning_two_results_and_an_error.go"),
}, {
name: "Multiple named results",
args: args{
srcPath: `testdata/test026.go`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/multiple_named_results.go"),
}, {
name: "Two different structs with same method name",
args: args{
srcPath: `testdata/test027.go`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/two_different_structs_with_same_method_name.go"),
}, {
name: "Underlying types",
args: args{
srcPath: `testdata/test028.go`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/underlying_types.go"),
}, {
name: "Struct receiver with multiple fields",
args: args{
srcPath: `testdata/test029.go`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/struct_receiver_with_multiple_fields.go"),
}, {
name: "Struct receiver with anonymous fields",
args: args{
srcPath: `testdata/test030.go`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/struct_receiver_with_anonymous_fields.go"),
}, {
name: "io.Writer parameters",
args: args{
srcPath: `testdata/test031.go`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/io_writer_parameters.go"),
}, {
name: "Two structs with same method name",
args: args{
srcPath: `testdata/test032.go`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/two_structs_with_same_method_name.go"),
}, {
name: "Functions and methods with 'name' receivers, parameters, and results",
args: args{
srcPath: `testdata/test033.go`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/functions_and_methods_with_name_receivers_parameters_and_results.go"),
}, {
name: "Receiver struct with reserved field names",
args: args{
srcPath: `testdata/test034.go`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/receiver_struct_with_reserved_field_names.go"),
}, {
name: "Receiver struct with fields with complex package names",
args: args{
srcPath: `testdata/test035.go`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/receiver_struct_with_fields_with_complex_package_names.go"),
}, {
name: "Functions and receivers with same names except exporting",
args: args{
srcPath: `testdata/test036.go`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/functions_and_receivers_with_same_names_except_exporting.go"),
}, {
name: "Receiver is indirect imported struct",
args: args{
srcPath: `testdata/test037.go`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/receiver_is_indirect_imported_struct.go"),
}, {
name: "Multiple functions",
args: args{
srcPath: `testdata/test_filter.go`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/multiple_functions.go"),
}, {
name: "Multiple functions with only",
args: args{
srcPath: `testdata/test_filter.go`,
only: regexp.MustCompile("FooFilter|bazFilter"),
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/multiple_functions_with_only.go"),
}, {
name: "Multiple functions with only regexp without matches",
args: args{
srcPath: `testdata/test_filter.go`,
only: regexp.MustCompile("asdf"),
},
wantNoTests: true,
}, {
name: "Multiple functions with case-insensitive only",
args: args{
srcPath: `testdata/test_filter.go`,
only: regexp.MustCompile("(?i)fooFilter|BazFilter"),
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/multiple_functions_with_case-insensitive_only.go"),
}, {
name: "Multiple functions with only filtering on receiver",
args: args{
srcPath: `testdata/test_filter.go`,
only: regexp.MustCompile("^BarBarFilter$"),
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/multiple_functions_with_only_filtering_on_receiver.go"),
}, {
name: "Multiple functions with only filtering on method",
args: args{
srcPath: `testdata/test_filter.go`,
only: regexp.MustCompile("^(BarFilter)$"),
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/multiple_functions_with_only_filtering_on_method.go"),
}, {
name: "Multiple functions filtering exported",
args: args{
srcPath: `testdata/test_filter.go`,
exported: true,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/multiple_functions_filtering_exported.go"),
}, {
name: "Multiple functions filtering exported with only",
args: args{
srcPath: `testdata/test_filter.go`,
only: regexp.MustCompile(`FooFilter`),
exported: true,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/multiple_functions_filtering_exported_with_only.go"),
}, {
name: "Multiple functions filtering all out",
args: args{
srcPath: `testdata/test_filter.go`,
only: regexp.MustCompile("fooFilter"),
},
wantNoTests: true,
}, {
name: "Multiple functions with excl",
args: args{
srcPath: `testdata/test_filter.go`,
excl: regexp.MustCompile("FooFilter|bazFilter"),
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/multiple_functions_with_excl.go"),
}, {
name: "Multiple functions with case-insensitive excl",
args: args{
srcPath: `testdata/test_filter.go`,
excl: regexp.MustCompile("(?i)foOFilter|BaZFilter"),
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/multiple_functions_with_case-insensitive_excl.go"),
}, {
name: "Multiple functions filtering exported with excl",
args: args{
srcPath: `testdata/test_filter.go`,
excl: regexp.MustCompile(`FooFilter`),
exported: true,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/multiple_functions_filtering_exported_with_excl.go"),
}, {
name: "Multiple functions excluding all",
args: args{
srcPath: `testdata/test_filter.go`,
excl: regexp.MustCompile("bazFilter|FooFilter|BarFilter"),
},
wantNoTests: true,
}, {
name: "Multiple functions excluding on receiver",
args: args{
srcPath: `testdata/test_filter.go`,
excl: regexp.MustCompile("^BarBarFilter$"),
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/multiple_functions_excluding_on_receiver.go"),
}, {
name: "Multiple functions excluding on method",
args: args{
srcPath: `testdata/test_filter.go`,
excl: regexp.MustCompile("^BarFilter$"),
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/multiple_functions_excluding_on_method.go"),
}, {
name: "Multiple functions with both only and excl",
args: args{
srcPath: `testdata/test_filter.go`,
only: regexp.MustCompile("BarFilter"),
excl: regexp.MustCompile("FooFilter"),
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/multiple_functions_with_both_only_and_excl.go"),
}, {
name: "Multiple functions with only and excl competing",
args: args{
srcPath: `testdata/test_filter.go`,
only: regexp.MustCompile("FooFilter|BarFilter"),
excl: regexp.MustCompile("FooFilter|bazFilter"),
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/multiple_functions_with_only_and_excl_competing.go"),
}, {
name: "Custom importer fails",
args: args{
srcPath: `testdata/test_filter.go`,
importer: &fakeImporter{err: errors.New("error")},
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/custom_importer_fails.go"),
}, {
name: "Existing test file",
args: args{
srcPath: `testdata/test100.go`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/existing_test_file.go"),
}, {
name: "Existing test file with just package declaration",
args: args{
srcPath: `testdata/test101.go`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/existing_test_file_with_just_package_declaration.go"),
}, {
name: "Existing test file with no functions",
args: args{
srcPath: `testdata/test102.go`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/existing_test_file_with_no_functions.go"),
}, {
name: "Existing test file with multiple imports",
args: args{
srcPath: `testdata/test200.go`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/existing_test_file_with_multiple_imports.go"),
}, {
name: "Entire testdata directory",
args: args{
srcPath: `testdata/`,
},
wantMultipleTests: true,
}, {
name: "Different packages in same directory - part 1",
args: args{
srcPath: `testdata/mixedpkg/bar.go`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/different_packages_in_same_directory_-_part_1.go"),
}, {
name: "Different packages in same directory - part 2",
args: args{
srcPath: `testdata/mixedpkg/foo.go`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/different_packages_in_same_directory_-_part_2.go"),
}, {
name: "Empty test file",
args: args{
srcPath: `testdata/blanktest/blank.go`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/empty_test_file.go"),
}, {
name: "Test file with syntax errors",
args: args{
srcPath: `testdata/syntaxtest/syntax.go`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/test_file_with_syntax_errors.go"),
}, {
name: "Undefined types",
args: args{
srcPath: `testdata/undefinedtypes/undefined.go`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/undefined_types.go"),
}, {
name: "Subtest Edition - Functions and receivers with same names except exporting",
args: args{
srcPath: `testdata/test036.go`,
subtests: true,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/subtest_edition_-_functions_and_receivers_with_same_names_except_exporting.go"),
},
{
name: "Init function",
args: args{
srcPath: `testdata/init_func.go`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/no_init_funcs.go"),
},
{
name: "Existing test file with package level comments",
args: args{
srcPath: `testdata/test_existing_test_file_with_comments.go`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/existing_test_file_with_package_level_comments.go"),
},
{
name: "Existing test file with package level comments with newline",
args: args{
srcPath: `testdata/test_existing_test_file_with_comments.go`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/existing_test_file_with_package_level_comments.go"),
},
{
name: "Existing test file with package level comments without newline",
args: args{
srcPath: `testdata/test_existing_test_file_wih_comments_without_newline.go`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/existing_test_file_with_package_level_comments_without_newline.go"),
},
{
name: "Existing test file with mixed types package level comments",
args: args{
srcPath: `testdata/test_existing_test_file_wih_mixed_comments.go`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/existing_test_file_with_package_level_mixed_types_comments.go"),
},
{
name: "Naked function with subtests",
args: args{
srcPath: "testdata/naked_function_with_subtests.go",
subtests: true,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/naked_function_with_subtests.go"),
},
{
name: "Naked function without subtests",
args: args{
srcPath: "testdata/naked_function_without_subtests.go",
subtests: false,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/naked_function_without_subtests.go"),
},
{
name: "Test non existing template path",
args: args{
srcPath: `testdata/calculator.go`,
templateDir: `/tmp/not/exising/path`,
},
wantErr: true,
wantNoTests: true,
},
{
name: "Test non bad template formatting",
args: args{
srcPath: `testdata/calculator.go`,
templateDir: `testdata/bad_customtemplates`,
},
wantErr: true,
wantNoTests: true,
},
{
name: "Test custom template path",
args: args{
srcPath: `testdata/test004.go`,
templateDir: `testdata/customtemplates`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/function_with_return_value_custom_template.go"),
},
{
name: "Test interface embedding",
args: args{
srcPath: `testdata/undefinedtypes/interface_embedding.go`,
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/interface_embedding.go"),
wantNoTests: !versionGreaterOrEqualThan("go1.11"),
wantErr: !versionGreaterOrEqualThan("go1.11"),
},
}
tmp, err := ioutil.TempDir("", "gotests_test")
if err != nil {
t.Fatalf("ioutil.TempDir: %v", err)
}
for _, tt := range tests {
gts, err := GenerateTests(tt.args.srcPath, &Options{
Only: tt.args.only,
Exclude: tt.args.excl,
Exported: tt.args.exported,
PrintInputs: tt.args.printInputs,
Subtests: tt.args.subtests,
Importer: func() types.Importer { return tt.args.importer },
TemplateDir: tt.args.templateDir,
})
if (err != nil) != tt.wantErr {
t.Errorf("%q. GenerateTests(%v) error = %v, wantErr %v", tt.name, tt.args.srcPath, err, tt.wantErr)
continue
}
if (len(gts) == 0) != tt.wantNoTests {
t.Errorf("%q. GenerateTests(%v) returned no tests", tt.name, tt.args.srcPath)
continue
}
if (len(gts) > 1) != tt.wantMultipleTests {
t.Errorf("%q. GenerateTests(%v) returned too many tests", tt.name, tt.args.srcPath)
continue
}
if tt.wantNoTests || tt.wantMultipleTests {
continue
}
if got := string(gts[0].Output); got != tt.want {
t.Errorf("%q. GenerateTests(%v) = \n%v, want \n%v", tt.name, tt.args.srcPath, got, tt.want)
outputResult(t, tmp, tt.name, gts[0].Output)
}
}
}
func versionGreaterOrEqualThan(version string) bool {
prefixes := []string{"go1.9", "go1.10", "go1.11", "go1.12", "go1.13"}
v := runtime.Version()
for _, prefix := range prefixes {
if strings.Contains(version, prefix) {
return true
}
if strings.Contains(v, prefix) {
return false
}
}
return true
}
func mustReadAndFormatGoFile(t *testing.T, filename string) string {
fmted, err := imports.Process(filename, nil, nil)
if err != nil {
t.Fatalf("reading and formatting file: %v", err)
}
return string(fmted)
}
func outputResult(t *testing.T, tmpDir, testName string, got []byte) {
tmpResult := path.Join(tmpDir, toSnakeCase(testName)+".go")
if err := ioutil.WriteFile(tmpResult, got, 0644); err != nil {
t.Errorf("ioutil.WriteFile: %v", err)
}
t.Logf(tmpResult)
}
func toSnakeCase(s string) string {
var res []rune
for _, r := range []rune(s) {
r = unicode.ToLower(r)
switch r {
case ' ', '.':
r = '_'
case ',', '\'', '{', '}':
continue
}
res = append(res, r)
}
return string(res)
}
// 249032394 ns/op
func BenchmarkGenerateTests(b *testing.B) {
for i := 0; i < b.N; i++ {
GenerateTests("testdata/", &Options{})
}
}
// A fake importer.
type fakeImporter struct {
err error
}
func (f *fakeImporter) Import(path string) (*types.Package, error) {
return nil, f.err
}