-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththrower_test.go
90 lines (78 loc) · 1.65 KB
/
thrower_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
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
package thrower
import (
"errors"
"testing"
)
var (
errMine = errors.New("my error")
errTheirs = errors.New("not my error")
)
func helper(t *testing.T, e error, notError error) (err error) {
t.Helper()
defer RecoverError(&err)
ThrowIfError(e)
return notError
}
func TestThrow(t *testing.T) {
err := helper(t, errMine, errTheirs)
if err != errMine {
t.Error("Didn't catch my error", err)
}
}
func TestThrowIfNil(t *testing.T) {
errMine := errors.New("my error")
err := helper(t, nil, errMine)
if err != errMine {
t.Error("Was not supposed to throw an error")
}
}
func TestThrowIfError(t *testing.T) {
err := helper(t, errMine, errTheirs)
if err != errMine {
t.Error("Didn't catch my error", err)
}
}
func TestDisabled(t *testing.T) {
DisableCatching()
_ = func() (err error) {
defer func() {
if r := recover(); r != nil {
return
}
t.Error("panic should not have been caught")
}()
defer RecoverError(&err)
Throw(errMine) // should cause a panic that doesn't get caught.
return nil
}()
ReEnableCatching()
_ = func() (err error) {
defer func() {
if r := recover(); r != nil {
t.Error("panic should have been caught")
}
}()
defer RecoverError(&err)
Throw(errMine) // should cause a panic that gets caught.
return nil
}()
}
func TestOtherPanic(t *testing.T) {
_ = func() (err error) {
defer func() {
if r := recover(); r != nil {
return
}
t.Error("panic should not have been caught")
}()
defer RecoverError(&err)
panic(errors.New("something"))
}()
}
func TestString(t *testing.T) {
th := newThrown(errMine)
s := th.Error()
if s != "my error" {
t.Error("wrong string", s)
}
}