mirror of https://github.com/stretchr/testify.git
Print types in failed numeric equality assertions
This addresses vague and misleading output when asserting on the equality of two values of differing numeric types. Example: assert.Equal(t, int64(123), int32(123)) Previously, the user would have received a vague message claiming that 123 != 123. Now the message will read "int64(123) != int32(123)".pull/327/head
parent
d77da356e5
commit
3bfb674961
|
@ -262,14 +262,53 @@ func Equal(t TestingT, expected, actual interface{}, msgAndArgs ...interface{})
|
|||
|
||||
if !ObjectsAreEqual(expected, actual) {
|
||||
diff := diff(expected, actual)
|
||||
return Fail(t, fmt.Sprintf("Not equal: %#v (expected)\n"+
|
||||
" != %#v (actual)%s", expected, actual, diff), msgAndArgs...)
|
||||
expected, actual = formatUnequalValues(expected, actual)
|
||||
return Fail(t, fmt.Sprintf("Not equal: %v (expected)\n"+
|
||||
" != %v (actual)%s", expected, actual, diff), msgAndArgs...)
|
||||
}
|
||||
|
||||
return true
|
||||
|
||||
}
|
||||
|
||||
// formatUnequalValues takes two values of arbitrary types and returns string
|
||||
// representations appropriate to be presented to the user.
|
||||
//
|
||||
// If the values are not of like type, the returned strings will be prefixed
|
||||
// with the type name, and the value will be enclosed in parenthesis similar
|
||||
// to a type conversion in the Go grammar.
|
||||
func formatUnequalValues(expected, actual interface{}) (e string, a string) {
|
||||
aType := reflect.TypeOf(expected)
|
||||
bType := reflect.TypeOf(actual)
|
||||
|
||||
if aType != bType && isNumericType(aType) && isNumericType(bType) {
|
||||
return fmt.Sprintf("%v(%#v)", aType, expected),
|
||||
fmt.Sprintf("%v(%#v)", bType, actual)
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%#v", expected),
|
||||
fmt.Sprintf("%#v", actual)
|
||||
}
|
||||
|
||||
var numericTypes = map[string]bool{
|
||||
"float32": true,
|
||||
"float64": true,
|
||||
"int": true,
|
||||
"int8": true,
|
||||
"int16": true,
|
||||
"int32": true,
|
||||
"int64": true,
|
||||
"uint": true,
|
||||
"uint8": true,
|
||||
"uint16": true,
|
||||
"uint32": true,
|
||||
"uint64": true,
|
||||
}
|
||||
|
||||
func isNumericType(t reflect.Type) bool {
|
||||
return numericTypes[t.String()]
|
||||
}
|
||||
|
||||
// EqualValues asserts that two objects are equal or convertable to the same types
|
||||
// and equal.
|
||||
//
|
||||
|
|
|
@ -195,6 +195,20 @@ func TestEqual(t *testing.T) {
|
|||
|
||||
}
|
||||
|
||||
func TestFormatUnequalValues(t *testing.T) {
|
||||
expected, actual := formatUnequalValues("foo", "bar")
|
||||
Equal(t, `"foo"`, expected, "value should not include type")
|
||||
Equal(t, `"bar"`, actual, "value should not include type")
|
||||
|
||||
expected, actual = formatUnequalValues(123, 123)
|
||||
Equal(t, `123`, expected, "value should not include type")
|
||||
Equal(t, `123`, actual, "value should not include type")
|
||||
|
||||
expected, actual = formatUnequalValues(int64(123), int32(123))
|
||||
Equal(t, `int64(123)`, expected, "value should include type")
|
||||
Equal(t, `int32(123)`, actual, "value should include type")
|
||||
}
|
||||
|
||||
func TestNotNil(t *testing.T) {
|
||||
|
||||
mockT := new(testing.T)
|
||||
|
|
Loading…
Reference in New Issue