-
Notifications
You must be signed in to change notification settings - Fork 55
/
test.go
143 lines (120 loc) · 2.75 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
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
package zlsgo
import (
"fmt"
"reflect"
"runtime"
"strconv"
"strings"
"testing"
)
// TestUtil Test aid
type TestUtil struct {
t testing.TB
}
// NewTest testing object
func NewTest(t testing.TB) *TestUtil {
return &TestUtil{
t: 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 := file
if !strings.HasSuffix(basename, "_test.go") {
continue
}
funcName := runtime.FuncForPC(pc).Name()
index := strings.LastIndex(funcName, ".Test")
if index == -1 {
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) + ")"
info = basename + ":" + strconv.Itoa(line)
continue
}
info = basename + ":" + strconv.Itoa(line)
break
}
if info == "" {
info = "<Unable to get information>"
}
return info
}
// Equal Equal
func (u *TestUtil) Equal(expected, actual interface{}) bool {
if !reflect.DeepEqual(expected, actual) {
u.t.Helper()
fmt.Printf(" %s 期待:%v (type %v) - 结果:%v (type %v)\n", u.PrintMyName(), expected, reflect.TypeOf(expected), actual, reflect.TypeOf(actual))
u.t.Fail()
return false
}
return true
}
// EqualTrue EqualTrue
func (u *TestUtil) EqualTrue(actual interface{}) {
u.Equal(true, actual)
}
// EqualNil EqualNil
func (u *TestUtil) EqualNil(actual interface{}) {
u.Equal(nil, actual)
}
// NoError NoError
func (u *TestUtil) NoError(err error, exit ...bool) bool {
if err == nil {
return true
}
fmt.Printf(" %s Error: %s\n", u.PrintMyName(), err)
if len(exit) > 0 && exit[0] {
u.t.Fatal()
} else {
u.t.Fail()
}
return false
}
// EqualExit EqualExit
func (u *TestUtil) EqualExit(expected, actual interface{}) {
if !reflect.DeepEqual(expected, actual) {
fmt.Printf(" %s 期待:%v (type %v) - 结果:%v (type %v)\n", u.PrintMyName(), expected, reflect.TypeOf(expected), actual, reflect.TypeOf(actual))
u.t.Fatal()
}
}
// Log log
func (u *TestUtil) Log(v ...interface{}) {
u.t.Helper()
u.t.Log(v...)
}
// Logf Logf
func (u *TestUtil) Logf(format string, args ...interface{}) {
u.t.Helper()
u.t.Logf(format, args...)
}
// Fatal Fatal
func (u *TestUtil) Fatal(v ...interface{}) {
u.t.Helper()
u.t.Fatal(v...)
}
// PrintMyName PrintMyName
func (u *TestUtil) PrintMyName() string {
return u.GetCallerInfo()
}
func (u *TestUtil) Run(name string, f func(tt *TestUtil)) {
u.t.Helper()
u.t.(*testing.T).Run(name, func(t *testing.T) {
f(NewTest(t))
})
}
func (u *TestUtil) T() *testing.T {
return u.t.(*testing.T)
}