-
Notifications
You must be signed in to change notification settings - Fork 8
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
empty: add Equal empty string #119
Comments
+ string conversion |
+ len support assert.Empty(t, len(whateverStr)) |
@ccoVeille, hi! This is thin ice. Looks like if we will support
But I am not sure about 2), that this is the best/common practice. |
I updated issue description with both of them |
I'm OK with letting zero typed outside the scope of About the fact to handle other types that support len. I would say, let's start with string, we could add the other later. What do you think? There is no problem to iterate. My remark is based on the fact that ot would require to check if there are real life examples where empty could be used with Len and other types that are compatible with len. You somehow urged me not to implement, support strange use case such as zero and empty with compare and bool compare checkers. While they were valid wrong usages, you told me to forget them because they are likely to never have been used. I found wrong usages of asserters on string that could be replaced by Empty, you agree to catch them. let's support them. Other can be added later, if needed. |
Related to Antonboom#119
I found new use cases to be added to empty https://github.com/search?q=language%3Ago+%22assert.True%28t%2C+0+%3D%3D+len%28%22&type=code assert.True(t, 0 == len(read.RelatedSlice)) https://github.com/search?q=language%3Ago+%22assert.True%28t%2C+0+%3C+len%28%22&type=code assert.True(t, 0 < len(notify.Hash())) Let me know if you want me to add them to #129 |
I guess this is already covered by chain |
Agreed about assert.Empty(t, string(whatever)) -> assert.Empty But have doubts about assert.Equal(t, "", whatever) -> assert.Empty
assert.Equal(t, "", string(whatever)) -> assert.Empty Do you have some proofs of this as a best practice? I am not sure. Examples from your search: assert.Nil(t, err, "send %v", err)
assert.Equal(t, 0, int(ret.ExitCode))
assert.Equal(t, 0, int(ret.GasUsed))
assert.Equal(t, "", string(ret.ReturnData)) // Why do require Empty here? assert.EqualValues(t, 0, nextCreateAt, "should have finished iterating through drafts")
assert.Equal(t, "", nextUserId, "should have finished iterating through drafts") // Why do require Empty here? |
Not exactly var arr []string
assert.True(t, 0 < len(arr)) becomes var arr []string
assert.Positive(t, len(l)) But this one won't be detected, I agree with you, it could/should become assert.NotEmpty(t, l) So it's a new use case to add to
|
Maybe we are facing the same debate as should we check an empty string with: if len(str) == 0 {
} or if str == "" {
} This is defintely a debate. But here we have testify, it exists and provides asserters, let's consider them
Let's put aside Zero, we already talked about it multiple times this one is not adapted. Empty exists, and it's documentation talk about:
Let's take each example described one by one:
So except channel, map, slices would be fine. Also a string is a slice of characters, so somehow a slice of runes/bytes in Go When we are facing this Even more, if you look about the usage of require.NotEqual(t, "", link3, "Link should not be empty") assert.Equal(t, "", f.Stderr.String(), "stderr should be empty") require.Equalf(t, "", path, "Expected empty path but got %s", path) assert.Equalf(t, "", errorAt, "Expected decodeMetadataValueHeader to return empty errorAt, not %v", errorAt) require.NotEqualf(t, "", line, "%s must not be empty", path) For me, we should add and by extension |
BTW, we do not detect error used with empty, right now var err error
assert.Empty(t, err) We should recommend using and this pattern is widely used https://github.com/search?q=language%3Ago+%22assert.Empty%28t%2C+err%22&type=code Same for var err error
assert.Zero(t, err) https://github.com/search?q=language%3Ago+%22assert.Zero%28t%2C+err%29%22&type=code These two are candidates for |
To add to this discussion, I just stumbled over this code in our tests: assert.NotEqual(t, ``, somevar) and I think, the linter should suggest assert.NotEmpty(t, somevar) in this case. |
Indeed this pattern is present https://github.com/search?q=%22assert.Equal%28t%2C+%60%60%2C%22&type=code I edited the issue description to support it. And I also added https://github.com/search?q=%22assert.Equal%28t%2C+%60%60%2C+string%28%22&type=code (I'm unsure if it would differ in term of detection, but at least we will add it to the unit tests) |
This pattern is common usef
And it could be replaced
Examples:
https://github.com/search?q=language%3Ago+%22assert.Equal%28t%2C+%5C%22%5C%22%22&type=code
https://github.com/search?q=language%3Ago+%22assert.Equal%28t%2C+%5C%22%5C%22%2C+string%22&type=code
https://github.com/search?q=language%3Ago+%22assert.Empty%28t%2C+string%28%22&type=code
https://github.com/search?q=%22assert.Equal%28t%2C+%60%60%2C%22&type=code
https://github.com/search?q=%22assert.Equal%28t%2C+%60%60%2C+string%28%22&type=code
The text was updated successfully, but these errors were encountered: