-
Notifications
You must be signed in to change notification settings - Fork 6
/
gen_compares.go
114 lines (96 loc) · 3.41 KB
/
gen_compares.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
package main
import (
"fmt"
"go/token"
"strings"
"text/template"
"github.com/Antonboom/testifylint/internal/checkers"
)
type ComparesTestsGenerator struct{}
func (ComparesTestsGenerator) Checker() checkers.Checker {
return checkers.NewCompares()
}
func (g ComparesTestsGenerator) TemplateData() any {
var (
checker = g.Checker().Name()
report = checker + ": use %s.%s"
)
boolOps := []token.Token{token.LAND, token.LOR}
ignored := make([]Assertion, 0, len(boolOps)*2)
for _, tok := range boolOps {
ignored = append(ignored,
Assertion{Fn: "True", Argsf: fmt.Sprintf("c %s d", tok)},
Assertion{Fn: "False", Argsf: fmt.Sprintf("d %s c", tok)},
)
}
return struct {
CheckerName CheckerName
InvalidAssertions []Assertion
ValidAssertions []Assertion
IgnoredAssertions []Assertion
}{
CheckerName: CheckerName(checker),
InvalidAssertions: []Assertion{
{Fn: "True", Argsf: "a == b", ReportMsgf: report, ProposedFn: "Equal", ProposedArgsf: "a, b"},
{Fn: "True", Argsf: "a != b", ReportMsgf: report, ProposedFn: "NotEqual", ProposedArgsf: "a, b"},
{Fn: "True", Argsf: "a > b", ReportMsgf: report, ProposedFn: "Greater", ProposedArgsf: "a, b"},
{Fn: "True", Argsf: "a >= b", ReportMsgf: report, ProposedFn: "GreaterOrEqual", ProposedArgsf: "a, b"},
{Fn: "True", Argsf: "a < b", ReportMsgf: report, ProposedFn: "Less", ProposedArgsf: "a, b"},
{Fn: "True", Argsf: "a <= b", ReportMsgf: report, ProposedFn: "LessOrEqual", ProposedArgsf: "a, b"},
{Fn: "False", Argsf: "a == b", ReportMsgf: report, ProposedFn: "NotEqual", ProposedArgsf: "a, b"},
{Fn: "False", Argsf: "a != b", ReportMsgf: report, ProposedFn: "Equal", ProposedArgsf: "a, b"},
{Fn: "False", Argsf: "a > b", ReportMsgf: report, ProposedFn: "LessOrEqual", ProposedArgsf: "a, b"},
{Fn: "False", Argsf: "a >= b", ReportMsgf: report, ProposedFn: "Less", ProposedArgsf: "a, b"},
{Fn: "False", Argsf: "a < b", ReportMsgf: report, ProposedFn: "GreaterOrEqual", ProposedArgsf: "a, b"},
{Fn: "False", Argsf: "a <= b", ReportMsgf: report, ProposedFn: "Greater", ProposedArgsf: "a, b"},
},
ValidAssertions: []Assertion{
{Fn: "Equal", Argsf: "a, b"},
{Fn: "NotEqual", Argsf: "a, b"},
{Fn: "Greater", Argsf: "a, b"},
{Fn: "GreaterOrEqual", Argsf: "a, b"},
{Fn: "Less", Argsf: "a, b"},
{Fn: "LessOrEqual", Argsf: "a, b"},
},
IgnoredAssertions: ignored,
}
}
func (ComparesTestsGenerator) ErroredTemplate() Executor {
return template.Must(template.New("ComparesTestsGenerator.ErroredTemplate").
Funcs(fm).
Parse(comparesTestTmpl))
}
func (ComparesTestsGenerator) GoldenTemplate() Executor {
return template.Must(template.New("ComparesTestsGenerator.GoldenTemplate").
Funcs(fm).
Parse(strings.ReplaceAll(comparesTestTmpl, "NewAssertionExpander", "NewAssertionExpander.AsGolden")))
}
const comparesTestTmpl = header + `
package {{ .CheckerName.AsPkgName }}
import (
"testing"
"github.com/stretchr/testify/assert"
)
func {{ .CheckerName.AsTestName }}(t *testing.T) {
var a, b int
var c, d bool
// 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 }}
}
}
`