+
Skip to content

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

Merged
merged 4 commits into from
Sep 25, 2016
Merged

Conversation

jveski
Copy link
Contributor

@jveski jveski commented Jul 7, 2016

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:

assert.Equal(t, int64(123), int32(123))
//
// Not equal: 123 (expected)
//       != 123 (actual)

After:

assert.Equal(t, int64(123), int32(123))
//
// Not equal: int64(123) (expected)
//      != int32(123) (actual)

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)".
Copy link
Member

@ernesto-jimenez ernesto-jimenez left a 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()]
}
Copy link
Member

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")
}
Copy link
Member

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...)
Copy link
Member

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.
@jveski
Copy link
Contributor Author

jveski commented Sep 25, 2016

@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. 👍

Copy link
Member

@ernesto-jimenez ernesto-jimenez left a 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.

@ernesto-jimenez ernesto-jimenez merged commit 69483b4 into stretchr:master Sep 25, 2016
jonnyreeves pushed a commit to jonnyreeves/testify that referenced this pull request Jan 5, 2017
Clarify assert.Equal failure message given mismatched numeric types
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载