-
Notifications
You must be signed in to change notification settings - Fork 7
/
assertion_expander.go
152 lines (129 loc) · 4.3 KB
/
assertion_expander.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
package main
import (
"fmt"
"regexp"
"slices"
"strings"
)
type expandMode int
const (
expandModeFull expandMode = iota
expandModeNotFmtSet
expandModeFmtSet
expandModeNotFmtSingle
expandModeFmtSingle
expandModeExtreme
)
type AssertionExpander struct {
mode expandMode
asGolden bool
}
func NewAssertionExpander() *AssertionExpander {
return &AssertionExpander{
asGolden: false,
mode: expandModeExtreme,
}
}
// AsGolden turns AssertionExpander into "golden file" mode.
// This means that assertions from Expand will be constructed based on Assertion.Proposed* fields.
func (e *AssertionExpander) AsGolden() *AssertionExpander {
e.asGolden = true
return e
}
func (e *AssertionExpander) FullMode() *AssertionExpander {
e.mode = expandModeFull
return e
}
func (e *AssertionExpander) FmtSingleMode() *AssertionExpander {
e.mode = expandModeFmtSingle
return e
}
func (e *AssertionExpander) NotFmtSetMode() *AssertionExpander {
e.mode = expandModeNotFmtSet
return e
}
func (e *AssertionExpander) NotFmtSingleMode() *AssertionExpander {
e.mode = expandModeNotFmtSingle
return e
}
// Expand expands incoming Assertion into several assertion strings according to the rules:
//
// - build arguments string: testingTParam + Assertion.Argsf + argValues, e.g.
// "t" + "%s, 0" + "arr" = "t, arr, 0"
//
// - build assertion string: selector + Assertion.Fn + arguments, e.g.
// "assert" + "Len" + "t, arr, 0" = "assert.Len(t, arr, 0)"
//
// - if Assertion.ReportMsgf defined then append analysistest.Run diagnostic comment, e.g.
// "assert.Len(t, arr, 0)" + "empty: use %s.%s" = `assert.Len(t, arr, 0) // want "empty: use assert\\.Empty"`
//
// For diagnostic comment formatting Expand uses Assertion.ProposedSelector and Assertion.ProposedFn
// or selector and Assertion.Fn if one of them is not defined. If you do not need formatting use `%.s` (or `%.0s`).
//
// The final string is copied in several variants, depending on the current AssertionExpander mode.
func (e *AssertionExpander) Expand(assrn Assertion, selector, testingTParam string, argValues []any) string {
fn, args := assrn.Fn, assrn.Argsf
if e.asGolden {
selector = or(assrn.ProposedSelector, selector)
fn = or(assrn.ProposedFn, fn)
args = or(assrn.ProposedArgsf, args)
}
if testingTParam != "" {
args = testingTParam + ", " + args
}
if len(argValues) > 0 {
args = fmt.Sprintf(args, argValues...)
}
as := map[expandMode][]string{ // as is for "assertionSets".
expandModeNotFmtSet: {
buildAssertion(selector, fn, args, assrn.ReportMsgf, assrn.ProposedSelector, assrn.ProposedFn),
},
}
for _, newArgs := range []string{
`, "msg"`,
`, "msg with arg %d", 42`,
`, "msg with args %d %s", 42, "42"`,
} {
as[expandModeNotFmtSet] = append(as[expandModeNotFmtSet],
buildAssertion(selector, fn, args+newArgs, assrn.ReportMsgf, assrn.ProposedSelector, assrn.ProposedFn))
as[expandModeFmtSet] = append(as[expandModeFmtSet],
buildFmtAssertion(selector, fn, args+newArgs, assrn.ReportMsgf, assrn.ProposedSelector, assrn.ProposedFn))
}
as[expandModeFull] = append(slices.Clone(as[expandModeNotFmtSet]), as[expandModeFmtSet]...)
as[expandModeNotFmtSingle] = []string{as[expandModeNotFmtSet][0]}
as[expandModeFmtSingle] = []string{as[expandModeFmtSet][len(as[expandModeFmtSet])-1]}
as[expandModeExtreme] = append(slices.Clone(as[expandModeNotFmtSingle]), as[expandModeFmtSingle]...)
assertions, ok := as[e.mode]
if !ok {
panic(fmt.Sprintf("unsupported expand mode: %v", e.mode))
}
return strings.Join(assertions, "\n")
}
func buildAssertion(selector, fn, args, reportedMsgf, proposedSel, proposedFn string) string {
s := fmt.Sprintf("%s.%s(%s)", selector, fn, args)
if reportedMsgf != "" {
if or(proposedSel, proposedFn) != "" {
reportedMsgf = fmt.Sprintf(reportedMsgf, or(proposedSel, selector), or(proposedFn, fn))
}
s += " // want " + QuoteReport(reportedMsgf)
}
return s
}
func buildFmtAssertion(selector, fn, args, reportedMsgf, proposedSel, proposedFn string) string {
return buildAssertion(selector, withSuffixF(fn), args, reportedMsgf, proposedSel, withSuffixF(proposedFn))
}
func or(a, b string) string {
if a != "" {
return a
}
return b
}
func withSuffixF(s string) string {
if s == "" {
return s
}
return s + "f"
}
func QuoteReport(msg string) string {
return fmt.Sprintf("%q", regexp.QuoteMeta(msg))
}