Skip to content

Commit f93802e

Browse files
authored
util: use cmp.Diff to compare structs in tests (google#136)
`cmp.Diff` is a better way to compare two structs since the diff is easier to read. This PR improves the tests by replacing `reflect.DeepEqual` with `cmp.Diff`.
1 parent 47c3bda commit f93802e

13 files changed

Lines changed: 62 additions & 46 deletions

File tree

util/maven/dependency_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,6 @@ func TestProcessDependencies(t *testing.T) {
175175
}
176176
proj.ProcessDependencies(getDependencyManagement)
177177
if diff := cmp.Diff(proj, want); diff != "" {
178-
t.Errorf("processDependencies: got %v\n, want %v", proj, want)
178+
t.Errorf("processDependencies:\n(-got, +want):\n%s", diff)
179179
}
180180
}

util/maven/metadata_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,6 @@ func TestMetadata(t *testing.T) {
6868
t.Fatalf("failed to unmarshal input: %v", err)
6969
}
7070
if diff := cmp.Diff(got, want); diff != "" {
71-
t.Errorf("unmarshal input got: %v,\n want: %v", got, want)
71+
t.Errorf("unmarshal input:\n(-got, +want):\n%s", diff)
7272
}
7373
}

util/maven/profile_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func TestProfile(t *testing.T) {
100100
t.Fatalf("failed to unmarshal input: %v", err)
101101
}
102102
if diff := cmp.Diff(project.Profiles, want); diff != "" {
103-
t.Errorf("unmarshal profiles: got %v, want %v", project.Profiles, want)
103+
t.Errorf("unmarshal profiles:\n(-got, +want):\n%s", diff)
104104
}
105105
}
106106

@@ -486,6 +486,6 @@ func TestMergeProfiles(t *testing.T) {
486486
}
487487
proj.Profiles = nil
488488
if diff := cmp.Diff(proj, want); diff != "" {
489-
t.Fatalf("mergeProfiles does not have match result:\n got %v\n want%v\n", proj, want)
489+
t.Fatalf("mergeProfiles does not have match result:\n(-got, +want):\n%s", diff)
490490
}
491491
}

util/maven/project_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ func TestProject(t *testing.T) {
207207
t.Fatalf("failed to unmarshal input: %v", err)
208208
}
209209
if diff := cmp.Diff(got, want); diff != "" {
210-
t.Errorf("unmarshal input: got %v\n, want %v", got, want)
210+
t.Errorf("unmarshal input:\n(- got, + want):\n%s", diff)
211211
}
212212
}
213213

@@ -596,7 +596,7 @@ func TestMergeParent(t *testing.T) {
596596
}
597597
proj2.MergeParent(parent)
598598
if diff := cmp.Diff(proj2, want2); diff != "" {
599-
t.Errorf("mergeParent: got %v\n, want %v", proj2, want2)
599+
t.Errorf("mergeParent:\n(-got, +want):\n%s", diff)
600600
}
601601
}
602602

@@ -781,6 +781,6 @@ func TestInterpolate(t *testing.T) {
781781
}
782782
proj.Interpolate()
783783
if diff := cmp.Diff(proj, want); diff != "" {
784-
t.Errorf("interpolate: got %v\n, want %v", proj, want)
784+
t.Errorf("interpolate:\n(-got, +want):\n%s", diff)
785785
}
786786
}

util/maven/properties_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,6 @@ func TestPropertyMap(t *testing.T) {
9292
t.Fatalf("failed to make property map: %v", err)
9393
}
9494
if diff := cmp.Diff(got, want); diff != "" {
95-
t.Errorf("property map mistmatch: got %v want %v", got, want)
95+
t.Errorf("property map mistmatch:\n(-got, +want):\n%s", diff)
9696
}
9797
}

util/resolve/internal/deptest/deptest_test.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ package deptest
1616

1717
import (
1818
"errors"
19-
"reflect"
2019
"testing"
2120

21+
"github.com/google/go-cmp/cmp"
22+
2223
"deps.dev/util/resolve/dep"
2324
)
2425

@@ -142,8 +143,14 @@ func TestParseStringErrors(t *testing.T) {
142143
}
143144

144145
for _, c := range cases {
145-
if _, err := ParseString(c.s); !reflect.DeepEqual(err, c.err) {
146-
t.Errorf("unexpected error for %s:\n got: %v\nwant: %v", c.s, err, c.err)
146+
_, err := ParseString(c.s)
147+
if diff := cmp.Diff(err, c.err, cmp.Comparer(func(x, y error) bool {
148+
if x == nil || y == nil {
149+
return x == nil && y == nil
150+
}
151+
return x.Error() == y.Error()
152+
})); diff != "" {
153+
t.Errorf("unexpected error for %s:\n(-got, +want):\n%s", c.s, diff)
147154
}
148155
}
149156
}

util/resolve/internal/resolvetest/resolvetest_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package resolvetest
1616

