Skip to content

Commit

Permalink
empty: detect more cases
Browse files Browse the repository at this point in the history
Related to Antonboom#119
  • Loading branch information
ccoVeille committed Jun 18, 2024
1 parent 679ad66 commit b4aef09
Show file tree
Hide file tree
Showing 7 changed files with 279 additions and 54 deletions.
48 changes: 26 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,36 +219,40 @@ due to the inappropriate recursive nature of `assert.Equal` (based on

```go
assert.Len(t, arr, 0)
assert.Equal(t, 0, len(arr))
assert.EqualValues(t, 0, len(arr))
assert.Exactly(t, 0, len(arr))
assert.LessOrEqual(t, len(arr), 0)
assert.GreaterOrEqual(t, 0, len(arr))
assert.Less(t, len(arr), 0)
assert.Greater(t, 0, len(arr))
assert.Less(t, len(arr), 1)
assert.Greater(t, 1, len(arr))
assert.Zero(t, len(arr))
assert.Empty(t, len(arr))

assert.NotEqual(t, 0, len(arr))
assert.NotEqualValues(t, 0, len(arr))
assert.Less(t, 0, len(arr))
assert.Greater(t, len(arr), 0)
assert.Positive(t, len(arr))
assert.NotZero(t, len(arr))
assert.NotEmpty(t, len(arr))
assert.Len(t, a, 0)
assert.Equal(t, 0, len(a))
assert.EqualValues(t, 0, len(a))
assert.Exactly(t, 0, len(a))
assert.LessOrEqual(t, len(a), 0)
assert.GreaterOrEqual(t, 0, len(a))
assert.Less(t, len(a), 0)
assert.Greater(t, 0, len(a))
assert.Less(t, len(a), 1)
assert.Greater(t, 1, len(a))
assert.Zero(t, len(a))
assert.Empty(t, len(a))
assert.Empty(t, string(a))

assert.NotEqual(t, 0, len(a))
assert.NotEqualValues(t, 0, len(a))
assert.Less(t, 0, len(a))
assert.Greater(t, len(a), 0)
assert.Positive(t, len(a))
assert.NotZero(t, len(a))
assert.NotEmpty(t, len(a))
assert.NotEmpty(t, string(a))

assert.Empty(t, arr)
assert.NotEmpty(t, err)
assert.Empty(t, a)
assert.NotEmpty(t, a)
```

**Autofix**: true. <br>
**Enabled by default**: true. <br>
**Reason**: More appropriate `testify` API with clearer failure message.

String conversion (like `assert.Len(t, string(b), 0)`) are also supported.

---

### error-is-as
Expand Down
64 changes: 62 additions & 2 deletions analyzer/testdata/src/checkers-default/empty/empty_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 62 additions & 2 deletions analyzer/testdata/src/checkers-default/empty/empty_test.go.golden
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import (
)

func TestEmptyChecker(t *testing.T) {
var elems []string
var (
elems []any
str string
b []byte
)

// assert.Empty cases.
{
Expand Down Expand Up @@ -40,6 +44,30 @@ func TestEmptyChecker(t *testing.T) {
assert.Emptyf(t, elems, "msg with args %d %s", 42, "42") // want "empty: use assert\\.Emptyf"
assert.Empty(t, elems) // want "empty: use assert\\.Empty"
assert.Emptyf(t, elems, "msg with args %d %s", 42, "42") // want "empty: use assert\\.Emptyf"
assert.Empty(t, b) // want "empty: use assert\\.Empty"
assert.Emptyf(t, b, "msg with args %d %s", 42, "42") // want "empty: use assert\\.Emptyf"
assert.Empty(t, b) // want "empty: use assert\\.Empty"
assert.Emptyf(t, b, "msg with args %d %s", 42, "42") // want "empty: use assert\\.Emptyf"
assert.Empty(t, b) // want "empty: use assert\\.Empty"
assert.Emptyf(t, b, "msg with args %d %s", 42, "42") // want "empty: use assert\\.Emptyf"
assert.Empty(t, str) // want "empty: use assert\\.Empty"
assert.Emptyf(t, str, "msg with args %d %s", 42, "42") // want "empty: use assert\\.Emptyf"
assert.Empty(t, b) // want "empty: use assert\\.Empty"
assert.Emptyf(t, b, "msg with args %d %s", 42, "42") // want "empty: use assert\\.Emptyf"
assert.Empty(t, b) // want "empty: use assert\\.Empty"
assert.Emptyf(t, b, "msg with args %d %s", 42, "42") // want "empty: use assert\\.Emptyf"
assert.Empty(t, str) // want "empty: use assert\\.Empty"
assert.Emptyf(t, str, "msg with args %d %s", 42, "42") // want "empty: use assert\\.Emptyf"
assert.Empty(t, b) // want "empty: use assert\\.Empty"
assert.Emptyf(t, b, "msg with args %d %s", 42, "42") // want "empty: use assert\\.Emptyf"
assert.Empty(t, str) // want "empty: use assert\\.Empty"
assert.Emptyf(t, str, "msg with args %d %s", 42, "42") // want "empty: use assert\\.Emptyf"
assert.Empty(t, b) // want "empty: use assert\\.Empty"
assert.Emptyf(t, b, "msg with args %d %s", 42, "42") // want "empty: use assert\\.Emptyf"
assert.Empty(t, str) // want "empty: use assert\\.Empty"
assert.Emptyf(t, str, "msg with args %d %s", 42, "42") // want "empty: use assert\\.Emptyf"
assert.Empty(t, b) // want "empty: use assert\\.Empty"
assert.Emptyf(t, b, "msg with args %d %s", 42, "42") // want "empty: use assert\\.Emptyf"
assert.Empty(t, elems) // want "empty: use assert\\.Empty"
assert.Emptyf(t, elems, "msg with args %d %s", 42, "42") // want "empty: use assert\\.Emptyf"
assert.Empty(t, elems) // want "empty: use assert\\.Empty"
Expand Down Expand Up @@ -75,6 +103,24 @@ func TestEmptyChecker(t *testing.T) {
assert.NotEmptyf(t, elems, "msg with args %d %s", 42, "42") // want "empty: use assert\\.NotEmptyf"
assert.NotEmpty(t, elems) // want "empty: use assert\\.NotEmpty"
assert.NotEmptyf(t, elems, "msg with args %d %s", 42, "42") // want "empty: use assert\\.NotEmptyf"
assert.NotEmpty(t, str) // want "empty: use assert\\.NotEmpty"
assert.NotEmptyf(t, str, "msg with args %d %s", 42, "42") // want "empty: use assert\\.NotEmptyf"
assert.NotEmpty(t, str) // want "empty: use assert\\.NotEmpty"
assert.NotEmptyf(t, str, "msg with args %d %s", 42, "42") // want "empty: use assert\\.NotEmptyf"
assert.NotEmpty(t, b) // want "empty: use assert\\.NotEmpty"
assert.NotEmptyf(t, b, "msg with args %d %s", 42, "42") // want "empty: use assert\\.NotEmptyf"
assert.NotEmpty(t, b) // want "empty: use assert\\.NotEmpty"
assert.NotEmptyf(t, b, "msg with args %d %s", 42, "42") // want "empty: use assert\\.NotEmptyf"
assert.NotEmpty(t, b) // want "empty: use assert\\.NotEmpty"
assert.NotEmptyf(t, b, "msg with args %d %s", 42, "42") // want "empty: use assert\\.NotEmptyf"
assert.NotEmpty(t, str) // want "empty: use assert\\.NotEmpty"
assert.NotEmptyf(t, str, "msg with args %d %s", 42, "42") // want "empty: use assert\\.NotEmptyf"
assert.NotEmpty(t, b) // want "empty: use assert\\.NotEmpty"
assert.NotEmptyf(t, b, "msg with args %d %s", 42, "42") // want "empty: use assert\\.NotEmptyf"
assert.NotEmpty(t, str) // want "empty: use assert\\.NotEmpty"
assert.NotEmptyf(t, str, "msg with args %d %s", 42, "42") // want "empty: use assert\\.NotEmptyf"
assert.NotEmpty(t, b) // want "empty: use assert\\.NotEmpty"
assert.NotEmptyf(t, b, "msg with args %d %s", 42, "42") // want "empty: use assert\\.NotEmptyf"

// Valid.
assert.NotEmpty(t, elems)
Expand All @@ -84,24 +130,32 @@ func TestEmptyChecker(t *testing.T) {

func TestEmptyChecker_LenVarIndependence(t *testing.T) {
var (
elems []any
arr [0]int
arrPtr *[0]int
sl []int
mp map[int]int
str string
b []byte
ch chan int
)

assert.Empty(t, elems) // want "empty: use assert\\.Empty"
assert.Empty(t, arr) // want "empty: use assert\\.Empty"
assert.Empty(t, arrPtr) // want "empty: use assert\\.Empty"
assert.Empty(t, sl) // want "empty: use assert\\.Empty"
assert.Empty(t, mp) // want "empty: use assert\\.Empty"
assert.Empty(t, str) // want "empty: use assert\\.Empty"
assert.Empty(t, ch) // want "empty: use assert\\.Empty"
assert.Empty(t, b) // want "empty: use assert\\.Empty"
}

func TestEmptyChecker_Ignored(t *testing.T) {
var elems []string
var (
elems []any
str string
b []byte
)

assert.Len(t, elems, len(elems))
assert.Lenf(t, elems, len(elems), "msg with args %d %s", 42, "42")
Expand Down Expand Up @@ -147,6 +201,12 @@ func TestEmptyChecker_Ignored(t *testing.T) {
assert.LessOrEqualf(t, len(elems), 2, "msg with args %d %s", 42, "42")
assert.LessOrEqual(t, 2, len(elems))
assert.LessOrEqualf(t, 2, len(elems), "msg with args %d %s", 42, "42")
assert.Zero(t, elems)
assert.Zerof(t, elems, "msg with args %d %s", 42, "42")
assert.Zero(t, str)
assert.Zerof(t, str, "msg with args %d %s", 42, "42")
assert.Zero(t, string(b))
assert.Zerof(t, string(b), "msg with args %d %s", 42, "42")
assert.Greater(t, len(elems), 1)
assert.Greaterf(t, len(elems), 1, "msg with args %d %s", 42, "42")
assert.Less(t, 1, len(elems))
Expand Down
Loading

0 comments on commit b4aef09

Please sign in to comment.