Skip to content

Commit

Permalink
Merge pull request #327 from jveski/issue-155
Browse files Browse the repository at this point in the history
Clarify assert.Equal failure message given mismatched numeric types
  • Loading branch information
ernesto-jimenez committed Sep 25, 2016
2 parents 0858597 + d2b5f58 commit 69483b4
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
37 changes: 35 additions & 2 deletions assert/assertions.go
Expand Up @@ -278,14 +278,47 @@ 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: %s (expected)\n"+
" != %s (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)
}

func isNumericType(t reflect.Type) bool {
switch t.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return true
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return true
case reflect.Float32, reflect.Float64:
return true
}

return false
}

// EqualValues asserts that two objects are equal or convertable to the same types
// and equal.
//
Expand Down
22 changes: 22 additions & 0 deletions assert/assertions_test.go
Expand Up @@ -195,6 +195,28 @@ 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")

type testStructType struct {
Val string
}

expected, actual = formatUnequalValues(&testStructType{Val: "test"}, &testStructType{Val: "test"})
Equal(t, `&assert.testStructType{Val:"test"}`, expected, "value should not include type annotation")
Equal(t, `&assert.testStructType{Val:"test"}`, actual, "value should not include type annotation")
}

func TestNotNil(t *testing.T) {

mockT := new(testing.T)
Expand Down

0 comments on commit 69483b4

Please sign in to comment.