-
Notifications
You must be signed in to change notification settings - Fork 7
/
gen_nil_compare.go
197 lines (167 loc) · 6.84 KB
/
gen_nil_compare.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package main
import (
"strings"
"text/template"
"github.com/Antonboom/testifylint/internal/checkers"
)
type NilCompareTestsGenerator struct{}
func (NilCompareTestsGenerator) Checker() checkers.Checker {
return checkers.NewNilCompare()
}
func (g NilCompareTestsGenerator) TemplateData() any {
var (
checker = g.Checker().Name()
report = checker + ": use %s.%s"
)
var unsupportedAssertions []Assertion
for _, fn := range []string{"Equal", "EqualValues", "Exactly", "NotEqual", "NotEqualValues"} {
unsupportedAssertions = append(unsupportedAssertions, []Assertion{
{Fn: fn, Argsf: "(chan struct{})(nil), ch"},
{Fn: fn, Argsf: "(func())(nil), fn"},
{Fn: fn, Argsf: "(any)(nil), iface"},
{Fn: fn, Argsf: "(map[int]int)(nil), mp"},
{Fn: fn, Argsf: "(*int)(nil), ptr"},
{Fn: fn, Argsf: "[]int(nil), slice"},
{Fn: fn, Argsf: "(unsafe.Pointer)(nil), unsafePtr"},
}...)
}
return struct {
CheckerName CheckerName
BullshitAssertions []Assertion
InvalidAssertions []Assertion
UnsupportedAssertions []Assertion
ValidAssertions []Assertion
IgnoredAssertions []Assertion
UntypedNilBug []Assertion
}{
CheckerName: CheckerName(checker),
BullshitAssertions: []Assertion{
{Fn: "Equal", Argsf: "nil, %s", ReportMsgf: report, ProposedFn: "Nil", ProposedArgsf: "%s"},
{Fn: "Equal", Argsf: "%s, nil", ReportMsgf: report, ProposedFn: "Nil", ProposedArgsf: "%s"},
{Fn: "EqualValues", Argsf: "nil, %s", ReportMsgf: report, ProposedFn: "Nil", ProposedArgsf: "%s"},
{Fn: "EqualValues", Argsf: "%s, nil", ReportMsgf: report, ProposedFn: "Nil", ProposedArgsf: "%s"},
{Fn: "Exactly", Argsf: "nil, %s", ReportMsgf: report, ProposedFn: "Nil", ProposedArgsf: "%s"},
{Fn: "Exactly", Argsf: "%s, nil", ReportMsgf: report, ProposedFn: "Nil", ProposedArgsf: "%s"},
{Fn: "NotEqual", Argsf: "nil, %s", ReportMsgf: report, ProposedFn: "NotNil", ProposedArgsf: "%s"},
{Fn: "NotEqual", Argsf: "%s, nil", ReportMsgf: report, ProposedFn: "NotNil", ProposedArgsf: "%s"},
{Fn: "NotEqualValues", Argsf: "nil, %s", ReportMsgf: report, ProposedFn: "NotNil", ProposedArgsf: "%s"},
{Fn: "NotEqualValues", Argsf: "%s, nil", ReportMsgf: report, ProposedFn: "NotNil", ProposedArgsf: "%s"},
},
InvalidAssertions: []Assertion{
{Fn: "Equal", Argsf: "nil, iface", ReportMsgf: report, ProposedFn: "Nil", ProposedArgsf: "iface"},
{Fn: "EqualValues", Argsf: "nil, iface", ReportMsgf: report, ProposedFn: "Nil", ProposedArgsf: "iface"},
{Fn: "Exactly", Argsf: "nil, iface", ReportMsgf: report, ProposedFn: "Nil", ProposedArgsf: "iface"},
{Fn: "NotEqual", Argsf: "nil, iface", ReportMsgf: report, ProposedFn: "NotNil", ProposedArgsf: "iface"},
{Fn: "NotEqualValues", Argsf: "nil, iface", ReportMsgf: report, ProposedFn: "NotNil", ProposedArgsf: "iface"},
},
UnsupportedAssertions: unsupportedAssertions,
ValidAssertions: []Assertion{
{Fn: "Nil", Argsf: "iface"},
{Fn: "NotNil", Argsf: "iface"},
},
IgnoredAssertions: []Assertion{
{Fn: "Equal", Argsf: "iface, iface"},
{Fn: "Equal", Argsf: "nil, nil"},
{Fn: "EqualValues", Argsf: "iface, iface"},
{Fn: "EqualValues", Argsf: "nil, nil"},
{Fn: "Exactly", Argsf: "iface, iface"},
{Fn: "Exactly", Argsf: "nil, nil"},
{Fn: "NotEqual", Argsf: "iface, iface"},
{Fn: "NotEqual", Argsf: "nil, nil"},
{Fn: "NotEqualValues", Argsf: "iface, iface"},
{Fn: "NotEqualValues", Argsf: "nil, nil"},
},
// Some special cases that covers bug from the past. The reason is
// pass.TypesInfo.TypeOf(interface).(*types.Basic).Kind() == types.UntypedNil
// https://github.com/golang/go/blob/3e54329cbe41e887f309c733a619206bf6351768/src/go/types/expr.go#L428
UntypedNilBug: []Assertion{
{Fn: "Equal", Argsf: `nil, Row["col"]`, ReportMsgf: report, ProposedFn: "Nil", ProposedArgsf: `Row["col"]`},
{Fn: "EqualValues", Argsf: `nil, Row["col"]`, ReportMsgf: report, ProposedFn: "Nil", ProposedArgsf: `Row["col"]`},
{Fn: "Exactly", Argsf: `nil, Row["col"]`, ReportMsgf: report, ProposedFn: "Nil", ProposedArgsf: `Row["col"]`},
{Fn: "NotEqual", Argsf: `nil, Row["col"]`, ReportMsgf: report, ProposedFn: "NotNil", ProposedArgsf: `Row["col"]`},
{Fn: "NotEqualValues", Argsf: `nil, Row["col"]`, ReportMsgf: report, ProposedFn: "NotNil", ProposedArgsf: `Row["col"]`},
{Fn: "Equal", Argsf: `Row["col"], "foo"`},
{Fn: "Equal", Argsf: `"foo", Row["col"]`},
{Fn: "Equal", Argsf: `Row["col"], Row["col"]`},
{Fn: "EqualValues", Argsf: `Row["col"], "foo"`},
{Fn: "EqualValues", Argsf: `"foo", Row["col"]`},
{Fn: "EqualValues", Argsf: `Row["col"], Row["col"]`},
{Fn: "Exactly", Argsf: `Row["col"], "foo"`},
{Fn: "Exactly", Argsf: `"foo", Row["col"]`},
{Fn: "Exactly", Argsf: `Row["col"], Row["col"]`},
{Fn: "NotEqual", Argsf: `Row["col"], "foo"`},
{Fn: "NotEqual", Argsf: `"foo", Row["col"]`},
{Fn: "NotEqual", Argsf: `Row["col"], Row["col"]`},
{Fn: "NotEqualValues", Argsf: `Row["col"], "foo"`},
{Fn: "NotEqualValues", Argsf: `"foo", Row["col"]`},
{Fn: "NotEqualValues", Argsf: `Row["col"], Row["col"]`},
},
}
}
func (NilCompareTestsGenerator) ErroredTemplate() Executor {
return template.Must(template.New("NilCompareTestsGenerator.ErroredTemplate").
Funcs(fm).
Parse(nilCompareTestTmpl))
}
func (NilCompareTestsGenerator) GoldenTemplate() Executor {
return template.Must(template.New("NilCompareTestsGenerator.GoldenTemplate").
Funcs(fm).
Parse(strings.ReplaceAll(nilCompareTestTmpl, "NewAssertionExpander", "NewAssertionExpander.AsGolden")))
}
const nilCompareTestTmpl = header + `
package {{ .CheckerName.AsPkgName }}
import (
"testing"
"unsafe"
"github.com/stretchr/testify/assert"
)
func {{ .CheckerName.AsTestName }}(t *testing.T) {
var (
ch chan struct{}
fn func()
iface any
mp map[int]int
ptr *int
slice []int
unsafePtr unsafe.Pointer
)
// Not working as expected!
{
{{- range $ai, $assrn := $.BullshitAssertions }}
{{- range $vi, $var := (arr "ch" "fn" "mp" "ptr" "slice" "unsafePtr") }}
{{ NewAssertionExpander.Expand $assrn "assert" "t" (arr $var) }}
{{- end }}
{{- end }}
}
// Invalid.
{
{{- range $ai, $assrn := $.InvalidAssertions }}
{{ NewAssertionExpander.Expand $assrn "assert" "t" nil }}
{{- end }}
}
// Invalid, but unsupported.
{
{{- range $ai, $assrn := $.UnsupportedAssertions }}
{{ 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 }}_UntypedNilBug(t *testing.T) {
var Row map[string]any
{{ range $ai, $assrn := $.UntypedNilBug }}
{{ NewAssertionExpander.Expand $assrn "assert" "t" nil }}
{{- end }}
}
`