-
Notifications
You must be signed in to change notification settings - Fork 338
/
assert.go
272 lines (238 loc) · 6.92 KB
/
assert.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
package rxgo
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
)
// AssertPredicate is a custom predicate based on the items.
type AssertPredicate func(items []interface{}) error
// RxAssert lists the Observable assertions.
type RxAssert interface {
apply(*rxAssert)
itemsToBeChecked() (bool, []interface{})
itemsNoOrderedToBeChecked() (bool, []interface{})
noItemsToBeChecked() bool
someItemsToBeChecked() bool
raisedErrorToBeChecked() (bool, error)
raisedErrorsToBeChecked() (bool, []error)
raisedAnErrorToBeChecked() (bool, error)
notRaisedErrorToBeChecked() bool
itemToBeChecked() (bool, interface{})
noItemToBeChecked() (bool, interface{})
customPredicatesToBeChecked() (bool, []AssertPredicate)
}
type rxAssert struct {
f func(*rxAssert)
checkHasItems bool
checkHasNoItems bool
checkHasSomeItems bool
items []interface{}
checkHasItemsNoOrder bool
itemsNoOrder []interface{}
checkHasRaisedError bool
err error
checkHasRaisedErrors bool
errs []error
checkHasRaisedAnError bool
checkHasNotRaisedError bool
checkHasItem bool
item interface{}
checkHasNoItem bool
checkHasCustomPredicate bool
customPredicates []AssertPredicate
}
func (ass *rxAssert) apply(do *rxAssert) {
ass.f(do)
}
func (ass *rxAssert) itemsToBeChecked() (bool, []interface{}) {
return ass.checkHasItems, ass.items
}
func (ass *rxAssert) itemsNoOrderedToBeChecked() (bool, []interface{}) {
return ass.checkHasItemsNoOrder, ass.itemsNoOrder
}
func (ass *rxAssert) noItemsToBeChecked() bool {
return ass.checkHasNoItems
}
func (ass *rxAssert) someItemsToBeChecked() bool {
return ass.checkHasSomeItems
}
func (ass *rxAssert) raisedErrorToBeChecked() (bool, error) {
return ass.checkHasRaisedError, ass.err
}
func (ass *rxAssert) raisedErrorsToBeChecked() (bool, []error) {
return ass.checkHasRaisedErrors, ass.errs
}
func (ass *rxAssert) raisedAnErrorToBeChecked() (bool, error) {
return ass.checkHasRaisedAnError, ass.err
}
func (ass *rxAssert) notRaisedErrorToBeChecked() bool {
return ass.checkHasNotRaisedError
}
func (ass *rxAssert) itemToBeChecked() (bool, interface{}) {
return ass.checkHasItem, ass.item
}
func (ass *rxAssert) noItemToBeChecked() (bool, interface{}) {
return ass.checkHasNoItem, ass.item
}
func (ass *rxAssert) customPredicatesToBeChecked() (bool, []AssertPredicate) {
return ass.checkHasCustomPredicate, ass.customPredicates
}
func newAssertion(f func(*rxAssert)) *rxAssert {
return &rxAssert{
f: f,
}
}
// HasItems checks that the observable produces the corresponding items.
func HasItems(items ...interface{}) RxAssert {
return newAssertion(func(a *rxAssert) {
a.checkHasItems = true
a.items = items
})
}
// HasItem checks if a single or optional single has a specific item.
func HasItem(i interface{}) RxAssert {
return newAssertion(func(a *rxAssert) {
a.checkHasItem = true
a.item = i
})
}
// HasItemsNoOrder checks that an observable produces the corresponding items regardless of the order.
func HasItemsNoOrder(items ...interface{}) RxAssert {
return newAssertion(func(a *rxAssert) {
a.checkHasItemsNoOrder = true
a.itemsNoOrder = items
})
}
// IsNotEmpty checks that the observable produces some items.
func IsNotEmpty() RxAssert {
return newAssertion(func(a *rxAssert) {
a.checkHasSomeItems = true
})
}
// IsEmpty checks that the observable has not produce any item.
func IsEmpty() RxAssert {
return newAssertion(func(a *rxAssert) {
a.checkHasNoItems = true
})
}
// HasError checks that the observable has produce a specific error.
func HasError(err error) RxAssert {
return newAssertion(func(a *rxAssert) {
a.checkHasRaisedError = true
a.err = err
})
}
// HasAnError checks that the observable has produce an error.
func HasAnError() RxAssert {
return newAssertion(func(a *rxAssert) {
a.checkHasRaisedAnError = true
})
}
// HasErrors checks that the observable has produce a set of errors.
func HasErrors(errs ...error) RxAssert {
return newAssertion(func(a *rxAssert) {
a.checkHasRaisedErrors = true
a.errs = errs
})
}
// HasNoError checks that the observable has not raised any error.
func HasNoError() RxAssert {
return newAssertion(func(a *rxAssert) {
a.checkHasRaisedError = true
})
}
// CustomPredicate checks a custom predicate.
func CustomPredicate(predicate AssertPredicate) RxAssert {
return newAssertion(func(a *rxAssert) {
if !a.checkHasCustomPredicate {
a.checkHasCustomPredicate = true
a.customPredicates = make([]AssertPredicate, 0)
}
a.customPredicates = append(a.customPredicates, predicate)
})
}
func parseAssertions(assertions ...RxAssert) RxAssert {
ass := new(rxAssert)
for _, assertion := range assertions {
assertion.apply(ass)
}
return ass
}
// Assert asserts the result of an iterable against a list of assertions.
func Assert(ctx context.Context, t *testing.T, iterable Iterable, assertions ...RxAssert) {
ass := parseAssertions(assertions...)
got := make([]interface{}, 0)
errs := make([]error, 0)
observe := iterable.Observe()
loop:
for {
select {
case <-ctx.Done():
break loop
case item, ok := <-observe:
if !ok {
break loop
}
if item.Error() {
errs = append(errs, item.E)
} else {
got = append(got, item.V)
}
}
}
if checked, predicates := ass.customPredicatesToBeChecked(); checked {
for _, predicate := range predicates {
err := predicate(got)
if err != nil {
assert.Fail(t, err.Error())
}
}
}
if checkHasItems, expectedItems := ass.itemsToBeChecked(); checkHasItems {
assert.Equal(t, expectedItems, got)
}
if checkHasItemsNoOrder, itemsNoOrder := ass.itemsNoOrderedToBeChecked(); checkHasItemsNoOrder {
m := make(map[interface{}]interface{})
for _, v := range itemsNoOrder {
m[v] = nil
}
for _, v := range got {
delete(m, v)
}
if len(m) != 0 {
assert.Fail(t, "missing elements", "%v", got)
}
}
if checkHasItem, value := ass.itemToBeChecked(); checkHasItem {
length := len(got)
if length != 1 {
assert.FailNow(t, "wrong number of items", "expected 1, got %d", length)
}
assert.Equal(t, value, got[0])
}
if ass.noItemsToBeChecked() {
assert.Equal(t, 0, len(got))
}
if ass.someItemsToBeChecked() {
assert.NotEqual(t, 0, len(got))
}
if checkHasRaisedError, expectedError := ass.raisedErrorToBeChecked(); checkHasRaisedError {
if expectedError == nil {
assert.Equal(t, 0, len(errs))
} else {
if len(errs) == 0 {
assert.FailNow(t, "no error raised", "expected %v", expectedError)
}
assert.Equal(t, expectedError, errs[0])
}
}
if checkHasRaisedErrors, expectedErrors := ass.raisedErrorsToBeChecked(); checkHasRaisedErrors {
assert.Equal(t, expectedErrors, errs)
}
if checkHasRaisedAnError, expectedError := ass.raisedAnErrorToBeChecked(); checkHasRaisedAnError {
assert.Nil(t, expectedError)
}
if ass.notRaisedErrorToBeChecked() {
assert.Equal(t, 0, len(errs))
}
}