-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassert_panic.go
98 lines (80 loc) · 2.18 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package actually
import (
"testing"
)
// Panic asserts that a test function you got panics
/*
actually.Got(func(){ panic("OMG") }).Panic(t) // Pass
*/
func (a *testingA) Panic(t *testing.T, testNames ...string) *testingA {
invalidCall(a)
a.name = a.naming(testNames...)
a.t = t
a.t.Helper()
if !isFuncType(a.got) {
wi := a.wi().Got(a.got)
return a.fail(wi, reason_GotShouldFuncType)
}
if didPanic, _ := didPanic(a.got.(func())); !didPanic {
wi := a.wi().Got(a.got)
return a.fail(wi, reason_ExpectPanic)
}
return a
}
// PanicMessage asserts that a test function you got panics, and
// a recovered panic message is same as you expect
/*
actually.Got(func(){ panic("OMG") }).Expect("OMG").PanicMessage(t) // Pass
*/
func (a *testingA) PanicMessage(t *testing.T, testNames ...string) *testingA {
invalidCall(a)
a.name = a.naming(testNames...)
a.t = t
a.t.Helper()
if !isFuncType(a.got) {
wi := a.wi().Got(a.got)
return a.fail(wi, reason_GotShouldFuncType)
}
didPanic, panicMessage := didPanic(a.got.(func()))
if !didPanic {
wi := a.wi().Got(a.got)
return a.fail(wi, reason_ExpectPanic)
}
if !objectsAreSameType(a.expect, panicMessage) {
wi := a.wi().Got(panicMessage).Message(gotFunc_Label, Dump(a.got)).Expect(a.expect)
return a.fail(wi, reason_PanicButMsgwrongType)
}
if !objectsAreSame(a.expect, panicMessage) {
wi := a.wi().Got(panicMessage).Message(gotFunc_Label, Dump(a.got)).Expect(a.expect)
return a.fail(wi, reason_PanicButMsgDifferent)
}
return a
}
// NoPanic asserts that a test function you got doesn't panic
/*
actually.Got(func(){ panic("OMG") }).NoPanic(t) // Fail
*/
func (a *testingA) NoPanic(t *testing.T, testNames ...string) *testingA {
invalidCall(a)
a.name = a.naming(testNames...)
a.t = t
a.t.Helper()
if !isFuncType(a.got) {
wi := a.wi().Got(a.got)
return a.fail(wi, reason_GotShouldFuncType)
}
if didPanic, panicMessage := didPanic(a.got.(func())); didPanic {
wi := a.wi().Got(panicMessage).Message(gotFunc_Label, Dump(a.got))
return a.fail(wi, reason_ExpectNoPanic)
}
return a
}
func didPanic(f func()) (did bool, panicMessage any) {
did = true
defer func() {
panicMessage = recover()
}()
f()
did = false
return
}