Documentation ¶
Index ¶
- Constants
- Variables
- func All() []string
- func EnabledByDefault() []string
- func IsEnabledByDefault(name string) bool
- func IsKnown(name string) bool
- func SortByPriority(checkers []string)
- type AdvancedChecker
- type BlankImport
- type BoolCompare
- type CallMeta
- type Checker
- type Compares
- type Empty
- type ErrorIsAs
- type ErrorNil
- type ExpectedActual
- type FloatCompare
- type FnMeta
- type GoRequire
- type Len
- type NegativePositive
- type NilCompare
- type RegularChecker
- type RequireError
- type SuiteDontUsePkg
- type SuiteExtraAssertCall
- type SuiteExtraAssertCallMode
- type SuiteTHelper
- type UselessAssert
Constants ¶
const DefaultSuiteExtraAssertCallMode = SuiteExtraAssertCallModeRemove
Variables ¶
var DefaultExpectedVarPattern = regexp.MustCompile(
`(^(exp(ected)?|want(ed)?)([A-Z]\w*)?$)|(^(\w*[a-z])?(Exp(ected)?|Want(ed)?)$)`)
DefaultExpectedVarPattern matches variables with "expected" or "wanted" prefix or suffix in the name.
Functions ¶
func EnabledByDefault ¶
func EnabledByDefault() []string
EnabledByDefault returns checkers enabled by default sorted by checker's priority.
func IsEnabledByDefault ¶
IsEnabledByDefault returns true if a checker is enabled by default. Returns false if there is no such checker in the registry. For pre-validation use Get or IsKnown.
func SortByPriority ¶
func SortByPriority(checkers []string)
SortByPriority mutates the input checkers names by sorting them in checker priority order. Ignores unknown checkers. For pre-validation use Get or IsKnown.
Types ¶
type AdvancedChecker ¶
type AdvancedChecker interface { Checker Check(pass *analysis.Pass, inspector *inspector.Inspector) []analysis.Diagnostic }
AdvancedChecker implements complex Check logic different from trivial CallMeta check.
type BlankImport ¶ added in v1.1.0
type BlankImport struct{}
BlankImport detects useless blank imports of testify's packages. These imports are useless since testify doesn't do any magic with init() function.
The checker detects situations like
import ( "testing" _ "github.com/stretchr/testify" _ "github.com/stretchr/testify/assert" _ "github.com/stretchr/testify/http" _ "github.com/stretchr/testify/mock" _ "github.com/stretchr/testify/require" _ "github.com/stretchr/testify/suite" )
and requires
import ( "testing" )
func NewBlankImport ¶ added in v1.1.0
func NewBlankImport() BlankImport
NewBlankImport constructs BlankImport checker.
func (BlankImport) Check ¶ added in v1.1.0
func (checker BlankImport) Check(pass *analysis.Pass, _ *inspector.Inspector) (diagnostics []analysis.Diagnostic)
func (BlankImport) Name ¶ added in v1.1.0
func (BlankImport) Name() string
type BoolCompare ¶
type BoolCompare struct {
// contains filtered or unexported fields
}
BoolCompare detects situations like
assert.Equal(t, false, result) assert.EqualValues(t, false, result) assert.Exactly(t, false, result) assert.NotEqual(t, true, result) assert.NotEqualValues(t, true, result) assert.False(t, !result) assert.True(t, result == true) ...
and requires
assert.False(t, result) assert.True(t, result)
func NewBoolCompare ¶
func NewBoolCompare() *BoolCompare
NewBoolCompare constructs BoolCompare checker.
func (BoolCompare) Check ¶
func (checker BoolCompare) Check(pass *analysis.Pass, call *CallMeta) *analysis.Diagnostic
func (BoolCompare) Name ¶
func (BoolCompare) Name() string
func (*BoolCompare) SetIgnoreCustomTypes ¶ added in v1.2.0
func (checker *BoolCompare) SetIgnoreCustomTypes(v bool) *BoolCompare
type CallMeta ¶
type CallMeta struct { // Range contains start and end position of assertion call. analysis.Range // IsPkg true if this is package (not object) call. IsPkg bool // IsAssert true if this is "testify/assert" package (or object) call. IsAssert bool // Selector is the AST expression of "assert.Equal". Selector *ast.SelectorExpr // SelectorXStr is a string representation of Selector's left part – value before point, e.g. "assert". SelectorXStr string // Fn stores meta info about assertion function itself. Fn FnMeta // Args stores assertion call arguments but without `t *testing.T` argument. // E.g [42, result, "helpful comment"]. Args []ast.Expr // ArgsRaw stores assertion call initial arguments. // E.g [t, 42, result, "helpful comment"]. ArgsRaw []ast.Expr }
CallMeta stores meta info about assertion function/method call, for example
assert.Equal(t, 42, result, "helpful comment")
func NewCallMeta ¶ added in v1.0.0
NewCallMeta returns meta information about testify assertion call. Returns nil if ast.CallExpr is not testify call.
type Compares ¶
type Compares struct{}
Compares detects situations like
assert.True(t, a == b) assert.True(t, a != b) assert.True(t, a > b) assert.True(t, a >= b) assert.True(t, a < b) assert.True(t, a <= b) assert.False(t, a == b) ...
and requires
assert.Equal(t, a, b) assert.NotEqual(t, a, b) assert.Greater(t, a, b) assert.GreaterOrEqual(t, a, b) assert.Less(t, a, b) assert.LessOrEqual(t, a, b)
type Empty ¶
type Empty struct{}
Empty detects situations like
assert.Len(t, arr, 0) assert.Equal(t, 0, len(arr)) assert.EqualValues(t, 0, len(arr)) assert.Exactly(t, 0, len(arr)) assert.LessOrEqual(t, len(arr), 0) assert.GreaterOrEqual(t, 0, len(arr)) assert.Less(t, len(arr), 0) assert.Greater(t, 0, len(arr)) assert.Less(t, len(arr), 1) assert.Greater(t, 1, len(arr)) assert.NotEqual(t, 0, len(arr)) assert.NotEqualValues(t, 0, len(arr)) assert.Less(t, 0, len(arr)) assert.Greater(t, len(arr), 0)
and requires
assert.Empty(t, arr) assert.NotEmpty(t, arr)
type ErrorIsAs ¶
type ErrorIsAs struct{}
ErrorIsAs detects situations like
assert.Error(t, err, errSentinel) assert.NoError(t, err, errSentinel) assert.True(t, errors.Is(err, errSentinel)) assert.False(t, errors.Is(err, errSentinel)) assert.True(t, errors.As(err, &target))
and requires
assert.ErrorIs(t, err, errSentinel) assert.NotErrorIs(t, err, errSentinel) assert.ErrorAs(t, err, &target)
Also ErrorIsAs repeats go vet's "errorsas" check logic.
type ErrorNil ¶
type ErrorNil struct{}
ErrorNil detects situations like
assert.Nil(t, err) assert.NotNil(t, err) assert.Equal(t, nil, err) assert.EqualValues(t, nil, err) assert.Exactly(t, nil, err) assert.ErrorIs(t, err, nil) assert.NotEqual(t, nil, err) assert.NotEqualValues(t, nil, err) assert.NotErrorIs(t, err, nil)
and requires
assert.NoError(t, err) assert.Error(t, err)
type ExpectedActual ¶
type ExpectedActual struct {
// contains filtered or unexported fields
}
ExpectedActual detects situation like
assert.Equal(t, result, expected) assert.EqualExportedValues(t, resultObj, User{Name: "Anton"}) assert.EqualValues(t, result, 42) assert.Exactly(t, result, int64(42)) assert.JSONEq(t, result, `{"version": 3}`) assert.InDelta(t, result, 42.42, 1.0) assert.InDeltaMapValues(t, result, map[string]float64{"score": 0.99}, 1.0) assert.InDeltaSlice(t, result, []float64{0.98, 0.99}, 1.0) assert.InEpsilon(t, result, 42.42, 0.0001) assert.InEpsilonSlice(t, result, []float64{0.9801, 0.9902}, 0.0001) assert.IsType(t, result, (*User)(nil)) assert.NotEqual(t, result, "expected") assert.NotEqualValues(t, result, "expected") assert.NotSame(t, resultPtr, &value) assert.Same(t, resultPtr, &value) assert.WithinDuration(t, resultTime, time.Date(2023, 01, 12, 11, 46, 33, 0, nil), time.Second) assert.YAMLEq(t, result, "version: '3'")
and requires
assert.Equal(t, expected, result) assert.EqualExportedValues(t, User{Name: "Anton"}, resultObj) assert.EqualValues(t, 42, result) ...
func NewExpectedActual ¶
func NewExpectedActual() *ExpectedActual
NewExpectedActual constructs ExpectedActual checker using DefaultExpectedVarPattern.
func (ExpectedActual) Check ¶
func (checker ExpectedActual) Check(pass *analysis.Pass, call *CallMeta) *analysis.Diagnostic
func (ExpectedActual) Name ¶
func (ExpectedActual) Name() string
func (*ExpectedActual) SetExpVarPattern ¶
func (checker *ExpectedActual) SetExpVarPattern(p *regexp.Regexp) *ExpectedActual
type FloatCompare ¶
type FloatCompare struct{}
FloatCompare detects situation like
assert.Equal(t, 42.42, result) assert.EqualValues(t, 42.42, result) assert.Exactly(t, 42.42, result) assert.True(t, result == 42.42) assert.False(t, result != 42.42)
and requires
assert.InEpsilon(t, 42.42, result, 0.0001) // Or assert.InDelta
func NewFloatCompare ¶
func NewFloatCompare() FloatCompare
NewFloatCompare constructs FloatCompare checker.
func (FloatCompare) Check ¶
func (checker FloatCompare) Check(pass *analysis.Pass, call *CallMeta) *analysis.Diagnostic
func (FloatCompare) Name ¶
func (FloatCompare) Name() string
type FnMeta ¶
type FnMeta struct { // Range contains start and end position of function Name. analysis.Range // Name is a function name. Name string // NameFTrimmed is a function name without "f" suffix. NameFTrimmed string // IsFmt is true if function is formatted, e.g. "Equalf". IsFmt bool }
FnMeta stores meta info about assertion function itself, for example "Equal".
type GoRequire ¶ added in v1.0.0
type GoRequire struct {
// contains filtered or unexported fields
}
GoRequire takes idea from go vet's "testinggoroutine" check and detects usage of require package's functions or assert.FailNow in the non-test goroutines
go func() { conn, err = lis.Accept() require.NoError(t, err) if assert.Error(err) { assert.FailNow(t, msg) } }()
func NewGoRequire ¶ added in v1.0.0
func NewGoRequire() *GoRequire
NewGoRequire constructs GoRequire checker.
func (GoRequire) Check ¶ added in v1.0.0
func (checker GoRequire) Check(pass *analysis.Pass, inspector *inspector.Inspector) (diagnostics []analysis.Diagnostic)
Check should be consistent with https://cs.opensource.google/go/x/tools/+/master:go/analysis/passes/testinggoroutine/testinggoroutine.go
But due to the fact that the Check covers cases missed by go vet, the implementation turned out to be terribly complicated.
In simple words, the algorithm is as follows:
- we walk along the call tree and store the status, whether we are in the test goroutine or not;
- if we are in a test goroutine, then require is allowed, otherwise not;
- when we encounter the launch of a subtest or `go` statement, the status changes;
- in order to correctly handle the return to the correct status when exiting the current function, we have to store a stack of statuses (inGoroutineRunningTestFunc).
Other test functions called in the test function are also analyzed to make a verdict about the current function. This leads to recursion, which the cache of processed functions (processedFuncs) helps reduce the impact of. Also, because of this, we have to pre-collect a list of test function declarations (testsDecls).
func (*GoRequire) SetIgnoreHTTPHandlers ¶ added in v1.3.0
type Len ¶
type Len struct{}
Len detects situations like
assert.Equal(t, 3, len(arr)) assert.EqualValues(t, 3, len(arr)) assert.Exactly(t, 3, len(arr)) assert.True(t, len(arr) == 3)
and requires
assert.Len(t, arr, 3)
type NegativePositive ¶ added in v1.3.0
type NegativePositive struct{}
NegativePositive detects situations like
assert.Less(t, a, 0) assert.Greater(t, 0, a) assert.True(t, a < 0) assert.True(t, 0 > a) assert.False(t, a >= 0) assert.False(t, 0 <= a) assert.Greater(t, a, 0) assert.Less(t, 0, a) assert.True(t, a > 0) assert.True(t, 0 < a) assert.False(t, a <= 0) assert.False(t, 0 >= a)
and requires
assert.Negative(t, value) assert.Positive(t, value)
func NewNegativePositive ¶ added in v1.3.0
func NewNegativePositive() NegativePositive
NewNegativePositive constructs NegativePositive checker.
func (NegativePositive) Check ¶ added in v1.3.0
func (checker NegativePositive) Check(pass *analysis.Pass, call *CallMeta) *analysis.Diagnostic
func (NegativePositive) Name ¶ added in v1.3.0
func (NegativePositive) Name() string
type NilCompare ¶ added in v1.0.0
type NilCompare struct{}
NilCompare detects situations like
assert.Equal(t, nil, value) assert.EqualValues(t, nil, value) assert.Exactly(t, nil, value) assert.NotEqual(t, nil, value) assert.NotEqualValues(t, nil, value)
and requires
assert.Nil(t, value) assert.NotNil(t, value)
func NewNilCompare ¶ added in v1.0.0
func NewNilCompare() NilCompare
NewNilCompare constructs NilCompare checker.
func (NilCompare) Check ¶ added in v1.0.0
func (checker NilCompare) Check(pass *analysis.Pass, call *CallMeta) *analysis.Diagnostic
func (NilCompare) Name ¶ added in v1.0.0
func (NilCompare) Name() string
type RegularChecker ¶
type RegularChecker interface { Checker Check(pass *analysis.Pass, call *CallMeta) *analysis.Diagnostic }
RegularChecker check assertion call presented in CallMeta form.
type RequireError ¶
type RequireError struct {
// contains filtered or unexported fields
}
RequireError detects error assertions like
assert.Error(t, err) // s.Error(err), s.Assert().Error(err) assert.ErrorIs(t, err, io.EOF) assert.ErrorAs(t, err, &target) assert.EqualError(t, err, "end of file") assert.ErrorContains(t, err, "end of file") assert.NoError(t, err) assert.NotErrorIs(t, err, io.EOF)
and requires
require.Error(t, err) // s.Require().Error(err), s.Require().Error(err) require.ErrorIs(t, err, io.EOF) require.ErrorAs(t, err, &target) ...
RequireError ignores: - assertions in the `if` condition; - assertions in the bool expression; - the entire `if-else[-if]` block, if there is an assertion in any `if` condition; - the last assertion in the block, if there are no methods/functions calls after it; - assertions in an explicit goroutine (including `http.Handler`); - assertions in an explicit testing cleanup function or suite teardown methods; - sequence of NoError assertions.
func NewRequireError ¶
func NewRequireError() *RequireError
NewRequireError constructs RequireError checker.
func (RequireError) Check ¶
func (checker RequireError) Check(pass *analysis.Pass, inspector *inspector.Inspector) []analysis.Diagnostic
func (RequireError) Name ¶
func (RequireError) Name() string
func (*RequireError) SetFnPattern ¶ added in v1.0.0
func (checker *RequireError) SetFnPattern(p *regexp.Regexp) *RequireError
type SuiteDontUsePkg ¶
type SuiteDontUsePkg struct{}
SuiteDontUsePkg detects situation like
func (s *MySuite) TestSomething() { assert.Equal(s.T(), 42, value) }
and requires
func (s *MySuite) TestSomething() { s.Equal(42, value) }
func NewSuiteDontUsePkg ¶
func NewSuiteDontUsePkg() SuiteDontUsePkg
NewSuiteDontUsePkg constructs SuiteDontUsePkg checker.
func (SuiteDontUsePkg) Check ¶
func (checker SuiteDontUsePkg) Check(pass *analysis.Pass, call *CallMeta) *analysis.Diagnostic
func (SuiteDontUsePkg) Name ¶
func (SuiteDontUsePkg) Name() string
type SuiteExtraAssertCall ¶
type SuiteExtraAssertCall struct {
// contains filtered or unexported fields
}
SuiteExtraAssertCall detects situation like
func (s *MySuite) TestSomething() { s.Assert().Equal(42, value) }
and requires
func (s *MySuite) TestSomething() { s.Equal(42, value) }
or vice versa (depending on the configurable mode).
func NewSuiteExtraAssertCall ¶
func NewSuiteExtraAssertCall() *SuiteExtraAssertCall
NewSuiteExtraAssertCall constructs SuiteExtraAssertCall checker.
func (SuiteExtraAssertCall) Check ¶
func (checker SuiteExtraAssertCall) Check(pass *analysis.Pass, call *CallMeta) *analysis.Diagnostic
func (SuiteExtraAssertCall) Name ¶
func (SuiteExtraAssertCall) Name() string
func (*SuiteExtraAssertCall) SetMode ¶
func (checker *SuiteExtraAssertCall) SetMode(m SuiteExtraAssertCallMode) *SuiteExtraAssertCall
type SuiteExtraAssertCallMode ¶
type SuiteExtraAssertCallMode int
SuiteExtraAssertCallMode reflects different modes of work of SuiteExtraAssertCall checker.
const ( SuiteExtraAssertCallModeRemove SuiteExtraAssertCallMode = iota SuiteExtraAssertCallModeRequire )
type SuiteTHelper ¶
type SuiteTHelper struct{}
SuiteTHelper requires t.Helper() call in suite helpers:
func (s *RoomSuite) assertRoomRound(roundID RoundID) { s.T().Helper() s.Equal(roundID, s.getRoom().CurrentRound.ID) }
func NewSuiteTHelper ¶
func NewSuiteTHelper() SuiteTHelper
NewSuiteTHelper constructs SuiteTHelper checker.
func (SuiteTHelper) Check ¶
func (checker SuiteTHelper) Check(pass *analysis.Pass, inspector *inspector.Inspector) (diagnostics []analysis.Diagnostic)
func (SuiteTHelper) Name ¶
func (SuiteTHelper) Name() string
type UselessAssert ¶ added in v1.1.0
type UselessAssert struct{}
UselessAssert detects useless asserts like
1) Asserting of the same variable
assert.Equal(t, tt.value, tt.value) assert.ElementsMatch(t, users, users) ... assert.True(t, num > num) assert.False(t, num == num)
2) Open for contribution...
func NewUselessAssert ¶ added in v1.1.0
func NewUselessAssert() UselessAssert
NewUselessAssert constructs UselessAssert checker.
func (UselessAssert) Check ¶ added in v1.1.0
func (checker UselessAssert) Check(pass *analysis.Pass, call *CallMeta) *analysis.Diagnostic
func (UselessAssert) Name ¶ added in v1.1.0
func (UselessAssert) Name() string
Source Files ¶
- blank_import.go
- bool_compare.go
- call_meta.go
- checker.go
- checkers_registry.go
- compares.go
- empty.go
- error_is_as.go
- error_nil.go
- expected_actual.go
- float_compare.go
- go_require.go
- helpers.go
- helpers_basic_type.go
- helpers_bool.go
- helpers_comparison.go
- helpers_context.go
- helpers_diagnostic.go
- helpers_error.go
- helpers_format.go
- helpers_http.go
- helpers_interface.go
- helpers_len.go
- helpers_naming.go
- helpers_nil.go
- helpers_suite.go
- helpers_testing.go
- len.go
- negative_postive.go
- nil_compare.go
- require_error.go
- suite_dont_use_pkg.go
- suite_extra_assert_call.go
- suite_thelper.go
- useless_assert.go