-
Notifications
You must be signed in to change notification settings - Fork 0
/
expectation.message.go
213 lines (183 loc) · 5.33 KB
/
expectation.message.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
package testkit
import (
"fmt"
"reflect"
"github.com/dogmatiq/configkit/message"
"github.com/dogmatiq/dogma"
"github.com/dogmatiq/testkit/envelope"
"github.com/dogmatiq/testkit/fact"
"github.com/dogmatiq/testkit/internal/inflect"
"github.com/dogmatiq/testkit/internal/report"
"github.com/dogmatiq/testkit/internal/typecmp"
)
// ToExecuteCommand returns an expectation that passes if a command is executed
// that is equal to m.
func ToExecuteCommand(m dogma.Command) Expectation {
if err := validateMessage(m); err != nil {
panic(fmt.Sprintf("ToExecuteCommand(%T): %s", m, err))
}
return &messageExpectation{
expectedMessage: m,
expectedType: message.TypeOf(m),
expectedRole: message.CommandRole,
}
}
// ToRecordEvent returns an expectation that passes if an event is recorded that
// is equal to m.
func ToRecordEvent(m dogma.Command) Expectation {
if err := validateMessage(m); err != nil {
panic(fmt.Sprintf("ToRecordEvent(%T): %s", m, err))
}
return &messageExpectation{
expectedMessage: m,
expectedType: message.TypeOf(m),
expectedRole: message.EventRole,
}
}
// messageTypeExpectation is an Expectation that checks that specific message is
// produced.
//
// It is the implementation used by ToExecuteCommand() and ToRecordEvent().
type messageExpectation struct {
expectedMessage dogma.Message
expectedType message.Type
expectedRole message.Role
}
func (e *messageExpectation) Caption() string {
return inflect.Sprintf(
e.expectedRole,
"to <produce> a specific '%s' <message>",
e.expectedType,
)
}
func (e *messageExpectation) Predicate(s PredicateScope) (Predicate, error) {
return &messagePredicate{
messageComparator: s.Options.MessageComparator,
expectedMessage: e.expectedMessage,
expectedType: e.expectedType,
expectedRole: e.expectedRole,
bestMatchDistance: typecmp.Unrelated,
tracker: tracker{
role: e.expectedRole,
options: s.Options,
},
}, validateRole(s, e.expectedType, e.expectedRole)
}
// messagePredicate is the Predicate implementation for messageExpectation.
type messagePredicate struct {
messageComparator MessageComparator
expectedMessage dogma.Message
expectedRole message.Role
expectedType message.Type
ok bool
bestMatch *envelope.Envelope
bestMatchDistance typecmp.Distance
bestMatchIsEqual bool
tracker tracker
}
// Notify updates the expectation's state in response to a new fact.
func (p *messagePredicate) Notify(f fact.Fact) {
if p.ok {
return
}
if env, ok := p.tracker.Notify(f); ok {
p.messageProduced(env)
}
}
// messageProduced updates the predicate's state to reflect the fact that a
// message has been produced.
func (p *messagePredicate) messageProduced(env *envelope.Envelope) {
isEqual := p.messageComparator
if isEqual == nil {
isEqual = DefaultMessageComparator
}
if !isEqual(env.Message, p.expectedMessage) {
p.updateBestMatch(env)
return
}
p.bestMatch = env
p.bestMatchDistance = typecmp.Identical
p.bestMatchIsEqual = true
if env.Role == p.expectedRole {
p.ok = true
}
}
// updateBestMatch replaces p.bestMatch with env if it is a better match.
func (p *messagePredicate) updateBestMatch(env *envelope.Envelope) {
dist := typecmp.MeasureDistance(
reflect.TypeOf(p.expectedMessage),
reflect.TypeOf(env.Message),
)
if dist < p.bestMatchDistance {
p.bestMatch = env
p.bestMatchDistance = dist
}
}
func (p *messagePredicate) Ok() bool {
return p.ok
}
func (p *messagePredicate) Done() {
}
func (p *messagePredicate) Report(ctx ReportGenerationContext) *Report {
rep := &Report{
TreeOk: ctx.TreeOk,
Ok: p.ok,
Criteria: inflect.Sprintf(
p.expectedRole,
"<produce> a specific '%s' <message>",
message.TypeOf(p.expectedMessage),
),
}
if p.ok || ctx.TreeOk || ctx.IsInverted {
return rep
}
if p.bestMatch == nil {
reportNoMatch(rep, &p.tracker)
return rep
}
s := rep.Section(suggestionsSection)
if p.bestMatchDistance == typecmp.Identical {
if p.bestMatch.Origin == nil {
rep.Explanation = inflect.Sprint(
p.expectedRole,
"a similar <message> was <produced> via a <dispatcher>",
)
} else {
rep.Explanation = inflect.Sprintf(
p.expectedRole,
"a similar <message> was <produced> by the '%s' %s message handler",
p.bestMatch.Origin.Handler.Identity().Name,
p.bestMatch.Origin.HandlerType,
)
}
s.AppendListItem("check the content of the message")
} else {
if p.bestMatch.Origin == nil {
rep.Explanation = inflect.Sprint(
p.expectedRole,
"a <message> of a similar type was <produced> via a <dispatcher>",
)
} else {
rep.Explanation = inflect.Sprintf(
p.expectedRole,
"a <message> of a similar type was <produced> by the '%s' %s message handler",
p.bestMatch.Origin.Handler.Identity().Name,
p.bestMatch.Origin.HandlerType,
)
}
// note this language here is deliberately vague, it doesn't imply
// whether it currently is or isn't a pointer, just questions if it
// should be.
s.AppendListItem("check the message type, should it be a pointer?")
}
p.buildDiff(ctx, rep)
return rep
}
// buildDiff adds a "message diff" section to the result.
func (p *messagePredicate) buildDiff(ctx ReportGenerationContext, rep *Report) {
report.WriteDiff(
&rep.Section("Message Diff").Content,
ctx.renderMessage(p.expectedMessage),
ctx.renderMessage(p.bestMatch.Message),
)
}