-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjust_test.go
295 lines (267 loc) · 6.99 KB
/
just_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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package just
import (
"errors"
"flag"
"fmt"
"math/rand"
"testing"
"github.com/stretchr/testify/assert"
)
func call(e error, f func(error) error) func(t *testing.T) {
return func(t *testing.T) {
if e != nil {
assert.EqualError(t, f(e), e.Error())
} else {
assert.Nil(t, f(e))
}
}
}
func pcall(e error, f func(error) error, requireCatable bool) func(t *testing.T) {
return func(t *testing.T) {
assert.Panics(t, func() {
assert.NoError(t, f(e))
})
func() {
defer func() {
x := recover()
if _, ok := x.(Catchable); requireCatable && !ok {
assert.Fail(t, fmt.Sprintf("<%v: %T> is not a catchable error", x, x))
}
}()
assert.NoError(t, f(e))
}()
}
}
func TestFormatError(t *testing.T) {
e := func() (err error) {
defer AnnotateAndReturn("some annotation")(&err)
Try(errors.New("oops"))
return
}()
for _, v := range []string{"%v", "%s", "%q"} {
assert.Equal(t, "some annotation: oops", fmt.Sprintf(v, e))
}
assert.Equal(t, "oops\nsome annotation", fmt.Sprintf("%+v", e))
}
func TestTryReturn(t *testing.T) {
t.Run("return nil", call(nil, func(_ error) (err error) {
defer Return(&err)
TryValues(func() error { return nil }())
return
}))
t.Run("return nil without catch", call(nil, func(e error) (err error) {
TryValues(func() error { return nil }())
return
}))
t.Run("return err", call(errors.New("an error"), func(e error) (err error) {
defer Return(&err)
TryValues(func() error { return e }())
t.Fail()
return
}))
t.Run("return err without catch", pcall(errors.New("an error"), func(e error) (err error) {
TryValues(func() error { return e }())
t.Fail()
return
}, true))
answer := func() (int, error) { return 42, nil }
t.Run("return val and nil", call(nil, func(_ error) (err error) {
defer Return(&err)
assert.Equal(t, 42, TryValues(answer()).Nth(0))
assert.Equal(t, 42, Try(answer()))
return
}))
t.Run("return val and nil without catch", call(nil, func(e error) (err error) {
assert.Equal(t, 42, TryValues(answer()).Nth(0))
assert.Equal(t, 42, Try(answer()))
return
}))
t.Run("return val and err", call(errors.New("an error"), func(e error) (err error) {
defer Return(&err)
Try(func() (int, error) { return 42, e }())
t.Fail()
return
}))
t.Run("return val and err without catch", pcall(errors.New("an error"), func(e error) (err error) {
TryValues(func() error { return e }())
t.Fail()
return
}, true))
}
func TestThrowReturn(t *testing.T) {
for _, a := range []interface{}{errors.New("an error"), "a string", 42, 3.14} {
t.Run(fmt.Sprintf("throw %v", a), call(fmt.Errorf("%v", a), func(e error) (err error) {
defer Return(&err)
Throw(a)
return
}))
t.Run(fmt.Sprintf("panic %v", a), pcall(nil, func(e error) (err error) {
defer Return(&err)
panic(a)
}, false))
}
assert.NotPanics(t, func() {
defer Return(nil)
Throwf("Life, the Universe and Everything: %d", 42)
})
assert.NotPanics(t, func() {
defer Return(nil)
panic(AsCatchable(errors.New("another error")))
})
}
func TestAnnotateAndReturn(t *testing.T) {
for _, a := range []interface{}{errors.New("an error"), "a string", 42, 3.14} {
t.Run(fmt.Sprintf("annotate %v", a), call(fmt.Errorf("oops: %v", a), func(e error) (err error) {
defer AnnotateAndReturn("oops")(&err)
Throw(a)
t.Fail()
return
}))
}
}
func TestCatch(t *testing.T) {
for _, a := range []interface{}{errors.New("an error"), "a string", 42, 3.14} {
t.Run(fmt.Sprintf("catch %v", a), call(nil, func(e error) (err error) {
defer Catch(func(c Catchable) {
assert.EqualError(t, c.Why(), fmt.Sprintf("%v", a))
})
Throw(a)
t.Fail()
return
}))
}
}
func TestCatchInnerError(t *testing.T) {
e := errors.New("oops")
defer Catch(func(c Catchable) {
assert.Equal(t, e, c.Why())
})
func() { func() { Throw(e) }() }()
t.Fail()
}
func TestTryTo(t *testing.T) {
t.Run("try to call func return nil with msg", call(nil, func(e error) (err error) {
defer Return(&err)
TryValuesWithMsg("call func return nil")(func() error { return nil }())
return
}))
t.Run("try to call func return err with msg", call(errors.New("call func return err: oops"), func(e error) (err error) {
defer Return(&err)
TryValuesWithMsg("call func return err")(func() error { return errors.New("oops") }())
return
}))
t.Run("try to call func return nil", call(nil, func(e error) (err error) {
defer Return(&err)
TryTo("call func return nil")(func() error { return nil }())
return
}))
t.Run("try to call func return err", call(errors.New("call func return err: oops"), func(e error) (err error) {
defer Return(&err)
TryTo("call func return err")(func() error { return errors.New("oops") }())
return
}))
}
func TestPanicInHandle(t *testing.T) {
var panicInHandle = func(c Catchable) error {
panic(c)
}
assert.NotPanics(t, func() {
defer HandleAndReturn(panicInHandle)(nil)
})
assert.Panics(t, func() {
defer HandleAndReturn(panicInHandle)(nil)
Throw("oops")
})
}
func TestAsCatchable(t *testing.T) {
e := errors.New("oops")
c1 := AsCatchable(e)
assert.Equal(t, c1, AsCatchable(c1))
assert.EqualError(t, c1.Why(), e.Error())
c2 := AsCatchable(c1, "some error")
assert.EqualError(t, c2.Why(), "some error: "+e.Error())
assert.Error(t, AsCatchable(nil).Why())
// Also test wrap
assert.Equal(t, nil, TraceFn(id).wrap(nil))
assert.Nil(t, TraceFn(func(_ error) error { return nil }).wrap(e))
}
func TestPack(t *testing.T) {
assert.Nil(t, Pack().Error())
assert.Nil(t, Pack(nil).Error())
assert.NotNil(t, Pack(errors.New("oops")).Error())
}
func TestNthValue(t *testing.T) {
xs := TryValues(1, 2, 3)
for i := -len(xs); i < len(xs); i++ {
assert.NotNil(t, xs.Nth(i))
}
assert.Nil(t, xs.Nth(len(xs)))
assert.Nil(t, xs.Nth(-len(xs)-1))
}
func TestExtractError(t *testing.T) {
for _, tt := range []struct {
xs []interface{}
hasErr bool
}{
{[]interface{}{}, false},
{[]interface{}{1}, false},
{[]interface{}{1, 2}, false},
{[]interface{}{1, 2, errors.New("oops")}, true},
{[]interface{}{errors.New("oops")}, true},
} {
if tt.hasErr {
assert.Error(t, ExtractError(tt.xs...))
} else {
assert.NoError(t, ExtractError(tt.xs...))
}
}
}
func TestSetTraceFn(t *testing.T) {
defer SetTraceFn(nil)
type traced struct {
error
info string
}
SetTraceFn(func(err error) error {
return traced{err, "attached info"}
})
e := errors.New("oops")
te := AsCatchable(e).Why().(traced)
assert.Equal(t, e, te.error)
assert.Equal(t, "attached info", te.info)
}
func BenchmarkJust(b *testing.B) {
f := func() (err error) {
defer Return(&err)
x := Try(benchFn()).(float64)
x += 1
return
}
for i := 0; i < b.N; i++ {
f()
}
}
func BenchmarkIfError(b *testing.B) {
f := func() error {
x, err := benchFn()
if err != nil {
return err
}
x += 1
return nil
}
for i := 0; i < b.N; i++ {
f()
}
}
var (
benchErrRate = flag.Float64("err-rate", .1, "bench error rate")
benchErr = errors.New("bench error")
)
func benchFn() (float64, error) {
x := rand.Float64()
if x < *benchErrRate {
return 0, benchErr
}
return x, nil
}