From 21382c1202e028111edce746cde2f40b4a4240eb Mon Sep 17 00:00:00 2001 From: James Bowes Date: Tue, 3 Jun 2014 19:01:48 -0300 Subject: [PATCH] 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. --- assert/assertions.go | 13 ++++++++++++- assert/assertions_test.go | 6 ++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/assert/assertions.go b/assert/assertions.go index 5cc17cb..3ea9204 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -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 } diff --git a/assert/assertions_test.go b/assert/assertions_test.go index ff735ff..75cbf48 100644 --- a/assert/assertions_test.go +++ b/assert/assertions_test.go @@ -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") + } }