forked from golangci/golangci-lint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestifylint.go
98 lines (79 loc) · 3.64 KB
/
testifylint.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//golangcitest:args -Etestifylint
package testdata
import (
"fmt"
"io"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)
type Bool bool
func TestTestifylint(t *testing.T) {
var (
predicate bool
resultInt int
resultFloat float64
arr []string
err error
)
assert.Equal(t, predicate, true) // want "bool-compare: use assert\\.True"
assert.Equal(t, Bool(predicate), false) // want "bool-compare: use assert\\.False"
assert.True(t, resultInt == 1) // want "compares: use assert\\.Equal"
assert.Equal(t, len(arr), 0) // want "empty: use assert\\.Empty"
assert.Error(t, err, io.EOF) // want "error-is-as: invalid usage of assert\\.Error, use assert\\.ErrorIs instead"
assert.Nil(t, err) // want "error-nil: use assert\\.NoError"
assert.Equal(t, resultInt, 42) // want "expected-actual: need to reverse actual and expected values"
assert.Equal(t, resultFloat, 42.42) // want "float-compare: use assert\\.InEpsilon \\(or InDelta\\)"
assert.Equal(t, len(arr), 10) // want "expected-actual: need to reverse actual and expected values"
assert.Equal(t, 10, len(arr)) // want "len: use assert\\.Len"
assert.True(t, predicate)
assert.Equal(t, resultInt, 1) // want "expected-actual: need to reverse actual and expected values"
assert.Empty(t, arr)
assert.ErrorIs(t, err, io.EOF) // want "require-error: for error assertions use require"
assert.NoError(t, err) // want "require-error: for error assertions use require"
assert.Equal(t, 42, resultInt)
assert.InEpsilon(t, 42.42, resultFloat, 0.0001)
assert.Len(t, arr, 10)
require.ErrorIs(t, err, io.EOF)
require.NoError(t, err)
t.Run("formatted", func(t *testing.T) {
assert.Equal(t, predicate, true, "message") // want "bool-compare: use assert\\.True"
assert.Equal(t, predicate, true, "message %d", 42) // want "bool-compare: use assert\\.True"
assert.Equalf(t, predicate, true, "message") // want "bool-compare: use assert\\.Truef"
assert.Equalf(t, predicate, true, "message %d", 42) // want "bool-compare: use assert\\.Truef"
assert.Equal(t, 1, 2, fmt.Sprintf("msg")) // want "formatter: remove unnecessary fmt\\.Sprintf"
assert.Equalf(t, 1, 2, "msg with arg", "arg") // want "formatter: assert\\.Equalf call has arguments but no formatting directives"
})
assert.Equal(t, arr, nil) // want "nil-compare: use assert\\.Nil"
assert.Nil(t, arr)
go func() {
if assert.Error(t, err) {
require.ErrorIs(t, err, io.EOF) // want "go-require: require must only be used in the goroutine running the test function"
}
}()
}
type SuiteExample struct {
suite.Suite
}
func TestSuiteExample(t *testing.T) {
suite.Run(t, new(SuiteExample))
}
func (s *SuiteExample) TestAll() {
var b bool
s.Assert().True(b) // want "suite-extra-assert-call: need to simplify the assertion to s\\.True"
}
func (s *SuiteExample) TestOne() {
s.T().Parallel() // want "suite-broken-parallel: testify v1 does not support suite's parallel tests and subtests"
s.T().Run("subtest", func(t *testing.T) { // want "suite-subtest-run: use s\\.Run to run subtest"
t.Parallel() // want "suite-broken-parallel: testify v1 does not support suite's parallel tests and subtests"
assert.Equal(s.T(), 1, 2) // want "suite-dont-use-pkg: use s\\.Equal"
s.Equal(1, 2)
})
s.Run("subtest", func() {
s.T().Parallel() // want "suite-broken-parallel: testify v1 does not support suite's parallel tests and subtests"
s.Equal(1, 2)
})
var b bool
s.Assert().True(b) // want "suite-extra-assert-call: need to simplify the assertion to s\\.True"
}