Skip to content

Commit c3a5161

Browse files
committed
Add symmetric tests instead of duplicating them with swapped arguments.
This is to ensure that the function being tested is symmetric in its arguments, which is a common expectation for equality checks.
1 parent 038b8ed commit c3a5161

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

assert/assertions_test.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ func TestObjectsAreEqual(t *testing.T) {
123123
{time.Now, time.Now, false},
124124
{func() {}, func() {}, false},
125125
{uint32(10), int32(10), false},
126+
{math.NaN(), math.NaN(), false},
127+
{math.Inf(1), math.Inf(1), true},
128+
{math.Inf(-1), math.Inf(-1), true},
129+
{math.Inf(1), math.Inf(-1), false},
130+
{math.Copysign(0, -1), 0.0, true}, // -0 should compare equal to 0
126131
}
127132

128133
for _, c := range cases {
@@ -132,6 +137,10 @@ func TestObjectsAreEqual(t *testing.T) {
132137
if res != c.result {
133138
t.Errorf("ObjectsAreEqual(%#v, %#v) should return %#v", c.expected, c.actual, c.result)
134139
}
140+
141+
if ObjectsAreEqual(c.actual, c.expected) != res {
142+
t.Errorf("ObjectsAreEqual should be symmetric: ObjectsAreEqual(%#v, %#v) should return the same as ObjectsAreEqual(%#v, %#v)", c.expected, c.actual, c.actual, c.expected)
143+
}
135144
})
136145
}
137146
}
@@ -148,20 +157,15 @@ func TestObjectsAreEqualValues(t *testing.T) {
148157
}{
149158
{uint32(10), int32(10), true},
150159
{0, nil, false},
151-
{nil, 0, false},
152160
{now, now.In(time.Local), false}, // should not be time zone independent
153161
{int(270), int8(14), false}, // should handle overflow/underflow
154-
{int8(14), int(270), false},
155162
{[]int{270, 270}, []int8{14, 14}, false},
156163
{complex128(1e+100 + 1e+100i), complex64(complex(math.Inf(0), math.Inf(0))), false},
157164
{complex64(complex(math.Inf(0), math.Inf(0))), complex128(1e+100 + 1e+100i), false},
158165
{complex128(1e+100 + 1e+100i), 270, false},
159166
{270, complex128(1e+100 + 1e+100i), false},
160167
{complex128(1e+100 + 1e+100i), 3.14, false},
161-
{3.14, complex128(1e+100 + 1e+100i), false},
162168
{complex128(1e+10 + 1e+10i), complex64(1e+10 + 1e+10i), true},
163-
{complex64(1e+10 + 1e+10i), complex128(1e+10 + 1e+10i), true},
164-
{float32(10.1), float64(10.1), true},
165169
{float64(10.1), float32(10.1), true},
166170
{float32(10.123456), float64(10.12345600), true},
167171
{float32(10.123456), float64(10.12345678), false},
@@ -175,6 +179,10 @@ func TestObjectsAreEqualValues(t *testing.T) {
175179
if res != c.result {
176180
t.Errorf("ObjectsAreEqualValues(%#v, %#v) should return %#v", c.expected, c.actual, c.result)
177181
}
182+
183+
if ObjectsAreEqualValues(c.actual, c.expected) != res {
184+
t.Errorf("ObjectsAreEqualValues should be symmetric: ObjectsAreEqualValues(%#v, %#v) should return the same as ObjectsAreEqualValues(%#v, %#v)", c.expected, c.actual, c.actual, c.expected)
185+
}
178186
})
179187
}
180188
}

0 commit comments

Comments
 (0)