Skip to content

Commit

Permalink
len: support len-len and value-len cases (#204)
Browse files Browse the repository at this point in the history
* expected-actual: support len

* len: support len-len and value-len cases

* len: add disclaimer about not-len-first assetions

* len: add TestLenInterface as a proof

* len: support []byte/string/raw.Message type conversions

* len: empty: add TestBytesReadability

* Revert "len: support []byte/string/raw.Message type conversions"

This reverts commit e2e1092.

* debug: fix typo
  • Loading branch information
Antonboom authored Nov 17, 2024
1 parent 639ce85 commit ea24c79
Show file tree
Hide file tree
Showing 17 changed files with 352 additions and 162 deletions.
36 changes: 31 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,8 @@ assert.Error(t, err)
```go
assert.Equal(t, result, expected)
assert.Equal(t, result, len(expected))
assert.Equal(t, len(resultFields), len(expectedFields))
assert.EqualExportedValues(t, resultObj, User{Name: "Rob"})
assert.EqualValues(t, result, 42)
assert.Exactly(t, result, int64(42))
Expand All @@ -389,6 +391,8 @@ assert.YAMLEq(t, result, "version: '3'")

assert.Equal(t, expected, result)
assert.Equal(t, len(expected), result)
assert.Equal(t, len(expectedFields), len(resultFields))
assert.EqualExportedValues(t, User{Name: "Rob"}, resultObj)
assert.EqualValues(t, 42, result)
// And so on...
Expand Down Expand Up @@ -618,19 +622,41 @@ P.S. Look at [testify's issue](https://github.com/stretchr/testify/issues/772),

```go
assert.Equal(t, 3, len(arr))
assert.EqualValues(t, 3, len(arr))
assert.Exactly(t, 3, len(arr))
assert.True(t, len(arr) == 3)
assert.Equal(t, 42, len(arr))
assert.Equal(t, len(arr), 42)
assert.EqualValues(t, 42, len(arr))
assert.EqualValues(t, len(arr), 42)
assert.Exactly(t, 42, len(arr))
assert.Exactly(t, len(arr), 42)
assert.True(t, 42 == len(arr))
assert.True(t, len(arr) == 42)

assert.Equal(t, value, len(arr))
assert.EqualValues(t, value, len(arr))
assert.Exactly(t, value, len(arr))
assert.True(t, len(arr) == value)

assert.Equal(t, len(expArr), len(arr))
assert.EqualValues(t, len(expArr), len(arr))
assert.Exactly(t, len(expArr), len(arr))
assert.True(t, len(arr) == len(expArr))

assert.Len(t, arr, 3)
assert.Len(t, arr, 42)
assert.Len(t, arr, value)
assert.Len(t, arr, len(expArr))
```

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

> [!CAUTION]
> The checker ignores assertions in which length checking is not a priority, e.g.
> ```go
> assert.Equal(t, len(arr), value)
> ```
---
### negative-positive
Expand Down
4 changes: 2 additions & 2 deletions analyzer/checkers_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ func Test_newCheckers(t *testing.T) {
checkers.NewFloatCompare(),
checkers.NewBoolCompare(),
checkers.NewEmpty(),
checkers.NewLen(),
checkers.NewNegativePositive(),
checkers.NewCompares(),
checkers.NewContains(),
Expand All @@ -27,6 +26,7 @@ func Test_newCheckers(t *testing.T) {
checkers.NewErrorIsAs(),
checkers.NewEncodedCompare(),
checkers.NewExpectedActual(),
checkers.NewLen(),
checkers.NewRegexp(),
checkers.NewSuiteExtraAssertCall(),
checkers.NewSuiteDontUsePkg(),
Expand All @@ -37,7 +37,6 @@ func Test_newCheckers(t *testing.T) {
checkers.NewFloatCompare(),
checkers.NewBoolCompare(),
checkers.NewEmpty(),
checkers.NewLen(),
checkers.NewNegativePositive(),
checkers.NewCompares(),
checkers.NewContains(),
Expand All @@ -46,6 +45,7 @@ func Test_newCheckers(t *testing.T) {
checkers.NewErrorIsAs(),
checkers.NewEncodedCompare(),
checkers.NewExpectedActual(),
checkers.NewLen(),
checkers.NewRegexp(),
checkers.NewSuiteExtraAssertCall(),
checkers.NewSuiteDontUsePkg(),
Expand Down

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

Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func TestExpectedActualChecker(t *testing.T) {
expectedVal := func() any { return nil }
var expectedObj struct{ Val int }
var expectedObjPtr = &expectedObj
var expectedFields []string

var result any

Expand Down Expand Up @@ -81,6 +82,8 @@ func TestExpectedActualChecker(t *testing.T) {
assert.Equalf(t, *(tt.expPtr()), result, "msg with args %d %s", 42, "42") // want "expected-actual: need to reverse actual and expected values"
assert.Equal(t, ttPtr.expected, result) // want "expected-actual: need to reverse actual and expected values"
assert.Equalf(t, ttPtr.expected, result, "msg with args %d %s", 42, "42") // want "expected-actual: need to reverse actual and expected values"
assert.Equal(t, len(expectedFields), result) // want "expected-actual: need to reverse actual and expected values"
assert.Equalf(t, len(expectedFields), result, "msg with args %d %s", 42, "42") // want "expected-actual: need to reverse actual and expected values"
assert.Equal(t, 42, result) // want "expected-actual: need to reverse actual and expected values"
assert.Equalf(t, 42, result, "msg with args %d %s", 42, "42") // want "expected-actual: need to reverse actual and expected values"
assert.Equal(t, 3.14, result) // want "expected-actual: need to reverse actual and expected values"
Expand Down Expand Up @@ -140,6 +143,8 @@ func TestExpectedActualChecker(t *testing.T) {
assert.NotEqualf(t, *(tt.expPtr()), result, "msg with args %d %s", 42, "42") // want "expected-actual: need to reverse actual and expected values"
assert.NotEqual(t, ttPtr.expected, result) // want "expected-actual: need to reverse actual and expected values"
assert.NotEqualf(t, ttPtr.expected, result, "msg with args %d %s", 42, "42") // want "expected-actual: need to reverse actual and expected values"
assert.NotEqual(t, len(expectedFields), result) // want "expected-actual: need to reverse actual and expected values"
assert.NotEqualf(t, len(expectedFields), result, "msg with args %d %s", 42, "42") // want "expected-actual: need to reverse actual and expected values"
assert.NotEqual(t, 42, result) // want "expected-actual: need to reverse actual and expected values"
assert.NotEqualf(t, 42, result, "msg with args %d %s", 42, "42") // want "expected-actual: need to reverse actual and expected values"
assert.NotEqual(t, 3.14, result) // want "expected-actual: need to reverse actual and expected values"
Expand Down Expand Up @@ -202,6 +207,8 @@ func TestExpectedActualChecker(t *testing.T) {
assert.Equalf(t, *(tt.expPtr()), result, "msg with args %d %s", 42, "42")
assert.Equal(t, ttPtr.expected, result)
assert.Equalf(t, ttPtr.expected, result, "msg with args %d %s", 42, "42")
assert.Equal(t, len(expectedFields), result)
assert.Equalf(t, len(expectedFields), result, "msg with args %d %s", 42, "42")
assert.Equal(t, 42, result)
assert.Equalf(t, 42, result, "msg with args %d %s", 42, "42")
assert.Equal(t, 3.14, result)
Expand Down Expand Up @@ -261,6 +268,8 @@ func TestExpectedActualChecker(t *testing.T) {
assert.NotEqualf(t, *(tt.expPtr()), result, "msg with args %d %s", 42, "42")
assert.NotEqual(t, ttPtr.expected, result)
assert.NotEqualf(t, ttPtr.expected, result, "msg with args %d %s", 42, "42")
assert.NotEqual(t, len(expectedFields), result)
assert.NotEqualf(t, len(expectedFields), result, "msg with args %d %s", 42, "42")
assert.NotEqual(t, 42, result)
assert.NotEqualf(t, 42, result, "msg with args %d %s", 42, "42")
assert.NotEqual(t, 3.14, result)
Expand All @@ -278,15 +287,18 @@ func TestExpectedActualChecker(t *testing.T) {

func TestExpectedActualChecker_Other(t *testing.T) {
var (
result, expected any
resultPtr, expectedPtr *int
resultObj, expectedObj user
resultTime, expectedTime time.Time
value int
result, expected any
resultPtr, expectedPtr *int
resultObj, expectedObj user
resultTime, expectedTime time.Time
value int
actualFields, expectedFields []string
)

// Invalid.
{
assert.Equal(t, len(expectedFields), len(actualFields)) // want "expected-actual: need to reverse actual and expected values"
assert.Equalf(t, len(expectedFields), len(actualFields), "msg with args %d %s", 42, "42") // want "expected-actual: need to reverse actual and expected values"
assert.EqualExportedValues(t, expectedObj, resultObj) // want "expected-actual: need to reverse actual and expected values"
assert.EqualExportedValuesf(t, expectedObj, resultObj, "msg with args %d %s", 42, "42") // want "expected-actual: need to reverse actual and expected values"
assert.EqualExportedValues(t, user{Name: "Rob"}, resultObj) // want "expected-actual: need to reverse actual and expected values"
Expand Down Expand Up @@ -1150,10 +1162,11 @@ func TestExpectedActualChecker_CannotDetectVariablesLookedLikeConsts(t *testing.

func TestExpectedActualChecker_Ignored(t *testing.T) {
var (
result, expected any
resultPtr, expectedPtr *int
value int
expectedTime time.Time
result, expected any
resultPtr, expectedPtr *int
value int
expectedTime time.Time
expectedFields, interfaces, directions []string
)

assert.Equal(t, nil, nil)
Expand All @@ -1162,6 +1175,10 @@ func TestExpectedActualChecker_Ignored(t *testing.T) {
assert.Equalf(t, "value", "value", "msg with args %d %s", 42, "42")
assert.Equal(t, expected, expected)
assert.Equalf(t, expected, expected, "msg with args %d %s", 42, "42")
assert.Equal(t, len(expectedFields), len(expectedFields))
assert.Equalf(t, len(expectedFields), len(expectedFields), "msg with args %d %s", 42, "42")
assert.Equal(t, len(interfaces), len(directions))
assert.Equalf(t, len(interfaces), len(directions), "msg with args %d %s", 42, "42")
assert.Equal(t, value, &resultPtr)
assert.Equalf(t, value, &resultPtr, "msg with args %d %s", 42, "42")
assert.Equal(t, []int{1, 2}, map[int]int{1: 2})
Expand Down
Loading

0 comments on commit ea24c79

Please sign in to comment.