forked from duke-git/lancet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trycatch_test.go
172 lines (125 loc) · 3.77 KB
/
trycatch_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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package xerror
import (
"context"
"errors"
"testing"
"github.com/duke-git/lancet/v2/internal"
)
func TestTryCatchSuccess(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestTryCatchSuccess")
counter := 0
calledCatch := false
calledFinally := false
tc := NewTryCatch(context.Background())
tc.Try(func(ctx context.Context) error {
counter++
return nil
}).Catch(func(ctx context.Context, err error) {
calledCatch = true
t.Errorf("Catch should not be called")
}).Finally(func(ctx context.Context) {
calledFinally = true
}).Do()
assert.Equal(1, counter)
assert.Equal(false, calledCatch)
assert.Equal(true, calledFinally)
}
func TestTryCatchError(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestTryCatchError")
var catchedError error
calledFinally := false
tc := NewTryCatch(context.Background())
tc.Try(func(ctx context.Context) error {
return errors.New("error")
}).Catch(func(ctx context.Context, err error) {
catchedError = errors.New("catched error")
}).Finally(func(ctx context.Context) {
calledFinally = true
}).Do()
assert.Equal("catched error", catchedError.Error())
assert.Equal(true, calledFinally)
}
func TestTryCatchPanic(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestTryCatchPanic")
var catchedError error
calledFinally := false
tc := NewTryCatch(context.Background())
tc.Try(func(ctx context.Context) error {
panic("panic info")
}).Catch(func(ctx context.Context, err error) {
catchedError = errors.New("catched error")
}).Finally(func(ctx context.Context) {
calledFinally = true
}).Do()
assert.Equal("catched error", catchedError.Error())
assert.Equal(true, calledFinally)
}
func TestTryCatchContextCancelled(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestTryCatchContextCancelled")
var catchedError error
calledFinally := false
ctx, cancel := context.WithCancel(context.Background())
cancel()
tc := NewTryCatch(ctx)
tc.Try(func(ctx context.Context) error {
return nil
}).Catch(func(ctx context.Context, err error) {
catchedError = errors.New("catched error")
}).Finally(func(ctx context.Context) {
calledFinally = true
}).Do()
assert.Equal("catched error", catchedError.Error())
assert.Equal(true, calledFinally)
}
func TestTryCatchContextTimeout(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestTryCatchContextTimeout")
var catchedError error
calledFinally := false
ctx, cancel := context.WithTimeout(context.Background(), 0)
defer cancel()
tc := NewTryCatch(ctx)
tc.Try(func(ctx context.Context) error {
return nil
}).Catch(func(ctx context.Context, err error) {
catchedError = errors.New("catched error")
}).Finally(func(ctx context.Context) {
calledFinally = true
}).Do()
assert.Equal("catched error", catchedError.Error())
assert.Equal(true, calledFinally)
}
func TestTryCatchContextError(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestTryCatchContextError")
var catchedError error
calledFinally := false
ctx, cancel := context.WithTimeout(context.Background(), 0)
defer cancel()
tc := NewTryCatch(ctx)
tc.Try(func(ctx context.Context) error {
return errors.New("error")
}).Catch(func(ctx context.Context, err error) {
catchedError = errors.New("catched error")
}).Finally(func(ctx context.Context) {
calledFinally = true
}).Do()
assert.Equal("catched error", catchedError.Error())
assert.Equal(true, calledFinally)
}
func TestTryCatchNoCatch(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestTryCatchNoCatch")
calledFinally := false
tc := NewTryCatch(context.Background())
tc.Try(func(ctx context.Context) error {
return errors.New("error")
}).Finally(func(ctx context.Context) {
calledFinally = true
}).Do()
assert.Equal(true, calledFinally)
}