-
Notifications
You must be signed in to change notification settings - Fork 0
/
assert_panic.go
79 lines (64 loc) · 1.47 KB
/
assert_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
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
package goassert
import (
"reflect"
"testing"
)
/*
Asserts that the given function panics
*/
func Panic(t testing.TB, underTest func()) {
t.Helper()
defer func() {
if r := recover(); r == nil {
t.Error("Expected panic but there was no panic")
}
}()
underTest()
}
/*
Asserts that the given function does not panic
*/
func NotPanic(t testing.TB, underTest func()) {
t.Helper()
defer func() {
if r := recover(); r != nil {
t.Errorf("Expected no panic but there was panic: %v", r)
}
}()
underTest()
}
/*
Asserts that the given function panics with the specified error.
The actual error must be of the same type and value as the given error
*/
func PanicWithError[T any](t testing.TB, expectedError T, underTest func()) {
t.Helper()
defer func() {
r := recover()
if r == nil {
t.Error("Expected panic but there was no panic")
return
}
if !reflect.DeepEqual(expectedError, r) {
t.Errorf("Expected panic with %v error but got %v error", expectedError, r)
}
}()
underTest()
}
/*
Asserts that the given function does not panic with the specified error.
The assertion succeeds if the given function does not panic or panics with a different error
*/
func NotPanicWithError[T any](t testing.TB, expectedError T, underTest func()) {
t.Helper()
defer func() {
r := recover()
if r == nil {
return
}
if reflect.DeepEqual(expectedError, r) {
t.Errorf("Expected panic with different error than %v error", expectedError)
}
}()
underTest()
}