Reverse ConvertibleTo check to avoid panic

in `ObjectsAreEqual`, `expectedValue.Convert(actualType)` was being called when
`actualType.ConvertibleTo(reflect.TypeOf(expected))` was true.  This was a problem
for situations such as when expected was an int and actual was a string, since
ints are `ConvertibleTo` strings, but the reverse is not true.

Changing the ConvertibleTo check to `expectedValue.Type().ConvertibleTo(actualType)`
solves the issue.
pull/115/head
Samuel Nelson 2015-01-10 12:18:57 -07:00
parent 85138dbfa5
commit 02a8ab057b
1 changed files with 2 additions and 2 deletions

View File

@ -37,8 +37,8 @@ func ObjectsAreEqual(expected, actual interface{}) bool {
}
actualType := reflect.TypeOf(actual)
if actualType.ConvertibleTo(reflect.TypeOf(expected)) {
expectedValue := reflect.ValueOf(expected)
expectedValue := reflect.ValueOf(expected)
if expectedValue.Type().ConvertibleTo(actualType) {
// Attempt comparison after type conversion
if reflect.DeepEqual(actual, expectedValue.Convert(actualType).Interface()) {
return true