-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeh_test.go
403 lines (350 loc) · 11.1 KB
/
meh_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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
package meh
import (
"encoding/json"
"errors"
"fmt"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"os"
"testing"
)
// ErrorErrorSuite tests Error.Error.
type ErrorErrorSuite struct {
suite.Suite
}
func (suite *ErrorErrorSuite) TestSingleWithoutWrapped() {
e := &Error{Message: "woof"}
suite.Equal("woof", e.Error(), "should return correct message")
}
func (suite *ErrorErrorSuite) TestWrappers() {
e := &Error{
Message: "outer",
WrappedErr: &Error{
Message: "inner",
WrappedErr: errors.New("original"),
},
}
suite.Equal("outer: inner: original", e.Error(), "should return correct message")
}
func TestError_Error(t *testing.T) {
suite.Run(t, new(ErrorErrorSuite))
}
// TestErrorCode tests ErrorCode.
func TestErrorCode(t *testing.T) {
e := &Error{
Code: ErrNeutral,
WrappedErr: &Error{
Code: ErrNeutral,
WrappedErr: &Error{
Code: ErrInternal,
WrappedErr: &Error{
Code: ErrBadInput,
WrappedErr: &Error{
Code: ErrNeutral,
WrappedErr: &Error{
Code: ErrNotFound,
},
},
},
},
},
}
assert.Equal(t, ErrInternal, ErrorCode(e), "should return correct code")
}
// WrapSuite tests Wrap.
type WrapSuite struct {
suite.Suite
}
func (suite *WrapSuite) TestEmptyMessage() {
d := Details{"Hello": "World!"}
originalErr := &Error{}
e := Wrap(originalErr, "", d).(*Error)
suite.Equal(ErrNeutral, e.Code, "should have set correct code")
suite.Empty(e.Message, "should have have set empty message")
suite.Equal(d, e.Details, "should have set details")
suite.Equal(originalErr, e.WrappedErr, "should have kept original error")
}
func (suite *WrapSuite) TestNilDetails() {
originalErr := &Error{Details: Details{"woof": "meow"}}
e := Wrap(originalErr, "baa", nil).(*Error)
suite.Equal(ErrNeutral, e.Code, "should have set correct code")
suite.Equal("baa", e.Message, "should have set correct message")
suite.Equal(originalErr, e.WrappedErr, "should have kept original error")
}
func TestWrap(t *testing.T) {
suite.Run(t, new(WrapSuite))
}
// ApplyDetailsSuite tests ApplyDetails.
type ApplyDetailsSuite struct {
suite.Suite
}
func (suite *ApplyDetailsSuite) TestOldDetailsNil() {
originalErr := &Error{}
e := ApplyDetails(originalErr, Details{"hello": "world"}).(*Error)
suite.Equal(originalErr, e.WrappedErr, "should have kept error")
suite.Require().NotEmpty(e.Details, "should have set details")
suite.Equal("world", e.Details["hello"], "should have set correct details")
}
func (suite *ApplyDetailsSuite) TestNewDetailsNil() {
originalErr := &Error{Details: Details{"hello": "world"}}
e := ApplyDetails(originalErr, nil).(*Error)
suite.Equal(originalErr, e.WrappedErr, "should have kept original error")
suite.Empty(e.Details, "should not have set details")
}
func TestApplyDetails(t *testing.T) {
suite.Run(t, new(ApplyDetailsSuite))
}
// CastSuite tests Cast.
type CastSuite struct {
suite.Suite
}
func (suite *CastSuite) TestOK() {
e := &Error{
Code: ErrInternal,
WrappedErr: errors.New("moo"),
Message: "Hello World!",
Details: Details{"oink": "cluck"},
}
suite.Equal(e, Cast(e), "should cast correctly")
}
func (suite *CastSuite) TestNoMehError() {
err := errors.New("chirp")
e := Cast(err)
suite.IsType(&Error{}, e, "should have created error with correct type")
suite.Equal(fromErr(err), e, "should have created a new meh error from given")
}
func TestCast(t *testing.T) {
suite.Run(t, new(CastSuite))
}
// fromErrSuite tests fromErr.
type fromErrSuite struct {
suite.Suite
}
func (suite *fromErrSuite) TestNilError() {
e := fromErr(nil)
suite.Equal(ErrUnexpected, e.Code, "should have set correct code")
suite.NotEmpty(e.Message, "should not have empty message")
}
func (suite *fromErrSuite) TestOK() {
originalErr := errors.New("chirp")
e := fromErr(originalErr)
suite.Equal(ErrUnexpected, e.Code, "should have set correct code")
suite.Equal(originalErr, e.WrappedErr, "should have wrapped original error")
suite.Empty(e.Message, "should not have applied any error message")
}
func Test_fromErr(t *testing.T) {
suite.Run(t, new(fromErrSuite))
}
// ToMapSuite tests ToMap.
type ToMapSuite struct {
suite.Suite
}
func (suite *ToMapSuite) TestOK() {
d := Details{"roar": "meow"}
e := &Error{
Code: ErrInternal,
WrappedErr: errors.New("woof"),
Message: "chirp",
Details: d,
}
ed := ToMap(e)
suite.Equal(ErrInternal, ed[MapFieldErrorCode], "should have set correct code")
suite.Equal(e.Error(), ed[MapFieldErrorMessage], "should have set correct error message")
suite.Equal("meow", ed["0/roar"], "should have kept old details")
// Assure copied and not set in place.
_, ok := d[MapFieldErrorCode]
suite.False(ok, "should not have touched original details")
}
func (suite *ToMapSuite) TestNoMehError() {
err := errors.New("woof")
ed := ToMap(err)
suite.Equal(ErrUnexpected, ed[MapFieldErrorCode], "should set error code correctly")
suite.Equal(err.Error(), ed[MapFieldErrorMessage], "should set error message correctly")
}
func (suite *ToMapSuite) TestDuplicateDetailKeys() {
e := &Error{
Code: ErrInternal,
Details: Details{
"detail_0": "meow",
"detail_to_mask_1": "woof",
},
WrappedErr: &Error{
Code: ErrNeutral,
Details: Details{
"detail_to_mask_2": "chirp",
"detail_to_mask_1": "ola",
"detail_3": "wow",
"x_masked": []string{"a", "b", "c"},
},
WrappedErr: &Error{
Code: ErrNotFound,
Details: Details{
"detail_to_mask_2": "oh no",
"detail_to_mask_1": "cluck",
},
},
},
}
suite.Equal(map[string]interface{}{
"0/detail_0": "meow",
"0/detail_to_mask_1": "woof",
"1/detail_to_mask_2": "chirp",
"1/detail_to_mask_1": "ola",
"1/detail_3": "wow",
"1/x_masked": []string{"a", "b", "c"},
"2/detail_to_mask_2": "oh no",
"2/detail_to_mask_1": "cluck",
MapFieldErrorMessage: "",
MapFieldErrorCode: ErrInternal,
}, ToMap(e), "should set correct details")
}
func TestToMap(t *testing.T) {
suite.Run(t, new(ToMapSuite))
}
// TestApplyCode tests ApplyCode.
func TestApplyCode(t *testing.T) {
originalErr := &Error{
Code: ErrNotFound,
Message: "sad life",
}
e := ApplyCode(originalErr, ErrInternal).(*Error)
assert.Equal(t, originalErr, e.WrappedErr, "should have kept original error")
assert.Equal(t, ErrInternal, ErrorCode(e), "should have applied correct error code")
}
// NilOrWrapSuite tests NilOrWrap.
type NilOrWrapSuite struct {
suite.Suite
}
func (suite *NilOrWrapSuite) TestNil() {
e := NilOrWrap(nil, "meow", nil)
suite.Nil(e, "should return nil")
}
func (suite *NilOrWrapSuite) TestNotNil() {
originalErr := NewInternalErr("sad life", Details{"meow": "woof"})
e := NilOrWrap(originalErr, "hello", Details{"hello": "world"})
suite.Equal(Wrap(originalErr, "hello", Details{"hello": "world"}), e, "should have been wrapped")
}
func TestNilOrWrap(t *testing.T) {
suite.Run(t, new(NilOrWrapSuite))
}
// FinalizeSuite tests Finalize.
type FinalizeSuite struct {
suite.Suite
}
func (suite *FinalizeSuite) TestNilError() {
finalized := Finalize(nil)
suite.Nil(finalized)
}
func (suite *FinalizeSuite) TestNativeError() {
e := errors.New("sad life")
finalized := Finalize(e)
suite.Equal(ErrUnexpected, ErrorCode(finalized), "should set correct error code")
suite.Contains(finalized.Error(), e.Error())
}
func (suite *FinalizeSuite) TestMehErrorNoPassThrough() {
e := NewInternalErr("what is love", Details{"explain": "guide"})
finalized := Finalize(e)
suite.Equal(e, finalized, "should not change error")
}
func (suite *FinalizeSuite) TestMehErrorWrappedNoPassThrough() {
original := errors.New("sad life")
e := NewBadInputErrFromErr(original, "do not say", nil)
finalized := Finalize(e)
suite.Equal(e, finalized, "should not change error")
}
func (suite *FinalizeSuite) TestMehErrorPassThrough() {
original := errors.New("sad life")
e := NewPassThroughErr(original, ErrForbidden, "forbidden", Details{"package": "than"})
finalized := Finalize(e)
suite.Equal(original, finalized, "should return pass-through error")
}
func (suite *FinalizeSuite) TestMehErrorWrappedPassThrough() {
original := errors.New("sad life")
e := NewPassThroughErr(original, ErrForbidden, "forbidden", Details{"package": "than"})
e = Wrap(e, "cucumber wrap", nil)
finalized := Finalize(e)
suite.Equal(original, finalized, "should return pass-through error")
}
func TestFinalize(t *testing.T) {
suite.Run(t, new(FinalizeSuite))
}
// clearPassThroughSuite tests ClearPassThrough.
type ClearPassThroughSuite struct {
suite.Suite
}
func (suite *ClearPassThroughSuite) TestNil() {
cleared := ClearPassThrough(nil)
suite.Nil(cleared)
}
func (suite *ClearPassThroughSuite) TestNativeErr() {
e := errors.New("sad life")
cleared := ClearPassThrough(e)
suite.Equal(e, cleared, "should return native error")
}
func (suite *ClearPassThroughSuite) TestSingleWithPassThrough() {
e := NewPassThroughErr(errors.New("sad life"), ErrInternal, "beans", Details{"explode": "request"})
cleared := ClearPassThrough(e)
e.(*Error).WrappedErrPassThrough = false
suite.Equal(e, cleared, "should return same error without pass-through")
suite.Equal(Finalize(cleared), cleared, "should return same error after finalize")
}
func (suite *ClearPassThroughSuite) TestSingleWithoutPassThrough() {
e := NewForbiddenErr("plan", Details{"shield": "request"})
cleared := ClearPassThrough(e)
suite.Equal(e, cleared, "should return same error without pass-through")
suite.Equal(Finalize(cleared), cleared, "should return same error after finalize")
}
func (suite *ClearPassThroughSuite) TestWrappedWithPassThroughs() {
e := NewPassThroughErr(errors.New("pt1"), ErrNotFound, "not found", nil)
e = Wrap(e, "damp", Details{"swell": "ever"})
e = Wrap(e, "sore", Details{"somebody": "universe"})
cleared := ClearPassThrough(e)
tmpE := e
for tmpE != nil {
if me, ok := tmpE.(*Error); ok {
me.WrappedErrPassThrough = false
tmpE = me.WrappedErr
} else {
break
}
}
suite.Equal(e, cleared, "should clear all pass-through without losing information")
suite.Equal(Finalize(cleared), cleared, "should return same error after finalize")
}
func TestClearPassThrough(t *testing.T) {
suite.Run(t, new(ClearPassThroughSuite))
}
type ErrorMarshallingSuite struct {
suite.Suite
}
func (suite *ErrorMarshallingSuite) TestOK() {
original := &Error{
Code: ErrNeutral,
WrappedErr: &Error{
Code: ErrNotFound,
WrappedErr: os.ErrExist,
Message: "read file",
Details: Details{
"filename": "hello.world",
},
},
Message: "get users",
Details: Details{
"since": "yesterday",
},
}
originalJSON, err := json.Marshal(original)
suite.Require().NoError(err, "marshal original should not fail")
parsed := &Error{}
err = json.Unmarshal(originalJSON, &parsed)
suite.Require().NoError(err, "unmarshal original should not fail")
fmt.Println(string(originalJSON))
fmt.Println(parsed)
assert.Equal(suite.T(), ErrNotFound, ErrorCode(parsed), "should keep correct error code")
assert.Equal(suite.T(), original.Message, parsed.Message, "should keep message")
assert.Equal(suite.T(), original.Details, parsed.Details, "should keep details")
assert.Equal(suite.T(), original.Error(), parsed.Error())
}
func TestErrorMarshalling(t *testing.T) {
suite.Run(t, new(ErrorMarshallingSuite))
}