-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpanic_test.go
More file actions
148 lines (139 loc) · 2.67 KB
/
panic_test.go
File metadata and controls
148 lines (139 loc) · 2.67 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package assert_test
import (
"fmt"
"testing"
"go.arcalot.io/assert"
)
func panicsTest() {
panic("This is for testing purposes")
}
func panicWithArgs(panicValue any, _ int) {
panic(panicValue)
}
func TestCatchesPanic(t *testing.T) {
// Does panic
assert.Panics(t, panicsTest)
assert.Panics(t, func() {
panicWithArgs("This is for testing purposes", 0)
})
// Does not panic
testFailure(t, func(t *testing.T) {
assert.Panics(t, func() {})
})
}
func TestNoPanic(t *testing.T) {
// Does not panic
assert.NoPanic(t, func() {})
// Does panic
testFailure(t, func(t *testing.T) {
assert.NoPanic(t, panicsTest)
})
// Does panic
testFailure(t, func(t *testing.T) {
assert.NoPanic(t, func() {
panicWithArgs(0, 0)
})
})
}
func TestCatchesPanicString(t *testing.T) {
// Does panic
// Test with string panic
assert.PanicsContains(
t,
func() {
panicWithArgs("This is for testing purposes", 0)
},
"testing purposes",
)
// Test with error panic
assert.PanicsContains(
t,
func() {
panicWithArgs(fmt.Errorf("this is for testing purposes"), 0)
},
"testing purposes",
)
// Panic value fails
// Test with string
testFailure(t, func(t *testing.T) {
assert.PanicsContains(
t,
func() {
panicWithArgs("This is for testing purposes", 0)
},
"wrong substr",
)
})
// Test with error
testFailure(t, func(t *testing.T) {
assert.PanicsContains(
t,
func() {
panicWithArgs(fmt.Errorf("this is for testing purposes"), 0)
},
"wrong substr",
)
})
// Test incompatible panic. PanicsContains expects a string or error type.
testFailure(t, func(t *testing.T) {
assert.PanicsContains(
t,
func() {
panicWithArgs(0, 0)
},
"abc",
)
})
// Does not panic
testFailure(t, func(t *testing.T) {
assert.PanicsContains(
t,
func() {},
"",
)
})
}
func TestCatchesPanicWithValidation(t *testing.T) {
// Does panic
// Test with int panic
assert.PanicsWithValidation(
t,
func() {
panicWithArgs(1, 0)
},
func(t *testing.T, panicValue any) {
assert.Equals(t, panicValue, 1)
},
)
// Test with string panic
assert.PanicsWithValidation(
t,
func() {
panicWithArgs("abc", 0)
},
func(t *testing.T, panicValue any) {
assert.Equals(t, panicValue, "abc")
},
)
// Panic's value validation fails
// Test with string
testFailure(t, func(t *testing.T) {
assert.PanicsWithValidation(
t,
func() {
panicWithArgs("This is for testing purposes", 0)
},
func(t *testing.T, panicValue any) {
assert.Equals(t, panicValue, "abc")
},
)
})
// Does not panic
testFailure(t, func(t *testing.T) {
assert.PanicsWithValidation(
t,
func() {},
func(t *testing.T, a any) {},
)
})
}