-
Notifications
You must be signed in to change notification settings - Fork 55
/
test.go
104 lines (87 loc) · 2.18 KB
/
test.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
package zlsgo
import (
"path"
"reflect"
"runtime"
"strconv"
"strings"
"testing"
)
// TestUtil Test aid
type TestUtil struct {
T *testing.T
}
// NewTest testing object
func NewTest(t *testing.T) *TestUtil {
return &TestUtil{t}
}
// GetCallerInfo GetCallerInfo
func (u *TestUtil) GetCallerInfo() string {
var info string
for i := 0; ; i++ {
pc, file, line, ok := runtime.Caller(i)
if !ok {
break
}
basename := path.Base(file)
if !strings.HasSuffix(basename, "_test.go") {
continue
}
funcName := runtime.FuncForPC(pc).Name()
index := strings.LastIndex(funcName, ".Test")
if -1 == index {
index = strings.LastIndex(funcName, ".Benchmark")
if index == -1 {
continue
}
}
funcName = funcName[index+1:]
if index := strings.IndexByte(funcName, '.'); index > -1 {
funcName = funcName[:index]
info = funcName + "(" + basename + ":" + strconv.Itoa(line) + ")"
continue
}
info = funcName + "(" + basename + ":" + strconv.Itoa(line) + ")"
break
}
if info == "" {
info = "<Unable to get information>"
}
return info
}
// Equal Equal
func (u *TestUtil) Equal(expected, actual interface{}) {
if !reflect.DeepEqual(expected, actual) {
u.T.Errorf("%s 期待:%v (type %v) - 结果:%v (type %v)", u.PrintMyName(), expected, reflect.TypeOf(expected), actual, reflect.TypeOf(actual))
}
}
// EqualTrue EqualTrue
func (u *TestUtil) EqualTrue(actual interface{}) {
u.Equal(true, actual)
}
// EqualNil EqualNil
func (u *TestUtil) EqualNil(actual interface{}) {
u.Equal(nil, actual)
}
// EqualExit EqualExit
func (u *TestUtil) EqualExit(expected, actual interface{}) {
if !reflect.DeepEqual(expected, actual) {
u.T.Fatalf("%s 期待:%v (type %v) - 结果:%v (type %v)", u.PrintMyName(), expected, reflect.TypeOf(expected), actual, reflect.TypeOf(actual))
}
}
// Log log
func (u *TestUtil) Log(v ...interface{}) {
tip := []interface{}{"\n " + u.PrintMyName()}
va := append(tip, v...)
u.T.Log(va...)
}
// Fatal Fatal
func (u *TestUtil) Fatal(v ...interface{}) {
tip := []interface{}{"\n " + u.PrintMyName()}
va := append(tip, v...)
u.T.Fatal(va...)
}
// PrintMyName PrintMyName
func (u *TestUtil) PrintMyName() string {
return "@" + u.GetCallerInfo()
}