Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compares: add Zero and Empty #111

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ assert.True(t, a >= b)
assert.True(t, a < b)
assert.True(t, a <= b)
assert.False(t, a == b)
assert.NotEmpty(t, a == b)
assert.NotZero(t, a == b)
assert.Zero(t, a == b)
assert.Empty(t, a == b)
// And so on...

Expand Down
48 changes: 48 additions & 0 deletions analyzer/testdata/src/checkers-default/compares/compares_test.go

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 @@ -15,6 +15,54 @@ func TestComparesChecker(t *testing.T) {

// Invalid.
{
assert.Equal(t, a, b) // want "compares: use assert\\.Equal"
assert.Equalf(t, a, b, "msg with args %d %s", 42, "42") // want "compares: use assert\\.Equalf"
assert.NotEqual(t, a, b) // want "compares: use assert\\.NotEqual"
assert.NotEqualf(t, a, b, "msg with args %d %s", 42, "42") // want "compares: use assert\\.NotEqualf"
assert.Greater(t, a, b) // want "compares: use assert\\.Greater"
assert.Greaterf(t, a, b, "msg with args %d %s", 42, "42") // want "compares: use assert\\.Greaterf"
assert.GreaterOrEqual(t, a, b) // want "compares: use assert\\.GreaterOrEqual"
assert.GreaterOrEqualf(t, a, b, "msg with args %d %s", 42, "42") // want "compares: use assert\\.GreaterOrEqualf"
assert.Less(t, a, b) // want "compares: use assert\\.Less"
assert.Lessf(t, a, b, "msg with args %d %s", 42, "42") // want "compares: use assert\\.Lessf"
assert.LessOrEqual(t, a, b) // want "compares: use assert\\.LessOrEqual"
assert.LessOrEqualf(t, a, b, "msg with args %d %s", 42, "42") // want "compares: use assert\\.LessOrEqualf"
assert.NotEqual(t, a, b) // want "compares: use assert\\.NotEqual"
assert.NotEqualf(t, a, b, "msg with args %d %s", 42, "42") // want "compares: use assert\\.NotEqualf"
assert.Equal(t, a, b) // want "compares: use assert\\.Equal"
assert.Equalf(t, a, b, "msg with args %d %s", 42, "42") // want "compares: use assert\\.Equalf"
assert.LessOrEqual(t, a, b) // want "compares: use assert\\.LessOrEqual"
assert.LessOrEqualf(t, a, b, "msg with args %d %s", 42, "42") // want "compares: use assert\\.LessOrEqualf"
assert.Less(t, a, b) // want "compares: use assert\\.Less"
assert.Lessf(t, a, b, "msg with args %d %s", 42, "42") // want "compares: use assert\\.Lessf"
assert.GreaterOrEqual(t, a, b) // want "compares: use assert\\.GreaterOrEqual"
assert.GreaterOrEqualf(t, a, b, "msg with args %d %s", 42, "42") // want "compares: use assert\\.GreaterOrEqualf"
assert.Greater(t, a, b) // want "compares: use assert\\.Greater"
assert.Greaterf(t, a, b, "msg with args %d %s", 42, "42") // want "compares: use assert\\.Greaterf"
assert.Equal(t, a, b) // want "compares: use assert\\.Equal"
assert.Equalf(t, a, b, "msg with args %d %s", 42, "42") // want "compares: use assert\\.Equalf"
assert.NotEqual(t, a, b) // want "compares: use assert\\.NotEqual"
assert.NotEqualf(t, a, b, "msg with args %d %s", 42, "42") // want "compares: use assert\\.NotEqualf"
assert.Greater(t, a, b) // want "compares: use assert\\.Greater"
assert.Greaterf(t, a, b, "msg with args %d %s", 42, "42") // want "compares: use assert\\.Greaterf"
assert.GreaterOrEqual(t, a, b) // want "compares: use assert\\.GreaterOrEqual"
assert.GreaterOrEqualf(t, a, b, "msg with args %d %s", 42, "42") // want "compares: use assert\\.GreaterOrEqualf"
assert.Less(t, a, b) // want "compares: use assert\\.Less"
assert.Lessf(t, a, b, "msg with args %d %s", 42, "42") // want "compares: use assert\\.Lessf"
assert.LessOrEqual(t, a, b) // want "compares: use assert\\.LessOrEqual"
assert.LessOrEqualf(t, a, b, "msg with args %d %s", 42, "42") // want "compares: use assert\\.LessOrEqualf"
assert.NotEqual(t, a, b) // want "compares: use assert\\.NotEqual"
assert.NotEqualf(t, a, b, "msg with args %d %s", 42, "42") // want "compares: use assert\\.NotEqualf"
assert.Equal(t, a, b) // want "compares: use assert\\.Equal"
assert.Equalf(t, a, b, "msg with args %d %s", 42, "42") // want "compares: use assert\\.Equalf"
assert.LessOrEqual(t, a, b) // want "compares: use assert\\.LessOrEqual"
assert.LessOrEqualf(t, a, b, "msg with args %d %s", 42, "42") // want "compares: use assert\\.LessOrEqualf"
assert.Less(t, a, b) // want "compares: use assert\\.Less"
assert.Lessf(t, a, b, "msg with args %d %s", 42, "42") // want "compares: use assert\\.Lessf"
assert.GreaterOrEqual(t, a, b) // want "compares: use assert\\.GreaterOrEqual"
assert.GreaterOrEqualf(t, a, b, "msg with args %d %s", 42, "42") // want "compares: use assert\\.GreaterOrEqualf"
assert.Greater(t, a, b) // want "compares: use assert\\.Greater"
assert.Greaterf(t, a, b, "msg with args %d %s", 42, "42") // want "compares: use assert\\.Greaterf"
assert.Equal(t, a, b) // want "compares: use assert\\.Equal"
assert.Equalf(t, a, b, "msg with args %d %s", 42, "42") // want "compares: use assert\\.Equalf"
assert.NotEqual(t, a, b) // want "compares: use assert\\.NotEqual"
Expand Down
8 changes: 6 additions & 2 deletions internal/checkers/compares.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import (
// assert.True(t, a < b)
// assert.True(t, a <= b)
// assert.False(t, a == b)
// assert.NotEmpty(t, a == b)
// assert.NotZero(t, a == b)
// assert.Zero(t, a == b)
// assert.Empty(t, a == b)
// ...
//
// and requires
Expand Down Expand Up @@ -48,9 +52,9 @@ func (checker Compares) Check(pass *analysis.Pass, call *CallMeta) *analysis.Dia
var tokenToProposedFn map[token.Token]string

switch call.Fn.NameFTrimmed {
case "True":
case "True", "NotZero", "NotEmpty":
tokenToProposedFn = tokenToProposedFnInsteadOfTrue
case "False":
case "False", "Zero", "Empty":
tokenToProposedFn = tokenToProposedFnInsteadOfFalse
default:
return nil
Expand Down
52 changes: 31 additions & 21 deletions internal/testgen/gen_compares.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,33 +30,43 @@ func (g ComparesTestsGenerator) TemplateData() any {
)
}

var invalidAssertions []Assertion //nolint:prealloc
for checker, oppositeChecker := range map[string]string{
"True": "False",
"NotEmpty": "Empty",
"NotZero": "Zero",
} {
invalidAssertions = append(invalidAssertions,
Assertion{Fn: checker, Argsf: "a == b", ReportMsgf: report, ProposedFn: "Equal", ProposedArgsf: "a, b"},
Assertion{Fn: checker, Argsf: "a != b", ReportMsgf: report, ProposedFn: "NotEqual", ProposedArgsf: "a, b"},
Assertion{Fn: checker, Argsf: "a > b", ReportMsgf: report, ProposedFn: "Greater", ProposedArgsf: "a, b"},
Assertion{Fn: checker, Argsf: "a >= b", ReportMsgf: report, ProposedFn: "GreaterOrEqual", ProposedArgsf: "a, b"},
Assertion{Fn: checker, Argsf: "a < b", ReportMsgf: report, ProposedFn: "Less", ProposedArgsf: "a, b"},
Assertion{Fn: checker, Argsf: "a <= b", ReportMsgf: report, ProposedFn: "LessOrEqual", ProposedArgsf: "a, b"},
Assertion{Fn: oppositeChecker, Argsf: "a == b", ReportMsgf: report, ProposedFn: "NotEqual", ProposedArgsf: "a, b"},
Assertion{Fn: oppositeChecker, Argsf: "a != b", ReportMsgf: report, ProposedFn: "Equal", ProposedArgsf: "a, b"},
Assertion{Fn: oppositeChecker, Argsf: "a > b", ReportMsgf: report, ProposedFn: "LessOrEqual", ProposedArgsf: "a, b"},
Assertion{Fn: oppositeChecker, Argsf: "a >= b", ReportMsgf: report, ProposedFn: "Less", ProposedArgsf: "a, b"},
Assertion{Fn: oppositeChecker, Argsf: "a < b", ReportMsgf: report, ProposedFn: "GreaterOrEqual", ProposedArgsf: "a, b"},
Assertion{Fn: oppositeChecker, Argsf: "a <= b", ReportMsgf: report, ProposedFn: "Greater", ProposedArgsf: "a, b"},
)
}

invalidAssertions = append(invalidAssertions,
Assertion{Fn: "True", Argsf: "ptrA == ptrB", ReportMsgf: report, ProposedFn: "Same", ProposedArgsf: "ptrA, ptrB"},
Assertion{Fn: "True", Argsf: "ptrA != ptrB", ReportMsgf: report, ProposedFn: "NotSame", ProposedArgsf: "ptrA, ptrB"},
Assertion{Fn: "False", Argsf: "ptrA == ptrB", ReportMsgf: report, ProposedFn: "NotSame", ProposedArgsf: "ptrA, ptrB"},
Assertion{Fn: "False", Argsf: "ptrA != ptrB", ReportMsgf: report, ProposedFn: "Same", ProposedArgsf: "ptrA, ptrB"},
)

return struct {
CheckerName CheckerName
InvalidAssertions []Assertion
ValidAssertions []Assertion
IgnoredAssertions []Assertion
}{
CheckerName: CheckerName(checker),
InvalidAssertions: []Assertion{
{Fn: "True", Argsf: "a == b", ReportMsgf: report, ProposedFn: "Equal", ProposedArgsf: "a, b"},
{Fn: "True", Argsf: "a != b", ReportMsgf: report, ProposedFn: "NotEqual", ProposedArgsf: "a, b"},
{Fn: "True", Argsf: "a > b", ReportMsgf: report, ProposedFn: "Greater", ProposedArgsf: "a, b"},
{Fn: "True", Argsf: "a >= b", ReportMsgf: report, ProposedFn: "GreaterOrEqual", ProposedArgsf: "a, b"},
{Fn: "True", Argsf: "a < b", ReportMsgf: report, ProposedFn: "Less", ProposedArgsf: "a, b"},
{Fn: "True", Argsf: "a <= b", ReportMsgf: report, ProposedFn: "LessOrEqual", ProposedArgsf: "a, b"},

{Fn: "False", Argsf: "a == b", ReportMsgf: report, ProposedFn: "NotEqual", ProposedArgsf: "a, b"},
{Fn: "False", Argsf: "a != b", ReportMsgf: report, ProposedFn: "Equal", ProposedArgsf: "a, b"},
{Fn: "False", Argsf: "a > b", ReportMsgf: report, ProposedFn: "LessOrEqual", ProposedArgsf: "a, b"},
{Fn: "False", Argsf: "a >= b", ReportMsgf: report, ProposedFn: "Less", ProposedArgsf: "a, b"},
{Fn: "False", Argsf: "a < b", ReportMsgf: report, ProposedFn: "GreaterOrEqual", ProposedArgsf: "a, b"},
{Fn: "False", Argsf: "a <= b", ReportMsgf: report, ProposedFn: "Greater", ProposedArgsf: "a, b"},

{Fn: "True", Argsf: "ptrA == ptrB", ReportMsgf: report, ProposedFn: "Same", ProposedArgsf: "ptrA, ptrB"},
{Fn: "True", Argsf: "ptrA != ptrB", ReportMsgf: report, ProposedFn: "NotSame", ProposedArgsf: "ptrA, ptrB"},
{Fn: "False", Argsf: "ptrA == ptrB", ReportMsgf: report, ProposedFn: "NotSame", ProposedArgsf: "ptrA, ptrB"},
{Fn: "False", Argsf: "ptrA != ptrB", ReportMsgf: report, ProposedFn: "Same", ProposedArgsf: "ptrA, ptrB"},
},
CheckerName: CheckerName(checker),
InvalidAssertions: invalidAssertions,
ValidAssertions: []Assertion{
{Fn: "Equal", Argsf: "a, b"},
{Fn: "NotEqual", Argsf: "a, b"},
Expand Down
Loading