-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexit_test.go
106 lines (88 loc) · 1.9 KB
/
exit_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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package exit
import (
"errors"
"io"
"reflect"
"strconv"
"testing"
)
func TestWith(t *testing.T) {
cases := []struct {
err error
exp int
}{
0: {nil, 0},
1: {io.EOF, 1},
2: {errors.New("error"), 1},
3: {Wrap(2, io.EOF), 2},
4: {Wrapf(4, "failed doing something: %w", io.EOF), 4},
}
for caseIndex := range cases {
kase := cases[caseIndex]
t.Run(strconv.Itoa(caseIndex), func(t *testing.T) {
var got int
defer capture(&got)()
With(kase.err)
if kase.exp != got {
t.Errorf("\nexp: %q\ngot: %q", kase.exp, got)
}
})
}
}
func capture(into *int) func() {
current := terminate
terminate = func(code int) {
*into = code
}
return func() { terminate = current }
}
func TestUnwrap(t *testing.T) {
exp := errors.New("some error")
p1 := reflect.ValueOf(exp).Pointer()
got := errors.Unwrap(Wrap(12, exp))
p2 := reflect.ValueOf(got).Pointer()
if p1 != p2 {
t.Fatalf("\nexp: %p %#v\ngot: %p %#v", exp, exp, got, got)
}
}
func TestIs(t *testing.T) {
cases := []struct {
err error
exp bool
}{
0: {nil, false},
1: {io.EOF, false},
2: {Wrap(2, io.EOF), true},
3: {Wrap(3, Wrap(1, io.ErrUnexpectedEOF)), true},
}
for caseIndex := range cases {
kase := cases[caseIndex]
t.Run(strconv.Itoa(caseIndex), func(t *testing.T) {
if got := Is(kase.err); kase.exp != got {
t.Errorf("\nexp: %t\ngot: %t", kase.exp, got)
}
})
}
}
func TestCarries(t *testing.T) {
const code = 5
cases := []struct {
err error
exp bool
}{
{nil, false},
{io.EOF, false},
{Wrap(code, io.EOF), true},
{Wrapf(code, "failed with: %w", io.EOF), true},
{Wrap(code-1, io.EOF), false},
{Wrapf(code+1, "failed with: %w", io.EOF), false},
}
for caseIndex := range cases {
kase := cases[caseIndex]
t.Run(strconv.Itoa(caseIndex), func(t *testing.T) {
if got := Carries(kase.err, code); kase.exp != got {
t.Errorf("\nexp: %t\ngot: %t", kase.exp, got)
}
})
}
}