-
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.
* new checker `contains` Co-authored-by: ccoVeille <[email protected]> * contains: maintainer review --------- Co-authored-by: ccoVeille <[email protected]> Co-authored-by: Anton Telyshev <[email protected]>
- Loading branch information
1 parent
cea8c9c
commit 8afa731
Showing
11 changed files
with
441 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
66 changes: 66 additions & 0 deletions
66
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.
66 changes: 66 additions & 0 deletions
66
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,66 @@ | ||
// Code generated by testifylint/internal/testgen. DO NOT EDIT. | ||
|
||
package contains | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestContainsChecker(t *testing.T) { | ||
var ( | ||
s = "abc123" | ||
b = []byte(s) | ||
errSentinel = errors.New("user not found") | ||
) | ||
|
||
// Invalid. | ||
{ | ||
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" | ||
assert.Contains(t, string(b), "abc123") // want "contains: use assert\\.Contains" | ||
assert.Containsf(t, string(b), "abc123", "msg with args %d %s", 42, "42") // want "contains: use assert\\.Containsf" | ||
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" | ||
assert.Contains(t, string(b), "abc123") // want "contains: use assert\\.Contains" | ||
assert.Containsf(t, string(b), "abc123", "msg with args %d %s", 42, "42") // want "contains: use assert\\.Containsf" | ||
assert.NotContains(t, s, "abc123") // want "contains: use assert\\.NotContains" | ||
assert.NotContainsf(t, s, "abc123", "msg with args %d %s", 42, "42") // want "contains: use assert\\.NotContainsf" | ||
assert.NotContains(t, string(b), "abc123") // want "contains: use assert\\.NotContains" | ||
assert.NotContainsf(t, string(b), "abc123", "msg with args %d %s", 42, "42") // want "contains: use assert\\.NotContainsf" | ||
assert.NotContains(t, s, "abc123") // want "contains: use assert\\.NotContains" | ||
assert.NotContainsf(t, s, "abc123", "msg with args %d %s", 42, "42") // want "contains: use assert\\.NotContainsf" | ||
assert.NotContains(t, string(b), "abc123") // want "contains: use assert\\.NotContains" | ||
assert.NotContainsf(t, string(b), "abc123", "msg with args %d %s", 42, "42") // want "contains: use assert\\.NotContainsf" | ||
} | ||
|
||
// Valid. | ||
{ | ||
assert.Contains(t, s, "abc123") | ||
assert.Containsf(t, s, "abc123", "msg with args %d %s", 42, "42") | ||
assert.Contains(t, string(b), "abc123") | ||
assert.Containsf(t, string(b), "abc123", "msg with args %d %s", 42, "42") | ||
assert.NotContains(t, s, "abc123") | ||
assert.NotContainsf(t, s, "abc123", "msg with args %d %s", 42, "42") | ||
assert.NotContains(t, string(b), "abc123") | ||
assert.NotContainsf(t, string(b), "abc123", "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.True(t, bytes.Contains(b, []byte("a"))) | ||
assert.Truef(t, bytes.Contains(b, []byte("a")), "msg with args %d %s", 42, "42") | ||
assert.False(t, !bytes.Contains(b, []byte("a"))) | ||
assert.Falsef(t, !bytes.Contains(b, []byte("a")), "msg with args %d %s", 42, "42") | ||
assert.False(t, bytes.Contains(b, []byte("a"))) | ||
assert.Falsef(t, bytes.Contains(b, []byte("a")), "msg with args %d %s", 42, "42") | ||
assert.True(t, !bytes.Contains(b, []byte("a"))) | ||
assert.Truef(t, !bytes.Contains(b, []byte("a")), "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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package debug | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestContainsNotContainsReplacements_String(t *testing.T) { | ||
var tm tMock | ||
|
||
const substr = "abc123" | ||
|
||
for _, s := range []string{"", "random", substr + substr} { | ||
t.Run(s, func(t *testing.T) { | ||
assert.Equal(t, | ||
assert.True(tm, strings.Contains(s, substr)), | ||
assert.Contains(tm, s, substr), | ||
) | ||
|
||
assert.Equal(t, | ||
assert.False(tm, !strings.Contains(s, substr)), | ||
assert.Contains(tm, s, substr), | ||
) | ||
|
||
assert.Equal(t, | ||
assert.False(tm, strings.Contains(s, substr)), | ||
assert.NotContains(tm, s, substr), | ||
) | ||
|
||
assert.Equal(t, | ||
assert.True(tm, !strings.Contains(s, substr)), | ||
assert.NotContains(tm, s, substr), | ||
) | ||
}) | ||
} | ||
} | ||
|
||
func TestContainsNotContainsReplacements_Bytes(t *testing.T) { | ||
var tm tMock | ||
|
||
subbytes := []byte("abc123") | ||
|
||
for _, s := range [][]byte{nil, []byte("random"), append(subbytes, subbytes...)} { | ||
t.Run(fmt.Sprintf("[]byte(%s)", s), func(t *testing.T) { | ||
assert.Equal(t, | ||
assert.True(tm, bytes.Contains(s, subbytes)), | ||
assert.Contains(tm, s, subbytes), | ||
) | ||
|
||
assert.Equal(t, | ||
assert.False(tm, !bytes.Contains(s, subbytes)), | ||
assert.Contains(tm, s, subbytes), | ||
) | ||
|
||
assert.Equal(t, | ||
assert.False(tm, bytes.Contains(s, subbytes)), | ||
assert.NotContains(tm, s, subbytes), | ||
) | ||
|
||
assert.Equal(t, | ||
assert.True(tm, !bytes.Contains(s, subbytes)), | ||
assert.NotContains(tm, s, subbytes), | ||
) | ||
}) | ||
} | ||
} |
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,72 @@ | ||
package checkers | ||
|
||
import ( | ||
"go/ast" | ||
|
||
"golang.org/x/tools/go/analysis" | ||
) | ||
|
||
// Contains detects situations like | ||
// | ||
// assert.True(t, strings.Contains(a, "abc123")) | ||
// assert.False(t, !strings.Contains(a, "abc123")) | ||
// | ||
// assert.False(t, strings.Contains(a, "abc123")) | ||
// assert.True(t, !strings.Contains(a, "abc123")) | ||
// | ||
// and requires | ||
// | ||
// assert.Contains(t, a, "abc123") | ||
// assert.NotContains(t, a, "abc123") | ||
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 { | ||
if len(call.Args) < 1 { | ||
return nil | ||
} | ||
|
||
expr := call.Args[0] | ||
unpacked, isNeg := isNegation(expr) | ||
if isNeg { | ||
expr = unpacked | ||
} | ||
|
||
ce, ok := expr.(*ast.CallExpr) | ||
if !ok || len(ce.Args) != 2 { | ||
return nil | ||
} | ||
|
||
if !isStringsContainsCall(pass, ce) { | ||
return nil | ||
} | ||
|
||
var proposed string | ||
switch call.Fn.NameFTrimmed { | ||
default: | ||
return nil | ||
|
||
case "True": | ||
proposed = "Contains" | ||
if isNeg { | ||
proposed = "NotContains" | ||
} | ||
|
||
case "False": | ||
proposed = "NotContains" | ||
if isNeg { | ||
proposed = "Contains" | ||
} | ||
} | ||
|
||
return newUseFunctionDiagnostic(checker.Name(), call, proposed, | ||
newSuggestedFuncReplacement(call, proposed, analysis.TextEdit{ | ||
Pos: call.Args[0].Pos(), | ||
End: call.Args[0].End(), | ||
NewText: formatAsCallArgs(pass, ce.Args[0], ce.Args[1]), | ||
}), | ||
) | ||
} |
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
Oops, something went wrong.