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 pathis.go
381 lines (332 loc) · 11 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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package is
import (
"fmt"
"log"
"reflect"
"testing"
"time"
)
// Equaler is used to define equality for types.
//
// For example, this is useful if you have a struct that includes time.Time
// fields. You can implement this method and use time.Time.Equal() to do the
// comparison.
//
// Deprecated
type Equaler interface {
Equal(in interface{}) bool
}
// EqualityChecker is used to define equality for types during testing.
//
// For example, this is useful if you have a struct that includes time.Time
// fields. You can implement this method and use time.Time.Equal() to do the
// comparison.
type EqualityChecker interface {
IsEqual(in interface{}) bool
}
// Asserter provides methods that leverage the existing testing capabilities found
// in the Go test framework. The methods provided allow for a more natural,
// efficient and expressive approach to writing tests. The goal is to write
// fewer lines of code while improving communication of intent.
type Asserter interface {
// tb returns the testing object with which this Asserter was originally
// initialized.
TB() testing.TB
// Msg defines a message to print in the event of a failure. This allows you
// to print out additional information about a failure if it happens.
Msg(format string, args ...interface{}) Asserter
// AddMsg appends a message to print in the event of a failure. This allows
// you to build a failure message in multiple steps. If no message was
// previously set, simply sets the message.
//
// This method is most useful as a way of setting a default error message,
// then adding additional information to the output for specific assertions.
// For example:
//
// assert := is.New(t).Msg("User ID: %d",u.ID)
// /*do things*/
// assert.AddMsg("Raw Response: %s",body).Equal(res.StatusCode, http.StatusCreated)
AddMsg(format string, args ...interface{}) Asserter
// Equal performs a deep compare of the provided objects and fails if they are
// not equal.
//
// Equal does not respect type differences. If the types are different and
// comparable (eg int32 and int64), they will be compared as though they are
// the same type.
Equal(actual interface{}, expected interface{})
// NotEqual performs a deep compare of the provided objects and fails if they are
// equal.
//
// NotEqual does not respect type differences. If the types are different and
// comparable (eg int32 and int64), they will be compared as though they are
// the same type.
NotEqual(a interface{}, b interface{})
// OneOf performs a deep compare of the provided object and an array of
// comparison objects. It fails if the first object is not equal to one of the
// comparison objects.
//
// OneOf does not respect type differences. If the types are different and
// comparable (eg int32 and int64), they will be compared as though they are
// the same type.
OneOf(a interface{}, b ...interface{})
// NotOneOf performs a deep compare of the provided object and an array of
// comparison objects. It fails if the first object is equal to one of the
// comparison objects.
//
// NotOneOf does not respect type differences. If the types are different and
// comparable (eg int32 and int64), they will be compared as though they are
// the same type.
NotOneOf(a interface{}, b ...interface{})
// Err checks the provided error object to determine if an error is present.
Err(e error)
// NotErr checks the provided error object to determine if an error is not
// present.
NotErr(e error)
// Nil checks the provided object to determine if it is nil.
Nil(o interface{})
// NotNil checks the provided object to determine if it is not nil.
NotNil(o interface{})
// True checks the provided boolean to determine if it is true.
True(b bool)
// False checks the provided boolean to determine if is false.
False(b bool)
// Zero checks the provided object to determine if it is the zero value
// for the type of that object. The zero value is the same as what the object
// would contain when initialized but not assigned.
//
// This method, for example, would be used to determine if a string is empty,
// an array is empty or a map is empty. It could also be used to determine if
// a number is 0.
//
// In cases such as slice, map, array and chan, a nil value is treated the
// same as an object with len == 0
Zero(o interface{})
// NotZero checks the provided object to determine if it is not the zero
// value for the type of that object. The zero value is the same as what the
// object would contain when initialized but not assigned.
//
// This method, for example, would be used to determine if a string is not
// empty, an array is not empty or a map is not empty. It could also be used
// to determine if a number is not 0.
//
// In cases such as slice, map, array and chan, a nil value is treated the
// same as an object with len == 0
NotZero(o interface{})
// Len checks the provided object to determine if it is the same length as the
// provided length argument.
//
// If the object is not one of type array, slice or map, it will fail.
Len(o interface{}, l int)
// ShouldPanic expects the provided function to panic. If the function does
// not panic, this assertion fails.
ShouldPanic(f func())
// EqualType checks the type of the two provided objects and
// fails if they are not the same.
EqualType(expected, actual interface{})
// WaitForTrue waits until the provided func returns true. If the timeout is
// reached before the function returns true, the test will fail.
WaitForTrue(timeout time.Duration, f func() bool)
// Lax accepts a function inside which a failed assertion will not halt
// test execution. After the function returns, if any assertion had failed,
// an additional message will be printed and test execution will be halted.
//
// This is useful for running assertions on, for example, many values in a struct
// and having all the failed assertions print in one go, rather than having to run
// the test multiple times, correcting a single failure per run.
Lax(fn func(lax Asserter))
}
type asserter struct {
tb testing.TB
strict bool
failFormat string
failArgs []interface{}
failed bool
}
var _ Asserter = (*asserter)(nil)
// New returns a new Asserter containing the testing object provided.
func New(tb testing.TB) Asserter {
if tb == nil {
log.Fatalln("You must provide a testing object.")
}
return &asserter{tb: tb, strict: true}
}
func (self *asserter) TB() testing.TB {
return self.tb
}
// Msg defines a message to print in the event of a failure. This allows you
// to print out additional information about a failure if it happens.
func (self *asserter) Msg(format string, args ...interface{}) Asserter {
return &asserter{
tb: self.tb,
strict: self.strict,
failFormat: format,
failArgs: args,
}
}
func (self *asserter) AddMsg(format string, args ...interface{}) Asserter {
if self.failFormat == "" {
return self.Msg(format, args...)
}
return &asserter{
tb: self.tb,
strict: self.strict,
failFormat: fmt.Sprintf("%s - %s", self.failFormat, format),
failArgs: append(self.failArgs, args...),
}
}
func (self *asserter) Equal(actual interface{}, expected interface{}) {
self.tb.Helper()
if !isEqual(actual, expected) {
fail(self, "actual value '%v' (%s) should be equal to expected value '%v' (%s)%s",
actual, objectTypeName(actual),
expected, objectTypeName(expected),
diff(actual, expected),
)
}
}
func (self *asserter) NotEqual(actual interface{}, expected interface{}) {
self.tb.Helper()
if isEqual(actual, expected) {
fail(self, "actual value '%v' (%s) should not be equal to expected value '%v' (%s)",
actual, objectTypeName(actual),
expected, objectTypeName(expected))
}
}
func (self *asserter) OneOf(a interface{}, b ...interface{}) {
self.tb.Helper()
result := false
for _, o := range b {
result = isEqual(a, o)
if result {
break
}
}
if !result {
fail(self, "expected object '%s' to be equal to one of '%s', but got: %v and %v",
objectTypeName(a),
objectTypeNames(b), a, b)
}
}
func (self *asserter) NotOneOf(a interface{}, b ...interface{}) {
self.tb.Helper()
result := false
for _, o := range b {
result = isEqual(a, o)
if result {
break
}
}
if result {
fail(self, "expected object '%s' not to be equal to one of '%s', but got: %v and %v",
objectTypeName(a),
objectTypeNames(b), a, b)
}
}
func (self *asserter) Err(err error) {
self.tb.Helper()
if isNil(err) {
fail(self, "expected error")
}
}
func (self *asserter) NotErr(err error) {
self.tb.Helper()
if !isNil(err) {
fail(self, "expected no error, but got: %v", err)
}
}
func (self *asserter) Nil(o interface{}) {
self.tb.Helper()
if !isNil(o) {
fail(self, "expected object '%s' to be nil, but got: %v", objectTypeName(o), o)
}
}
func (self *asserter) NotNil(o interface{}) {
self.tb.Helper()
if isNil(o) {
fail(self, "expected object '%s' not to be nil", objectTypeName(o))
}
}
func (self *asserter) True(b bool) {
self.tb.Helper()
if !b {
fail(self, "expected boolean to be true")
}
}
func (self *asserter) False(b bool) {
self.tb.Helper()
if b {
fail(self, "expected boolean to be false")
}
}
func (self *asserter) Zero(o interface{}) {
self.tb.Helper()
if !isZero(o) {
fail(self, "expected object '%s' to be zero value, but it was: %v", objectTypeName(o), o)
}
}
func (self *asserter) NotZero(o interface{}) {
self.tb.Helper()
if isZero(o) {
fail(self, "expected object '%s' not to be zero value", objectTypeName(o))
}
}
func (self *asserter) Len(obj interface{}, length int) {
self.tb.Helper()
t := reflect.TypeOf(obj)
if obj == nil ||
(t.Kind() != reflect.Array &&
t.Kind() != reflect.Slice &&
t.Kind() != reflect.Map) {
fail(self, "expected object '%s' to be of length '%d', but the object is not one of array, slice or map", objectTypeName(obj), length)
return
}
rLen := reflect.ValueOf(obj).Len()
if rLen != length {
fail(self, "expected object '%s' to be of length '%d' but it was: %d", objectTypeName(obj), length, rLen)
}
}
func (self *asserter) ShouldPanic(fn func()) {
self.tb.Helper()
defer func() {
r := recover()
if r == nil {
fail(self, "expected function to panic")
}
}()
fn()
}
func (self *asserter) EqualType(expected, actual interface{}) {
self.tb.Helper()
if reflect.TypeOf(expected) != reflect.TypeOf(actual) {
fail(self, "expected objects '%s' to be of the same type as object '%s'", objectTypeName(expected), objectTypeName(actual))
}
}
func (self *asserter) WaitForTrue(timeout time.Duration, f func() bool) {
self.tb.Helper()
after := time.After(timeout)
for {
select {
case <-after:
fail(self, "function did not return true within the timeout of %v", timeout)
return
default:
if f() {
return
}
time.Sleep(100 * time.Millisecond)
}
}
}
func (self *asserter) Lax(fn func(lax Asserter)) {
lax := &asserter{
tb: self.tb,
strict: false,
failFormat: self.failFormat,
failArgs: self.failArgs,
failed: false,
}
fn(lax)
if lax.failed {
fail(self, "at least one assertion in the Lax function failed")
}
}