forked from gavv/httpexpect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject.go
712 lines (614 loc) · 15.5 KB
/
object.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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
package httpexpect
import (
"errors"
"fmt"
"reflect"
"sort"
)
// Object provides methods to inspect attached map[string]interface{} object
// (Go representation of JSON object).
type Object struct {
chain *chain
value map[string]interface{}
}
// NewObject returns a new Object instance.
//
// Both reporter and value should not be nil. If value is nil, failure is
// reported.
//
// Example:
//
// object := NewObject(t, map[string]interface{}{"foo": 123})
func NewObject(reporter Reporter, value map[string]interface{}) *Object {
return newObject(newChainWithDefaults("Object()", reporter), value)
}
func newObject(parent *chain, val map[string]interface{}) *Object {
o := &Object{parent.clone(), nil}
if val == nil {
o.chain.fail(AssertionFailure{
Type: AssertNotNil,
Actual: &AssertionValue{val},
Errors: []error{
errors.New("expected: non-nil map"),
},
})
} else {
o.value, _ = canonMap(o.chain, val)
}
return o
}
// Raw returns underlying value attached to Object.
// This is the value originally passed to NewObject, converted to canonical form.
//
// Example:
//
// object := NewObject(t, map[string]interface{}{"foo": 123})
// assert.Equal(t, map[string]interface{}{"foo": 123.0}, object.Raw())
func (o *Object) Raw() map[string]interface{} {
return o.value
}
// Path is similar to Value.Path.
func (o *Object) Path(path string) *Value {
o.chain.enter("Path(%q)", path)
defer o.chain.leave()
return jsonPath(o.chain, o.value, path)
}
// Schema is similar to Value.Schema.
func (o *Object) Schema(schema interface{}) *Object {
o.chain.enter("Schema()")
defer o.chain.leave()
jsonSchema(o.chain, o.value, schema)
return o
}
// Keys returns a new Array instance with object's keys.
// Keys are sorted.
//
// Example:
//
// object := NewObject(t, map[string]interface{}{"foo": 123, "bar": 456})
// object.Keys().ContainsOnly("foo", "bar")
func (o *Object) Keys() *Array {
o.chain.enter("Keys()")
defer o.chain.leave()
if o.chain.failed() {
return newArray(o.chain, nil)
}
keys := []interface{}{}
for k := range o.value {
keys = append(keys, k)
}
sort.Slice(keys, func(i, j int) bool {
return keys[i].(string) < keys[j].(string)
})
return newArray(o.chain, keys)
}
// Values returns a new Array instance with object's values.
// Values are sorted by keys.
//
// Example:
//
// object := NewObject(t, map[string]interface{}{"foo": 123, "bar": 456})
// object.Values().ContainsOnly(123, 456)
func (o *Object) Values() *Array {
o.chain.enter("Values()")
defer o.chain.leave()
if o.chain.failed() {
return newArray(o.chain, nil)
}
keys := []string{}
for k := range o.value {
keys = append(keys, k)
}
sort.Slice(keys, func(i, j int) bool {
return keys[i] < keys[j]
})
values := []interface{}{}
for _, k := range keys {
values = append(values, o.value[k])
}
return newArray(o.chain, values)
}
// Value returns a new Value instance with value for given key.
//
// Example:
//
// object := NewObject(t, map[string]interface{}{"foo": 123})
// object.Value("foo").Number().Equal(123)
func (o *Object) Value(key string) *Value {
o.chain.enter("Value(%q)", key)
defer o.chain.leave()
if o.chain.failed() {
return newValue(o.chain, nil)
}
value, ok := o.value[key]
if !ok {
o.chain.fail(AssertionFailure{
Type: AssertContainsKey,
Actual: &AssertionValue{o.value},
Expected: &AssertionValue{key},
Errors: []error{
errors.New("expected: map contains key"),
},
})
return newValue(o.chain, nil)
}
return newValue(o.chain, value)
}
// Every runs the passed function for all the key value pairs in the object
//
// If assertion inside function fails, the original Object is marked failed.
//
// Every will execute the function for all values in the object irrespective
// of assertion failures for some values in the object.
//
// Example:
//
// object := NewObject(t, map[string]interface{}{"foo": 123, "bar": 456})
//
// object.Every(func(key string, value *httpexpect.Value) {
// value.String().NotEmpty()
// })
func (o *Object) Every(fn func(key string, value *Value)) *Object {
o.chain.enter("Every()")
defer o.chain.leave()
if o.chain.failed() {
return o
}
if fn == nil {
o.chain.fail(AssertionFailure{
Type: AssertUsage,
Errors: []error{
errors.New("unexpected nil function argument"),
},
})
return o
}
chainFailure := false
for key, val := range o.value {
valueChain := o.chain.clone()
valueChain.replace("Every[%q]", key)
valueChain.setFailCallback(func() {
chainFailure = true
})
fn(key, newValue(valueChain, val))
}
if chainFailure {
o.chain.setFailed()
}
return o
}
// Empty succeeds if object is empty.
//
// Example:
//
// object := NewObject(t, map[string]interface{}{})
// object.Empty()
func (o *Object) Empty() *Object {
o.chain.enter("Empty()")
defer o.chain.leave()
if o.chain.failed() {
return o
}
if !(len(o.value) == 0) {
o.chain.fail(AssertionFailure{
Type: AssertEmpty,
Actual: &AssertionValue{o.value},
Errors: []error{
errors.New("expected: map is empty"),
},
})
}
return o
}
// NotEmpty succeeds if object is non-empty.
//
// Example:
//
// object := NewObject(t, map[string]interface{}{"foo": 123})
// object.NotEmpty()
func (o *Object) NotEmpty() *Object {
o.chain.enter("NotEmpty()")
defer o.chain.leave()
if o.chain.failed() {
return o
}
if !(len(o.value) != 0) {
o.chain.fail(AssertionFailure{
Type: AssertNotEmpty,
Actual: &AssertionValue{o.value},
Errors: []error{
errors.New("expected: map is non-empty"),
},
})
}
return o
}
// Equal succeeds if object is equal to given value.
// Before comparison, both object and value are converted to canonical form.
//
// value should be map[string]interface{} or struct.
//
// Example:
//
// object := NewObject(t, map[string]interface{}{"foo": 123})
// object.Equal(map[string]interface{}{"foo": 123})
func (o *Object) Equal(value interface{}) *Object {
o.chain.enter("Equal()")
defer o.chain.leave()
if o.chain.failed() {
return o
}
expected, ok := canonMap(o.chain, value)
if !ok {
return o
}
if !reflect.DeepEqual(expected, o.value) {
o.chain.fail(AssertionFailure{
Type: AssertEqual,
Actual: &AssertionValue{o.value},
Expected: &AssertionValue{expected},
Errors: []error{
errors.New("expected: maps are equal"),
},
})
}
return o
}
// NotEqual succeeds if object is not equal to given value.
// Before comparison, both object and value are converted to canonical form.
//
// value should be map[string]interface{} or struct.
//
// Example:
//
// object := NewObject(t, map[string]interface{}{"foo": 123})
// object.Equal(map[string]interface{}{"bar": 123})
func (o *Object) NotEqual(value interface{}) *Object {
o.chain.enter("NotEqual()")
defer o.chain.leave()
if o.chain.failed() {
return o
}
expected, ok := canonMap(o.chain, value)
if !ok {
return o
}
if reflect.DeepEqual(expected, o.value) {
o.chain.fail(AssertionFailure{
Type: AssertNotEqual,
Actual: &AssertionValue{o.value},
Expected: &AssertionValue{expected},
Errors: []error{
errors.New("expected: maps are non-equal"),
},
})
}
return o
}
// ContainsKey succeeds if object contains given key.
//
// Example:
//
// object := NewObject(t, map[string]interface{}{"foo": 123})
// object.ContainsKey("foo")
func (o *Object) ContainsKey(key string) *Object {
o.chain.enter("ContainsKey()")
defer o.chain.leave()
if o.chain.failed() {
return o
}
if !o.containsKey(key) {
o.chain.fail(AssertionFailure{
Type: AssertContainsKey,
Actual: &AssertionValue{o.value},
Expected: &AssertionValue{key},
Errors: []error{
errors.New("expected: map contains key"),
},
})
}
return o
}
// NotContainsKey succeeds if object doesn't contain given key.
//
// Example:
//
// object := NewObject(t, map[string]interface{}{"foo": 123})
// object.NotContainsKey("bar")
func (o *Object) NotContainsKey(key string) *Object {
o.chain.enter("NotContainsKey()")
defer o.chain.leave()
if o.chain.failed() {
return o
}
if o.containsKey(key) {
o.chain.fail(AssertionFailure{
Type: AssertNotContainsKey,
Actual: &AssertionValue{o.value},
Expected: &AssertionValue{key},
Errors: []error{
errors.New("expected: map does not contain key"),
},
})
}
return o
}
// ContainsValue succeeds if object contains given value with any key.
// Before comparison, both object and value are converted to canonical form.
//
// Example:
//
// object := NewObject(t, map[string]interface{}{"foo": 123})
// object.ContainsValue(123)
func (o *Object) ContainsValue(value interface{}) *Object {
o.chain.enter("ContainsValue()")
defer o.chain.leave()
if o.chain.failed() {
return o
}
if _, ok := o.containsValue(value); !ok {
o.chain.fail(AssertionFailure{
Type: AssertContainsElement,
Actual: &AssertionValue{o.value},
Expected: &AssertionValue{value},
Errors: []error{
errors.New("expected: map contains element (with any key)"),
},
})
}
return o
}
// NotContainsValue succeeds if object does not contain given value with any key.
// Before comparison, both object and value are converted to canonical form.
//
// Example:
//
// object := NewObject(t, map[string]interface{}{"foo": 123})
// object.NotContainsValue(456)
func (o *Object) NotContainsValue(value interface{}) *Object {
o.chain.enter("NotContainsValue()")
defer o.chain.leave()
if o.chain.failed() {
return o
}
if key, ok := o.containsValue(value); ok {
o.chain.fail(AssertionFailure{
Type: AssertNotContainsElement,
Actual: &AssertionValue{o.value},
Expected: &AssertionValue{value},
Errors: []error{
errors.New("expected: map does not contain element (with any key)"),
fmt.Errorf("found matching element with key %q", key),
},
})
}
return o
}
// ContainsSubset succeeds if given value is a subset of object.
// Before comparison, both object and value are converted to canonical form.
//
// value should be map[string]interface{} or struct.
//
// Example:
//
// object := NewObject(t, map[string]interface{}{
// "foo": 123,
// "bar": []interface{}{"x", "y"},
// "bar": map[string]interface{}{
// "a": true,
// "b": false,
// },
// })
//
// object.ContainsSubset(map[string]interface{}{ // success
// "foo": 123,
// "bar": map[string]interface{}{
// "a": true,
// },
// })
//
// object.ContainsSubset(map[string]interface{}{ // failure
// "foo": 123,
// "qux": 456,
// })
//
// object.ContainsSubset(map[string]interface{}{ // failure, slices should match exactly
// "bar": []interface{}{"x"},
// })
func (o *Object) ContainsSubset(value interface{}) *Object {
o.chain.enter("ContainsSubset()")
defer o.chain.leave()
if o.chain.failed() {
return o
}
if !o.containsSubset(value) {
o.chain.fail(AssertionFailure{
Type: AssertContainsSubset,
Actual: &AssertionValue{o.value},
Expected: &AssertionValue{value},
Errors: []error{
errors.New("expected: map contains sub-map"),
},
})
}
return o
}
// NotContainsSubset succeeds if given value is not a subset of object.
// Before comparison, both object and value are converted to canonical form.
//
// value should be map[string]interface{} or struct.
//
// Example:
//
// object := NewObject(t, map[string]interface{}{"foo": 123, "bar": 456})
// object.NotContainsSubset(map[string]interface{}{"foo": 123, "bar": "no-no-no"})
func (o *Object) NotContainsSubset(value interface{}) *Object {
o.chain.enter("NotContainsSubset()")
defer o.chain.leave()
if o.chain.failed() {
return o
}
if o.containsSubset(value) {
o.chain.fail(AssertionFailure{
Type: AssertNotContainsSubset,
Actual: &AssertionValue{o.value},
Expected: &AssertionValue{value},
Errors: []error{
errors.New("expected: map does not contain sub-map"),
},
})
}
return o
}
// Deprecated: use ContainsSubset instead.
func (o *Object) ContainsMap(value interface{}) *Object {
return o.ContainsSubset(value)
}
// Deprecated: use NotContainsSubset instead.
func (o *Object) NotContainsMap(value interface{}) *Object {
return o.NotContainsSubset(value)
}
// ValueEqual succeeds if object's value for given key is equal to given value.
// Before comparison, both values are converted to canonical form.
//
// value should be map[string]interface{} or struct.
//
// Example:
//
// object := NewObject(t, map[string]interface{}{"foo": 123})
// object.ValueEqual("foo", 123)
func (o *Object) ValueEqual(key string, value interface{}) *Object {
o.chain.enter("ValueEqual(%q)", key)
defer o.chain.leave()
if o.chain.failed() {
return o
}
if !o.containsKey(key) {
o.chain.fail(AssertionFailure{
Type: AssertContainsKey,
Actual: &AssertionValue{o.value},
Expected: &AssertionValue{key},
Errors: []error{
errors.New("expected: map contains key"),
},
})
return o
}
expected, ok := canonValue(o.chain, value)
if !ok {
return o
}
if !reflect.DeepEqual(expected, o.value[key]) {
o.chain.fail(AssertionFailure{
Type: AssertEqual,
Actual: &AssertionValue{o.value[key]},
Expected: &AssertionValue{value},
Errors: []error{
fmt.Errorf(
"expected: map value for key %q is equal to given value",
key),
},
})
return o
}
return o
}
// NotValueEqual succeeds if object's value for given key is not equal to given
// value. Before comparison, both values are converted to canonical form.
//
// value should be map[string]interface{} or struct.
//
// If object doesn't contain any value for given key, failure is reported.
//
// Example:
//
// object := NewObject(t, map[string]interface{}{"foo": 123})
// object.NotValueEqual("foo", "bad value") // success
// object.NotValueEqual("bar", "bad value") // failure! (key is missing)
func (o *Object) NotValueEqual(key string, value interface{}) *Object {
o.chain.enter("ValueNotEqual(%q)", key)
defer o.chain.leave()
if o.chain.failed() {
return o
}
if !o.containsKey(key) {
o.chain.fail(AssertionFailure{
Type: AssertContainsKey,
Actual: &AssertionValue{o.value},
Expected: &AssertionValue{key},
Errors: []error{
errors.New("expected: map contains key"),
},
})
return o
}
expected, ok := canonValue(o.chain, value)
if !ok {
return o
}
if reflect.DeepEqual(expected, o.value[key]) {
o.chain.fail(AssertionFailure{
Type: AssertNotEqual,
Actual: &AssertionValue{o.value[key]},
Expected: &AssertionValue{value},
Errors: []error{
fmt.Errorf(
"expected: map value for key %q is non-equal to given value",
key),
},
})
return o
}
return o
}
// Deprecated: use NotValueEqual instead.
func (o *Object) ValueNotEqual(key string, value interface{}) *Object {
return o.NotValueEqual(key, value)
}
func (o *Object) containsKey(arg string) bool {
for k := range o.value {
if k == arg {
return true
}
}
return false
}
func (o *Object) containsValue(arg interface{}) (string, bool) {
value, ok := canonValue(o.chain, arg)
if !ok {
return "", false
}
for k, v := range o.value {
if reflect.DeepEqual(value, v) {
return k, true
}
}
return "", false
}
func (o *Object) containsSubset(arg interface{}) bool {
value, ok := canonMap(o.chain, arg)
if !ok {
return false
}
return checkSubset(o.value, value)
}
func checkSubset(outer, inner map[string]interface{}) bool {
for k, iv := range inner {
ov, ok := outer[k]
if !ok {
return false
}
if ovm, ok := ov.(map[string]interface{}); ok {
if ivm, ok := iv.(map[string]interface{}); ok {
if !checkSubset(ovm, ivm) {
return false
}
continue
}
}
if !reflect.DeepEqual(ov, iv) {
return false
}
}
return true
}