forked from smarty/assertions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
panic.go
128 lines (107 loc) · 3.13 KB
/
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
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
package assertions
import (
"errors"
"fmt"
)
// ShouldPanic receives a void, niladic function and expects to recover a panic.
func ShouldPanic(actual interface{}, expected ...interface{}) (message string) {
if fail := need(0, expected); fail != success {
return fail
}
action, _ := actual.(func())
if action == nil {
message = shouldUseVoidNiladicFunction
return
}
defer func() {
recovered := recover()
if recovered == nil {
message = shouldHavePanicked
} else {
message = success
}
}()
action()
return
}
// ShouldNotPanic receives a void, niladic function and expects to execute the function without any panic.
func ShouldNotPanic(actual interface{}, expected ...interface{}) (message string) {
if fail := need(0, expected); fail != success {
return fail
}
action, _ := actual.(func())
if action == nil {
message = shouldUseVoidNiladicFunction
return
}
defer func() {
recovered := recover()
if recovered != nil {
message = fmt.Sprintf(shouldNotHavePanicked, recovered)
} else {
message = success
}
}()
action()
return
}
// ShouldPanicWith receives a void, niladic function and expects to recover a panic with the second argument as the content.
// If the expected value is an error and the recovered value is an error, errors.Is is used to compare them.
func ShouldPanicWith(actual interface{}, expected ...interface{}) (message string) {
if fail := need(1, expected); fail != success {
return fail
}
action, _ := actual.(func())
if action == nil {
message = shouldUseVoidNiladicFunction
return
}
defer func() {
recovered := recover()
if recovered == nil {
message = shouldHavePanicked
} else {
recoveredErr, errFound := recovered.(error)
expectedErr, expectedFound := expected[0].(error)
if errFound && expectedFound && errors.Is(recoveredErr, expectedErr) {
message = success
} else if equal := ShouldEqual(recovered, expected[0]); equal != success {
message = serializer.serialize(expected[0], recovered, fmt.Sprintf(shouldHavePanickedWith, expected[0], recovered))
} else {
message = success
}
}
}()
action()
return
}
// ShouldNotPanicWith receives a void, niladic function and expects to recover a panic whose content differs from the second argument.
// If the expected value is an error and the recovered value is an error, errors.Is is used to compare them.
func ShouldNotPanicWith(actual interface{}, expected ...interface{}) (message string) {
if fail := need(1, expected); fail != success {
return fail
}
action, _ := actual.(func())
if action == nil {
message = shouldUseVoidNiladicFunction
return
}
defer func() {
recovered := recover()
if recovered == nil {
message = success
} else {
recoveredErr, errFound := recovered.(error)
expectedErr, expectedFound := expected[0].(error)
if errFound && expectedFound && errors.Is(recoveredErr, expectedErr) {
message = fmt.Sprintf(shouldNotHavePanickedWith, expected[0])
} else if equal := ShouldEqual(recovered, expected[0]); equal == success {
message = fmt.Sprintf(shouldNotHavePanickedWith, expected[0])
} else {
message = success
}
}
}()
action()
return
}