-
Notifications
You must be signed in to change notification settings - Fork 567
/
require.go
145 lines (128 loc) · 3.31 KB
/
require.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
144
145
package require
import (
"reflect"
"regexp"
"runtime"
"testing"
)
func Matches(tb testing.TB, expectedMatch string, actual string, msgAndArgs ...interface{}) {
r, err := regexp.Compile(expectedMatch)
if err != nil {
fatal(tb, msgAndArgs, "Match string provided (%v) is invalid", expectedMatch)
}
if !r.MatchString(actual) {
fatal(tb, msgAndArgs, "Actual string (%v) does not match pattern (%v)", actual, expectedMatch)
}
}
func Equal(tb testing.TB, expected interface{}, actual interface{}, msgAndArgs ...interface{}) {
if !reflect.DeepEqual(expected, actual) {
fatal(
tb,
msgAndArgs,
"Not equal: %#v (expected)\n"+
" != %#v (actual)", expected, actual)
}
}
func NotEqual(tb testing.TB, expected interface{}, actual interface{}, msgAndArgs ...interface{}) {
if reflect.DeepEqual(expected, actual) {
fatal(
tb,
msgAndArgs,
"Equal: %#v (expected)\n"+
" == %#v (actual)", expected, actual)
}
}
func EqualOneOf(tb testing.TB, expecteds []interface{}, actual interface{}, msgAndArgs ...interface{}) {
equal := false
for _, expected := range expecteds {
if reflect.DeepEqual(expected, actual) {
equal = true
break
}
}
if !equal {
fatal(
tb,
msgAndArgs,
"Not equal 1 of: %#v (expecteds)\n"+
" != %#v (actual)", expecteds, actual)
}
}
func OneOfEquals(tb testing.TB, expected interface{}, actuals []interface{}, msgAndArgs ...interface{}) {
equal := false
for _, actual := range actuals {
if reflect.DeepEqual(expected, actual) {
equal = true
break
}
}
if !equal {
fatal(
tb,
msgAndArgs,
"Not equal : %#v (expected)\n"+
" one of != %#v (actuals)", expected, actuals)
}
}
func NoError(tb testing.TB, err error, msgAndArgs ...interface{}) {
if err != nil {
fatal(tb, msgAndArgs, "No error is expected but got %v", err)
}
}
func YesError(tb testing.TB, err error, msgAndArgs ...interface{}) {
if err == nil {
fatal(tb, msgAndArgs, "Error is expected but got %v", err)
}
}
func NotNil(tb testing.TB, object interface{}, msgAndArgs ...interface{}) {
success := true
if object == nil {
success = false
} else {
value := reflect.ValueOf(object)
kind := value.Kind()
if kind >= reflect.Chan && kind <= reflect.Slice && value.IsNil() {
success = false
}
}
if !success {
fatal(tb, msgAndArgs, "Expected value not to be nil.")
}
}
func Nil(tb testing.TB, object interface{}, msgAndArgs ...interface{}) {
if object == nil {
return
}
value := reflect.ValueOf(object)
kind := value.Kind()
if kind >= reflect.Chan && kind <= reflect.Slice && value.IsNil() {
return
}
fatal(tb, msgAndArgs, "Expected value to be nil.")
}
func True(tb testing.TB, value bool, msgAndArgs ...interface{}) {
if !value {
fatal(tb, msgAndArgs, "Should be true.")
}
}
func False(tb testing.TB, value bool, msgAndArgs ...interface{}) {
if value {
fatal(tb, msgAndArgs, "Should be false.")
}
}
func logMessage(tb testing.TB, msgAndArgs []interface{}) {
if len(msgAndArgs) == 1 {
tb.Logf(msgAndArgs[0].(string))
}
if len(msgAndArgs) > 1 {
tb.Logf(msgAndArgs[0].(string), msgAndArgs[1:]...)
}
}
func fatal(tb testing.TB, userMsgAndArgs []interface{}, msgFmt string, msgArgs ...interface{}) {
logMessage(tb, userMsgAndArgs)
_, file, line, ok := runtime.Caller(2)
if ok {
tb.Logf("%s:%d", file, line)
}
tb.Fatalf(msgFmt, msgArgs...)
}