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
/
hashable_test.go
117 lines (108 loc) · 2.75 KB
/
hashable_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
package maperr_test
import (
"errors"
"testing"
"github.com/iZettle/maperr/v4"
"github.com/stretchr/testify/assert"
"go.uber.org/multierr"
)
func TestMap_Mapped(t *testing.T) {
layerOneFailed := errors.New("layer 1 failed")
layerTwoFailed := errors.New("layer 2 failed")
layerThreeFailed := errors.New("layer 3 failed")
type iteration struct {
mapErr maperr.HashableMapper
err error
}
tests := []struct {
name string
iterations []iteration
expectedErr string
defaultErr error
}{
{
name: "error is not mapped",
iterations: []iteration{
{
mapErr: maperr.NewHashableMapper(),
err: errors.New("random error"),
},
},
expectedErr: "random error",
},
{
name: "error is not mapped",
iterations: []iteration{
{
mapErr: maperr.NewHashableMapper(),
err: errors.New("random error"),
},
},
expectedErr: "random error; some default description for generic error",
defaultErr: errors.New("some default description for generic error"),
},
{
name: "error going through one layers",
iterations: []iteration{
{
mapErr: maperr.NewHashableMapper().
Append(layerOneFailed, layerTwoFailed),
err: layerOneFailed,
},
},
expectedErr: "layer 1 failed; layer 2 failed",
},
{
name: "error going through three layers",
iterations: []iteration{
{
mapErr: maperr.NewHashableMapper().
Append(layerOneFailed, layerTwoFailed),
err: layerOneFailed,
},
{
mapErr: maperr.NewHashableMapper().
Append(layerTwoFailed, layerThreeFailed),
err: layerTwoFailed,
},
},
expectedErr: "layer 1 failed; layer 2 failed; layer 3 failed",
},
}
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_Mapped_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.
NewHashableMapper().
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)
}
}