-
Notifications
You must be signed in to change notification settings - Fork 0
/
panic.go
52 lines (49 loc) · 860 Bytes
/
panic.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
package assert
import (
"fmt"
"runtime/debug"
"testing"
)
// Panics asserts that the code inside the function f panics.
//
// It returns the recovered value.
func Panics(tb testing.TB, f func(), opts ...Option) (rec any, ok bool) {
tb.Helper()
ok = true
defer func() {
tb.Helper()
rec = recover()
if rec == nil {
ok = false
Fail(
tb,
"panics",
"no panic",
opts...,
)
}
}()
f()
return
}
// NotPanics asserts that the code inside the function f does not panic.
func NotPanics(tb testing.TB, f func(), opts ...Option) (ok bool) {
tb.Helper()
ok = true
defer func() {
tb.Helper()
rec := recover()
if rec != nil {
ok = false
st := string(debug.Stack())
Fail(
tb,
"not_panics",
fmt.Sprintf("panic:\npanic = %s\nstack = %s", ValueStringer(rec), st),
opts...,
)
}
}()
f()
return
}