/
assert.go
87 lines (71 loc) · 1.91 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
package assert
import (
"reflect"
"testing"
)
type FailFunc func(format string, args ...interface{})
var GetFailFunc func(t *testing.T) FailFunc
func init() {
GetFailFunc = func(t *testing.T) FailFunc {
return t.Errorf
}
}
// pack a number of values into a slice containing those values
func Pack(vals ...interface{}) []interface{} {
return vals
}
func True(t *testing.T, val bool) {
if !val {
GetFailFunc(t)("Expected: [true] got: [%v]\n%s", val, SourceInfo(2))
}
}
func False(t *testing.T, val bool) {
if val {
GetFailFunc(t)("Expected: [false] got: [%v]\n%s", val, SourceInfo(2))
}
}
func Equal(t *testing.T, val, expect interface{}) {
if !reflect.DeepEqual(val, expect) {
GetFailFunc(t)("Expected: [%v] got: [%v]\n%s", expect, val, SourceInfo(2))
}
}
func NotEqual(t *testing.T, val, expect interface{}) {
if reflect.DeepEqual(val, expect) {
GetFailFunc(t)("Not Expecting: [%v] got: [%v]\n%s", expect, val, SourceInfo(2))
}
}
func Nil(t *testing.T, val interface{}) {
if val != nil && !reflect.ValueOf(val).IsNil() {
GetFailFunc(t)("Expecting: [nil] got: [%v]\n%s", val, SourceInfo(2))
}
}
func NotNil(t *testing.T, val interface{}) {
if val == nil || reflect.ValueOf(val).IsNil() {
GetFailFunc(t)("Expecting: [not nil] got: [%v]\n%s", val, SourceInfo(2))
}
}
func NoError(t *testing.T, vals []interface{}) {
for _, v := range vals {
if err, ok := v.(error); ok {
GetFailFunc(t)("Expecting: [no error] got error: [%v]\n%s", err, SourceInfo(2))
}
}
}
func HasError(t *testing.T, vals []interface{}) {
for _, v := range vals {
if _, ok := v.(error); ok {
return
}
}
GetFailFunc(t)("Expecting: [error] got no error:\n%s", SourceInfo(2))
}
type TestFunc func(t *testing.T)
func MustPanic(t *testing.T, f TestFunc) {
defer func() {
if r := recover(); r != nil {
}
}()
f(t)
ci, _ := GetCallerInfo(1)
GetFailFunc(t)("Expecting Panic:\n%s:[%d]\n%s", ci.filename, ci.lineNum, ci.lineSrc)
}