-
-
Notifications
You must be signed in to change notification settings - Fork 239
/
array.go
1835 lines (1588 loc) · 41.7 KB
/
array.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
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package httpexpect
import (
"errors"
"fmt"
"reflect"
)
// Array provides methods to inspect attached []interface{} object
// (Go representation of JSON array).
type Array struct {
noCopy noCopy
chain *chain
value []interface{}
}
// NewArray returns a new Array instance.
//
// If reporter is nil, the function panics.
// If value is nil, failure is reported.
//
// Example:
//
// array := NewArray(t, []interface{}{"foo", 123})
func NewArray(reporter Reporter, value []interface{}) *Array {
return newArray(newChainWithDefaults("Array()", reporter), value)
}
// NewArrayC returns a new Array instance with config.
//
// Requirements for config are same as for WithConfig function.
// If value is nil, failure is reported.
//
// Example:
//
// array := NewArrayC(config, []interface{}{"foo", 123})
func NewArrayC(config Config, value []interface{}) *Array {
return newArray(newChainWithConfig("Array()", config.withDefaults()), value)
}
func newArray(parent *chain, val []interface{}) *Array {
a := &Array{chain: parent.clone(), value: nil}
opChain := a.chain.enter("")
defer opChain.leave()
if val == nil {
opChain.fail(AssertionFailure{
Type: AssertNotNil,
Actual: &AssertionValue{val},
Errors: []error{
errors.New("expected: non-nil array"),
},
})
} else {
a.value, _ = canonArray(opChain, val)
}
return a
}
// Raw returns underlying value attached to Array.
// This is the value originally passed to NewArray, converted to canonical form.
//
// Example:
//
// array := NewArray(t, []interface{}{"foo", 123})
// assert.Equal(t, []interface{}{"foo", 123.0}, array.Raw())
func (a *Array) Raw() []interface{} {
return a.value
}
// Decode unmarshals the underlying value attached to the Array to a target variable.
// target should be one of these:
//
// - pointer to an empty interface
// - pointer to a slice of any type
//
// Example:
//
// type S struct{
// Foo int `json:foo`
// }
// value := []interface{}{
// map[string]interface{}{
// "foo": 123,
// },
// map[string]interface{}{
// "foo": 456,
// },
// }
// array := NewArray(t, value)
//
// var target []S
// arr.Decode(&target)
//
// assert.Equal(t, []S{{123}, {456}}, target)
func (a *Array) Decode(target interface{}) *Array {
opChain := a.chain.enter("Decode()")
defer opChain.leave()
if opChain.failed() {
return a
}
canonDecode(opChain, a.value, target)
return a
}
// Alias is similar to Value.Alias.
func (a *Array) Alias(name string) *Array {
opChain := a.chain.enter("Alias(%q)", name)
defer opChain.leave()
a.chain.setAlias(name)
return a
}
// Path is similar to Value.Path.
func (a *Array) Path(path string) *Value {
opChain := a.chain.enter("Path(%q)", path)
defer opChain.leave()
return jsonPath(opChain, a.value, path)
}
// Schema is similar to Value.Schema.
func (a *Array) Schema(schema interface{}) *Array {
opChain := a.chain.enter("Schema()")
defer opChain.leave()
jsonSchema(opChain, a.value, schema)
return a
}
// Length returns a new Number instance with array length.
//
// Example:
//
// array := NewArray(t, []interface{}{1, 2, 3})
// array.Length().IsEqual(3)
func (a *Array) Length() *Number {
opChain := a.chain.enter("Length()")
defer opChain.leave()
if opChain.failed() {
return newNumber(opChain, 0)
}
return newNumber(opChain, float64(len(a.value)))
}
// Value returns a new Value instance with array element for given index.
//
// If index is out of array bounds, Value reports failure and returns empty
// (but non-nil) instance.
//
// Example:
//
// array := NewArray(t, []interface{}{"foo", 123})
// array.Value(0).String().IsEqual("foo")
// array.Value(1).Number().IsEqual(123)
func (a *Array) Value(index int) *Value {
opChain := a.chain.enter("Value(%d)", index)
defer opChain.leave()
if opChain.failed() {
return newValue(opChain, nil)
}
if index < 0 || index >= len(a.value) {
opChain.fail(AssertionFailure{
Type: AssertInRange,
Actual: &AssertionValue{index},
Expected: &AssertionValue{AssertionRange{
Min: 0,
Max: len(a.value) - 1,
}},
Errors: []error{
errors.New("expected: valid element index"),
},
})
return newValue(opChain, nil)
}
return newValue(opChain, a.value[index])
}
// Deprecated: use Value instead.
func (a *Array) Element(index int) *Value {
return a.Value(index)
}
// HasValue succeeds if array's value at the given index is equal to given value.
//
// Before comparison, both values are converted to canonical form. value should be
// map[string]interface{} or struct.
//
// Example:
//
// array := NewArray(t, []interface{}{"foo", "123"})
// array.HasValue(1, 123)
func (a *Array) HasValue(index int, value interface{}) *Array {
opChain := a.chain.enter("HasValue(%d)", index)
defer opChain.leave()
if opChain.failed() {
return a
}
if index < 0 || index >= len(a.value) {
opChain.fail(AssertionFailure{
Type: AssertInRange,
Actual: &AssertionValue{index},
Expected: &AssertionValue{AssertionRange{
Min: 0,
Max: len(a.value) - 1,
}},
Errors: []error{
errors.New("expected: valid element index"),
},
})
return a
}
expected, ok := canonValue(opChain, value)
if !ok {
return a
}
if !reflect.DeepEqual(expected, a.value[index]) {
opChain.fail(AssertionFailure{
Type: AssertEqual,
Actual: &AssertionValue{a.value[index]},
Expected: &AssertionValue{value},
Errors: []error{
fmt.Errorf(
"expected: array value at index %d is equal to given value",
index),
},
})
return a
}
return a
}
// NotHasValue succeeds if array's value at the given index is not equal to given value.
//
// Before comparison, both values are converted to canonical form. value should be
// map[string]interface{} or struct.
//
// Example:
//
// array := NewArray(t, []interface{}{"foo", "123"})
// array.NotHasValue(1, 234)
func (a *Array) NotHasValue(index int, value interface{}) *Array {
opChain := a.chain.enter("NotHasValue(%d)", index)
defer opChain.leave()
if opChain.failed() {
return a
}
if index < 0 || index >= len(a.value) {
opChain.fail(AssertionFailure{
Type: AssertInRange,
Actual: &AssertionValue{index},
Expected: &AssertionValue{AssertionRange{
Min: 0,
Max: len(a.value) - 1,
}},
Errors: []error{
errors.New("expected: valid element index"),
},
})
return a
}
expected, ok := canonValue(opChain, value)
if !ok {
return a
}
if reflect.DeepEqual(expected, a.value[index]) {
opChain.fail(AssertionFailure{
Type: AssertNotEqual,
Actual: &AssertionValue{a.value[index]},
Expected: &AssertionValue{value},
Errors: []error{
fmt.Errorf(
"expected: array value at index %d is not equal to given value",
index),
},
})
return a
}
return a
}
// Deprecated: use Value or HasValue instead.
func (a *Array) First() *Value {
opChain := a.chain.enter("First()")
defer opChain.leave()
if opChain.failed() {
return newValue(opChain, nil)
}
if len(a.value) == 0 {
opChain.fail(AssertionFailure{
Type: AssertNotEmpty,
Actual: &AssertionValue{a.value},
Errors: []error{
errors.New("expected: non-empty array"),
},
})
return newValue(opChain, nil)
}
return newValue(opChain, a.value[0])
}
// Deprecated: use Value or HasValue instead.
func (a *Array) Last() *Value {
opChain := a.chain.enter("Last()")
defer opChain.leave()
if opChain.failed() {
return newValue(opChain, nil)
}
if len(a.value) == 0 {
opChain.fail(AssertionFailure{
Type: AssertNotEmpty,
Actual: &AssertionValue{a.value},
Errors: []error{
errors.New("expected: non-empty array"),
},
})
return newValue(opChain, nil)
}
return newValue(opChain, a.value[len(a.value)-1])
}
// Iter returns a new slice of Values attached to array elements.
//
// Example:
//
// strings := []interface{}{"foo", "bar"}
// array := NewArray(t, strings)
//
// for index, value := range array.Iter() {
// value.String().IsEqual(strings[index])
// }
func (a *Array) Iter() []Value {
opChain := a.chain.enter("Iter()")
defer opChain.leave()
if opChain.failed() {
return []Value{}
}
ret := []Value{}
for index, element := range a.value {
func() {
valueChain := opChain.replace("Iter[%d]", index)
defer valueChain.leave()
ret = append(ret, *newValue(valueChain, element))
}()
}
return ret
}
// Every runs the passed function on all the elements in the array.
//
// If assertion inside function fails, the original Array is marked failed.
//
// Every will execute the function for all values in the array irrespective
// of assertion failures for some values in the array.
//
// Example:
//
// array := NewArray(t, []interface{}{"foo", "bar"})
//
// array.Every(func(index int, value *httpexpect.Value) {
// value.String().NotEmpty()
// })
func (a *Array) Every(fn func(index int, value *Value)) *Array {
opChain := a.chain.enter("Every()")
defer opChain.leave()
if opChain.failed() {
return a
}
if fn == nil {
opChain.fail(AssertionFailure{
Type: AssertUsage,
Errors: []error{
errors.New("unexpected nil function argument"),
},
})
return a
}
for index, element := range a.value {
func() {
valueChain := opChain.replace("Every[%d]", index)
defer valueChain.leave()
fn(index, newValue(valueChain, element))
}()
}
return a
}
// Filter accepts a function that returns a boolean. The function is ran
// over the array elements. If the function returns true, the element passes
// the filter and is added to the new array of filtered elements. If false,
// the element is skipped (or in other words filtered out). After iterating
// through all the elements of the original array, the new filtered array
// is returned.
//
// If there are any failed assertions in the filtering function, the
// element is omitted without causing test failure.
//
// Example:
//
// array := NewArray(t, []interface{}{1, 2, "foo", "bar"})
// filteredArray := array.Filter(func(index int, value *httpexpect.Value) bool {
// value.String().NotEmpty() //fails on 1 and 2
// return value.Raw() != "bar" //fails on "bar"
// })
// filteredArray.IsEqual([]interface{}{"foo"}) //succeeds
func (a *Array) Filter(fn func(index int, value *Value) bool) *Array {
opChain := a.chain.enter("Filter()")
defer opChain.leave()
if opChain.failed() {
return newArray(opChain, nil)
}
if fn == nil {
opChain.fail(AssertionFailure{
Type: AssertUsage,
Errors: []error{
errors.New("unexpected nil function argument"),
},
})
return newArray(opChain, nil)
}
filteredArray := []interface{}{}
for index, element := range a.value {
func() {
valueChain := opChain.replace("Filter[%d]", index)
defer valueChain.leave()
valueChain.setRoot()
valueChain.setSeverity(SeverityLog)
if fn(index, newValue(valueChain, element)) && !valueChain.treeFailed() {
filteredArray = append(filteredArray, element)
}
}()
}
return newArray(opChain, filteredArray)
}
// Transform runs the passed function on all the elements in the array
// and returns a new array without effeecting original array.
//
// Example:
//
// array := NewArray(t, []interface{}{"foo", "bar"})
// transformedArray := array.Transform(
// func(index int, value interface{}) interface{} {
// return strings.ToUpper(value.(string))
// })
// transformedArray.IsEqual([]interface{}{"FOO", "BAR"})
func (a *Array) Transform(fn func(index int, value interface{}) interface{}) *Array {
opChain := a.chain.enter("Transform()")
defer opChain.leave()
if opChain.failed() {
return newArray(opChain, nil)
}
if fn == nil {
opChain.fail(AssertionFailure{
Type: AssertUsage,
Errors: []error{
errors.New("unexpected nil function argument"),
},
})
return newArray(opChain, nil)
}
transformedArray := []interface{}{}
for index, element := range a.value {
transformedArray = append(transformedArray, fn(index, element))
}
return newArray(opChain, transformedArray)
}
// Find accepts a function that returns a boolean, runs it over the array
// elements, and returns the first element on which it returned true.
//
// If there are any failed assertions in the predicate function, the
// element is skipped without causing test failure.
//
// If no elements were found, a failure is reported.
//
// Example:
//
// array := NewArray(t, []interface{}{1, "foo", 101, "bar", 201})
// foundValue := array.Find(func(index int, value *httpexpect.Value) bool {
// num := value.Number() // skip if element is not a number
// return num.Raw() > 100 // check element value
// })
// foundValue.IsEqual(101) // succeeds
func (a *Array) Find(fn func(index int, value *Value) bool) *Value {
opChain := a.chain.enter("Find()")
defer opChain.leave()
if opChain.failed() {
return newValue(opChain, nil)
}
if fn == nil {
opChain.fail(AssertionFailure{
Type: AssertUsage,
Errors: []error{
errors.New("unexpected nil function argument"),
},
})
return newValue(opChain, nil)
}
for index, element := range a.value {
found := false
func() {
valueChain := opChain.replace("Find[%d]", index)
defer valueChain.leave()
valueChain.setRoot()
valueChain.setSeverity(SeverityLog)
if fn(index, newValue(valueChain, element)) && !valueChain.treeFailed() {
found = true
}
}()
if found {
return newValue(opChain, element)
}
}
opChain.fail(AssertionFailure{
Type: AssertValid,
Actual: &AssertionValue{a.value},
Errors: []error{
errors.New("expected: at least one array element matches predicate"),
},
})
return newValue(opChain, nil)
}
// FindAll accepts a function that returns a boolean, runs it over the array
// elements, and returns all the elements on which it returned true.
//
// If there are any failed assertions in the predicate function, the
// element is skipped without causing test failure.
//
// If no elements were found, empty slice is returned without reporting error.
//
// Example:
//
// array := NewArray(t, []interface{}{1, "foo", 101, "bar", 201})
// foundValues := array.FindAll(func(index int, value *httpexpect.Value) bool {
// num := value.Number() // skip if element is not a number
// return num.Raw() > 100 // check element value
// })
//
// assert.Equal(t, len(foundValues), 2)
// foundValues[0].IsEqual(101)
// foundValues[1].IsEqual(201)
func (a *Array) FindAll(fn func(index int, value *Value) bool) []*Value {
opChain := a.chain.enter("FindAll()")
defer opChain.leave()
if opChain.failed() {
return []*Value{}
}
if fn == nil {
opChain.fail(AssertionFailure{
Type: AssertUsage,
Errors: []error{
errors.New("unexpected nil function argument"),
},
})
return []*Value{}
}
foundValues := make([]*Value, 0, len(a.value))
for index, element := range a.value {
func() {
valueChain := opChain.replace("FindAll[%d]", index)
defer valueChain.leave()
valueChain.setRoot()
valueChain.setSeverity(SeverityLog)
if fn(index, newValue(valueChain, element)) && !valueChain.treeFailed() {
foundValues = append(foundValues, newValue(opChain, element))
}
}()
}
return foundValues
}
// NotFind accepts a function that returns a boolean, runs it over the array
// elelements, and checks that it does not return true for any of the elements.
//
// If there are any failed assertions in the predicate function, the
// element is skipped without causing test failure.
//
// If the predicate function did not fail and returned true for at least
// one element, a failure is reported.
//
// Example:
//
// array := NewArray(t, []interface{}{1, "foo", 2, "bar"})
// array.NotFind(func(index int, value *httpexpect.Value) bool {
// num := value.Number() // skip if element is not a number
// return num.Raw() > 100 // check element value
// }) // succeeds
func (a *Array) NotFind(fn func(index int, value *Value) bool) *Array {
opChain := a.chain.enter("NotFind()")
defer opChain.leave()
if opChain.failed() {
return a
}
if fn == nil {
opChain.fail(AssertionFailure{
Type: AssertUsage,
Errors: []error{
errors.New("unexpected nil function argument"),
},
})
return a
}
for index, element := range a.value {
found := false
func() {
valueChain := opChain.replace("NotFind[%d]", index)
defer valueChain.leave()
valueChain.setRoot()
valueChain.setSeverity(SeverityLog)
if fn(index, newValue(valueChain, element)) && !valueChain.treeFailed() {
found = true
}
}()
if found {
opChain.fail(AssertionFailure{
Type: AssertNotContainsElement,
Expected: &AssertionValue{element},
Actual: &AssertionValue{a.value},
Errors: []error{
errors.New("expected: none of the array elements match predicate"),
fmt.Errorf("element with index %d matches predicate", index),
},
})
return a
}
}
return a
}
// IsEmpty succeeds if array is empty.
//
// Example:
//
// array := NewArray(t, []interface{}{})
// array.IsEmpty()
func (a *Array) IsEmpty() *Array {
opChain := a.chain.enter("IsEmpty()")
defer opChain.leave()
if opChain.failed() {
return a
}
if !(len(a.value) == 0) {
opChain.fail(AssertionFailure{
Type: AssertEmpty,
Actual: &AssertionValue{a.value},
Errors: []error{
errors.New("expected: empty array"),
},
})
}
return a
}
// NotEmpty succeeds if array is non-empty.
//
// Example:
//
// array := NewArray(t, []interface{}{"foo", 123})
// array.NotEmpty()
func (a *Array) NotEmpty() *Array {
opChain := a.chain.enter("NotEmpty()")
defer opChain.leave()
if opChain.failed() {
return a
}
if len(a.value) == 0 {
opChain.fail(AssertionFailure{
Type: AssertNotEmpty,
Actual: &AssertionValue{a.value},
Errors: []error{
errors.New("expected: non-empty array"),
},
})
}
return a
}
// Deprecated: use IsEmpty instead.
func (a *Array) Empty() *Array {
return a.IsEmpty()
}
// IsEqual succeeds if array is equal to given value.
// Before comparison, both array and value are converted to canonical form.
//
// value should be a slice of any type.
//
// Example:
//
// array := NewArray(t, []interface{}{"foo", 123})
// array.IsEqual([]interface{}{"foo", 123})
//
// array := NewArray(t, []interface{}{"foo", "bar"})
// array.IsEqual([]string{}{"foo", "bar"})
//
// array := NewArray(t, []interface{}{123, 456})
// array.IsEqual([]int{}{123, 456})
func (a *Array) IsEqual(value interface{}) *Array {
opChain := a.chain.enter("IsEqual()")
defer opChain.leave()
if opChain.failed() {
return a
}
expected, ok := canonArray(opChain, value)
if !ok {
return a
}
if !reflect.DeepEqual(expected, a.value) {
opChain.fail(AssertionFailure{
Type: AssertEqual,
Actual: &AssertionValue{a.value},
Expected: &AssertionValue{expected},
Errors: []error{
errors.New("expected: arrays are equal"),
},
})
}
return a
}
// NotEqual succeeds if array is not equal to given value.
// Before comparison, both array and value are converted to canonical form.
//
// value should be a slice of any type.
//
// Example:
//
// array := NewArray(t, []interface{}{"foo", 123})
// array.NotEqual([]interface{}{123, "foo"})
func (a *Array) NotEqual(value interface{}) *Array {
opChain := a.chain.enter("NotEqual()")
defer opChain.leave()
if opChain.failed() {
return a
}
expected, ok := canonArray(opChain, value)
if !ok {
return a
}
if reflect.DeepEqual(expected, a.value) {
opChain.fail(AssertionFailure{
Type: AssertNotEqual,
Actual: &AssertionValue{a.value},
Expected: &AssertionValue{expected},
Errors: []error{
errors.New("expected: arrays are non-equal"),
},
})
}
return a
}
// Deprecated: use IsEqual instead.
func (a *Array) Equal(value interface{}) *Array {
return a.IsEqual(value)
}
// IsEqualUnordered succeeds if array is equal to another array, ignoring element
// order. Before comparison, both arrays are converted to canonical form.
//
// Example:
//
// array := NewArray(t, []interface{}{"foo", 123})
// array.IsEqualUnordered([]interface{}{123, "foo"})
func (a *Array) IsEqualUnordered(value interface{}) *Array {
opChain := a.chain.enter("IsEqualUnordered()")
defer opChain.leave()
if opChain.failed() {
return a
}
expected, ok := canonArray(opChain, value)
if !ok {
return a
}
for _, element := range expected {
expectedCount := countElement(expected, element)
actualCount := countElement(a.value, element)
if actualCount != expectedCount {
if expectedCount == 1 && actualCount == 0 {
opChain.fail(AssertionFailure{
Type: AssertContainsElement,
Actual: &AssertionValue{a.value},
Expected: &AssertionValue{element},
Reference: &AssertionValue{value},
Errors: []error{
errors.New("expected: array contains element from reference array"),
},
})
} else {
opChain.fail(AssertionFailure{
Type: AssertNotContainsElement,
Actual: &AssertionValue{a.value},
Expected: &AssertionValue{element},
Reference: &AssertionValue{value},
Errors: []error{
fmt.Errorf(
"expected: element occurs %d time(s), as in reference array,"+
" but it occurs %d time(s)",
expectedCount,
actualCount),
},
})
}
return a
}
}
for _, element := range a.value {
expectedCount := countElement(expected, element)
actualCount := countElement(a.value, element)
if actualCount != expectedCount {
if expectedCount == 0 && actualCount == 1 {
opChain.fail(AssertionFailure{
Type: AssertNotContainsElement,
Actual: &AssertionValue{a.value},
Expected: &AssertionValue{element},
Reference: &AssertionValue{value},
Errors: []error{
errors.New("expected: array does not contain elements" +
" that are not present in reference array"),
},
})
} else {
opChain.fail(AssertionFailure{
Type: AssertNotContainsElement,
Actual: &AssertionValue{a.value},
Expected: &AssertionValue{element},
Reference: &AssertionValue{value},
Errors: []error{
fmt.Errorf(
"expected: element occurs %d time(s), as in reference array,"+
" but it occurs %d time(s)",
expectedCount,
actualCount),
},
})
}
return a
}
}
return a
}
// NotEqualUnordered succeeds if array is not equal to another array, ignoring
// element order. Before comparison, both arrays are converted to canonical form.
//
// Example:
//
// array := NewArray(t, []interface{}{"foo", 123})
// array.NotEqualUnordered([]interface{}{123, "foo", "bar"})
func (a *Array) NotEqualUnordered(value interface{}) *Array {
opChain := a.chain.enter("NotEqualUnordered()")
defer opChain.leave()
if opChain.failed() {
return a
}
expected, ok := canonArray(opChain, value)
if !ok {
return a
}
different := false
for _, element := range expected {
expectedCount := countElement(expected, element)
actualCount := countElement(a.value, element)
if actualCount != expectedCount {
different = true
break
}
}
for _, element := range a.value {
expectedCount := countElement(expected, element)
actualCount := countElement(a.value, element)
if actualCount != expectedCount {
different = true
break
}
}
if !different {
opChain.fail(AssertionFailure{
Type: AssertNotEqual,
Actual: &AssertionValue{a.value},
Expected: &AssertionValue{value},
Reference: &AssertionValue{value},
Errors: []error{
errors.New("expected: arrays are non-equal (ignoring order)"),
},
})
}
return a
}
// Deprecated: use IsEqualUnordered instead.
func (a *Array) EqualUnordered(value interface{}) *Array {
return a.IsEqualUnordered(value)
}
// InList succeeds if the whole array is equal to one of the values from given