-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #71 from Antonboom/fixes/bool-compare-custom-types
bool-compare: support custom types
- Loading branch information
Showing
13 changed files
with
590 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
analyzer/testdata/src/bool-compare-custom-types/bool_compare_custom_types_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
113 changes: 113 additions & 0 deletions
113
analyzer/testdata/src/bool-compare-custom-types/bool_compare_custom_types_test.go.golden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
19 changes: 19 additions & 0 deletions
19
analyzer/testdata/src/bool-compare-custom-types/builtin-override/bool_override_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
5
analyzer/testdata/src/bool-compare-custom-types/types/bool.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package types | ||
|
||
type SuperBool Bool | ||
|
||
type Bool bool |
Oops, something went wrong.