/
gen_error_nil.go
163 lines (141 loc) · 4.82 KB
/
gen_error_nil.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"strings"
"text/template"
"github.com/Antonboom/testifylint/internal/checkers"
)
type ErrorNilTestsGenerator struct{}
func (ErrorNilTestsGenerator) Checker() checkers.Checker {
return checkers.NewErrorNil()
}
func (g ErrorNilTestsGenerator) TemplateData() any {
var (
checker = g.Checker().Name()
report = checker + ": use %s.%s"
)
type errorDetectionTest struct {
Vars []string
Assrn Assertion
}
type validNilsTest struct {
Vars []string
Assertions []Assertion
}
return struct {
CheckerName CheckerName
ErrDetection errorDetectionTest
ValidNils validNilsTest
InvalidAssertions []Assertion
ValidAssertions []Assertion
IgnoredAssertions []Assertion
}{
CheckerName: CheckerName(checker),
ErrDetection: errorDetectionTest{
Vars: []string{"a", "b.Get1()", "c", "errOp()"},
Assrn: Assertion{Fn: "Nil", Argsf: "%s", ReportMsgf: report, ProposedFn: "NoError"},
},
ValidNils: validNilsTest{
Vars: []string{"ptr", "iface", "ch", "sl", "fn", "m", "uPtr"},
Assertions: []Assertion{
{Fn: "Nil", Argsf: "%s"},
{Fn: "NotNil", Argsf: "%s"},
},
},
InvalidAssertions: []Assertion{
{Fn: "Nil", Argsf: "err", ReportMsgf: report, ProposedFn: "NoError"},
{Fn: "NotNil", Argsf: "err", ReportMsgf: report, ProposedFn: "Error"},
{Fn: "Equal", Argsf: "err, nil", ReportMsgf: report, ProposedFn: "NoError", ProposedArgsf: "err"},
{Fn: "Equal", Argsf: "nil, err", ReportMsgf: report, ProposedFn: "NoError", ProposedArgsf: "err"},
{Fn: "EqualValues", Argsf: "err, nil", ReportMsgf: report, ProposedFn: "NoError", ProposedArgsf: "err"},
{Fn: "EqualValues", Argsf: "nil, err", ReportMsgf: report, ProposedFn: "NoError", ProposedArgsf: "err"},
{Fn: "Exactly", Argsf: "err, nil", ReportMsgf: report, ProposedFn: "NoError", ProposedArgsf: "err"},
{Fn: "Exactly", Argsf: "nil, err", ReportMsgf: report, ProposedFn: "NoError", ProposedArgsf: "err"},
{Fn: "NotEqual", Argsf: "err, nil", ReportMsgf: report, ProposedFn: "Error", ProposedArgsf: "err"},
{Fn: "NotEqual", Argsf: "nil, err", ReportMsgf: report, ProposedFn: "Error", ProposedArgsf: "err"},
{Fn: "NotEqualValues", Argsf: "err, nil", ReportMsgf: report, ProposedFn: "Error", ProposedArgsf: "err"},
{Fn: "NotEqualValues", Argsf: "nil, err", ReportMsgf: report, ProposedFn: "Error", ProposedArgsf: "err"},
{Fn: "ErrorIs", Argsf: "err, nil", ReportMsgf: report, ProposedFn: "NoError", ProposedArgsf: "err"},
{Fn: "NotErrorIs", Argsf: "err, nil", ReportMsgf: report, ProposedFn: "Error", ProposedArgsf: "err"},
},
ValidAssertions: []Assertion{
{Fn: "NoError", Argsf: "err"},
{Fn: "Error", Argsf: "err"},
},
IgnoredAssertions: []Assertion{
{Fn: "Nil", Argsf: "nil"},
{Fn: "NotNil", Argsf: "nil"},
{Fn: "Equal", Argsf: "err, err"},
{Fn: "Equal", Argsf: "nil, nil"},
{Fn: "NotEqual", Argsf: "err, err"},
{Fn: "NotEqual", Argsf: "nil, nil"},
},
}
}
func (ErrorNilTestsGenerator) ErroredTemplate() Executor {
return template.Must(template.New("ErrorNilTestsGenerator.ErroredTemplate").
Funcs(fm).
Parse(errorNilTestTmpl))
}
func (ErrorNilTestsGenerator) GoldenTemplate() Executor {
return template.Must(template.New("ErrorNilTestsGenerator.GoldenTemplate").
Funcs(fm).
Parse(strings.ReplaceAll(errorNilTestTmpl, "NewAssertionExpander", "NewAssertionExpander.AsGolden")))
}
const errorNilTestTmpl = header + `
package {{ .CheckerName.AsPkgName }}
import (
"io"
"testing"
"unsafe"
"github.com/stretchr/testify/assert"
)
func {{ .CheckerName.AsTestName }}(t *testing.T) {
var err error
// 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 }}
}
}
func {{ .CheckerName.AsTestName }}_ErrorDetection(t *testing.T) {
errOp := func() error { return io.EOF }
var a error
var b withErroredMethod
_, c := b.Get2()
{{ range $vi, $var := $.ErrDetection.Vars }}
{{ NewAssertionExpander.NotFmtSingleMode.Expand $.ErrDetection.Assrn "assert" "t" (arr $var) }}
{{- end }}
}
func {{ .CheckerName.AsTestName }}_ValidNils(t *testing.T) {
var (
ptr *int
iface any
ch chan error
sl []error
fn func()
m map[int]int
uPtr unsafe.Pointer
)
{{ range $vi, $var := $.ValidNils.Vars }}
{{- range $ai, $assrn := $.ValidNils.Assertions }}
{{ NewAssertionExpander.NotFmtSingleMode.Expand $assrn "assert" "t" (arr $var) }}
{{- end }}
{{- end }}
}
type withErroredMethod struct{}
func (withErroredMethod) Get1() error { return nil }
func (withErroredMethod) Get2() (int, error) { return 0, nil }
`