-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
101 lines (88 loc) · 2.56 KB
/
main.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
package equals
import (
"bytes"
"fmt"
"reflect"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func ElementsMatchRec(t require.TestingT, want interface{}, got interface{}) (equal bool) {
if !ElementsMatchRec2(t, want, got){
assert.Fail(t, fmt.Sprintf("Not equal: %v, %v", want, got))
return false
}
return true
}
func ElementsMatchRec2(t require.TestingT, want interface{}, got interface{}) (equal bool) {
var wantj, gotj = interfaceToJson(want, got)
return elementsMatchRecHelper(t, wantj, gotj)
}
// ElementsMatch asserts that the specified listA(array, slice...) is equal to specified
// listB(array, slice...) ignoring the order of the elements. If there are duplicate elements,
// the number of appearances of each of them in both lists should match.
//
// require.ElementsMatch(t, [1, 3, 2, 3], [1, 3, 3, 2])
func ElementsMatch(t require.TestingT, listA, listB interface{}, msgAndArgs ...interface{}) (ok bool) {
if isEmpty(listA) && isEmpty(listB) {
return true
}
if !isList(t, listA, msgAndArgs...) || !isList(t, listB, msgAndArgs...) {
return false
}
extraA, extraB := diffLists(listA, listB)
if len(extraA) == 0 && len(extraB) == 0 {
return true
}
return assert.Fail(t, formatListDiff(listA, listB, extraA, extraB), msgAndArgs...)
}
func elementsMatchRecHelper(t require.TestingT, want interface{}, got interface{}) (equal bool) {
defer func() {
if err := recover(); err != nil {
equal = false
}
}()
w := reflect.ValueOf(want)
g := reflect.ValueOf(got)
switch w.Type().Kind() {
case reflect.Array, reflect.Slice:
return ElementsMatch(t, want, got)
case reflect.Map:
for _, e := range w.MapKeys() {
if welem, gelem := w.MapIndex(e), g.MapIndex(e); welem.CanInterface() && gelem.CanInterface() {
if !elementsMatchRecHelper(t, welem.Interface(), gelem.Interface()) {
return false
}
} else {
if !ElementsMatch(t, welem, gelem) {
return false
}
}
}
return w.Len() == g.Len()
default:
return AssertJson(t, want, got)
}
}
// objectsAreEqual determines if two objects are considered equal.
//
// This function does no assertion of any kind.
func objectsAreEqual(expected, actual interface{}) bool {
if expected == nil || actual == nil {
return expected == actual
}
exp, ok := expected.([]byte)
if !ok {
if !equalJson(expected, actual) {
return ElementsMatchRec2(RequireNull{}, expected, actual)
}
return true
}
act, ok := actual.([]byte)
if !ok {
return false
}
if exp == nil || act == nil {
return exp == nil && act == nil
}
return bytes.Equal(exp, act)
}