-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors_test.go
303 lines (287 loc) · 6.52 KB
/
errors_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
// The tests here only cover the aspects of errors that are not covered
// by tests elsewhere.
package fuzzdump_test
import (
"errors"
"fmt"
"testing"
. "github.com/antichris/go-fuzzdump"
"github.com/stretchr/testify/require"
)
func ExampleCorpusErrors_Capture() {
malformed := func(name string) error {
return fmt.Errorf("parsing %q: %w", name, ErrMalformedEntry)
}
badVersion := func(name string) error {
return fmt.Errorf("parsing %q: %w", name, ErrUnsupportedVersion)
}
fn := func() error {
var errs CorpusErrors
// Perform operations that return errors.
err := malformed("foo")
if e := errs.Capture(err); e != nil {
return e
}
err = badVersion("bar")
if e := errs.Capture(err); e != nil {
return e
}
// Execution will continue, as long as the captured error is not
// a critical one.
fmt.Println("hello world")
// Rinse and repeat, as needed.
return errs.AsError()
}
fmt.Println(fn())
// Output:
// hello world
// fuzz corpus has errors:
// parsing "foo": must include version and at least one value
// parsing "bar": unsupported encoding version
}
func TestCorpusErrors_Error(t *testing.T) {
tests := map[string]struct {
err CorpusErrors
want string
}{"nil": {
err: nil,
want: "no fuzz corpus errors",
}, "snap": {
err: CorpusErrors{errSnap},
want: "fuzz corpus has errors:\n\tsnap",
}, "several": {
err: CorpusErrors{errSnap, errWhoops},
want: "fuzz corpus has errors:\n\t" + snap + "\n\t" + whoops,
}}
for n, tt := range tests {
t.Run(n, func(t *testing.T) {
require.EqualError(t, tt.err, tt.want)
})
}
}
func TestCorpusErrors_Is(t *testing.T) {
type key string
// Plain errors (and nil).
const (
nilKey key = "<nil>"
snap key = "snap"
whoops key = "whoops"
)
// CorpusErrors collections.
const (
errsNil key = "CorpusErrors(<nil>)"
errsEmpty key = "CorpusErrors{}"
errsSnap key = "CorpusErrors{snap}"
errsSnapWhoops key = "CorpusErrors{snap,whoops}"
errsWhoops key = "CorpusErrors{whoops}"
errsWrapSnap key = "CorpusErrors{&wrapError{snap}}"
)
// Errors wrapped by [fmt.Errorf].
const (
wrapSnap key = "&wrapError{snap}"
wrapErrsSnap key = "&wrapError{CorpusErrors{snap}}"
wrapErrsSnapWhoops key = "&wrapError{CorpusErrors{snap,whoops}}"
)
wrap := func(err error) error { return fmt.Errorf("wrapped: %w", err) }
var (
// CorpusErrors collections.
es = map[key]CorpusErrors{
errsNil: nil,
errsEmpty: {},
errsSnap: {errSnap},
errsSnapWhoops: {errSnap, errWhoops},
errsWhoops: {errWhoops},
errsWrapSnap: {wrap(errSnap)},
}
// Errors wrapped by [fmt.Errorf].
ws = map[key]error{
wrapSnap: wrap(errSnap),
wrapErrsSnap: wrap(CorpusErrors{errSnap}),
wrapErrsSnapWhoops: wrap(CorpusErrors{errSnap, errWhoops}),
}
// Target errors.
// Encompass plain errors, CorpusErrors collections and Errors
// wrapped by [fmt.Errorf].
ts = map[key]error{
nilKey: nil,
snap: errSnap,
whoops: errWhoops,
}
)
for k, v := range es {
ts[k] = v
}
for k, v := range ws {
ts[k] = v
}
// When a value for a key is not specified, it falls back to false.
type wantMap map[key]bool
wantEmpty := wantMap{
errsNil: true,
errsEmpty: true,
}
tests := map[key]wantMap{
errsNil: wantEmpty,
errsEmpty: wantEmpty,
errsSnap: {
snap: true,
errsSnap: true,
},
errsWrapSnap: {
snap: true,
errsSnap: true,
errsWrapSnap: true,
},
errsSnapWhoops: {
snap: true,
whoops: true,
errsSnap: true,
errsSnapWhoops: true,
errsWhoops: true,
},
wrapErrsSnap: {
snap: true,
errsSnap: true,
wrapErrsSnap: true,
},
wrapErrsSnapWhoops: {
snap: true,
whoops: true,
errsSnap: true,
errsSnapWhoops: true,
errsWhoops: true,
wrapErrsSnapWhoops: true,
},
}
for ek, tt := range tests {
t.Run(fmt.Sprintf("errs=%s", ek), func(t *testing.T) {
errs := ts[ek]
for tk, target := range ts {
t.Run(fmt.Sprintf("target=%s", tk), func(t *testing.T) {
want := tt[tk]
got := errors.Is(errs, target)
require.Equal(t, want, got)
})
}
})
}
}
func TestCorpusErrors_Unwrap(t *testing.T) {
tests := map[string]struct {
err CorpusErrors
want error
}{
"nil": {},
"snap": {CorpusErrors{errSnap}, nil},
"several": {CorpusErrors{errSnap, errWhoops}, CorpusErrors{errSnap}},
}
for n, tt := range tests {
t.Run(n, func(t *testing.T) {
got := tt.err.Unwrap()
require.Equal(t, tt.want, got)
})
}
}
func TestCorpusErrors_Capture(t *testing.T) {
var (
malf = ErrMalformedEntry
ver = ErrUnsupportedVersion
empt = ErrEmptyCorpus
args = ErrInconsistentArgCount
)
type CE = CorpusErrors
tests := map[string]struct {
err error
want error
wantE CE
}{"nil": {
err: nil,
want: nil,
wantE: nil,
}, "CorpusErrors{errSnap}": {
err: CE{errSnap},
want: errSnap,
wantE: nil,
}, "CorpusErrors{ErrMalformedEntry}": {
err: CE{malf},
want: nil,
wantE: CE{malf},
}, "CorpusErrors{ErrUnsupportedVersion,ErrEmptyCorpus}": {
err: CE{ver, empt},
want: empt,
wantE: CE{ver, empt},
}, "ErrInconsistentArgCount": {
err: args,
want: nil,
wantE: CE{args},
}, "snap": {
err: errSnap,
want: errSnap,
}}
for n, tt := range tests {
t.Run(n, func(t *testing.T) {
var e CE
got := e.Capture(tt.err)
req := require.New(t)
req.ErrorIs(got, tt.want)
req.Equal(tt.wantE, e)
})
}
t.Run("ErrEmptyCorpus yields exactly e", func(t *testing.T) {
e := CE{ver}
got := e.Capture(empt)
require.Equal(t, got, e)
})
}
func Test_readErr(t *testing.T) {
tests := map[string]struct {
err error
name string
want string
}{"nil": {
err: nil,
}, "snap": {
err: errSnap,
name: "foo",
want: `reading "foo": snap`,
}}
for n, tt := range tests {
t.Run(n, func(t *testing.T) {
got := XreadErr(tt.err, tt.name)
if tt.want != "" {
require.EqualError(t, got, tt.want)
} else {
require.NoError(t, got)
}
})
}
}
func Test_writeErr(t *testing.T) {
tests := map[string]struct {
err error
want string
}{"nil": {
err: nil,
}, "snap": {
err: errSnap,
want: `writing output: snap`,
}}
for n, tt := range tests {
t.Run(n, func(t *testing.T) {
got := XwriteErr(tt.err)
if tt.want != "" {
require.EqualError(t, got, tt.want)
} else {
require.NoError(t, got)
}
})
}
}
var (
errSnap = errors.New(snap)
errWhoops = errors.New(whoops)
)
const (
snap = "snap"
whoops = "whoops"
)