Merge branch 'neilconway-map-non-equal-panic'

pull/113/head
Tyler Bunnell 2014-12-22 10:16:00 -07:00
commit 3f860d8bc6
2 changed files with 11 additions and 8 deletions

View File

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

View File

@ -40,6 +40,9 @@ func TestObjectsAreEqual(t *testing.T) {
if !ObjectsAreEqual(nil, nil) { if !ObjectsAreEqual(nil, nil) {
t.Error("objectsAreEqual should return true") t.Error("objectsAreEqual should return true")
} }
if ObjectsAreEqual(map[int]int{5: 10}, map[int]int{10: 20}) {
t.Error("objectsAreEqual should return false")
}
} }
@ -223,10 +226,10 @@ func TestContains(t *testing.T) {
mockT := new(testing.T) mockT := new(testing.T)
list := []string{"Foo", "Bar"} list := []string{"Foo", "Bar"}
complexList := []*A{ complexList := []*A{
&A{"b", "c"}, {"b", "c"},
&A{"d", "e"}, {"d", "e"},
&A{"g", "h"}, {"g", "h"},
&A{"j", "k"}, {"j", "k"},
} }
if !Contains(mockT, "Hello World", "Hello") { if !Contains(mockT, "Hello World", "Hello") {