This repository has been archived by the owner on Oct 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
list_test.go
93 lines (82 loc) · 2.22 KB
/
list_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
package maperr_test
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
"go.uber.org/multierr"
"github.com/iZettle/maperr/v4"
)
func TestMap_MappedFormattedErrors(t *testing.T) {
errTextLayerOneFailed := "foo %d"
errLayerOneFailed := maperr.Errorf(errTextLayerOneFailed, 10)
errTextLayerTwoFailed := "bar %d"
errLayerTwoFailed := maperr.Errorf(errTextLayerTwoFailed, 20)
type iteration struct {
mapErr maperr.ListMapper
err error
}
tests := []struct {
name string
iterations []iteration
expectedErr string
defaultErr error
}{
{
name: "error going through three layers",
iterations: []iteration{
{
mapErr: maperr.NewListMapper().
Append(maperr.NewError("normal error"), errLayerOneFailed),
err: errors.New("normal error"),
},
{
mapErr: maperr.NewListMapper().
Appendf(errTextLayerOneFailed, errLayerTwoFailed),
err: errLayerOneFailed,
},
{
mapErr: maperr.NewListMapper().
Append(maperr.Errorf(errTextLayerTwoFailed), maperr.Errorf("abc")),
err: errLayerTwoFailed,
},
},
expectedErr: "normal error; foo 10; bar 20; abc",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
actualErr := test.iterations[0].err
for _, iteration := range test.iterations {
actualErr = maperr.NewMultiErr(iteration.mapErr).Mapped(actualErr, test.defaultErr)
}
if test.expectedErr == "" {
assert.NoError(t, actualErr)
} else {
assert.EqualError(t, actualErr, test.expectedErr)
}
})
}
}
func TestMap_ListMapper_FindAnyErrorInChain(t *testing.T) {
errSecond := errors.New("second error")
errChain := multierr.Combine(
errors.New("first error"),
errSecond,
errors.New("third error"),
errors.New("forth error"),
errors.New("fifth error"),
)
mappedErr := maperr.NewMultiErr(
maperr.
NewListMapper().
Append(errSecond, errors.New("this should be appended on mapErr()")),
).Mapped(errChain, nil)
if mappedErr == nil {
t.Fatal("expected err got nil")
}
expected := "first error; second error; third error; forth error; fifth error; this should be appended on mapErr()"
got := mappedErr.Error()
if got != expected {
t.Fatalf("expected %s got %s", expected, got)
}
}