-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpanic_test.go
More file actions
113 lines (96 loc) · 2.75 KB
/
Copy pathpanic_test.go
File metadata and controls
113 lines (96 loc) · 2.75 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
package assert
import (
"errors"
"testing"
)
func TestPanicAndNotPanic(t *testing.T) {
a := New(t)
mockA := New(new(testing.T))
testPanicAndNotPanic(a, mockA, func() {
// no panic
}, false)
testPanicAndNotPanic(a, mockA, func() {
panic("some panic")
}, true)
}
func testPanicAndNotPanic(a, mockA *Assertion, fn func(), isPanic bool) {
a.T.Helper()
// Panic
testAssertionFunction(a, "Panic", func() error {
return Panic(mockA.T, fn)
}, isPanic)
testAssertionFunction(a, "Assertion.Panic", func() error {
return mockA.Panic(fn)
}, isPanic)
// NotPanic
testAssertionFunction(a, "NotPanic", func() error {
return NotPanic(mockA.T, fn)
}, !isPanic)
testAssertionFunction(a, "Assertion.NotPanic", func() error {
return mockA.NotPanic(fn)
}, !isPanic)
// PanicNow
testAssertionNowFunction(a, "PanicNow", func() {
PanicNow(mockA.T, fn)
}, !isPanic)
testAssertionNowFunction(a, "Assertion.PanicNow", func() {
mockA.PanicNow(fn)
}, !isPanic)
// NotPanicNow
testAssertionNowFunction(a, "NotPanicNow", func() {
NotPanicNow(mockA.T, fn)
}, isPanic)
testAssertionNowFunction(a, "Assertion.NotPanicNow", func() {
mockA.NotPanicNow(fn)
}, isPanic)
}
func TestPanicOf(t *testing.T) {
a := New(t)
mockA := New(new(testing.T))
expectedErr := errors.New("expected error")
testPanicOf(a, mockA, func() {}, expectedErr, false)
testPanicOf(a, mockA, func() {
panic(expectedErr)
}, expectedErr, true)
testPanicOf(a, mockA, func() {
panic("not expected error")
}, expectedErr, false)
testPanicOf(a, mockA, func() {
panic("expected error")
}, "expected error", true)
}
func testPanicOf(a, mockA *Assertion, fn func(), expectErr any, isExpectedPanic bool) {
a.Helper()
testAssertionFunction(a, "PanicOf", func() error {
return PanicOf(mockA.T, fn, expectErr)
}, isExpectedPanic)
testAssertionFunction(a, "Assertion.PanicOf", func() error {
return mockA.PanicOf(fn, expectErr)
}, isExpectedPanic)
testAssertionFunction(a, "NotPanicOf", func() error {
return NotPanicOf(mockA.T, fn, expectErr)
}, !isExpectedPanic)
testAssertionFunction(a, "Assertion.NotPanicOf", func() error {
return mockA.NotPanicOf(fn, expectErr)
}, !isExpectedPanic)
testAssertionNowFunction(a, "PanicOfNow", func() {
PanicOfNow(mockA.T, fn, expectErr)
}, !isExpectedPanic)
testAssertionNowFunction(a, "Assertion.PanicOfNow", func() {
mockA.PanicOfNow(fn, expectErr)
}, !isExpectedPanic)
testAssertionNowFunction(a, "NotPanicOfNow", func() {
NotPanicOfNow(mockA.T, fn, expectErr)
}, isExpectedPanic)
testAssertionNowFunction(a, "Assertion.NotPanicOfNow", func() {
mockA.NotPanicOfNow(fn, expectErr)
}, isExpectedPanic)
}
func TestIsPanic(t *testing.T) {
Nil(t, isPanic(func() {
// no panic
}))
NotNil(t, isPanic(func() {
panic("unexpected panic")
}))
}