From 02a8ab057b8a83695b57d8612908b9d39b63c645 Mon Sep 17 00:00:00 2001 From: Samuel Nelson Date: Sat, 10 Jan 2015 12:18:57 -0700 Subject: [PATCH] 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. --- assert/assertions.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/assert/assertions.go b/assert/assertions.go index 780b172..c94b30a 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -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