Skip to content

Commit 68b2b3e

Browse files
authored
Merge pull request #71 from Antonboom/fixes/bool-compare-custom-types
bool-compare: support custom types
2 parents 54a7e02 + 249d252 commit 68b2b3e

13 files changed

+590
-22
lines changed

README.md

+16
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,22 @@ import (
137137
**Enabled by default**: true. <br>
138138
**Reason**: Code simplification.
139139

140+
Also `bool-compare` supports user defined types like
141+
142+
```go
143+
type Bool bool
144+
```
145+
146+
And fixes assertions via casting variable to builtin `bool`:
147+
148+
```go
149+
var predicate Bool
150+
❌ assert.Equal(t, false, predicate)
151+
✅ assert.False(t, bool(predicate))
152+
```
153+
154+
To turn off this behavior use the `--bool-compare.ignore-custom-types` flag.
155+
140156
---
141157

142158
### compares

analyzer/analyzer_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ func TestTestifyLint(t *testing.T) {
2121
dir: "base-test",
2222
flags: map[string]string{"disable-all": "true", "enable": checkers.NewBoolCompare().Name()},
2323
},
24+
{
25+
dir: "bool-compare-custom-types",
26+
flags: map[string]string{"disable-all": "true", "enable": checkers.NewBoolCompare().Name()},
27+
},
28+
{
29+
dir: "bool-compare-ignore-custom-types",
30+
flags: map[string]string{
31+
"disable-all": "true",
32+
"enable": checkers.NewBoolCompare().Name(),
33+
"bool-compare.ignore-custom-types": "true",
34+
},
35+
},
2436
{
2537
dir: "checkers-priority",
2638
flags: map[string]string{"enable-all": "true"},

analyzer/checkers_factory.go

+3
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ func newCheckers(cfg config.Config) ([]checkers.RegularChecker, []checkers.Advan
4949
}
5050

5151
switch c := ch.(type) {
52+
case *checkers.BoolCompare:
53+
c.SetIgnoreCustomTypes(cfg.BoolCompare.IgnoreCustomTypes)
54+
5255
case *checkers.ExpectedActual:
5356
c.SetExpVarPattern(cfg.ExpectedActual.ExpVarPattern.Regexp)
5457

analyzer/checkers_factory_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,20 @@ func Test_newCheckers(t *testing.T) {
136136
checkers.NewRequireError().Name(),
137137
}),
138138
},
139+
{
140+
name: "bool-compare ignore custom types",
141+
cfg: config.Config{
142+
DisableAll: true,
143+
EnabledCheckers: config.KnownCheckersValue{checkers.NewBoolCompare().Name()},
144+
BoolCompare: config.BoolCompareConfig{
145+
IgnoreCustomTypes: true,
146+
},
147+
},
148+
expRegular: []checkers.RegularChecker{
149+
checkers.NewBoolCompare().SetIgnoreCustomTypes(true),
150+
},
151+
expAdvanced: []checkers.AdvancedChecker{},
152+
},
139153
{
140154
name: "expected-actual pattern defined",
141155
cfg: config.Config{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package boolcomparecustomtypes_test
2+
3+
import (
4+
"testing"
5+
6+
"bool-compare-custom-types/types"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
type MyBool bool
11+
12+
func TestBoolCompareChecker_CustomTypes(t *testing.T) {
13+
var b MyBool
14+
{
15+
assert.Equal(t, false, b) // want "bool-compare: use assert\\.False"
16+
assert.EqualValues(t, false, b) // want "bool-compare: use assert\\.False"
17+
assert.Exactly(t, false, b)
18+
19+
assert.Equal(t, true, b) // want "bool-compare: use assert\\.True"
20+
assert.EqualValues(t, true, b) // want "bool-compare: use assert\\.True"
21+
assert.Exactly(t, true, b)
22+
23+
assert.NotEqual(t, false, b) // want "bool-compare: use assert\\.True"
24+
assert.NotEqualValues(t, false, b) // want "bool-compare: use assert\\.True"
25+
26+
assert.NotEqual(t, true, b) // want "bool-compare: use assert\\.False"
27+
assert.NotEqualValues(t, true, b) // want "bool-compare: use assert\\.False"
28+
29+
assert.True(t, b == true) // want "bool-compare: need to simplify the assertion"
30+
assert.True(t, b != false) // want "bool-compare: need to simplify the assertion"
31+
assert.True(t, b == false) // want "bool-compare: use assert\\.False"
32+
assert.True(t, b != true) // want "bool-compare: use assert\\.False"
33+
34+
assert.False(t, b == true) // want "bool-compare: need to simplify the assertion"
35+
assert.False(t, b != false) // want "bool-compare: need to simplify the assertion"
36+
assert.False(t, b == false) // want "bool-compare: use assert\\.True"
37+
assert.False(t, b != true) // want "bool-compare: use assert\\.True"
38+
}
39+
40+
var extB types.Bool
41+
{
42+
assert.Equal(t, false, extB) // want "bool-compare: use assert\\.False"
43+
assert.EqualValues(t, false, extB) // want "bool-compare: use assert\\.False"
44+
assert.Exactly(t, false, extB)
45+
46+
assert.Equal(t, true, extB) // want "bool-compare: use assert\\.True"
47+
assert.EqualValues(t, true, extB) // want "bool-compare: use assert\\.True"
48+
assert.Exactly(t, true, extB)
49+
50+
assert.NotEqual(t, false, extB) // want "bool-compare: use assert\\.True"
51+
assert.NotEqualValues(t, false, extB) // want "bool-compare: use assert\\.True"
52+
53+
assert.NotEqual(t, true, extB) // want "bool-compare: use assert\\.False"
54+
assert.NotEqualValues(t, true, extB) // want "bool-compare: use assert\\.False"
55+
56+
assert.True(t, extB == true) // want "bool-compare: need to simplify the assertion"
57+
assert.True(t, extB != false) // want "bool-compare: need to simplify the assertion"
58+
assert.True(t, extB == false) // want "bool-compare: use assert\\.False"
59+
assert.True(t, extB != true) // want "bool-compare: use assert\\.False"
60+
61+
assert.False(t, extB == true) // want "bool-compare: need to simplify the assertion"
62+
assert.False(t, extB != false) // want "bool-compare: need to simplify the assertion"
63+
assert.False(t, extB == false) // want "bool-compare: use assert\\.True"
64+
assert.False(t, extB != true) // want "bool-compare: use assert\\.True"
65+
}
66+
67+
var extSuperB types.SuperBool
68+
{
69+
assert.Equal(t, false, extSuperB) // want "bool-compare: use assert\\.False"
70+
assert.EqualValues(t, false, extSuperB) // want "bool-compare: use assert\\.False"
71+
assert.Exactly(t, false, extSuperB)
72+
73+
assert.Equal(t, true, extSuperB) // want "bool-compare: use assert\\.True"
74+
assert.EqualValues(t, true, extSuperB) // want "bool-compare: use assert\\.True"
75+
assert.Exactly(t, true, extSuperB)
76+
77+
assert.NotEqual(t, false, extSuperB) // want "bool-compare: use assert\\.True"
78+
assert.NotEqualValues(t, false, extSuperB) // want "bool-compare: use assert\\.True"
79+
80+
assert.NotEqual(t, true, extSuperB) // want "bool-compare: use assert\\.False"
81+
assert.NotEqualValues(t, true, extSuperB) // want "bool-compare: use assert\\.False"
82+
83+
assert.True(t, extSuperB == true) // want "bool-compare: need to simplify the assertion"
84+
assert.True(t, extSuperB != false) // want "bool-compare: need to simplify the assertion"
85+
assert.True(t, extSuperB == false) // want "bool-compare: use assert\\.False"
86+
assert.True(t, extSuperB != true) // want "bool-compare: use assert\\.False"
87+
88+
assert.False(t, extSuperB == true) // want "bool-compare: need to simplify the assertion"
89+
assert.False(t, extSuperB != false) // want "bool-compare: need to simplify the assertion"
90+
assert.False(t, extSuperB == false) // want "bool-compare: use assert\\.True"
91+
assert.False(t, extSuperB != true) // want "bool-compare: use assert\\.True"
92+
}
93+
94+
// Crazy cases:
95+
{
96+
assert.Equal(t, true, types.Bool(extSuperB)) // want "bool-compare: use assert\\.True"
97+
assert.Equal(t, true, types.SuperBool(b)) // want "bool-compare: use assert\\.True"
98+
assert.Equal(t, true, bool(types.SuperBool(b))) // want "bool-compare: use assert\\.True"
99+
assert.True(t, !bool(types.SuperBool(b))) // want "bool-compare: use assert\\.False"
100+
assert.False(t, !bool(types.SuperBool(b))) // want "bool-compare: use assert\\.True"
101+
}
102+
}
103+
104+
func TestBoolCompareChecker_CustomTypes_Format(t *testing.T) {
105+
var predicate MyBool
106+
assert.Equal(t, true, predicate) // want "bool-compare: use assert\\.True"
107+
assert.Equal(t, true, predicate, "msg") // want "bool-compare: use assert\\.True"
108+
assert.Equal(t, true, predicate, "msg with arg %d", 42) // want "bool-compare: use assert\\.True"
109+
assert.Equal(t, true, predicate, "msg with args %d %s", 42, "42") // want "bool-compare: use assert\\.True"
110+
assert.Equalf(t, true, predicate, "msg") // want "bool-compare: use assert\\.Truef"
111+
assert.Equalf(t, true, predicate, "msg with arg %d", 42) // want "bool-compare: use assert\\.Truef"
112+
assert.Equalf(t, true, predicate, "msg with args %d %s", 42, "42") // want "bool-compare: use assert\\.Truef"
113+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package boolcomparecustomtypes_test
2+
3+
import (
4+
"testing"
5+
6+
"bool-compare-custom-types/types"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
type MyBool bool
11+
12+
func TestBoolCompareChecker_CustomTypes(t *testing.T) {
13+
var b MyBool
14+
{
15+
assert.False(t, bool(b)) // want "bool-compare: use assert\\.False"
16+
assert.False(t, bool(b)) // want "bool-compare: use assert\\.False"
17+
assert.Exactly(t, false, b)
18+
19+
assert.True(t, bool(b)) // want "bool-compare: use assert\\.True"
20+
assert.True(t, bool(b)) // want "bool-compare: use assert\\.True"
21+
assert.Exactly(t, true, b)
22+
23+
assert.True(t, bool(b)) // want "bool-compare: use assert\\.True"
24+
assert.True(t, bool(b)) // want "bool-compare: use assert\\.True"
25+
26+
assert.False(t, bool(b)) // want "bool-compare: use assert\\.False"
27+
assert.False(t, bool(b)) // want "bool-compare: use assert\\.False"
28+
29+
assert.True(t, bool(b)) // want "bool-compare: need to simplify the assertion"
30+
assert.True(t, bool(b)) // want "bool-compare: need to simplify the assertion"
31+
assert.False(t, bool(b)) // want "bool-compare: use assert\\.False"
32+
assert.False(t, bool(b)) // want "bool-compare: use assert\\.False"
33+
34+
assert.False(t, bool(b)) // want "bool-compare: need to simplify the assertion"
35+
assert.False(t, bool(b)) // want "bool-compare: need to simplify the assertion"
36+
assert.True(t, bool(b)) // want "bool-compare: use assert\\.True"
37+
assert.True(t, bool(b)) // want "bool-compare: use assert\\.True"
38+
}
39+
40+
var extB types.Bool
41+
{
42+
assert.False(t, bool(extB)) // want "bool-compare: use assert\\.False"
43+
assert.False(t, bool(extB)) // want "bool-compare: use assert\\.False"
44+
assert.Exactly(t, false, extB)
45+
46+
assert.True(t, bool(extB)) // want "bool-compare: use assert\\.True"
47+
assert.True(t, bool(extB)) // want "bool-compare: use assert\\.True"
48+
assert.Exactly(t, true, extB)
49+
50+
assert.True(t, bool(extB)) // want "bool-compare: use assert\\.True"
51+
assert.True(t, bool(extB)) // want "bool-compare: use assert\\.True"
52+
53+
assert.False(t, bool(extB)) // want "bool-compare: use assert\\.False"
54+
assert.False(t, bool(extB)) // want "bool-compare: use assert\\.False"
55+
56+
assert.True(t, bool(extB)) // want "bool-compare: need to simplify the assertion"
57+
assert.True(t, bool(extB)) // want "bool-compare: need to simplify the assertion"
58+
assert.False(t, bool(extB)) // want "bool-compare: use assert\\.False"
59+
assert.False(t, bool(extB)) // want "bool-compare: use assert\\.False"
60+
61+
assert.False(t, bool(extB)) // want "bool-compare: need to simplify the assertion"
62+
assert.False(t, bool(extB)) // want "bool-compare: need to simplify the assertion"
63+
assert.True(t, bool(extB)) // want "bool-compare: use assert\\.True"
64+
assert.True(t, bool(extB)) // want "bool-compare: use assert\\.True"
65+
}
66+
67+
var extSuperB types.SuperBool
68+
{
69+
assert.False(t, bool(extSuperB)) // want "bool-compare: use assert\\.False"
70+
assert.False(t, bool(extSuperB)) // want "bool-compare: use assert\\.False"
71+
assert.Exactly(t, false, extSuperB)
72+
73+
assert.True(t, bool(extSuperB)) // want "bool-compare: use assert\\.True"
74+
assert.True(t, bool(extSuperB)) // want "bool-compare: use assert\\.True"
75+
assert.Exactly(t, true, extSuperB)
76+
77+
assert.True(t, bool(extSuperB)) // want "bool-compare: use assert\\.True"
78+
assert.True(t, bool(extSuperB)) // want "bool-compare: use assert\\.True"
79+
80+
assert.False(t, bool(extSuperB)) // want "bool-compare: use assert\\.False"
81+
assert.False(t, bool(extSuperB)) // want "bool-compare: use assert\\.False"
82+
83+
assert.True(t, bool(extSuperB)) // want "bool-compare: need to simplify the assertion"
84+
assert.True(t, bool(extSuperB)) // want "bool-compare: need to simplify the assertion"
85+
assert.False(t, bool(extSuperB)) // want "bool-compare: use assert\\.False"
86+
assert.False(t, bool(extSuperB)) // want "bool-compare: use assert\\.False"
87+
88+
assert.False(t, bool(extSuperB)) // want "bool-compare: need to simplify the assertion"
89+
assert.False(t, bool(extSuperB)) // want "bool-compare: need to simplify the assertion"
90+
assert.True(t, bool(extSuperB)) // want "bool-compare: use assert\\.True"
91+
assert.True(t, bool(extSuperB)) // want "bool-compare: use assert\\.True"
92+
}
93+
94+
// Crazy cases:
95+
{
96+
assert.True(t, bool(types.Bool(extSuperB))) // want "bool-compare: use assert\\.True"
97+
assert.True(t, bool(types.SuperBool(b))) // want "bool-compare: use assert\\.True"
98+
assert.True(t, bool(types.SuperBool(b))) // want "bool-compare: use assert\\.True"
99+
assert.False(t, bool(types.SuperBool(b))) // want "bool-compare: use assert\\.False"
100+
assert.True(t, bool(types.SuperBool(b))) // want "bool-compare: use assert\\.True"
101+
}
102+
}
103+
104+
func TestBoolCompareChecker_CustomTypes_Format(t *testing.T) {
105+
var predicate MyBool
106+
assert.True(t, bool(predicate)) // want "bool-compare: use assert\\.True"
107+
assert.True(t, bool(predicate), "msg") // want "bool-compare: use assert\\.True"
108+
assert.True(t, bool(predicate), "msg with arg %d", 42) // want "bool-compare: use assert\\.True"
109+
assert.True(t, bool(predicate), "msg with args %d %s", 42, "42") // want "bool-compare: use assert\\.True"
110+
assert.Truef(t, bool(predicate), "msg") // want "bool-compare: use assert\\.Truef"
111+
assert.Truef(t, bool(predicate), "msg with arg %d", 42) // want "bool-compare: use assert\\.Truef"
112+
assert.Truef(t, bool(predicate), "msg with args %d %s", 42, "42") // want "bool-compare: use assert\\.Truef"
113+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package boolcomparecustomtypes_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
type bool int
10+
11+
func TestBoolCompareChecker_BoolOverride(t *testing.T) {
12+
var mimic bool
13+
assert.Equal(t, false, mimic)
14+
assert.Equal(t, false, mimic)
15+
assert.EqualValues(t, false, mimic)
16+
assert.Exactly(t, false, mimic)
17+
assert.NotEqual(t, false, mimic)
18+
assert.NotEqualValues(t, false, mimic)
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package types
2+
3+
type SuperBool Bool
4+
5+
type Bool bool

0 commit comments

Comments
 (0)