1717
import (
1818
"context"
19-
"reflect"
2019
"testing"
2120

2221
"github.com/google/go-cmp/cmp"
@@ -56,7 +55,7 @@ func TestParseData(t *testing.T) {
5655
}
5756
g1, g2 := a.Graph["alice"], a.Graph["alice2"]
5857
if d := cmp.Diff(g1, g2); d != "" {
59-
t.Logf("Mismatching parsed graphs:(- alice, + alice1):\n%s", d)
58+
t.Logf("Mismatching parsed graphs:(-alice, +alice2):\n%s", d)
6059
}
6160

6261
if got, want := len(a.Test), 2; got != want {
@@ -97,8 +96,9 @@ func TestParseData(t *testing.T) {
9796
t.Fatalf("Unexpected graph name: got %q, want %q", got, want)
9897
}
9998

100-
if got, want := a.Test[0].Flags, map[string]bool{"flag1": true, "flag2": true}; !reflect.DeepEqual(got, want) {
101-
t.Fatalf("Unexpected test flags,\n got %v\nwant %v", got, want)
99+
got, want := a.Test[0].Flags, map[string]bool{"flag1": true, "flag2": true}
100+
if diff := cmp.Diff(got, want); diff != "" {
101+
t.Errorf("Unexpected test flags:\n(-got, +want):\n%s", diff)
102102
}
103103

104104
// Multiverse with overlap.

util/resolve/internal/versiontest/versiontest_test.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ package versiontest
1616

1717
import (
1818
"errors"
19-
"reflect"
2019
"testing"
2120

21+
"github.com/google/go-cmp/cmp"
22+
2223
"deps.dev/util/resolve/version"
2324
)
2425

@@ -102,8 +103,14 @@ func TestParseStringErrors(t *testing.T) {
102103
}
103104

104105
for _, c := range cases {
105-
if _, err := ParseString(c.s); !reflect.DeepEqual(err, c.err) {
106-
t.Errorf("unexpected error for %s:\n got: %v\nwant: %v", c.s, err, c.err)
106+
_, err := ParseString(c.s)
107+
if diff := cmp.Diff(err, c.err, cmp.Comparer(func(x, y error) bool {
108+
if x == nil || y == nil {
109+
return x == nil && y == nil
110+
}
111+
return x.Error() == y.Error()
112+
})); diff != "" {
113+
t.Errorf("unexpected error for %s:\n(-got, +want):\n%s", c.s, diff)
107114
}
108115
}
109116
}

util/resolve/match_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ package resolve
1616

1717
import (
1818
"math/rand"
19-
"reflect"
2019
"strings"
2120
"testing"
2221

22+
"github.com/google/go-cmp/cmp"
23+
2324
"deps.dev/util/resolve/dep"
2425
"deps.dev/util/resolve/version"
2526
)
@@ -52,8 +53,8 @@ func TestSortVersions(t *testing.T) {
5253
got[i], got[j] = got[j], got[i]
5354
})
5455
SortVersions(got)
55-
if !reflect.DeepEqual(got, vs) {
56-
t.Errorf("SortVersions:\nwant: %v\n got: %v", vs, got)
56+
if diff := cmp.Diff(got, vs); diff != "" {
57+
t.Errorf("SortVersions:\n(-got, +want):\n%s", diff)
5758
}
5859
}
5960
}
@@ -115,8 +116,8 @@ func TestSortNPMDependencies(t *testing.T) {
115116
got[i], got[j] = got[j], got[i]
116117
})
117118
SortDependencies(got)
118-
if !reflect.DeepEqual(got, want) {
119-
t.Errorf("SortDependencies:\n got: %v\nwant %v", got, want)
119+
if diff := cmp.Diff(got, want); diff != "" {
120+
t.Errorf("SortDependencies:\n(-got, +want):\n%s", diff)
120121
}
121122
}
122123
}
@@ -263,8 +264,8 @@ func TestMatchNPMRequirement(t *testing.T) {
263264
if got == nil {
264265
got = []Version{}
265266
}
266-
if !reflect.DeepEqual(got, c.want) {
267-
t.Errorf("matchNPMRequirement(%v):\nwant: %v\n got: %v", c.req, c.want, got)
267+
if diff := cmp.Diff(got, c.want); diff != "" {
268+
t.Errorf("matchNPMRequirement(%v):\n(-got, +want):\n%s", c.req, diff)
268269
}
269270
}
270271
}

util/resolve/maven_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package resolve
1616

1717
import (
1818
"context"
19-
"reflect"
2019
"testing"
2120

2221
"github.com/google/go-cmp/cmp"
@@ -130,8 +129,8 @@ func TestMavenVersion(t *testing.T) {
130129
VersionKey: vk,
131130
AttrSet: test.attr,
132131
}
133-
if !reflect.DeepEqual(got, want) {
134-
t.Errorf("Version(%v):\ngot: %v\nwant: %v\n", vk, got, want)
132+
if diff := cmp.Diff(got, want); diff != "" {
133+
t.Errorf("Version(%v):\n(-got, +want):\n%s", vk, diff)
135134
}
136135
}
137136
}
@@ -250,7 +249,7 @@ func TestMavenRequirements(t *testing.T) {
250249
},
251250
}
252251
if d := cmp.Diff(want, got); d != "" {
253-
t.Errorf("mavenRequirements:\n(- want, + got):\n%s", d)
252+
t.Errorf("mavenRequirements:\n(-want, +got):\n%s", d)
254253
}
255254
}
256255

0 commit comments

Comments
 (0)