forked from rotisserie/eris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheris_test.go
238 lines (222 loc) · 6.83 KB
/
eris_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
package eris_test
import (
"errors"
"fmt"
"testing"
"github.com/rotisserie/eris"
)
func setupTestCase(wrapf bool, cause error, input []string) error {
err := cause
for _, str := range input {
if wrapf {
err = eris.Wrapf(err, "%v", str)
} else {
err = eris.Wrap(err, str)
}
}
return err
}
func TestErrorWrapping(t *testing.T) {
tests := map[string]struct {
cause error // root error
input []string // input for error wrapping
output string // expected output
}{
"nil root error": {
cause: nil,
input: []string{"additional context"},
},
"standard error wrapping with internal root cause (eris.New)": {
cause: eris.New("root error"),
input: []string{"additional context", "even more context"},
output: "even more context: additional context: root error",
},
"standard error wrapping with external root cause (errors.New)": {
cause: errors.New("external error"),
input: []string{"additional context", "even more context"},
output: "even more context: additional context: external error",
},
"no error wrapping with internal root cause (eris.Errorf)": {
cause: eris.Errorf("%v", "root error"),
output: "root error",
},
"no error wrapping with external root cause (errors.New)": {
cause: errors.New("external error"),
output: "external error",
},
}
for desc, tc := range tests {
err := setupTestCase(false, tc.cause, tc.input)
if err != nil && tc.cause == nil {
t.Errorf("%v: wrapping nil errors should return nil but got { %v }", desc, err)
} else if err != nil && tc.output != err.Error() {
t.Errorf("%v: expected { %v } got { %v }", desc, tc.output, err)
}
}
}
func TestErrorUnwrap(t *testing.T) {
tests := map[string]struct {
cause error // root error
input []string // input for error wrapping
output []string // expected output
}{
"unwrapping error with internal root cause (eris.New)": {
cause: eris.New("root error"),
input: []string{"additional context", "even more context"},
output: []string{
"even more context: additional context: root error",
"additional context: root error",
"root error",
},
},
"unwrapping error with external root cause (errors.New)": {
cause: errors.New("external error"),
input: []string{"additional context", "even more context"},
output: []string{
"even more context: additional context: external error",
"additional context: external error",
"external error",
},
},
}
for desc, tc := range tests {
err := setupTestCase(true, tc.cause, tc.input)
for _, out := range tc.output {
if err == nil {
t.Errorf("%v: unwrapping error returned nil but expected { %v }", desc, out)
} else if out != err.Error() {
t.Errorf("%v: expected { %v } got { %v }", desc, out, err)
}
err = eris.Unwrap(err)
}
}
}
func TestErrorIs(t *testing.T) {
globalErr := eris.New("global error")
tests := map[string]struct {
cause error // root error
input []string // input for error wrapping
compare error // errors for comparison
output bool // expected comparison result
}{
"root error (internal)": {
cause: eris.New("root error"),
input: []string{"additional context", "even more context"},
compare: eris.New("root error"),
output: true,
},
"error not in chain": {
cause: eris.New("root error"),
compare: eris.New("other error"),
output: false,
},
"middle of chain (internal)": {
cause: eris.New("root error"),
input: []string{"additional context", "even more context"},
compare: eris.New("additional context"),
output: true,
},
"another in middle of chain (internal)": {
cause: eris.New("root error"),
input: []string{"additional context", "even more context"},
compare: eris.New("even more context"),
output: true,
},
"root error (external)": {
cause: errors.New("external error"),
input: []string{"additional context", "even more context"},
compare: eris.New("external error"),
output: true,
},
"wrapped error from global root error": {
cause: globalErr,
input: []string{"additional context", "even more context"},
compare: eris.Wrap(globalErr, "additional context"),
output: true,
},
"comparing against external error": {
cause: errors.New("external error"),
input: []string{"additional context", "even more context"},
compare: errors.New("external error"),
output: true,
},
"comparing against nil error": {
cause: eris.New("root error"),
compare: nil,
output: false,
},
"comparing error against itself": {
cause: globalErr,
compare: globalErr,
output: true,
},
"comparing two nil errors": {
cause: nil,
compare: nil,
output: true,
},
}
for desc, tc := range tests {
err := setupTestCase(false, tc.cause, tc.input)
if tc.output && !eris.Is(err, tc.compare) {
t.Errorf("%v: expected eris.Is('%v', '%v') to return true but got false", desc, err, tc.compare)
} else if !tc.output && eris.Is(err, tc.compare) {
t.Errorf("%v: expected eris.Is('%v', '%v') to return false but got true", desc, err, tc.compare)
}
}
}
func TestErrorCause(t *testing.T) {
globalErr := eris.New("global error")
tests := map[string]struct {
cause error // root error
input []string // input for error wrapping
output error // expected output
}{
"internal root error": {
cause: globalErr,
input: []string{"additional context", "even more context"},
output: globalErr,
},
"nil error": {
cause: nil,
output: nil,
},
}
for desc, tc := range tests {
err := setupTestCase(false, tc.cause, tc.input)
cause := eris.Cause(err)
if tc.output != eris.Cause(err) {
t.Errorf("%v: expected { %v } got { %v }", desc, tc.output, cause)
}
}
}
func TestErrorFormatting(t *testing.T) {
tests := map[string]struct {
cause error // root error
input []string // input for error wrapping
output string // expected output
}{
"standard error wrapping with internal root cause (eris.New)": {
cause: eris.New("root error"),
input: []string{"additional context", "even more context"},
output: "even more context: additional context: root error",
},
"standard error wrapping with external root cause (errors.New)": {
cause: errors.New("external error"),
input: []string{"additional context", "even more context"},
output: "even more context: additional context: external error",
},
}
for desc, tc := range tests {
err := setupTestCase(false, tc.cause, tc.input)
if err != nil && tc.cause == nil {
t.Errorf("%v: wrapping nil errors should return nil but got { %v }", desc, err)
} else if err != nil && tc.output != err.Error() {
t.Errorf("%v: expected { %v } got { %v }", desc, tc.output, err)
}
// todo: automate stack trace verification
_ = fmt.Sprintf("error formatting results (%v):\n", desc)
_ = fmt.Sprintf("%v\n", err)
_ = fmt.Sprintf("%+v", err)
}
}