-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsafely_test.go
65 lines (61 loc) · 959 Bytes
/
safely_test.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
package butcher
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestSafelyRun(t *testing.T) {
tests := []struct {
name string
f func() error
errMsg string
}{
{
"Normal",
func() error {
return nil
},
"",
},
{
"ReturnError",
func() error {
return fmt.Errorf("boom")
},
"boom",
},
{
"PanicError",
func() error {
panic(fmt.Errorf("panic boom"))
},
"panic boom",
},
{
"PanicSomething",
func() error {
panic("wtf")
},
"unexpected panic occurred: wtf",
},
{
"PanicSliceIndexOut",
func() error {
a := make([]int64, 5)
a[len(a)]++
return nil
},
"runtime error: index out of range [5] with length 5",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := safelyRun(tt.f)
if tt.errMsg == "" {
assert.NoError(t, err)
} else {
assert.EqualError(t, err, tt.errMsg)
}
})
}
}