forked from bitfinexcom/bitfinex-api-go
/
assert.go
150 lines (131 loc) · 3.29 KB
/
assert.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
package tests
import (
"bytes"
"reflect"
"testing"
)
func isZeroOfUnderlyingType(x interface{}) bool {
if x == nil {
return true
}
if _, ok := x.([]string); ok {
return true
}
return x == reflect.Zero(reflect.TypeOf(x)).Interface()
}
func objEq(expected, actual interface{}) bool {
if expected == nil || actual == nil {
return expected == actual
}
if exp, ok := expected.([]byte); ok {
act, ok := actual.([]byte)
if !ok {
return false
} else if exp == nil || act == nil {
return exp == nil && act == nil
}
return bytes.Equal(exp, act)
}
return reflect.DeepEqual(expected, actual)
}
func assertSlice(t *testing.T, expected, actual interface{}) {
if objEq(expected, actual) {
t.Logf("%s OK", reflect.TypeOf(actual))
return
}
actualType := reflect.TypeOf(actual)
if actualType == nil {
t.Fatal()
}
expectedValue := reflect.ValueOf(expected)
if expectedValue.IsValid() && expectedValue.Type().ConvertibleTo(actualType) {
// Attempt comparison after type conversion
if reflect.DeepEqual(expectedValue.Convert(actualType).Interface(), actual) {
t.Logf("%s OK", reflect.TypeOf(actual))
return
}
}
t.Fatalf("FAIL %s: expected %#v, got %#v", reflect.TypeOf(expected), expected, actual)
}
func isPrimitive(exp interface{}) bool {
t := reflect.TypeOf(exp)
switch t.Kind() {
case reflect.Interface:
return false
case reflect.Struct:
return false
case reflect.Array:
return false
case reflect.Func:
return false
case reflect.Map:
return false
case reflect.Ptr:
return false
case reflect.Slice:
return false
case reflect.UnsafePointer:
return false
default:
return true
}
}
// does not work on slices
func assert(t *testing.T, expected interface{}, actual interface{}) {
prexp := reflect.ValueOf(expected)
pract := reflect.ValueOf(actual)
if isPrimitive(actual) {
if expected != actual {
t.Fatalf("expected %#v, got %#v", expected, actual)
}
t.Logf("OK %s", reflect.TypeOf(expected).Name())
return
}
if pract.IsNil() {
t.Errorf("nil actual value: %#v", actual)
t.Fail()
return
}
exp := prexp.Elem()
act := pract.Elem()
if !exp.IsValid() {
t.Errorf("reflected expectation not valid (%#v)", expected)
t.Fail()
}
if exp.Type() != act.Type() {
t.Errorf("expected type %s, got %s", exp.Type(), act.Type())
t.Fail()
}
for i := 0; i < exp.NumField(); i++ {
expValueField := exp.Field(i)
expTypeField := exp.Type().Field(i)
actValueField := act.Field(i)
actTypeField := act.Type().Field(i)
if expTypeField.Name != actTypeField.Name {
t.Errorf("expected type %s, got %s", expTypeField.Name, actTypeField.Name)
t.Errorf("%#v", actual)
t.Fail()
}
if isZeroOfUnderlyingType(expValueField.Interface()) {
continue
}
if !isZeroOfUnderlyingType(expValueField.Interface()) && isZeroOfUnderlyingType(actValueField.Interface()) {
t.Errorf("expected %s, but was empty", expTypeField.Name)
t.Errorf("%#v", actual)
t.Fail()
return
}
assert(t, expValueField.Interface(), actValueField.Interface())
/*
if expValueField.Interface() != actValueField.Interface() {
t.Errorf("expected %s %#v, got %#v", expTypeField.Name, expValueField.Interface(), actValueField.Interface())
t.Fail()
}
*/
}
if t.Failed() {
t.Logf("FAIL %s", exp.Type().Name())
return
}
t.Logf("OK %s", exp.Type().Name())
}