-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpanic_example_test.go
More file actions
38 lines (33 loc) · 848 Bytes
/
panic_example_test.go
File metadata and controls
38 lines (33 loc) · 848 Bytes
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
package be_test
import "github.com/carlmjohnson/be"
func ExamplePanicked() {
// mock *testing.T for example purposes
t := &mockingT{}
divide := func(num, denom int) int {
return num / denom
}
// Test that division by zero panics
be.Nonzero(t, be.Panicked(func() {
divide(1, 0)
}))
// Because a panic fails a test by default,
// testing that an operation does not panic is less necessary,
// but may be helpful in a table test.
for _, testcase := range []struct {
num, denom, want int
shouldPanic bool
}{
{0, 1, 0, false},
{1, 1, 1, false},
{1, 0, 0xbadc0ffee, true},
{0, 0, 0xbadc0ffee, true},
} {
got := 0xbadc0ffee
panicVal := be.Panicked(func() {
got = divide(testcase.num, testcase.denom)
})
be.Equal(t, testcase.want, got)
be.Equal(t, testcase.shouldPanic, panicVal != nil)
}
// Output:
}