This repository has been archived by the owner on May 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathworkers.go
172 lines (155 loc) · 3.24 KB
/
workers.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
package is
import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"github.com/google/go-cmp/cmp"
)
func objectTypeName(o interface{}) string {
return fmt.Sprintf("%T", o)
}
func objectTypeNames(o []interface{}) string {
if o == nil {
return objectTypeName(o)
}
if len(o) == 1 {
return objectTypeName(o[0])
}
var b bytes.Buffer
b.WriteString(objectTypeName(o[0]))
for _, e := range o[1:] {
b.WriteString(",")
b.WriteString(objectTypeName(e))
}
return b.String()
}
func isNil(o interface{}) bool {
if o == nil {
return true
}
value := reflect.ValueOf(o)
kind := value.Kind()
if kind >= reflect.Chan &&
kind <= reflect.Slice &&
value.IsNil() {
return true
}
return false
}
func isZero(o interface{}) bool {
if o == nil {
return true
}
v := reflect.ValueOf(o)
switch v.Kind() {
case reflect.Ptr:
return reflect.DeepEqual(o,
reflect.New(v.Type().Elem()).Interface())
case reflect.Slice, reflect.Array, reflect.Map, reflect.Chan:
if v.Len() == 0 {
return true
}
return false
default:
return reflect.DeepEqual(o,
reflect.Zero(v.Type()).Interface())
}
}
func isEqual(a interface{}, b interface{}) bool {
if isNil(a) || isNil(b) {
if isNil(a) && !isNil(b) {
return false
}
if !isNil(a) && isNil(b) {
return false
}
return a == b
}
// Call a.Equaler if it is implemented
if e, ok := a.(Equaler); ok {
return e.Equal(b)
}
// Call a.EqualityChecker if it is implemented
if e, ok := a.(EqualityChecker); ok {
return e.IsEqual(b)
}
if reflect.DeepEqual(a, b) {
return true
}
aValue := reflect.ValueOf(a)
bValue := reflect.ValueOf(b)
// Convert types and compare
if bValue.Type().ConvertibleTo(aValue.Type()) {
return reflect.DeepEqual(a, bValue.Convert(aValue.Type()).Interface())
}
return false
}
// fail is a function variable that is called by test functions when they
// fail. It is overridden in test code for this package.
var fail = failDefault
// failDefault is the default failure function.
func failDefault(is *asserter, format string, args ...interface{}) {
is.tb.Helper()
is.failed = true
failFmt := format
if len(is.failFormat) != 0 {
failFmt = fmt.Sprintf("%s - %s", format, is.failFormat)
args = append(args, is.failArgs...)
}
if is.strict {
is.tb.Fatalf(failFmt, args...)
} else {
is.tb.Errorf(failFmt, args...)
}
}
func diff(actual interface{}, expected interface{}) string {
aKind := reflect.TypeOf(actual).Kind()
eKind := reflect.TypeOf(expected).Kind()
if aKind != eKind {
return ""
}
if aKind != reflect.Slice && aKind != reflect.Map {
return ""
}
f := func(src, dest interface{}) bool {
bytes, err := json.Marshal(src)
if err != nil {
return false
}
err = json.Unmarshal(bytes, &dest)
if err != nil {
return false
}
return true
}
var s string
switch aKind {
case reflect.Slice:
var aSlice []interface{}
var eSlice []interface{}
if !f(actual, &aSlice) {
return ""
}
if !f(expected, &eSlice) {
return ""
}
s = cmp.Diff(aSlice, eSlice)
case reflect.Map:
var aMap map[interface{}]interface{}
var eMap map[interface{}]interface{}
if !f(actual, &aMap) {
return ""
}
if !f(expected, &eMap) {
return ""
}
s = cmp.Diff(aMap, eMap)
default:
return ""
}
if s != "" {
return " - Diff:\n" + s
}
return ""
}