-
Notifications
You must be signed in to change notification settings - Fork 1
/
expect.go
493 lines (406 loc) · 11 KB
/
expect.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
package expect
import (
"encoding/json"
"fmt"
"regexp"
"sort"
mtjson "github.com/jefflinse/melatonin/json"
)
// A Predicate is a function that takes a test result value and possibly returns an error.
type Predicate func(any) error
// Then chains a new Predicate to run after the current Predicate.
func (p Predicate) Then(next Predicate) Predicate {
if next == nil {
return p
}
return func(actual any) error {
if err := p(actual); err != nil {
return err
}
return next(actual)
}
}
// And chains a new Predicate to run after the current Predicate if the current Predicate succeeds.
func (p Predicate) And(next Predicate) Predicate {
return p.Then(next)
}
// Or chains a new Predicate to run after the current Predicate if the current Predicate fails.
func (p Predicate) Or(next Predicate) Predicate {
if next == nil {
return p
}
return func(actual any) error {
if err := p(actual); err != nil {
return next(actual)
}
return nil
}
}
// Bool creates a predicate requiring a value to be a bool, optionally matching
// against a set of values.
func Bool(expected ...bool) Predicate {
return func(actual any) error {
n, ok := actual.(bool)
if !ok {
return wrongTypeError(true, actual)
}
if len(expected) > 0 {
for _, e := range expected {
if err := compareBoolValues(e, n); err == nil {
return nil
}
}
return fmt.Errorf("expected one of %+v, got %t", expected, n)
}
return nil
}
}
// Float creates a predicate requiring a value to be an floating point number,
// optionally matching against a set of values.
func Float(expected ...float64) Predicate {
return func(actual any) error {
n, ok := toFloat(actual)
if !ok {
return wrongTypeError(float64(0), actual)
}
if len(expected) > 0 {
for _, value := range expected {
if n == value {
return nil
}
}
return fmt.Errorf("expected one of %+v, got %g", expected, n)
}
return nil
}
}
// Int creates a predicate requiring a value to be an integer, optionally matching
// against a set of values.
func Int(expected ...int64) Predicate {
return func(actual any) error {
n, ok := toInt(actual)
if !ok {
return wrongTypeError(expected, actual)
}
if len(expected) > 0 {
for _, value := range expected {
if n == value {
return nil
}
}
return fmt.Errorf("expected one of %+v, got %d", expected, n)
}
return nil
}
}
// Map creates a predicate requiring a value to be a map, optionally matching
// against a set of values.
func Map(expected ...map[string]any) Predicate {
return func(actual any) error {
m, ok := actual.(map[string]any)
if !ok {
return fmt.Errorf("expected map, got %T: %+v", actual, actual)
}
if len(expected) > 0 {
for _, value := range expected {
if errs := CompareValues(value, m, true); len(errs) == 0 {
return nil
}
}
return fmt.Errorf("expected one of %+v, got %+v", expected, m)
}
return nil
}
}
// Pattern creates a predicate requiring a value to be a string that matches a
// regular expression, optionally matching against a set of values.
func Pattern(regex string) Predicate {
r, err := regexp.Compile(regex)
if err != nil {
return func(any) error {
return fmt.Errorf("invalid regex: %q", regex)
}
}
return Regex(r)
}
// Regex creates a predicate requiring a value to be a string that matches a
// regular expression, optionally matching against a set of values.
func Regex(regex *regexp.Regexp) Predicate {
return String().Then(func(actual any) error {
s, _ := actual.(string)
if !regex.MatchString(s) {
return fmt.Errorf("expected to match pattern %q, got %q", regex.String(), s)
}
return nil
})
}
// Slice creates a predicate requiring a value to be a slice, optionally matching
// against a set of values.
func Slice(expected ...[]any) Predicate {
return func(actual any) error {
s, ok := actual.([]any)
if !ok {
return fmt.Errorf("expected slice, got %T: %+v", actual, actual)
}
if len(expected) > 0 {
for _, value := range expected {
if errs := CompareValues(value, s, true); len(errs) == 0 {
return nil
}
}
return fmt.Errorf("expected one of %+v, got %+v", expected, s)
}
return nil
}
}
// String creates a predicate requiring a value to be a string, optionally matching
// against a set of values.
func String(expected ...string) Predicate {
return func(actual any) error {
s, ok := actual.(string)
if !ok {
return fmt.Errorf("expected string, got %T: %+v", actual, actual)
}
if len(expected) > 0 {
for _, value := range expected {
if s == value {
return nil
}
}
return fmt.Errorf("expected one of %+v, got %q", expected, s)
}
return nil
}
}
// CompareValues compares an expected value to an actual value.
func CompareValues(expected, actual any, exactJSON bool) []*FailedPredicateError {
errs := []*FailedPredicateError{}
if expected == nil && actual != nil {
errs = append(errs, failedPredicate(fmt.Errorf("expected nil, got %T: %+v", actual, actual)))
}
switch expectedValue := expected.(type) {
case bool:
if err := compareBoolValues(expectedValue, actual); err != nil {
errs = append(errs, err)
return errs
}
case *bool:
if err := compareBoolValues(*expectedValue, actual); err != nil {
errs = append(errs, err)
return errs
}
case float64:
if err := compareFloat64Values(expectedValue, actual); err != nil {
errs = append(errs, err)
return errs
}
case *float64:
if err := compareFloat64Values(*expectedValue, actual); err != nil {
errs = append(errs, err)
return errs
}
case int64:
if err := compareInt64Values(expectedValue, actual); err != nil {
errs = append(errs, err)
return errs
}
case *int64:
if err := compareInt64Values(*expectedValue, actual); err != nil {
errs = append(errs, err)
return errs
}
case string:
if err := compareStringValues(expectedValue, actual); err != nil {
errs = append(errs, err)
return errs
}
case *string:
if err := compareStringValues(*expectedValue, actual); err != nil {
errs = append(errs, err)
return errs
}
case mtjson.Object, map[string]any:
ev, ok := expectedValue.(map[string]any)
if !ok {
ev = map[string]any(expectedValue.(mtjson.Object))
}
return compareMapValues(ev, actual, exactJSON)
case mtjson.Array, []any:
ev, ok := expectedValue.([]any)
if !ok {
ev = []any(expectedValue.(mtjson.Array))
}
return compareSliceValues(ev, actual, exactJSON)
case Predicate, func(any) error:
f, ok := expectedValue.(Predicate)
if !ok {
f = Predicate(expectedValue.(func(any) error))
}
if err := f(actual); err != nil {
errs = append(errs, failedPredicate(err))
return errs
}
default:
errs = append(errs, failedPredicate(fmt.Errorf("unexpected value type: %T", actual)))
}
return nil
}
// compareBoolValues compares an expected bool to an actual bool.
func compareBoolValues(expected bool, actual any) *FailedPredicateError {
b, ok := actual.(bool)
if !ok {
return wrongTypeError(expected, actual)
}
if b != expected {
return wrongValueError([]any{expected}, actual)
}
return nil
}
// compareFloat64Values compares an expected float64 to an actual float64.
func compareFloat64Values(expected float64, actual any) *FailedPredicateError {
n, ok := actual.(float64)
if !ok {
return wrongTypeError(expected, actual)
}
if n != expected {
return wrongValueError([]any{expected}, actual)
}
return nil
}
// compareInt64Values compares an expected int64 to an actual int64.
func compareInt64Values(expected int64, actual any) *FailedPredicateError {
n, ok := actual.(int64)
if !ok {
f, ok := actual.(float64)
if !ok {
return wrongTypeError(expected, actual)
}
n, ok = floatToInt(f)
if !ok {
return wrongTypeError(expected, actual)
}
}
if n != expected {
return wrongValueError([]any{expected}, actual)
}
return nil
}
// compareMapValues compares an expected JSON object to an actual JSON object.
func compareMapValues(expected map[string]any, actual any, exact bool) []*FailedPredicateError {
errs := []*FailedPredicateError{}
m, ok := actual.(map[string]any)
if !ok {
errs = append(errs, wrongTypeError(expected, actual))
return errs
}
if exact {
if len(m) != len(expected) {
j, err := json.MarshalIndent(m, "", " ")
if err != nil {
errs = append(errs, failedPredicate(err))
}
errs = append(errs, failedPredicate(fmt.Errorf("expected %d fields, got %d:\n%+v", len(expected), len(m), string(j))))
return errs
}
expectedKeys := make([]string, 0, len(expected))
for k := range expected {
expectedKeys = append(expectedKeys, k)
}
actualKeys := make([]string, 0, len(m))
for k := range m {
actualKeys = append(actualKeys, k)
}
sort.Strings(expectedKeys)
sort.Strings(actualKeys)
for i := range expectedKeys {
if expectedKeys[i] != actualKeys[i] {
errs = append(errs, failedPredicate(fmt.Errorf("expected key %q, got %q: %+v", expectedKeys[i], actualKeys[i], m[actualKeys[i]])))
}
}
}
for k, v := range expected {
for _, err := range CompareValues(v, m[k], exact) {
err.PushField(k)
errs = append(errs, err)
}
}
return errs
}
// compareSliceValues compares an expected slice to an actual slice.
func compareSliceValues(expected []any, actual any, exact bool) []*FailedPredicateError {
errs := []*FailedPredicateError{}
a, ok := actual.([]any)
if !ok {
errs = append(errs, wrongTypeError(expected, actual))
return errs
}
if len(a) < len(expected) {
j, err := json.MarshalIndent(a, "", " ")
if err != nil {
errs = append(errs, failedPredicate(err))
}
errs = append(errs, failedPredicate(fmt.Errorf("expected at least %d elements, got %d: %+v", len(expected), len(a), string(j))))
return errs
} else if exact && len(a) > len(expected) {
j, err := json.MarshalIndent(a, "", " ")
if err != nil {
errs = append(errs, failedPredicate(err))
}
errs = append(errs, failedPredicate(fmt.Errorf("expected %d elements, got %d: %+v", len(expected), len(a), string(j))))
return errs
}
for i, v := range expected {
for _, err := range CompareValues(v, a[i], exact) {
err.PushField(fmt.Sprintf("[%d]", i))
errs = append(errs, err)
}
}
return errs
}
// compareStringValues compares an expected string to an actual string.
func compareStringValues(expected string, actual any) *FailedPredicateError {
s, ok := actual.(string)
if !ok {
return wrongTypeError(expected, actual)
}
if s != expected {
return wrongValueError([]any{expected}, actual)
}
return nil
}
func floatToInt(f float64) (int64, bool) {
n := int64(f)
return n, float64(n) == f
}
func toInt(v any) (int64, bool) {
switch v := v.(type) {
case int64:
return v, true
case float64:
return floatToInt(v)
case float32:
return floatToInt(float64(v))
case int:
return int64(v), true
case int8:
return int64(v), true
case int16:
return int64(v), true
case int32:
return int64(v), true
default:
return 0, false
}
}
func toFloat(v any) (float64, bool) {
switch v := v.(type) {
case float64:
return v, true
case float32:
return float64(v), true
}
if i, ok := toInt(v); ok {
return float64(i), true
}
return 0, false
}