mirror of https://github.com/stretchr/testify.git
Attempt type conversion in assert.Equal
If the two values being tested are of compatible types, convert one value to the other's type before comparing them. This allows `assert.Equal(int64(123), uint64(123))` to pass.pull/54/head
parent
410219ae82
commit
21382c1202
|
@ -25,11 +25,22 @@ type Comparison func() (success bool)
|
|||
// This function does no assertion of any kind.
|
||||
func ObjectsAreEqual(expected, actual interface{}) bool {
|
||||
|
||||
if expected == nil || actual == nil {
|
||||
return expected == actual
|
||||
}
|
||||
|
||||
if reflect.DeepEqual(expected, actual) {
|
||||
return true
|
||||
}
|
||||
|
||||
if reflect.ValueOf(expected) == reflect.ValueOf(actual) {
|
||||
expectedValue := reflect.ValueOf(expected)
|
||||
actualValue := reflect.ValueOf(actual)
|
||||
if expectedValue == actualValue {
|
||||
return true
|
||||
}
|
||||
|
||||
// Attempt comparison after type conversion
|
||||
if actualValue.Type().ConvertibleTo(expectedValue.Type()) && expectedValue == actualValue.Convert(expectedValue.Type()) {
|
||||
return true
|
||||
}
|
||||
|
||||
|
|
|
@ -87,6 +87,12 @@ func TestEqual(t *testing.T) {
|
|||
if !Equal(mockT, nil, nil) {
|
||||
t.Error("Equal should return true")
|
||||
}
|
||||
if !Equal(mockT, int32(123), int64(123)) {
|
||||
t.Error("Equal should return true")
|
||||
}
|
||||
if !Equal(mockT, int64(123), uint64(123)) {
|
||||
t.Error("Equal should return true")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue