Skip to content

Commit

Permalink
Merge pull request #71 from Antonboom/fixes/bool-compare-custom-types
Browse files Browse the repository at this point in the history
bool-compare: support custom types
  • Loading branch information
Antonboom authored Mar 3, 2024
2 parents 54a7e02 + 249d252 commit 68b2b3e
Show file tree
Hide file tree
Showing 13 changed files with 590 additions and 22 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,22 @@ import (
**Enabled by default**: true. <br>
**Reason**: Code simplification.

Also `bool-compare` supports user defined types like

```go
type Bool bool
```

And fixes assertions via casting variable to builtin `bool`:

```go
var predicate Bool
❌ assert.Equal(t, false, predicate)
✅ assert.False(t, bool(predicate))
```

To turn off this behavior use the `--bool-compare.ignore-custom-types` flag.

---

### compares
Expand Down
12 changes: 12 additions & 0 deletions analyzer/analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ func TestTestifyLint(t *testing.T) {
dir: "base-test",
flags: map[string]string{"disable-all": "true", "enable": checkers.NewBoolCompare().Name()},
},
{
dir: "bool-compare-custom-types",
flags: map[string]string{"disable-all": "true", "enable": checkers.NewBoolCompare().Name()},
},
{
dir: "bool-compare-ignore-custom-types",
flags: map[string]string{
"disable-all": "true",
"enable": checkers.NewBoolCompare().Name(),
"bool-compare.ignore-custom-types": "true",
},
},
{
dir: "checkers-priority",
flags: map[string]string{"enable-all": "true"},
Expand Down
3 changes: 3 additions & 0 deletions analyzer/checkers_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ func newCheckers(cfg config.Config) ([]checkers.RegularChecker, []checkers.Advan
}

switch c := ch.(type) {
case *checkers.BoolCompare:
c.SetIgnoreCustomTypes(cfg.BoolCompare.IgnoreCustomTypes)

case *checkers.ExpectedActual:
c.SetExpVarPattern(cfg.ExpectedActual.ExpVarPattern.Regexp)

Expand Down
14 changes: 14 additions & 0 deletions analyzer/checkers_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,20 @@ func Test_newCheckers(t *testing.T) {
checkers.NewRequireError().Name(),
}),
},
{
name: "bool-compare ignore custom types",
cfg: config.Config{
DisableAll: true,
EnabledCheckers: config.KnownCheckersValue{checkers.NewBoolCompare().Name()},
BoolCompare: config.BoolCompareConfig{
IgnoreCustomTypes: true,
},
},
expRegular: []checkers.RegularChecker{
checkers.NewBoolCompare().SetIgnoreCustomTypes(true),
},
expAdvanced: []checkers.AdvancedChecker{},
},
{
name: "expected-actual pattern defined",
cfg: config.Config{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package boolcomparecustomtypes_test

import (
"testing"

"bool-compare-custom-types/types"
"github.com/stretchr/testify/assert"
)

type MyBool bool

func TestBoolCompareChecker_CustomTypes(t *testing.T) {
var b MyBool
{
assert.Equal(t, false, b) // want "bool-compare: use assert\\.False"
assert.EqualValues(t, false, b) // want "bool-compare: use assert\\.False"
assert.Exactly(t, false, b)

assert.Equal(t, true, b) // want "bool-compare: use assert\\.True"
assert.EqualValues(t, true, b) // want "bool-compare: use assert\\.True"
assert.Exactly(t, true, b)

assert.NotEqual(t, false, b) // want "bool-compare: use assert\\.True"
assert.NotEqualValues(t, false, b) // want "bool-compare: use assert\\.True"

assert.NotEqual(t, true, b) // want "bool-compare: use assert\\.False"
assert.NotEqualValues(t, true, b) // want "bool-compare: use assert\\.False"

assert.True(t, b == true) // want "bool-compare: need to simplify the assertion"
assert.True(t, b != false) // want "bool-compare: need to simplify the assertion"
assert.True(t, b == false) // want "bool-compare: use assert\\.False"
assert.True(t, b != true) // want "bool-compare: use assert\\.False"

assert.False(t, b == true) // want "bool-compare: need to simplify the assertion"
assert.False(t, b != false) // want "bool-compare: need to simplify the assertion"
assert.False(t, b == false) // want "bool-compare: use assert\\.True"
assert.False(t, b != true) // want "bool-compare: use assert\\.True"
}

var extB types.Bool
{
assert.Equal(t, false, extB) // want "bool-compare: use assert\\.False"
assert.EqualValues(t, false, extB) // want "bool-compare: use assert\\.False"
assert.Exactly(t, false, extB)

assert.Equal(t, true, extB) // want "bool-compare: use assert\\.True"
assert.EqualValues(t, true, extB) // want "bool-compare: use assert\\.True"
assert.Exactly(t, true, extB)

assert.NotEqual(t, false, extB) // want "bool-compare: use assert\\.True"
assert.NotEqualValues(t, false, extB) // want "bool-compare: use assert\\.True"

assert.NotEqual(t, true, extB) // want "bool-compare: use assert\\.False"
assert.NotEqualValues(t, true, extB) // want "bool-compare: use assert\\.False"

assert.True(t, extB == true) // want "bool-compare: need to simplify the assertion"
assert.True(t, extB != false) // want "bool-compare: need to simplify the assertion"
assert.True(t, extB == false) // want "bool-compare: use assert\\.False"
assert.True(t, extB != true) // want "bool-compare: use assert\\.False"

assert.False(t, extB == true) // want "bool-compare: need to simplify the assertion"
assert.False(t, extB != false) // want "bool-compare: need to simplify the assertion"
assert.False(t, extB == false) // want "bool-compare: use assert\\.True"
assert.False(t, extB != true) // want "bool-compare: use assert\\.True"
}

var extSuperB types.SuperBool
{
assert.Equal(t, false, extSuperB) // want "bool-compare: use assert\\.False"
assert.EqualValues(t, false, extSuperB) // want "bool-compare: use assert\\.False"
assert.Exactly(t, false, extSuperB)

assert.Equal(t, true, extSuperB) // want "bool-compare: use assert\\.True"
assert.EqualValues(t, true, extSuperB) // want "bool-compare: use assert\\.True"
assert.Exactly(t, true, extSuperB)

assert.NotEqual(t, false, extSuperB) // want "bool-compare: use assert\\.True"
assert.NotEqualValues(t, false, extSuperB) // want "bool-compare: use assert\\.True"

assert.NotEqual(t, true, extSuperB) // want "bool-compare: use assert\\.False"
assert.NotEqualValues(t, true, extSuperB) // want "bool-compare: use assert\\.False"

assert.True(t, extSuperB == true) // want "bool-compare: need to simplify the assertion"
assert.True(t, extSuperB != false) // want "bool-compare: need to simplify the assertion"
assert.True(t, extSuperB == false) // want "bool-compare: use assert\\.False"
assert.True(t, extSuperB != true) // want "bool-compare: use assert\\.False"

assert.False(t, extSuperB == true) // want "bool-compare: need to simplify the assertion"
assert.False(t, extSuperB != false) // want "bool-compare: need to simplify the assertion"
assert.False(t, extSuperB == false) // want "bool-compare: use assert\\.True"
assert.False(t, extSuperB != true) // want "bool-compare: use assert\\.True"
}

// Crazy cases:
{
assert.Equal(t, true, types.Bool(extSuperB)) // want "bool-compare: use assert\\.True"
assert.Equal(t, true, types.SuperBool(b)) // want "bool-compare: use assert\\.True"
assert.Equal(t, true, bool(types.SuperBool(b))) // want "bool-compare: use assert\\.True"
assert.True(t, !bool(types.SuperBool(b))) // want "bool-compare: use assert\\.False"
assert.False(t, !bool(types.SuperBool(b))) // want "bool-compare: use assert\\.True"
}
}

func TestBoolCompareChecker_CustomTypes_Format(t *testing.T) {
var predicate MyBool
assert.Equal(t, true, predicate) // want "bool-compare: use assert\\.True"
assert.Equal(t, true, predicate, "msg") // want "bool-compare: use assert\\.True"
assert.Equal(t, true, predicate, "msg with arg %d", 42) // want "bool-compare: use assert\\.True"
assert.Equal(t, true, predicate, "msg with args %d %s", 42, "42") // want "bool-compare: use assert\\.True"
assert.Equalf(t, true, predicate, "msg") // want "bool-compare: use assert\\.Truef"
assert.Equalf(t, true, predicate, "msg with arg %d", 42) // want "bool-compare: use assert\\.Truef"
assert.Equalf(t, true, predicate, "msg with args %d %s", 42, "42") // want "bool-compare: use assert\\.Truef"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package boolcomparecustomtypes_test

import (
"testing"

"bool-compare-custom-types/types"
"github.com/stretchr/testify/assert"
)

type MyBool bool

func TestBoolCompareChecker_CustomTypes(t *testing.T) {
var b MyBool
{
assert.False(t, bool(b)) // want "bool-compare: use assert\\.False"
assert.False(t, bool(b)) // want "bool-compare: use assert\\.False"
assert.Exactly(t, false, b)

assert.True(t, bool(b)) // want "bool-compare: use assert\\.True"
assert.True(t, bool(b)) // want "bool-compare: use assert\\.True"
assert.Exactly(t, true, b)

assert.True(t, bool(b)) // want "bool-compare: use assert\\.True"
assert.True(t, bool(b)) // want "bool-compare: use assert\\.True"

assert.False(t, bool(b)) // want "bool-compare: use assert\\.False"
assert.False(t, bool(b)) // want "bool-compare: use assert\\.False"

assert.True(t, bool(b)) // want "bool-compare: need to simplify the assertion"
assert.True(t, bool(b)) // want "bool-compare: need to simplify the assertion"
assert.False(t, bool(b)) // want "bool-compare: use assert\\.False"
assert.False(t, bool(b)) // want "bool-compare: use assert\\.False"

assert.False(t, bool(b)) // want "bool-compare: need to simplify the assertion"
assert.False(t, bool(b)) // want "bool-compare: need to simplify the assertion"
assert.True(t, bool(b)) // want "bool-compare: use assert\\.True"
assert.True(t, bool(b)) // want "bool-compare: use assert\\.True"
}

var extB types.Bool
{
assert.False(t, bool(extB)) // want "bool-compare: use assert\\.False"
assert.False(t, bool(extB)) // want "bool-compare: use assert\\.False"
assert.Exactly(t, false, extB)

assert.True(t, bool(extB)) // want "bool-compare: use assert\\.True"
assert.True(t, bool(extB)) // want "bool-compare: use assert\\.True"
assert.Exactly(t, true, extB)

assert.True(t, bool(extB)) // want "bool-compare: use assert\\.True"
assert.True(t, bool(extB)) // want "bool-compare: use assert\\.True"

assert.False(t, bool(extB)) // want "bool-compare: use assert\\.False"
assert.False(t, bool(extB)) // want "bool-compare: use assert\\.False"

assert.True(t, bool(extB)) // want "bool-compare: need to simplify the assertion"
assert.True(t, bool(extB)) // want "bool-compare: need to simplify the assertion"
assert.False(t, bool(extB)) // want "bool-compare: use assert\\.False"
assert.False(t, bool(extB)) // want "bool-compare: use assert\\.False"

assert.False(t, bool(extB)) // want "bool-compare: need to simplify the assertion"
assert.False(t, bool(extB)) // want "bool-compare: need to simplify the assertion"
assert.True(t, bool(extB)) // want "bool-compare: use assert\\.True"
assert.True(t, bool(extB)) // want "bool-compare: use assert\\.True"
}

var extSuperB types.SuperBool
{
assert.False(t, bool(extSuperB)) // want "bool-compare: use assert\\.False"
assert.False(t, bool(extSuperB)) // want "bool-compare: use assert\\.False"
assert.Exactly(t, false, extSuperB)

assert.True(t, bool(extSuperB)) // want "bool-compare: use assert\\.True"
assert.True(t, bool(extSuperB)) // want "bool-compare: use assert\\.True"
assert.Exactly(t, true, extSuperB)

assert.True(t, bool(extSuperB)) // want "bool-compare: use assert\\.True"
assert.True(t, bool(extSuperB)) // want "bool-compare: use assert\\.True"

assert.False(t, bool(extSuperB)) // want "bool-compare: use assert\\.False"
assert.False(t, bool(extSuperB)) // want "bool-compare: use assert\\.False"

assert.True(t, bool(extSuperB)) // want "bool-compare: need to simplify the assertion"
assert.True(t, bool(extSuperB)) // want "bool-compare: need to simplify the assertion"
assert.False(t, bool(extSuperB)) // want "bool-compare: use assert\\.False"
assert.False(t, bool(extSuperB)) // want "bool-compare: use assert\\.False"

assert.False(t, bool(extSuperB)) // want "bool-compare: need to simplify the assertion"
assert.False(t, bool(extSuperB)) // want "bool-compare: need to simplify the assertion"
assert.True(t, bool(extSuperB)) // want "bool-compare: use assert\\.True"
assert.True(t, bool(extSuperB)) // want "bool-compare: use assert\\.True"
}

// Crazy cases:
{
assert.True(t, bool(types.Bool(extSuperB))) // want "bool-compare: use assert\\.True"
assert.True(t, bool(types.SuperBool(b))) // want "bool-compare: use assert\\.True"
assert.True(t, bool(types.SuperBool(b))) // want "bool-compare: use assert\\.True"
assert.False(t, bool(types.SuperBool(b))) // want "bool-compare: use assert\\.False"
assert.True(t, bool(types.SuperBool(b))) // want "bool-compare: use assert\\.True"
}
}

func TestBoolCompareChecker_CustomTypes_Format(t *testing.T) {
var predicate MyBool
assert.True(t, bool(predicate)) // want "bool-compare: use assert\\.True"
assert.True(t, bool(predicate), "msg") // want "bool-compare: use assert\\.True"
assert.True(t, bool(predicate), "msg with arg %d", 42) // want "bool-compare: use assert\\.True"
assert.True(t, bool(predicate), "msg with args %d %s", 42, "42") // want "bool-compare: use assert\\.True"
assert.Truef(t, bool(predicate), "msg") // want "bool-compare: use assert\\.Truef"
assert.Truef(t, bool(predicate), "msg with arg %d", 42) // want "bool-compare: use assert\\.Truef"
assert.Truef(t, bool(predicate), "msg with args %d %s", 42, "42") // want "bool-compare: use assert\\.Truef"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package boolcomparecustomtypes_test

import (
"testing"

"github.com/stretchr/testify/assert"
)

type bool int

func TestBoolCompareChecker_BoolOverride(t *testing.T) {
var mimic bool
assert.Equal(t, false, mimic)
assert.Equal(t, false, mimic)
assert.EqualValues(t, false, mimic)
assert.Exactly(t, false, mimic)
assert.NotEqual(t, false, mimic)
assert.NotEqualValues(t, false, mimic)
}
5 changes: 5 additions & 0 deletions analyzer/testdata/src/bool-compare-custom-types/types/bool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package types

type SuperBool Bool

type Bool bool
Loading

0 comments on commit 68b2b3e

Please sign in to comment.