-
Notifications
You must be signed in to change notification settings - Fork 180
/
assert.go
80 lines (70 loc) · 1.76 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
package test
import (
"path/filepath"
"reflect"
"runtime"
"strings"
"testing"
)
func Contains(t *testing.T, expected, actual interface{}) {
if reflect.TypeOf(expected) != reflect.TypeOf(actual) {
formattedLog(t, "value type not match %T (expected)\n\n\t != %T (actual)", expected, actual)
}
switch e := expected.(type) {
case string:
a := actual.(string)
if !strings.Contains(a, e) {
formattedLog(t, " %v (expected)\n\n\t!= %v (actual)",
expected, actual)
t.FailNow()
}
default:
formattedLog(t, "unsupported type %T(expected)", expected)
t.FailNow()
}
}
func Equal(t *testing.T, expected, actual interface{}) {
if !reflect.DeepEqual(expected, actual) {
formattedLog(t, " %v (expected)\n\n\t!= %v (actual)",
expected, actual)
t.FailNow()
}
}
func NotEqual(t *testing.T, expected, actual interface{}) {
if reflect.DeepEqual(expected, actual) {
formattedLog(t, "value should not equal %#v", actual)
t.FailNow()
}
}
func Nil(t *testing.T, object interface{}) {
if !isNil(object) {
formattedLog(t, " <nil> (expected)\n\n\t!= %#v (actual)", object)
t.FailNow()
}
}
func NotNil(t *testing.T, object interface{}) {
if isNil(object) {
formattedLog(t, "Expected value not to be <nil>", object)
t.FailNow()
}
}
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
}
func formattedLog(t *testing.T, fmt string, args ...interface{}) {
_, file, line, _ := runtime.Caller(2)
file = filepath.Base(file)
targs := make([]interface{}, len(args)+2)
targs[0] = file
targs[1] = line
copy(targs[2:], args)
t.Logf("\033[31m%s:%d:\n\n\t"+fmt+"\033[39m\n\n", targs...)
}