-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpanics_test.go
242 lines (227 loc) · 4.08 KB
/
panics_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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package panics
import (
"errors"
"fmt"
"testing"
)
func TestNow(t *testing.T) {
willPanic(t, "Panic now", func() {
Now()
})
for _, test := range []struct {
name string
msgAndArgs []interface{}
output string
}{
{
"no message",
nil,
"Panic now",
},
{
"no message",
[]interface{}{"foo"},
"foo",
},
{
"no message",
[]interface{}{"foo %s %d", "bar", 1},
"foo bar 1",
},
} {
t.Run(test.name, func(t *testing.T) {
willPanic(t, test.output, func() {
Now(test.msgAndArgs...)
})
})
}
}
func TestIf(t *testing.T) {
willPanic(t, "Panic because true", func() {
If(true)
})
willNotPanic(t, func() {
If(false)
})
}
func TestIfNot(t *testing.T) {
willPanic(t, "Panic because false", func() {
IfNot(false)
})
willNotPanic(t, func() {
IfNot(true)
})
}
func TestIfError(t *testing.T) {
willPanic(t, "Panic because error: foo", func() {
IfError(errors.New("foo"))
})
willNotPanic(t, func() {
IfError(nil)
})
}
func TestIfNotError(t *testing.T) {
willPanic(t, "Panic because no error", func() {
IfNotError(nil)
})
willNotPanic(t, func() {
IfNotError(errors.New("foo"))
})
}
func TestIfNil(t *testing.T) {
willPanic(t, "Panic because nil", func() {
IfNil(nil)
})
willNotPanic(t, func() {
IfNil(false)
})
}
func TestIfNotNil(t *testing.T) {
willPanic(t, "Panic because not nil: have 0 (int)", func() {
IfNotNil(0)
})
willNotPanic(t, func() {
IfNotNil(nil)
})
}
func TestIfEqual(t *testing.T) {
for _, test := range []struct {
name string
val interface{}
expected interface{}
msgAndArgs []interface{}
willPanic bool
output string
}{
{
"1=1",
1, 1,
nil,
true, "Panic because equal: have 1 (int)",
},
{
"1 != 2",
1, 2,
nil,
false, "",
},
{
"int(1) != int16(1)",
1, int16(1),
nil,
false, "",
},
} {
t.Run(test.name, func(t *testing.T) {
if test.willPanic {
willPanic(t, test.output, func() {
IfEqual(test.val, test.expected, test.msgAndArgs...)
})
} else {
willNotPanic(t, func() {
IfEqual(test.val, test.expected, test.msgAndArgs...)
})
}
})
}
}
func TestIfNotEqual(t *testing.T) {
for _, test := range []struct {
name string
val interface{}
expected interface{}
msgAndArgs []interface{}
willPanic bool
output string
}{
{
"1=1",
1, 1,
nil,
false, "",
},
{
"1 != 2",
1, 2,
nil,
true, "Panic because not equal: have 2 (int), want 1 (int)",
},
{
"int(1) != int16(1)",
1, int16(1),
nil,
true, "Panic because not equal: have 1 (int16), want 1 (int)",
},
} {
t.Run(test.name, func(t *testing.T) {
if test.willPanic {
willPanic(t, test.output, func() {
IfNotEqual(test.val, test.expected, test.msgAndArgs...)
})
} else {
willNotPanic(t, func() {
IfNotEqual(test.val, test.expected, test.msgAndArgs...)
})
}
})
}
}
func TestFormatMsgAndArgs(t *testing.T) {
for _, test := range []struct {
name string
msgAndArgs []interface{}
expected string
}{
{
"No message",
nil,
"",
},
{
"One message",
[]interface{}{"foo"},
"foo",
},
{
"Message format",
[]interface{}{"foo %s", "bar"},
"foo bar",
},
} {
t.Run(test.name, func(t *testing.T) {
actual := formatMsgAndArgs(test.msgAndArgs...)
if actual != test.expected {
t.Errorf(`Got "%s", want "%s"`, actual, test.expected)
}
})
}
}
func panicResult(fn func()) (interface{}, bool) {
didPanic := false
var message interface{}
func() {
defer func() {
if message = recover(); message != nil {
didPanic = true
}
}()
fn()
}()
return message, didPanic
}
func willNotPanic(t *testing.T, fn func()) {
if message, didPanic := panicResult(fn); didPanic {
t.Errorf("should not panic\n\tPanic value:\t%#v", message)
}
}
func willPanic(t *testing.T, msg string, fn func()) {
message, didPanic := panicResult(fn)
if !didPanic {
t.Errorf("should panic")
return
}
got := fmt.Sprintf("%v", message)
if got != msg {
t.Errorf("panic should have returned \"%s\", got \"%s\"", msg, got)
}
}