mirror of https://github.com/stretchr/testify.git
Fix panic when comparing unequal maps.
The change in #94 resulted in using == to compare two values that might not be comparable. Hence, this resulted in a panic for situations like: ObjectsAreEqual(map[int]int{5: 10}, map[int]int{10: 20}) The fix is to use reflect.DeepEqual() instead.pull/103/head
parent
faedd6eb63
commit
38aecdc957
|
@ -37,10 +37,10 @@ func ObjectsAreEqual(expected, actual interface{}) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
actualType := reflect.TypeOf(actual)
|
actualType := reflect.TypeOf(actual)
|
||||||
if reflect.TypeOf(actual).ConvertibleTo(reflect.TypeOf(expected)) {
|
if actualType.ConvertibleTo(reflect.TypeOf(expected)) {
|
||||||
expectedValue := reflect.ValueOf(expected)
|
expectedValue := reflect.ValueOf(expected)
|
||||||
// Attempt comparison after type conversion
|
// Attempt comparison after type conversion
|
||||||
if actual == expectedValue.Convert(actualType).Interface() {
|
if reflect.DeepEqual(actual, expectedValue.Convert(actualType).Interface()) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,6 +40,9 @@ func TestObjectsAreEqual(t *testing.T) {
|
||||||
if !ObjectsAreEqual(nil, nil) {
|
if !ObjectsAreEqual(nil, nil) {
|
||||||
t.Error("objectsAreEqual should return true")
|
t.Error("objectsAreEqual should return true")
|
||||||
}
|
}
|
||||||
|
if ObjectsAreEqual(map[int]int{5: 10}, map[int]int{10: 20}) {
|
||||||
|
t.Error("objectsAreEqual should return false")
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue