-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.go
320 lines (275 loc) · 7.55 KB
/
test.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package testkit
import (
"context"
"fmt"
"reflect"
"regexp"
"strings"
"time"
"github.com/dogmatiq/configkit"
"github.com/dogmatiq/dapper"
"github.com/dogmatiq/dogma"
"github.com/dogmatiq/iago/must"
"github.com/dogmatiq/testkit/engine"
"github.com/dogmatiq/testkit/fact"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
// Test contains the state of a single test.
type Test struct {
ctx context.Context
testingT TestingT
app configkit.RichApplication
virtualClock time.Time
engine *engine.Engine
executor CommandExecutor
predicateOptions PredicateOptions
operationOptions []engine.OperationOption
annotations []Annotation
}
// Begin starts a new test.
func Begin(
t TestingT,
app dogma.Application,
options ...TestOption,
) *Test {
return BeginContext(
context.Background(),
t,
app,
options...,
)
}
// BeginContext starts a new test within a context.
func BeginContext(
ctx context.Context,
t TestingT,
app dogma.Application,
options ...TestOption,
) *Test {
cfg := configkit.FromApplication(app)
test := &Test{
ctx: ctx,
testingT: t,
app: cfg,
virtualClock: time.Now(),
engine: engine.MustNew(
cfg,
engine.EnableProjectionCompactionDuringHandling(true),
),
operationOptions: []engine.OperationOption{
engine.EnableProjections(false),
engine.EnableIntegrations(false),
engine.WithObserver(
fact.NewLogger(func(s string) {
log(t, s)
}),
),
},
}
for _, opt := range options {
opt.applyTestOption(test)
}
return test
}
// Prepare performs a group of actions without making any expectations. It is
// used to place the application into a particular state.
func (t *Test) Prepare(actions ...Action) *Test {
t.testingT.Helper()
for _, act := range actions {
logf(t.testingT, "--- %s ---", act.Caption())
if err := t.doAction(act); err != nil {
t.testingT.Fatal(err)
}
}
return t
}
// Expect ensures that a single action results in some expected behavior.
func (t *Test) Expect(act Action, e Expectation) *Test {
t.testingT.Helper()
s := PredicateScope{
App: t.app,
Options: t.predicateOptions,
}
act.ConfigurePredicate(&s.Options)
logf(t.testingT, "--- expect %s %s ---", act.Caption(), e.Caption())
p, err := e.Predicate(s)
if err != nil {
t.testingT.Fatal(err)
return t // required when using a mock testingT that does not panic
}
// Using a defer inside a closure satisfies the requirements of the
// Expectation and Predicate interfaces which state that p.Done() must
// be called exactly once, and that it must be called before calling
// p.Report().
if err := func() error {
defer p.Done()
return t.doAction(act, engine.WithObserver(p))
}(); err != nil {
t.testingT.Fatal(err)
return t // required when using a mock testingT that does not panic
}
options := []dapper.Option{
dapper.WithPackagePaths(false),
dapper.WithUnexportedStructFields(false),
}
for _, a := range t.annotations {
rt := reflect.TypeOf(a.Value)
options = append(
options,
dapper.WithAnnotator(
func(v dapper.Value) string {
// Check that the types are EXACT, otherwise the annotation
// can be duplicated, for example, once when boxed in an
// interface, and again when descending into that boxed
// value.
if rt != v.Value.Type() {
return ""
}
if !equal(a.Value, v.Value.Interface()) {
return ""
}
return a.Text
},
),
)
}
ctx := ReportGenerationContext{
TreeOk: p.Ok(),
printer: dapper.NewPrinter(options...),
}
rep := p.Report(ctx)
buf := &strings.Builder{}
fmt.Fprint(buf, "--- TEST REPORT ---\n\n")
must.WriteTo(buf, rep)
t.testingT.Log(buf.String())
if !ctx.TreeOk {
t.testingT.FailNow()
}
return t
}
// CommandExecutor returns a dogma.CommandExecutor which can be used to execute
// commands within the context of this test.
//
// The executor can be obtained at any time, but it can only be used within
// specific test actions.
//
// Call() is the only built-in action that supports the command executor. It may
// be supported by other user-defined actions.
func (t *Test) CommandExecutor() dogma.CommandExecutor {
return &t.executor
}
// Annotate adds an annotation to v.
//
// The annotation text is displayed whenever v is rendered in a test report.
func (t *Test) Annotate(v any, text string) *Test {
t.annotations = append(t.annotations, Annotation{v, text})
return t
}
// EnableHandlers enables a set of handlers by name.
//
// It panics if any of the handler names are not recognized.
//
// By default all integration and projection handlers are disabled.
func (t *Test) EnableHandlers(names ...string) *Test {
return t.enableHandlers(names, true)
}
// DisableHandlers disables a set of handlers by name.
//
// It panics if any of the handler names are not recognized.
//
// By default all integration and projection handlers are disabled.
func (t *Test) DisableHandlers(names ...string) *Test {
return t.enableHandlers(names, false)
}
// EnableHandlersLike enables any handlers with a name that matches one of
// the given regular expression patterns.
//
// It panics if any of patterns do not match any handlers.
//
// By default all integration and projection handlers are disabled.
func (t *Test) EnableHandlersLike(patterns ...string) *Test {
return t.enableHandlersLike(patterns, true)
}
// DisableHandlersLike enables any handlers with a name that matches one of
// the given regular expression patterns.
//
// It panics if any of patterns do not match any handlers.
//
// By default all integration and projection handlers are disabled.
func (t *Test) DisableHandlersLike(patterns ...string) *Test {
return t.enableHandlersLike(patterns, false)
}
func (t *Test) enableHandlers(names []string, enable bool) *Test {
for _, n := range names {
h, ok := t.app.Handlers().ByName(n)
if !ok {
panic(fmt.Sprintf(
"the %q application does not have a handler named %q",
t.app.Identity().Name,
n,
))
}
if enable && h.IsDisabled() {
panic(fmt.Sprintf(
"cannot enable the %q handler, it has been disabled by a call to %sConfigurer.Disable()",
n,
cases.
Title(language.English).
String(
h.HandlerType().String(),
),
))
}
t.operationOptions = append(
t.operationOptions,
engine.EnableHandler(n, enable),
)
}
return t
}
func (t *Test) enableHandlersLike(patterns []string, enable bool) *Test {
names := map[string]struct{}{}
for _, p := range patterns {
re := regexp.MustCompile(p)
matched := false
for ident, h := range t.app.Handlers() {
if !h.IsDisabled() && re.MatchString(ident.Name) {
names[ident.Name] = struct{}{}
matched = true
}
}
if !matched {
panic(fmt.Sprintf(
"the %q application does not have any handlers with names that match the regular expression (%s), or all such handlers have been disabled by a call to ProjectionConfigurer.Disable()",
t.app.Identity().Name,
p,
))
}
}
for n := range names {
t.operationOptions = append(
t.operationOptions,
engine.EnableHandler(n, enable),
)
}
return t
}
// doAction calls act.Do() with a scope appropriate for this test.
func (t *Test) doAction(act Action, options ...engine.OperationOption) error {
opts := []engine.OperationOption{
engine.WithCurrentTime(t.virtualClock),
}
opts = append(opts, t.operationOptions...)
opts = append(opts, options...)
return act.Do(
t.ctx,
ActionScope{
App: t.app,
VirtualClock: &t.virtualClock,
Engine: t.engine,
Executor: &t.executor,
OperationOptions: opts,
},
)
}