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
/
workers.go
111 lines (98 loc) · 2.11 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
package is
import (
"bytes"
"fmt"
"reflect"
)
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)
}
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 *Is, format string, args ...interface{}) {
is.TB.Helper()
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...)
}
}