-
Notifications
You must be signed in to change notification settings - Fork 7
/
gen_require_error_fn_pattern.go
87 lines (72 loc) · 2.22 KB
/
gen_require_error_fn_pattern.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
package main
import (
"text/template"
"github.com/Antonboom/testifylint/internal/checkers"
)
type RequireErrorFnPatternTestsGenerator struct{}
func (g RequireErrorFnPatternTestsGenerator) TemplateData() any {
var (
checker = checkers.NewRequireError().Name()
report = checker + ": for error assertions use require"
)
return struct {
CheckerName CheckerName
ValidAssertions []Assertion
InvalidAssertions []Assertion
}{
CheckerName: CheckerName(checker),
ValidAssertions: []Assertion{
{Fn: "Error", Argsf: "err"},
{Fn: "ErrorIs", Argsf: "err, io.EOF"},
{Fn: "ErrorAs", Argsf: "err, &target"},
{Fn: "EqualError", Argsf: `err, "end of file"`},
{Fn: "ErrorContains", Argsf: `err, "end of file"`},
},
InvalidAssertions: []Assertion{
{Fn: "NoError", Argsf: "err", ReportMsgf: report},
{Fn: "NotErrorIs", Argsf: "err, io.EOF", ReportMsgf: report},
},
}
}
func (RequireErrorFnPatternTestsGenerator) ErroredTemplate() Executor {
return template.Must(template.New("RequireErrorFnPatternTestsGenerator.ErroredTemplate").
Funcs(fm).
Parse(requireErrorFnPatternTestTmpl))
}
func (RequireErrorFnPatternTestsGenerator) GoldenTemplate() Executor {
return nil
}
const requireErrorFnPatternTestTmpl = header + `
package {{ .CheckerName.AsPkgName }}
import (
"io"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func {{ .CheckerName.AsTestName }}(t *testing.T) {
var err error
var target = new(os.PathError)
// Invalid.
{{ range $ai, $assrn := $.InvalidAssertions }}
{{- if ne $assrn.Fn "NoError" -}}
{{- NewAssertionExpander.Expand $assrn "assert" "t" nil }}
{{ else }}
{{- NewAssertionExpander.NotFmtSingleMode.Expand $assrn "assert" "t" nil }}
nop()
{{ NewAssertionExpander.FmtSingleMode.Expand $assrn "assert" "t" nil }}
{{ end -}}
{{ end }}
// Valid.
{{ range $ai, $assrn := $.InvalidAssertions }}
{{- NewAssertionExpander.Expand $assrn.WithoutReport "require" "t" nil }}
{{ end -}}
{{ range $si, $sel := arr "assert" "require" }}
{{- range $ai, $assrn := $.ValidAssertions }}
{{- NewAssertionExpander.Expand $assrn $sel "t" nil }}
{{ end -}}
{{ end -}}
}
func nop() {} // Hack against ignoring of "NoError" sequence.
`