Skip to content

Commit

Permalink
Merge pull request #94 from neilconway/object-equality-golang-1.4
Browse files Browse the repository at this point in the history
Avoid relying on undefined behavior in assert.ObjectsAreEqual().
  • Loading branch information
Mat Ryer committed Nov 24, 2014
2 parents 8ce79b9 + 49c5c6c commit faedd6e
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions assert/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,13 @@ func ObjectsAreEqual(expected, actual interface{}) bool {
return true
}

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
actualType := reflect.TypeOf(actual)
if reflect.TypeOf(actual).ConvertibleTo(reflect.TypeOf(expected)) {
expectedValue := reflect.ValueOf(expected)
// Attempt comparison after type conversion
if actual == expectedValue.Convert(actualType).Interface() {
return true
}
}

// Last ditch effort
Expand Down

0 comments on commit faedd6e

Please sign in to comment.