From 38aecdc9572fcd0dd085e1616bd078a00da2f8e0 Mon Sep 17 00:00:00 2001 From: Neil Conway Date: Mon, 24 Nov 2014 16:11:51 -0800 Subject: [PATCH] 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. --- assert/assertions.go | 4 ++-- assert/assertions_test.go | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/assert/assertions.go b/assert/assertions.go index a7a24e7..780b172 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -37,10 +37,10 @@ func ObjectsAreEqual(expected, actual interface{}) bool { } actualType := reflect.TypeOf(actual) - if reflect.TypeOf(actual).ConvertibleTo(reflect.TypeOf(expected)) { + if actualType.ConvertibleTo(reflect.TypeOf(expected)) { expectedValue := reflect.ValueOf(expected) // Attempt comparison after type conversion - if actual == expectedValue.Convert(actualType).Interface() { + if reflect.DeepEqual(actual, expectedValue.Convert(actualType).Interface()) { return true } } diff --git a/assert/assertions_test.go b/assert/assertions_test.go index bb63be2..1a63b41 100644 --- a/assert/assertions_test.go +++ b/assert/assertions_test.go @@ -40,6 +40,9 @@ func TestObjectsAreEqual(t *testing.T) { if !ObjectsAreEqual(nil, nil) { t.Error("objectsAreEqual should return true") } + if ObjectsAreEqual(map[int]int{5: 10}, map[int]int{10: 20}) { + t.Error("objectsAreEqual should return false") + } }