Skip to content

Commit 3cb5a24

Browse files
committed
use go-cmp instead of reflect.DeepEqual
1 parent afa0a37 commit 3cb5a24

29 files changed

+178
-133
lines changed

gotests.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ import (
1616
"github.com/cweill/gotests/internal/output"
1717
)
1818

19+
var (
20+
cmpImport = &models.Import{
21+
Name: "",
22+
Path: `"github.com/google/go-cmp/cmp"`,
23+
}
24+
)
25+
1926
// Options provides custom filters and parameters for generating tests.
2027
type Options struct {
2128
Only *regexp.Regexp // Includes only functions that match.
@@ -142,8 +149,20 @@ func parseTestFile(p *goparser.Parser, testPath string, h *models.Header) (*mode
142149
return nil, nil, fmt.Errorf("Parser.Parse test file: %v", err)
143150
}
144151
var testFuncs []string
152+
cmpImportNeeded := false
145153
for _, fun := range tr.Funcs {
146154
testFuncs = append(testFuncs, fun.Name)
155+
if cmpImportNeeded {
156+
continue
157+
}
158+
for _, field := range fun.Parameters {
159+
if !(field.IsWriter() || field.IsBasicType()) {
160+
cmpImportNeeded = true
161+
}
162+
}
163+
}
164+
if cmpImportNeeded {
165+
tr.Header.Imports = append(tr.Header.Imports, cmpImport)
147166
}
148167
tr.Header.Imports = append(tr.Header.Imports, h.Imports...)
149168
h = tr.Header

gotests_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"testing"
1212
"unicode"
1313

14+
"github.com/google/go-cmp/cmp"
1415
"golang.org/x/tools/imports"
1516
)
1617

@@ -643,7 +644,7 @@ func TestGenerateTests(t *testing.T) {
643644
continue
644645
}
645646
if got := string(gts[0].Output); got != tt.want {
646-
t.Errorf("%q. GenerateTests(%v) = \n%v, want \n%v", tt.name, tt.args.srcPath, got, tt.want)
647+
t.Errorf("%q. GenerateTests(%v) = diff=%v", tt.name, tt.args.srcPath, cmp.Diff(got, tt.want))
647648
outputResult(t, tmp, tt.name, gts[0].Output)
648649
}
649650
}

internal/render/bindata/esc.go

Lines changed: 22 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/render/templates/function.tmpl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,10 @@ func {{.TestName}}(t *testing.T) {
7474
{{- else if .IsBasicType}}
7575
if {{if $f.OnlyReturnsOneValue}}{{Got .}} := {{template "inline" $f}}; {{end}} {{Got .}} != tt.{{Want .}} {
7676
{{- else}}
77-
if {{if $f.OnlyReturnsOneValue}}{{Got .}} := {{template "inline" $f}}; {{end}} !reflect.DeepEqual({{Got .}}, tt.{{Want .}}) {
77+
if {{if $f.OnlyReturnsOneValue}}{{Got .}} := {{template "inline" $f}}; {{end}} !cmp.Equal({{Got .}}, tt.{{Want .}}) {
7878
{{- end}}
79-
t.Errorf("{{template "message" $f}} {{if $f.ReturnsMultiple}}{{Got .}} {{end}}= %v, want %v", {{template "inputs" $f}} {{Got .}}, tt.{{Want .}})
79+
t.Errorf("{{template "message" $f}} {{if $f.ReturnsMultiple}}{{Got .}} {{end}}= %v, want %v{{ if (not ( or .IsWriter .IsBasicType))}}\ndiff=%v{{ end }}", {{template "inputs" $f}} {{Got .}}, tt.{{Want .}}
80+
{{- if (not ( or .IsWriter .IsBasicType))}}, cmp.Diff({{Got .}}, tt.{{Want .}}){{ end }})
8081
}
8182
{{- end}}
8283
{{- if .Subtests }} }) {{- end -}}

testdata/goldens/custom_importer_fails.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package testdata
22

33
import (
4-
"reflect"
54
"testing"
5+
6+
"github.com/google/go-cmp/cmp"
67
)
78

89
func TestFooFilter(t *testing.T) {
@@ -15,16 +16,16 @@ func TestFooFilter(t *testing.T) {
1516
want []*Bar
1617
wantErr bool
1718
}{
18-
// TODO: Add test cases.
19+
// TODO: Add test cases.
1920
}
2021
for _, tt := range tests {
2122
got, err := FooFilter(tt.args.strs)
2223
if (err != nil) != tt.wantErr {
2324
t.Errorf("%q. FooFilter() error = %v, wantErr %v", tt.name, err, tt.wantErr)
2425
continue
2526
}
26-
if !reflect.DeepEqual(got, tt.want) {
27-
t.Errorf("%q. FooFilter() = %v, want %v", tt.name, got, tt.want)
27+
if !cmp.Equal(got, tt.want) {
28+
t.Errorf("%q. FooFilter() = %v, want %v\ndiff=%v", tt.name, got, tt.want, cmp.Diff(got, tt.want))
2829
}
2930
}
3031
}
@@ -39,7 +40,7 @@ func TestBar_BarFilter(t *testing.T) {
3940
args args
4041
wantErr bool
4142
}{
42-
// TODO: Add test cases.
43+
// TODO: Add test cases.
4344
}
4445
for _, tt := range tests {
4546
b := &Bar{}
@@ -58,7 +59,7 @@ func Test_bazFilter(t *testing.T) {
5859
args args
5960
want float64
6061
}{
61-
// TODO: Add test cases.
62+
// TODO: Add test cases.
6263
}
6364
for _, tt := range tests {
6465
if got := bazFilter(tt.args.f); got != tt.want {

testdata/goldens/existing_test_file.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package testdata
22

33
import (
4-
"reflect"
54
"testing"
5+
6+
"github.com/google/go-cmp/cmp"
67
)
78

89
func TestBarBar100(t *testing.T) {
@@ -55,16 +56,16 @@ func TestFoo100(t *testing.T) {
5556
want []*Bar
5657
wantErr bool
5758
}{
58-
// TODO: Add test cases.
59+
// TODO: Add test cases.
5960
}
6061
for _, tt := range tests {
6162
got, err := Foo100(tt.args.strs)
6263
if (err != nil) != tt.wantErr {
6364
t.Errorf("%q. Foo100() error = %v, wantErr %v", tt.name, err, tt.wantErr)
6465
continue
6566
}
66-
if !reflect.DeepEqual(got, tt.want) {
67-
t.Errorf("%q. Foo100() = %v, want %v", tt.name, got, tt.want)
67+
if !cmp.Equal(got, tt.want) {
68+
t.Errorf("%q. Foo100() = %v, want %v\ndiff=%v", tt.name, got, tt.want, cmp.Diff(got, tt.want))
6869
}
6970
}
7071
}
@@ -79,7 +80,7 @@ func TestBar_Bar100(t *testing.T) {
7980
args args
8081
wantErr bool
8182
}{
82-
// TODO: Add test cases.
83+
// TODO: Add test cases.
8384
}
8485
for _, tt := range tests {
8586
b := &Bar{}
@@ -98,7 +99,7 @@ func Test_baz100(t *testing.T) {
9899
args args
99100
want float64
100101
}{
101-
// TODO: Add test cases.
102+
// TODO: Add test cases.
102103
}
103104
for _, tt := range tests {
104105
if got := baz100(tt.args.f); got != tt.want {

testdata/goldens/function_returning_two_results_and_an_error.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package testdata
22

33
import (
4-
"reflect"
54
"testing"
5+
6+
"github.com/google/go-cmp/cmp"
67
)
78

89
func TestFoo25(t *testing.T) {
@@ -16,7 +17,7 @@ func TestFoo25(t *testing.T) {
1617
want1 []byte
1718
wantErr bool
1819
}{
19-
// TODO: Add test cases.
20+
// TODO: Add test cases.
2021
}
2122
for _, tt := range tests {
2223
got, got1, err := Foo25(tt.args.in0)
@@ -27,8 +28,8 @@ func TestFoo25(t *testing.T) {
2728
if got != tt.want {
2829
t.Errorf("%q. Foo25() got = %v, want %v", tt.name, got, tt.want)
2930
}
30-
if !reflect.DeepEqual(got1, tt.want1) {
31-
t.Errorf("%q. Foo25() got1 = %v, want %v", tt.name, got1, tt.want1)
31+
if !cmp.Equal(got1, tt.want1) {
32+
t.Errorf("%q. Foo25() got1 = %v, want %v\ndiff=%v", tt.name, got1, tt.want1, cmp.Diff(got1, tt.want1))
3233
}
3334
}
3435
}
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package testdata
22

33
import (
4-
"reflect"
54
"testing"
5+
6+
"github.com/google/go-cmp/cmp"
67
)
78

89
func TestFoo23(t *testing.T) {
@@ -14,11 +15,11 @@ func TestFoo23(t *testing.T) {
1415
args args
1516
want chan string
1617
}{
17-
// TODO: Add test cases.
18+
// TODO: Add test cases.
1819
}
1920
for _, tt := range tests {
20-
if got := Foo23(tt.args.ch); !reflect.DeepEqual(got, tt.want) {
21-
t.Errorf("%q. Foo23() = %v, want %v", tt.name, got, tt.want)
21+
if got := Foo23(tt.args.ch); !cmp.Equal(got, tt.want) {
22+
t.Errorf("%q. Foo23() = %v, want %v\ndiff=%v", tt.name, got, tt.want, cmp.Diff(got, tt.want))
2223
}
2324
}
2425
}
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package testdata
22

33
import (
4-
"reflect"
54
"testing"
5+
6+
"github.com/google/go-cmp/cmp"
67
)
78

89
func TestFoo16(t *testing.T) {
@@ -14,11 +15,11 @@ func TestFoo16(t *testing.T) {
1415
args args
1516
want Bazzar
1617
}{
17-
// TODO: Add test cases.
18+
// TODO: Add test cases.
1819
}
1920
for _, tt := range tests {
20-
if got := Foo16(tt.args.in); !reflect.DeepEqual(got, tt.want) {
21-
t.Errorf("%q. Foo16() = %v, want %v", tt.name, got, tt.want)
21+
if got := Foo16(tt.args.in); !cmp.Equal(got, tt.want) {
22+
t.Errorf("%q. Foo16() = %v, want %v\ndiff=%v", tt.name, got, tt.want, cmp.Diff(got, tt.want))
2223
}
2324
}
2425
}

testdata/goldens/function_with_imported_interface_receiver_parameter_and_result.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ package testdata
22

33
import (
44
"io"
5-
"reflect"
65
"testing"
6+
7+
"github.com/google/go-cmp/cmp"
78
)
89

910
func TestFoo17(t *testing.T) {
@@ -15,11 +16,11 @@ func TestFoo17(t *testing.T) {
1516
args args
1617
want io.Reader
1718
}{
18-
// TODO: Add test cases.
19+
// TODO: Add test cases.
1920
}
2021
for _, tt := range tests {
21-
if got := Foo17(tt.args.r); !reflect.DeepEqual(got, tt.want) {
22-
t.Errorf("%q. Foo17() = %v, want %v", tt.name, got, tt.want)
22+
if got := Foo17(tt.args.r); !cmp.Equal(got, tt.want) {
23+
t.Errorf("%q. Foo17() = %v, want %v\ndiff=%v", tt.name, got, tt.want, cmp.Diff(got, tt.want))
2324
}
2425
}
2526
}

testdata/goldens/function_with_imported_struct_receiver_parameter_and_result.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ package testdata
22

33
import (
44
"os"
5-
"reflect"
65
"testing"
6+
7+
"github.com/google/go-cmp/cmp"
78
)
89

910
func TestFoo18(t *testing.T) {
@@ -15,11 +16,11 @@ func TestFoo18(t *testing.T) {
1516
args args
1617
want *os.File
1718
}{
18-
// TODO: Add test cases.
19+
// TODO: Add test cases.
1920
}
2021
for _, tt := range tests {
21-
if got := Foo18(tt.args.t); !reflect.DeepEqual(got, tt.want) {
22-
t.Errorf("%q. Foo18() = %v, want %v", tt.name, got, tt.want)
22+
if got := Foo18(tt.args.t); !cmp.Equal(got, tt.want) {
23+
t.Errorf("%q. Foo18() = %v, want %v\ndiff=%v", tt.name, got, tt.want, cmp.Diff(got, tt.want))
2324
}
2425
}
2526
}

0 commit comments

Comments
 (0)