-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathis.go
322 lines (295 loc) · 6.14 KB
/
is.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
package is
import (
"fmt"
"reflect"
"sync"
)
// I represents the is interface.
type I interface {
// OK asserts that the specified objects are all OK.
OK(o ...interface{})
// Equal asserts that the two values are
// considered equal. Non strict.
Equal(a, b interface{})
// NotEqual asserts that the two values are not
// considered equal. Non strict.
NotEqual(a, b interface{})
// NoErr asserts that the value is not an
// error.
NoErr(err ...error)
// Err asserts that the value is an error.
Err(err ...error)
// Nil asserts that the specified objects are
// all nil.
Nil(obj ...interface{})
// NotNil asserts that the specified objects are
// all nil.
NotNil(obj ...interface{})
// True asserts that the specified objects are
// all true
True(obj ...interface{})
// False asserts that the specified objects are
// all true
False(obj ...interface{})
// Panic asserts that the specified function
// panics.
Panic(fn func())
// PanicWith asserts that the specified function
// panics with the specific message.
PanicWith(m string, fn func())
// Fail indicates that the test has failed with the
// specified formatted arguments.
Fail(args ...interface{})
// Failf indicates that the test has failed with the
// formatted arguments.
Failf(format string, args ...interface{})
}
// New creates a new I capable of making
// assertions.
func New(t T) I {
return &i{t: t}
}
// Relaxed creates a new I capable of making
// assertions, but will not fail immediately
// allowing all assertions to run.
func Relaxed(t T) I {
return &i{t: t, relaxed: true}
}
// T represents the an interface for reporting
// failures.
// testing.T satisfied this interface.
type T interface {
FailNow()
}
// i represents an implementation of interface I.
type i struct {
t T
fails []string
l sync.Mutex
relaxed bool
}
func (i *i) Log(args ...interface{}) {
i.l.Lock()
fail := fmt.Sprint(args...)
i.fails = append(i.fails, fail)
fmt.Print(decorate(fail))
i.l.Unlock()
if !i.relaxed {
i.t.FailNow()
}
}
func (i *i) Logf(format string, args ...interface{}) {
i.l.Lock()
fail := fmt.Sprintf(format, args...)
i.fails = append(i.fails, fail)
fmt.Print(decorate(fail))
i.l.Unlock()
if !i.relaxed {
i.t.FailNow()
}
}
// OK asserts that the specified objects are all OK.
func (i *i) OK(o ...interface{}) {
for _, obj := range o {
if isNil(obj) {
i.Log("unexpected nil")
}
switch co := obj.(type) {
case func():
// shouldn't panic
var r interface{}
func() {
defer func() {
r = recover()
}()
co()
}()
if r != nil {
i.Logf("unexpected panic: %v", r)
}
return
case string:
if len(co) == 0 {
i.Log("unexpected \"\"")
}
return
case bool:
// false
if co == false {
i.Log("unexpected false")
return
}
}
if isNil(o) {
if _, ok := obj.(error); ok {
// nil errors are ok
return
}
i.Log("unexpected nil")
return
}
if obj == 0 {
i.Log("unexpected zero")
}
}
}
func (i *i) NoErr(errs ...error) {
for n, err := range errs {
if !isNil(err) {
p := "unexpected error"
if len(errs) > 1 {
p += fmt.Sprintf(" (%d)", n)
}
p += ": " + err.Error()
i.Logf(p)
}
}
}
func (i *i) Err(errs ...error) {
for n, err := range errs {
if isNil(err) {
p := "error expected"
if len(errs) > 1 {
p += fmt.Sprintf(" (%d)", n)
}
i.Logf(p)
}
}
}
func (i *i) Nil(o ...interface{}) {
for n, obj := range o {
if !isNil(obj) {
p := "expected nil"
if len(o) > 1 {
p += fmt.Sprintf(" (%d)", n)
}
p += ": " + fmt.Sprintf("%#v", obj)
i.Logf(p)
}
}
}
func (i *i) NotNil(o ...interface{}) {
for n, obj := range o {
if isNil(obj) {
p := "unexpected nil"
if len(o) > 1 {
p += fmt.Sprintf(" (%d)", n)
}
p += ": " + fmt.Sprintf("%#v", obj)
i.Logf(p)
}
}
}
func (i *i) True(o ...interface{}) {
for n, obj := range o {
if b, ok := obj.(bool); !ok || b != true {
p := "expected true"
if len(o) > 1 {
p += fmt.Sprintf(" (%d)", n)
}
p += ": " + fmt.Sprintf("%#v", obj)
i.Logf(p)
}
}
}
func (i *i) False(o ...interface{}) {
for n, obj := range o {
if b, ok := obj.(bool); !ok || b != false {
p := "expected false"
if len(o) > 1 {
p += fmt.Sprintf(" (%d)", n)
}
p += ": " + fmt.Sprintf("%#v", obj)
i.Logf(p)
}
}
}
// Equal asserts that the two values are
// considered equal. Non strict.
func (i *i) Equal(a, b interface{}) {
if !areEqual(a, b) {
i.Logf("%v != %v", a, b)
}
}
// NotEqual asserts that the two values are not
// considered equal. Non strict.
func (i *i) NotEqual(a, b interface{}) {
if areEqual(a, b) {
i.Logf("%v == %v", a, b)
}
}
func (i *i) Fail(args ...interface{}) {
i.Log(args...)
}
func (i *i) Failf(format string, args ...interface{}) {
i.Logf(format, args...)
}
// Panic asserts that the specified function
// panics.
func (i *i) Panic(fn func()) {
var r interface{}
func() {
defer func() {
r = recover()
}()
fn()
}()
if r == nil {
i.Log("expected panic")
}
}
// PanicWith asserts that the specified function
// panics with the specific message.
func (i *i) PanicWith(m string, fn func()) {
var r interface{}
func() {
defer func() {
r = recover()
}()
fn()
}()
if r != m {
i.Logf("expected panic: \"%s\"", m)
}
}
// isNil gets whether the object is nil or not.
func isNil(object interface{}) bool {
if object == nil {
return true
}
value := reflect.ValueOf(object)
kind := value.Kind()
if kind >= reflect.Chan && kind <= reflect.Slice && value.IsNil() {
return true
}
return false
}
// areEqual gets whether a equals b or not.
func areEqual(a, 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
}
if reflect.DeepEqual(a, b) {
return true
}
aValue := reflect.ValueOf(a)
bValue := reflect.ValueOf(b)
if aValue == bValue {
return true
}
// Attempt comparison after type conversion
if bValue.Type().ConvertibleTo(aValue.Type()) {
return reflect.DeepEqual(a, bValue.Convert(aValue.Type()).Interface())
}
// Last ditch effort
if fmt.Sprintf("%#v", a) == fmt.Sprintf("%#v", b) {
return true
}
return false
}