-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
assertions.go
382 lines (327 loc) · 7.85 KB
/
assertions.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
382
package got
import (
"errors"
"fmt"
"math"
"reflect"
"regexp"
"runtime"
"strings"
"sync/atomic"
"github.com/ysmood/got/lib/utils"
)
// Assertions helpers
type Assertions struct {
Testable
ErrorHandler AssertionError
must bool
desc []string
}
// Desc returns a clone with the format description. The description will be printed before the error message.
func (as Assertions) Desc(format string, args ...interface{}) Assertions {
n := as
n.desc = append(n.desc, fmt.Sprintf(format, args...))
return n
}
// Must returns a clone with the FailNow enabled. It will exit the current goroutine if the assertion fails.
func (as Assertions) Must() Assertions {
n := as
n.must = true
return n
}
// Eq asserts that x equals y when converted to the same type, such as compare float 1.0 and integer 1 .
// For strict value and type comparison use Assertions.Equal .
// For how comparison works, see [utils.SmartCompare] .
func (as Assertions) Eq(x, y interface{}) {
as.Helper()
if utils.SmartCompare(x, y) == 0 {
return
}
as.err(AssertionEq, x, y)
}
// Neq asserts that x not equals y even when converted to the same type.
// For how comparison works, see [utils.SmartCompare] .
func (as Assertions) Neq(x, y interface{}) {
as.Helper()
if utils.SmartCompare(x, y) != 0 {
return
}
_, xNil := utils.IsNil(x)
_, yNil := utils.IsNil(y)
if !xNil && !yNil && reflect.TypeOf(x).Kind() == reflect.TypeOf(y).Kind() {
as.err(AssertionNeqSame, x, y)
return
}
as.err(AssertionNeq, x, y)
}
// Equal asserts that x equals y.
// For loose type comparison use Assertions.Eq, such as compare float 1.0 and integer 1 .
func (as Assertions) Equal(x, y interface{}) {
as.Helper()
if utils.Compare(x, y) == 0 {
return
}
as.err(AssertionEq, x, y)
}
// Gt asserts that x is greater than y.
// For how comparison works, see [utils.SmartCompare] .
func (as Assertions) Gt(x, y interface{}) {
as.Helper()
if utils.SmartCompare(x, y) > 0 {
return
}
as.err(AssertionGt, x, y)
}
// Gte asserts that x is greater than or equal to y.
// For how comparison works, see [utils.SmartCompare] .
func (as Assertions) Gte(x, y interface{}) {
as.Helper()
if utils.SmartCompare(x, y) >= 0 {
return
}
as.err(AssertionGte, x, y)
}
// Lt asserts that x is less than y.
// For how comparison works, see [utils.SmartCompare] .
func (as Assertions) Lt(x, y interface{}) {
as.Helper()
if utils.SmartCompare(x, y) < 0 {
return
}
as.err(AssertionLt, x, y)
}
// Lte asserts that x is less than or equal to b.
// For how comparison works, see [utils.SmartCompare] .
func (as Assertions) Lte(x, y interface{}) {
as.Helper()
if utils.SmartCompare(x, y) <= 0 {
return
}
as.err(AssertionLte, x, y)
}
// InDelta asserts that x and y are within the delta of each other.
// For how comparison works, see [utils.SmartCompare] .
func (as Assertions) InDelta(x, y interface{}, delta float64) {
as.Helper()
if math.Abs(utils.SmartCompare(x, y)) <= delta {
return
}
as.err(AssertionInDelta, x, y, delta)
}
// True asserts that x is true.
func (as Assertions) True(x bool) {
as.Helper()
if x {
return
}
as.err(AssertionTrue)
}
// False asserts that x is false.
func (as Assertions) False(x bool) {
as.Helper()
if !x {
return
}
as.err(AssertionFalse)
}
// Nil asserts that the last item in args is nilable and nil
func (as Assertions) Nil(args ...interface{}) {
as.Helper()
if len(args) == 0 {
as.err(AssertionNoArgs)
return
}
last := args[len(args)-1]
if _, yes := utils.IsNil(last); yes {
return
}
as.err(AssertionNil, last, args)
}
// NotNil asserts that the last item in args is nilable and not nil
func (as Assertions) NotNil(args ...interface{}) {
as.Helper()
if len(args) == 0 {
as.err(AssertionNoArgs)
return
}
last := args[len(args)-1]
if last == nil {
as.err(AssertionNotNil, last, args)
return
}
nilable, yes := utils.IsNil(last)
if !nilable {
as.err(AssertionNotNilable, last, args)
return
}
if yes {
as.err(AssertionNotNilableNil, last, args)
}
}
// Zero asserts x is zero value for its type.
func (as Assertions) Zero(x interface{}) {
as.Helper()
if reflect.DeepEqual(x, reflect.Zero(reflect.TypeOf(x)).Interface()) {
return
}
as.err(AssertionZero, x)
}
// NotZero asserts that x is not zero value for its type.
func (as Assertions) NotZero(x interface{}) {
as.Helper()
if reflect.DeepEqual(x, reflect.Zero(reflect.TypeOf(x)).Interface()) {
as.err(AssertionNotZero, x)
}
}
// Regex asserts that str matches the regex pattern
func (as Assertions) Regex(pattern, str string) {
as.Helper()
if regexp.MustCompile(pattern).MatchString(str) {
return
}
as.err(AssertionRegex, pattern, str)
}
// Has asserts that container has item.
// The container can be a string, []byte, slice, array, or map.
// For how comparison works, see [utils.SmartCompare] .
func (as Assertions) Has(container, item interface{}) {
as.Helper()
if c, ok := container.(string); ok && hasStr(c, item) {
return
} else if c, ok := container.([]byte); ok && hasStr(string(c), item) {
return
}
cv := reflect.Indirect(reflect.ValueOf(container))
switch cv.Kind() {
case reflect.Slice, reflect.Array:
for i := 0; i < cv.Len(); i++ {
if utils.SmartCompare(cv.Index(i).Interface(), item) == 0 {
return
}
}
case reflect.Map:
keys := cv.MapKeys()
for _, k := range keys {
if utils.SmartCompare(cv.MapIndex(k).Interface(), item) == 0 {
return
}
}
}
as.err(AssertionHas, container, item)
}
// Len asserts that the length of list equals l
func (as Assertions) Len(list interface{}, l int) {
as.Helper()
actual := reflect.ValueOf(list).Len()
if actual == l {
return
}
as.err(AssertionLen, actual, l, list)
}
// Err asserts that the last item in args is error
func (as Assertions) Err(args ...interface{}) {
as.Helper()
if len(args) == 0 {
as.err(AssertionNoArgs)
return
}
last := args[len(args)-1]
if err, _ := last.(error); err != nil {
return
}
as.err(AssertionErr, last, args)
}
// E is a shortcut for Must().Nil(args...)
func (as Assertions) E(args ...interface{}) {
as.Helper()
as.Must().Nil(args...)
}
// Panic executes fn and asserts that fn panics
func (as Assertions) Panic(fn func()) (val interface{}) {
as.Helper()
defer func() {
as.Helper()
val = recover()
if val == nil {
as.err(AssertionPanic, fn)
}
}()
fn()
return
}
// Is asserts that x is kind of y, it uses reflect.Kind to compare.
// If x and y are both error type, it will use errors.Is to compare.
func (as Assertions) Is(x, y interface{}) {
as.Helper()
if x == nil && y == nil {
return
}
if ae, ok := x.(error); ok {
if be, ok := y.(error); ok {
if errors.Is(ae, be) {
return
}
as.err(AssertionIsInChain, x, y)
return
}
}
at := reflect.TypeOf(x)
bt := reflect.TypeOf(y)
if x != nil && y != nil && at.Kind() == bt.Kind() {
return
}
as.err(AssertionIsKind, x, y)
}
// Count asserts that the returned function will be called n times
func (as Assertions) Count(n int) func() {
as.Helper()
count := int64(0)
as.Cleanup(func() {
c := int(atomic.LoadInt64(&count))
if c != n {
as.Helper()
as.err(AssertionCount, n, c)
}
})
return func() {
atomic.AddInt64(&count, 1)
}
}
func (as Assertions) err(t AssertionErrType, details ...interface{}) {
as.Helper()
if len(as.desc) > 0 {
for _, d := range as.desc {
as.Logf("%s", d)
}
}
// TODO: we should take advantage of the Helper function
_, f, l, _ := runtime.Caller(2)
c := &AssertionCtx{
Type: t,
Details: details,
File: f,
Line: l,
}
as.Logf("%s", as.ErrorHandler.Report(c))
if as.must {
as.FailNow()
return
}
as.Fail()
}
func hasStr(c string, item interface{}) bool {
if it, ok := item.(string); ok {
if strings.Contains(c, it) {
return true
}
} else if it, ok := item.([]byte); ok {
if strings.Contains(c, string(it)) {
return true
}
} else if it, ok := item.(rune); ok {
if strings.ContainsRune(c, it) {
return true
}
}
return false
}