-
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.
- Loading branch information
Showing
10 changed files
with
309 additions
and
0 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
46 changes: 46 additions & 0 deletions
46
analyzer/testdata/src/checkers-default/contains/contains_test.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
46 changes: 46 additions & 0 deletions
46
analyzer/testdata/src/checkers-default/contains/contains_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,46 @@ | ||
// Code generated by testifylint/internal/testgen. DO NOT EDIT. | ||
|
||
package contains | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestContainsChecker(t *testing.T) { | ||
var ( | ||
s = "abc123" | ||
errSentinel = errors.New("user not found") | ||
) | ||
|
||
// Invalid. | ||
{ | ||
assert.NotContains(t, s, "456") // want "contains: use assert\\.NotContains" | ||
assert.NotContainsf(t, s, "456", "msg with args %d %s", 42, "42") // want "contains: use assert\\.NotContainsf" | ||
assert.Contains(t, s, "abc123") // want "contains: use assert\\.Contains" | ||
assert.Containsf(t, s, "abc123", "msg with args %d %s", 42, "42") // want "contains: use assert\\.Containsf" | ||
} | ||
|
||
// Valid. | ||
{ | ||
assert.Contains(t, s, "abc123") | ||
assert.Containsf(t, s, "abc123", "msg with args %d %s", 42, "42") | ||
assert.NotContains(t, s, "456") | ||
assert.NotContainsf(t, s, "456", "msg with args %d %s", 42, "42") | ||
} | ||
|
||
// Ignored. | ||
{ | ||
assert.Contains(t, errSentinel.Error(), "user") | ||
assert.Containsf(t, errSentinel.Error(), "user", "msg with args %d %s", 42, "42") | ||
assert.Equal(t, strings.Contains(s, "abc123"), true) | ||
assert.Equalf(t, strings.Contains(s, "abc123"), true, "msg with args %d %s", 42, "42") | ||
assert.False(t, !strings.Contains(s, "abc123")) | ||
assert.Falsef(t, !strings.Contains(s, "abc123"), "msg with args %d %s", 42, "42") | ||
assert.True(t, !strings.Contains(s, "456")) | ||
assert.Truef(t, !strings.Contains(s, "456"), "msg with args %d %s", 42, "42") | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package checkers | ||
|
||
import ( | ||
"go/ast" | ||
|
||
"golang.org/x/tools/go/analysis" | ||
) | ||
|
||
// Contains detects situations like | ||
// | ||
// assert.True(t, strings.Contains(s, "abc123")) | ||
// assert.False(t, strings.Contains(s, "456")) | ||
// | ||
// and requires | ||
// | ||
// assert.Contains(t, s, "abc123") | ||
// assert.NotContains(t, s, "456") | ||
type Contains struct{} | ||
|
||
// NewContains constructs Contains checker. | ||
func NewContains() Contains { return Contains{} } | ||
func (Contains) Name() string { return "contains" } | ||
|
||
func (checker Contains) Check(pass *analysis.Pass, call *CallMeta) *analysis.Diagnostic { | ||
switch call.Fn.NameFTrimmed { | ||
case "True": | ||
if len(call.Args) < 1 { | ||
return nil | ||
} | ||
|
||
ce, ok := call.Args[0].(*ast.CallExpr) | ||
if !ok { | ||
return nil | ||
} | ||
if len(ce.Args) != 2 { | ||
return nil | ||
} | ||
|
||
if isStringsContainsCall(pass, ce) { | ||
const proposed = "Contains" | ||
return newUseFunctionDiagnostic(checker.Name(), call, proposed, | ||
newSuggestedFuncReplacement(call, proposed, analysis.TextEdit{ | ||
Pos: ce.Pos(), | ||
End: ce.End(), | ||
NewText: formatAsCallArgs(pass, ce.Args[0], ce.Args[1]), | ||
}), | ||
) | ||
} | ||
|
||
case "False": | ||
if len(call.Args) < 1 { | ||
return nil | ||
} | ||
|
||
ce, ok := call.Args[0].(*ast.CallExpr) | ||
if !ok { | ||
return nil | ||
} | ||
if len(ce.Args) != 2 { | ||
return nil | ||
} | ||
|
||
if isStringsContainsCall(pass, ce) { | ||
const proposed = "NotContains" | ||
return newUseFunctionDiagnostic(checker.Name(), call, proposed, | ||
newSuggestedFuncReplacement(call, proposed, analysis.TextEdit{ | ||
Pos: ce.Pos(), | ||
End: ce.End(), | ||
NewText: formatAsCallArgs(pass, ce.Args[0], ce.Args[1]), | ||
}), | ||
) | ||
} | ||
} | ||
return nil | ||
} |
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,11 @@ | ||
package checkers | ||
|
||
import ( | ||
"go/ast" | ||
|
||
"golang.org/x/tools/go/analysis" | ||
) | ||
|
||
func isStringsContainsCall(pass *analysis.Pass, ce *ast.CallExpr) bool { | ||
return isPkgFnCall(pass, ce, "strings", "Contains") | ||
} |
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,106 @@ | ||
package main | ||
|
||
import ( | ||
"strings" | ||
"text/template" | ||
|
||
"github.com/Antonboom/testifylint/internal/checkers" | ||
) | ||
|
||
type ContainsTestsGenerator struct{} | ||
|
||
func (ContainsTestsGenerator) Checker() checkers.Checker { | ||
return checkers.NewContains() | ||
} | ||
|
||
func (g ContainsTestsGenerator) TemplateData() any { | ||
checker := g.Checker().Name() | ||
|
||
return struct { | ||
CheckerName CheckerName | ||
InvalidAssertions []Assertion | ||
ValidAssertions []Assertion | ||
IgnoredAssertions []Assertion | ||
}{ | ||
CheckerName: CheckerName(checker), | ||
InvalidAssertions: []Assertion{ | ||
{ | ||
Fn: "False", | ||
Argsf: `strings.Contains(s, "456")`, | ||
ReportMsgf: checker + ": use %s.%s", | ||
ProposedFn: "NotContains", | ||
ProposedArgsf: "s, \"456\"", | ||
}, | ||
{ | ||
Fn: "True", | ||
Argsf: `strings.Contains(s, "abc123")`, | ||
ReportMsgf: checker + ": use %s.%s", | ||
ProposedFn: "Contains", | ||
ProposedArgsf: "s, \"abc123\"", | ||
}, | ||
}, | ||
ValidAssertions: []Assertion{ | ||
{Fn: "Contains", Argsf: `s, "abc123"`}, | ||
{Fn: "NotContains", Argsf: `s, "456"`}, | ||
}, | ||
IgnoredAssertions: []Assertion{ | ||
{Fn: "Contains", Argsf: `errSentinel.Error(), "user"`}, | ||
{Fn: "Equal", Argsf: `strings.Contains(s, "abc123"), true`}, | ||
{Fn: "False", Argsf: `!strings.Contains(s, "abc123")`}, | ||
{Fn: "True", Argsf: `!strings.Contains(s, "456")`}, | ||
}, | ||
} | ||
} | ||
|
||
func (ContainsTestsGenerator) ErroredTemplate() Executor { | ||
return template.Must(template.New("ContainsTestsGenerator.ErroredTemplate"). | ||
Funcs(fm). | ||
Parse(constainsTestTmpl)) | ||
} | ||
|
||
func (ContainsTestsGenerator) GoldenTemplate() Executor { | ||
return template.Must(template.New("ContainsTestsGenerator.GoldenTemplate"). | ||
Funcs(fm). | ||
Parse(strings.ReplaceAll(constainsTestTmpl, "NewAssertionExpander", "NewAssertionExpander.AsGolden"))) | ||
} | ||
|
||
const constainsTestTmpl = header + ` | ||
package {{ .CheckerName.AsPkgName }} | ||
import ( | ||
"errors" | ||
"strings" | ||
"testing" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
func {{ .CheckerName.AsTestName }}(t *testing.T) { | ||
var ( | ||
s = "abc123" | ||
errSentinel = errors.New("user not found") | ||
) | ||
// Invalid. | ||
{ | ||
{{- range $ai, $assrn := $.InvalidAssertions }} | ||
{{ NewAssertionExpander.Expand $assrn "assert" "t" nil }} | ||
{{- end }} | ||
} | ||
// Valid. | ||
{ | ||
{{- range $ai, $assrn := $.ValidAssertions }} | ||
{{ NewAssertionExpander.Expand $assrn "assert" "t" nil }} | ||
{{- end }} | ||
} | ||
// Ignored. | ||
{ | ||
{{- range $ai, $assrn := $.IgnoredAssertions }} | ||
{{ NewAssertionExpander.Expand $assrn "assert" "t" nil }} | ||
{{- end }} | ||
} | ||
} | ||
` |
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