-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Clarify assert.Equal failure message given mismatched numeric types #327
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
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)".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jveski thanks, and sorry it has taken a while to reply. I've had some crazy busy time.
I've added some comments to make the code a bit more clear and check how structs are formatted too.
|
||
func isNumericType(t reflect.Type) bool { | ||
return numericTypes[t.String()] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of this, you can do:
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
}
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") | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Include a check for a struct.
" != %#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...) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since expected
and actual
are going to be strings, we can use %s
in Sprintf
Since the values are already strings, there is no reason to use %v.
This expands the TestFormatUnequalValues test case to cover the behavior of the function when provided two struct type'd values.
@ernesto-jimenez no worries on the delay and I just pushed three commits with the changes you requested. btw thanks for maintaining this lib. It's really useful, and I know being a maintainer of projects like this can be rough, so thanks. 👍 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the PR and the nice words! :)
I'll be merging it now.
Clarify assert.Equal failure message given mismatched numeric types
Resolves #155.
This change causes type names to be printed when the failure message would otherwise be ambiguous. Specifically, it addresses equality assertions on numeric values of unlike types.
Example
Before:
After: