-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpanic.go
More file actions
232 lines (197 loc) · 5.87 KB
/
panic.go
File metadata and controls
232 lines (197 loc) · 5.87 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
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
package assert
import (
"fmt"
"testing"
)
// Panic expects the function fn to panic, and it'll set the result to fail if the function doesn't
// panic.
//
// a := assert.New(t)
// a.Panic(func() {
// panic("some error")
// }) // success
//
// a.Panic(func() {
// // no panic
// }) // fail
func (a *Assertion) Panic(fn func(), message ...any) error {
a.Helper()
return tryPanic(a.T, false, fn, message...)
}
// PanicNow expects the function fn to panic. It'll set the result to fail if the function doesn't
// panic, and stop the execution.
//
// a := assert.New(t)
// a.PanicNow(func() {
// panic("some error")
// }) // success
//
// a.PanicNow(func() {
// // no panic
// }) // fail
// // never run
func (a *Assertion) PanicNow(fn func(), message ...any) error {
a.Helper()
return tryPanic(a.T, true, fn, message...)
}
// NotPanic asserts that the function fn does not panic, and it'll set the result to fail if the
// function panic.
//
// a := assert.New(t)
// a.NotPanic(func() {
// // no panic
// }) // success
//
// a.NotPanic(func() {
// panic("some error")
// }) // fail
func (a *Assertion) NotPanic(fn func(), message ...any) error {
a.Helper()
return tryNotPanic(a.T, false, fn, message...)
}
// NotPanicNow asserts that the function fn does not panic. It'll set the result to fail if the
// function panic, and it also stops the execution.
//
// a := assert.New(t)
// a.NotPanicNow(func() {
// // no panic
// }) // success
//
// a.NotPanicNow(func() {
// panic("some error")
// }) // fail and terminate
// // never run
func (a *Assertion) NotPanicNow(fn func(), message ...any) error {
a.Helper()
return tryNotPanic(a.T, true, fn, message...)
}
// tryPanic executes the function fn, and try to catching the panic error. It expect the function
// fn to panic, and returns error if fn does not panic.
func tryPanic(t *testing.T, failedNow bool, fn func(), message ...any) error {
t.Helper()
e := isPanic(fn)
if e != nil {
return nil
}
err := newAssertionError(defaultErrMessagePanic, message...)
failed(t, err, failedNow)
return err
}
// tryNotPanic executes the function fn, and try to catching the panic error. It expect the
// function fn does not to panic, and returns error if panic.
func tryNotPanic(t *testing.T, failedNow bool, fn func(), message ...any) error {
t.Helper()
e := isPanic(fn)
if e == nil {
return nil
}
err := newAssertionError(fmt.Sprintf(defaultErrMessageNotPanic, e), message...)
failed(t, err, failedNow)
return err
}
// PanicOf expects the function fn to panic by the expected error. If the function does not panic
// or panic for another reason, it will set the result to fail.
//
// a := assert.New(t)
// a.PanicOf(func() {
// panic("expected error")
// }, "expected error") // success
// a.PanicOf(func() {
// panic("unexpected error")
// }, "expected error") // fail
// a.PanicOf(func() {
// // ..., no panic
// }, "expected error") // fail
func (a *Assertion) PanicOf(fn func(), expectErr any, message ...any) error {
a.Helper()
return tryPanicOf(a.T, false, fn, expectErr, message...)
}
// PanicOfNow expects the function fn to panic by the expected error. If the function does not
// panic or panic for another reason, it will set the result to fail and terminate the execution.
//
// a := assert.New(t)
// a.PanicOfNow(func() {
// panic("expected error")
// }, "expected error") // success
// a.PanicOfNow(func() {
// panic("unexpected error")
// }, "expected error") // fail and terminated
// // never runs
func (a *Assertion) PanicOfNow(fn func(), expectErr any, message ...any) error {
a.Helper()
return tryPanicOf(a.T, true, fn, expectErr, message...)
}
// NotPanicOf expects the function fn not panic, or the function does not panic by the unexpected
// error. If the function panics by the unexpected error, it will set the result to fail.
//
// a := assert.New(t)
// a.NotPanicOf(func() {
// panic("other error")
// }, "unexpected error") // success
// a.NotPanicOf(func() {
// // ..., no panic
// }, "unexpected error") // success
// a.NotPanicOf(func() {
// panic("unexpected error")
// }, "unexpected error") // fail
func (a *Assertion) NotPanicOf(fn func(), unexpectedErr any, message ...any) error {
a.Helper()
return tryNotPanicOf(a.T, false, fn, unexpectedErr, message...)
}
// NotPanicOfNow expects the function fn not panic, or the function does not panic by the
// unexpected error. If the function panics by the unexpected error, it will set the result to fail
// and stop the execution.
//
// a := assert.New(t)
// a.NotPanicOfNow(func() {
// panic("other error")
// }, "unexpected error") // success
// a.NotPanicOfNow(func() {
// // ..., no panic
// }, "unexpected error") // success
// a.NotPanicOfNow(func() {
// panic("unexpected error")
// }, "unexpected error") // fail and terminate
// // never runs
func (a *Assertion) NotPanicOfNow(fn func(), unexpectedErr any, message ...any) error {
a.Helper()
return tryNotPanicOf(a.T, true, fn, unexpectedErr, message...)
}
// tryPanicOf executes the function fn, and it expects the function to panic by the expected error.
func tryPanicOf(t *testing.T, failedNow bool, fn func(), expectError any, message ...any) error {
t.Helper()
e := isPanic(fn)
if isEqual(e, expectError) {
return nil
}
err := newAssertionError(fmt.Sprintf(defaultErrMessagePanicOf, expectError, e), message...)
failed(t, err, failedNow)
return err
}
func tryNotPanicOf(
t *testing.T,
failedNow bool,
fn func(),
unexpectedError any,
message ...any,
) error {
t.Helper()
e := isPanic(fn)
if !isEqual(e, unexpectedError) {
return nil
}
err := newAssertionError(fmt.Sprintf(defaultErrMessageNotPanicOf, unexpectedError), message...)
failed(t, err, failedNow)
return err
}
// isPanic executes the function, and tries to catching and returns the return value from
// recover().
func isPanic(fn func()) (err any) {
defer func() {
if e := recover(); e != nil {
err = e
}
}()
fn()
return
}