-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtesting.go
More file actions
91 lines (80 loc) · 2.03 KB
/
testing.go
File metadata and controls
91 lines (80 loc) · 2.03 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
package gobbc
import (
"encoding/json"
"reflect"
"runtime/debug"
"strings"
"testing"
)
// TW testing.T wrap
type TW struct {
*testing.T
_continue bool //断言失败时是否继续测试(不执行FailNow)
}
// Copy .
func (tw *TW) Copy() *TW {
return &TW{
T: tw.T,
_continue: tw._continue,
}
}
// Continue 断言失败时是否继续测试(不执行FailNow)
func (tw *TW) Continue(_continue bool) *TW {
tw._continue = _continue
return tw
}
func (tw *TW) assertFailed(msg string, args ...interface{}) {
if tw._continue {
stack := strings.Join(
strings.SplitN(string(debug.Stack()), "\n", 7)[5:7],
"\n",
)
args = append(args, "\n"+stack)
tw.Errorf(msg, args...)
} else {
debug.PrintStack()
tw.Fatalf(msg, args...)
}
}
// Nil if x not nil, fatal
func (tw *TW) Nil(x interface{}, args ...interface{}) *TW {
if x != nil {
args = append(args, x)
tw.assertFailed("[test failed] interface{} not nil: %v", args)
}
return tw
}
// True if flag == false, fatal
func (tw *TW) True(flag bool, args ...interface{}) *TW {
if !flag {
tw.assertFailed("[test fatal] flag false, args: %v", args)
}
return tw
}
// IsZero if flag == false, fatal
func (tw *TW) IsZero(v interface{}, args ...interface{}) *TW {
if !reflect.ValueOf(v).IsZero() {
tw.assertFailed("[test fatal] not zero value, args: %v, %v", v, args)
}
return tw
}
// NotZero if flag == false, fatal
func (tw *TW) NotZero(v interface{}, args ...interface{}) *TW {
if reflect.ValueOf(v).IsZero() {
tw.assertFailed("[test fatal] is zero value, args: %v, %v", v, args)
}
return tw
}
// Equal reflect.DeepEqual
func (tw *TW) Equal(expected, actual interface{}, args ...interface{}) *TW {
if !reflect.DeepEqual(expected, actual) {
args = append([]interface{}{expected, expected, actual, actual}, args...)
tw.assertFailed("[test assert failed] should be equal: \n[%v(%T)] \n<-expected actual-> \n[%v(%T)]", args...)
}
return tw
}
// JSONIndent pretty json
func JSONIndent(v interface{}) string {
b, _ := json.MarshalIndent(v, "", " ")
return string(b)
}