-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathasserts.go
More file actions
471 lines (371 loc) · 9.89 KB
/
Copy pathasserts.go
File metadata and controls
471 lines (371 loc) · 9.89 KB
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
package biff
import (
"encoding/json"
"fmt"
"go/ast"
"go/parser"
"io/ioutil"
"os"
"reflect"
"runtime"
"runtime/debug"
"strings"
)
var exit = func() {
os.Exit(1)
}
// deprecated
// AssertNotEqual return true if `obtained` is not equal to `expected` otherwise
// it will print trace and exit.
func (a *A) AssertNotEqual(obtained, expected interface{}) bool {
if !reflect.DeepEqual(expected, obtained) {
l, r := printShould(expected)
fmt.Printf(" %s is not equal %s\n", l, r)
return true
}
printExpectedObtained(expected, obtained)
return false
}
// AssertNotEqual return true if `obtained` is not equal to `expected` otherwise
// it will print trace and exit.
func AssertNotEqual(obtained, expected interface{}) bool {
if !reflect.DeepEqual(expected, obtained) {
l, r := printShould(expected)
fmt.Printf(" %s is not equal %s\n", l, r)
return true
}
printExpectedObtained(expected, obtained)
return false
}
// deprecated
// AssertEqual return true if `obtained` is equal to `expected` otherwise it
// will print trace and exit.
func (a *A) AssertEqual(obtained, expected interface{}) bool {
if reflect.DeepEqual(expected, obtained) {
l, r := printShould(expected)
fmt.Printf(" %s is %s\n", l, r)
return true
}
printExpectedObtained(expected, obtained)
return false
}
// AssertEqual return true if `obtained` is equal to `expected` otherwise it
// will print trace and exit.
func AssertEqual(obtained, expected interface{}) bool {
if reflect.DeepEqual(expected, obtained) {
l, r := printShould(expected)
fmt.Printf(" %s is %s\n", l, r)
return true
}
printExpectedObtained(expected, obtained)
return false
}
func readFileLine(filename string, line int) string {
data, _ := ioutil.ReadFile(filename)
lines := strings.Split(string(data), "\n")
return lines[line-1]
}
// deprecated
// AssertEqualJson return true if `obtained` is equal to `expected`. Prior to
// comparison, both values are JSON Marshaled/Unmarshaled to avoid JSON type
// issues like int vs float etc. Otherwise it will print trace and exit.
func (a *A) AssertEqualJson(obtained, expected interface{}) bool {
e := interface{}(nil)
{
b, _ := json.Marshal(expected)
json.Unmarshal(b, &e)
}
o := interface{}(nil)
{
b, _ := json.Marshal(obtained)
json.Unmarshal(b, &o)
}
if reflect.DeepEqual(e, o) {
l, r := printShould(expected)
fmt.Printf(" %s is same JSON as %s\n", l, r)
return true
}
printExpectedObtained(e, o)
return false
}
// AssertEqualJson return true if `obtained` is equal to `expected`. Prior to
// comparison, both values are JSON Marshaled/Unmarshaled to avoid JSON type
// issues like int vs float etc. Otherwise it will print trace and exit.
func AssertEqualJson(obtained, expected interface{}) bool {
e := interface{}(nil)
{
b, _ := json.Marshal(expected)
json.Unmarshal(b, &e)
}
o := interface{}(nil)
{
b, _ := json.Marshal(obtained)
json.Unmarshal(b, &o)
}
if reflect.DeepEqual(e, o) {
l, r := printShould(expected)
fmt.Printf(" %s is same JSON as %s\n", l, r)
return true
}
printExpectedObtained(e, o)
return false
}
// deprecated
// AssertNil return true if `obtained` is nil, otherwise it will print trace and
// exit.
func (a *A) AssertNil(obtained interface{}) bool {
if nil == obtained || reflect.ValueOf(obtained).IsNil() {
l, _ := printShould(nil)
fmt.Printf(" %s is nil\n", l)
return true
}
printExpectedObtained(nil, obtained)
return false
}
// AssertNil return true if `obtained` is nil, otherwise it will print trace and
// exit.
func AssertNil(obtained interface{}) bool {
if nil == obtained || reflect.ValueOf(obtained).IsNil() {
l, _ := printShould(nil)
fmt.Printf(" %s is nil\n", l)
return true
}
printExpectedObtained(nil, obtained)
return false
}
// deprecated
// AssertNotNil return true if `obtained` is NOT nil, otherwise it will print trace
// and exit.
func (a *A) AssertNotNil(obtained interface{}) bool {
if isNil(obtained) {
line := getStackLine(2)
fmt.Printf(""+
" Expected: not nil\n"+
" Obtained: %#v\n"+
" at %s\n", obtained, line)
exit()
return false
}
l, _ := printShould(nil)
v := fmt.Sprintf("%#v", obtained)
if v != l {
v = " (" + v + ")"
}
fmt.Printf(" %s is not nil%s\n", l, v)
return true
}
// AssertNotNil return true if `obtained` is NOT nil, otherwise it will print trace
// and exit.
func AssertNotNil(obtained interface{}) bool {
if isNil(obtained) {
line := getStackLine(2)
fmt.Printf(""+
" Expected: not nil\n"+
" Obtained: %#v\n"+
" at %s\n", obtained, line)
exit()
return false
}
l, _ := printShould(nil)
v := fmt.Sprintf("%#v", obtained)
if v != l {
v = " (" + v + ")"
}
fmt.Printf(" %s is not nil%s\n", l, v)
return true
}
// deprecated
// AssertTrue return true if `obtained` is true, otherwise it will print trace
// and exit.
func (a *A) AssertTrue(obtained interface{}) bool {
if reflect.DeepEqual(true, obtained) {
l, _ := printShould(nil)
fmt.Printf(" %s is true\n", l)
return true
}
printExpectedObtained(true, obtained)
return false
}
// AssertTrue return true if `obtained` is true, otherwise it will print trace
// and exit.
func AssertTrue(obtained interface{}) bool {
if reflect.DeepEqual(true, obtained) {
l, _ := printShould(nil)
fmt.Printf(" %s is true\n", l)
return true
}
printExpectedObtained(true, obtained)
return false
}
// deprecated
// AssertFalse return true if `obtained` is false, otherwise it will print trace
// and exit.
func (a *A) AssertFalse(obtained interface{}) bool {
if reflect.DeepEqual(false, obtained) {
l, _ := printShould(nil)
fmt.Printf(" %s is false\n", l)
return true
}
printExpectedObtained(true, obtained)
return false
}
// AssertFalse return true if `obtained` is false, otherwise it will print trace
// and exit.
func AssertFalse(obtained interface{}) bool {
if reflect.DeepEqual(false, obtained) {
l, _ := printShould(nil)
fmt.Printf(" %s is false\n", l)
return true
}
printExpectedObtained(true, obtained)
return false
}
// deprecated
// AssertInArray return true if `item` match at least with one element of the
// array. Otherwise it will print trace and exit.
func (a *A) AssertInArray(array interface{}, item interface{}) bool {
v := reflect.ValueOf(array)
if v.Kind() != reflect.Array && v.Kind() != reflect.Slice {
line := getStackLine(2)
fmt.Printf("Expected second argument to be array:\n"+
" Obtained: %#v\n"+
" at %s\n", array, line)
exit()
}
l := v.Len()
for i := 0; i < l; i++ {
e := v.Index(i)
if reflect.DeepEqual(e.Interface(), item) {
l, r := printShould(item)
fmt.Printf(" %s[%d] is %s\n", l, i, r)
return true
}
}
line := getStackLine(2)
fmt.Printf(""+
" Expected item to be in array.\n"+
" Item: %#v\n"+
" Array: %#v\n"+
" at %s\n", item, array, line)
exit()
return false
}
// AssertInArray return true if `item` match at least with one element of the
// array. Otherwise it will print trace and exit.
func AssertInArray(array interface{}, item interface{}) bool {
v := reflect.ValueOf(array)
if v.Kind() != reflect.Array && v.Kind() != reflect.Slice {
line := getStackLine(2)
fmt.Printf("Expected second argument to be array:\n"+
" Obtained: %#v\n"+
" at %s\n", array, line)
exit()
}
l := v.Len()
for i := 0; i < l; i++ {
e := v.Index(i)
if reflect.DeepEqual(e.Interface(), item) {
l, r := printShould(item)
fmt.Printf(" %s[%d] is %s\n", l, i, r)
return true
}
}
line := getStackLine(2)
fmt.Printf(""+
" Expected item to be in array.\n"+
" Item: %#v\n"+
" Array: %#v\n"+
" at %s\n", item, array, line)
exit()
return false
}
func getStackLine(linesToSkip int) string {
stack := debug.Stack()
lines := make([]string, 0)
index := 0
for i := 0; i < len(stack); i++ {
if stack[i] == []byte("\n")[0] {
lines = append(lines, string(stack[index:i-1]))
index = i + 1
}
}
return lines[linesToSkip*2+3] + " " + lines[linesToSkip*2+4]
}
func printExpectedObtained(expected, obtained interface{}) {
line := getStackLine(3)
fmt.Printf(""+
" Expected: %#v\n"+
" Obtained: %#v\n"+
" at %s\n", expected, obtained, line)
exit()
}
func printShould(value interface{}) (arg0, arg1 string) {
arg0 = "It"
arg1 = fmt.Sprintf("%#v", value)
func() {
p := make([]runtime.StackRecord, 50)
_, ok := runtime.GoroutineProfile(p)
if !ok {
return
}
frames := runtime.CallersFrames(p[0].Stack())
// Make it compatible with latests golang versions (1.14 on)
frame, more := frames.Next()
for ; more; frame, more = frames.Next() {
if frame.Function == "github.com/fulldump/biff.printShould" {
break
}
}
frame, _ = frames.Next()
frame, _ = frames.Next()
l := readFileLine(frame.File, frame.Line)
a, err := parser.ParseExpr(l)
if nil != err {
return
}
aFunc, ok := a.(*ast.CallExpr)
if !ok {
return
}
a0 := aFunc.Args[0]
arg0 = l[a0.Pos()-1 : a0.End()-1]
if len(aFunc.Args) > 1 {
a1 := aFunc.Args[1]
arg1 = l[a1.Pos()-1 : a1.End()-1]
}
}()
v := fmt.Sprintf("%#v", value)
if v != arg1 {
arg1 = arg1 + " (" + v + ")"
}
return
}
// Source: https://sourcegraph.com/github.com/stretchr/testify/-/blob/assert/assertions.go#L520:6
// isNil checks if a specified object is nil or not, without Failing.
func isNil(object interface{}) bool {
if object == nil {
return true
}
value := reflect.ValueOf(object)
kind := value.Kind()
isNilableKind := containsKind(
[]reflect.Kind{
reflect.Chan, reflect.Func,
reflect.Interface, reflect.Map,
reflect.Ptr, reflect.Slice},
kind)
if isNilableKind && value.IsNil() {
return true
}
return false
}
// source: github.com/stretchr/testify/-/blob/assert/assertions.go#L524
// containsKind checks if a specified kind in the slice of kinds.
func containsKind(kinds []reflect.Kind, kind reflect.Kind) bool {
for i := 0; i < len(kinds); i++ {
if kind == kinds[i] {
return true
}
}
return false
}