Skip to content
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

incorrect-assert: check EqualValues vs bigger type #88

Open
ccoVeille opened this issue May 11, 2024 · 5 comments
Open

incorrect-assert: check EqualValues vs bigger type #88

ccoVeille opened this issue May 11, 2024 · 5 comments
Labels
enhancement New feature or request

Comments

@ccoVeille
Copy link
Contributor

Following stretchr/testify#1593 opened by Braken

Question for @Antonboom and @brackendawson:

Do you think there could have a need to detect and report something with testifylint?

I only surfaced the issue reported, I'm unsure I understood everything. But I wanted to raise the point here in case it could be a good thing to detect

@Antonboom Antonboom added the enhancement New feature or request label May 11, 2024
@brackendawson
Copy link

The intention of the assertion is to compare the values of convertible types, it's an abomination of an assertion and an affront to the concept of "Equal". It's also rife for unintended errors with floating point precision and overflows. The best thing a linter could do is to tell you not to use it, here's a few examples:

Unnecessary use of EqualValues 1

expected := int(7)
actual := GiveMeIntSeven()
assert.EqualValues(t, expected, actual)

✅ Just use Equal

expected := int(7)
actual := GiveMeIntSeven()
assert.Equal(t, expected, actual)

Unnecessary use of EqualValues 2

expected := uint(7)
actual := GiveMeIntSeven()
assert.EqualValues(t, expected, actual)

✅ Change type of expectation and use Equal

expected := int(7)
actual := GiveMeIntSeven()
assert.Equal(t, expected, actual)

Avoid this false positive

⚠️ This person isn't testing for state, you can lead a horse to water but you can't make it drink.

actual1, actual2 := GiveMeTwoDifferentTypes()
assert.EqualValues(t, actual1, actual2)

@ccoVeille
Copy link
Contributor Author

I'm fine with saying don't use it.

But what would be the legitimate usage of EqualValues ?

I assume that even if most of its usage is unnecessary (I loved the concept of being an affront to Equal 🤣), there are some use case where it would make sense, otherwise it won't have been created.

@brackendawson
Copy link

brackendawson commented May 31, 2024

It would only be useful to assert that two values of different types are convertible and equal after conversion (which is sensitive to the direction of the conversion), and where you didn't know what either of the actual values would be when you wrote the test. So basically someone who isn't "testing for state" and instead has a "dynamic" test (shudders).

@brackendawson
Copy link

History: There was a bug where Equal had the behaviour of EqualValues, that was fixed but some people wanted their "SemiKinaEqual" assertion back: stretchr/testify#129

As I said, an affront to Equal.

@Antonboom Antonboom changed the title Feature: EqualValues vs bigger type incorrect-assert: check EqualValues vs bigger type Jun 5, 2024
@ccoVeille
Copy link
Contributor Author

Thanks @brackendawson for giving context, I appreciate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants