-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexpectation.composite.go
196 lines (160 loc) · 3.78 KB
/
expectation.composite.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
package testkit
import (
"fmt"
"github.com/dogmatiq/testkit/fact"
)
// AllOf is an expectation that passes only if all of its children pass.
func AllOf(children ...Expectation) Expectation {
n := len(children)
if n == 0 {
panic("AllOf(): at least one child expectation must be provided")
}
if n == 1 {
return children[0]
}
return &compositeExpectation{
caption: fmt.Sprintf("to meet %d expectations", n),
criteria: "all of",
children: children,
pred: func(passed int) (string, bool) {
if passed == n {
return "", true
}
return fmt.Sprintf(
"%d of the expectations failed",
n-passed,
), false
},
}
}
// AnyOf is an expectation that passes if any of its children pass.
func AnyOf(children ...Expectation) Expectation {
n := len(children)
if n == 0 {
panic("AnyOf(): at least one child expectation must be provided")
}
if n == 1 {
return children[0]
}
return &compositeExpectation{
caption: fmt.Sprintf("to meet at least one of %d expectations", n),
criteria: "any of",
children: children,
pred: func(passed int) (string, bool) {
if passed > 0 {
return "", true
}
return fmt.Sprintf(
"all %d of the expectations failed",
n,
), false
},
}
}
// NoneOf is an expectation that passes only if all of its children fail.
func NoneOf(children ...Expectation) Expectation {
n := len(children)
if n == 0 {
panic("NoneOf(): at least one child expectation must be provided")
}
caption := fmt.Sprintf("not to meet any of %d expectations", n)
if n == 1 {
caption = fmt.Sprintf("not %s", children[0].Caption())
}
return &compositeExpectation{
caption: caption,
criteria: "none of",
children: children,
pred: func(passed int) (string, bool) {
if passed == 0 {
return "", true
}
if n == 1 {
return "the expectation passed unexpectedly", false
}
return fmt.Sprintf(
"%d of the expectations passed unexpectedly",
passed,
), false
},
isInverted: true,
}
}
// compositeExpectation is an Expectation that contains other expectations.
//
// It is the implementation used by AllOf(), AnyOf() and NoneOf().
//
// It uses a predicate function to determine whether the composite expectation
// is met based on how many of the "child" expectations are met.
type compositeExpectation struct {
caption string
criteria string
children []Expectation
pred func(passed int) (outcome string, ok bool)
isInverted bool
}
func (e *compositeExpectation) Caption() string {
return e.caption
}
func (e *compositeExpectation) Predicate(s PredicateScope) (Predicate, error) {
var children []Predicate
for _, c := range e.children {
p, err := c.Predicate(s)
if err != nil {
return nil, err
}
children = append(children, p)
}
return &compositePredicate{
criteria: e.criteria,
children: children,
pred: e.pred,
isInverted: e.isInverted,
}, nil
}
// compositePredicate is the Predicate implementation for compositeExpectation.
type compositePredicate struct {
criteria string
children []Predicate
pred func(int) (string, bool)
isInverted bool
}
func (p *compositePredicate) Notify(f fact.Fact) {
for _, c := range p.children {
c.Notify(f)
}
}
func (p *compositePredicate) Ok() bool {
_, ok := p.ok()
return ok
}
func (p *compositePredicate) Done() {
for _, c := range p.children {
c.Done()
}
}
func (p *compositePredicate) Report(ctx ReportGenerationContext) *Report {
if p.isInverted {
ctx.IsInverted = !ctx.IsInverted
}
m, ok := p.ok()
rep := &Report{
TreeOk: ctx.TreeOk,
Ok: ok,
Criteria: p.criteria,
Outcome: m,
}
for _, c := range p.children {
rep.Append(c.Report(ctx))
}
return rep
}
func (p *compositePredicate) ok() (string, bool) {
n := 0
for _, c := range p.children {
if c.Ok() {
n++
}
}
return p.pred(n)
}