-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsupport_test.go
90 lines (80 loc) · 2.08 KB
/
support_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
package fuzzdump_test
import (
"errors"
"io"
"testing"
"github.com/stretchr/testify/require"
)
func Test_predicateErrWriter_Write(t *testing.T) {
type bs = []byte
tests := map[string]struct {
io.Writer
pResult bool
err string
wErr string
}{"no error": {
Writer: io.Discard,
pResult: false,
wErr: "",
}, "own error": {
Writer: io.Discard,
pResult: true,
err: snap,
wErr: snap,
}, "passed error": {
Writer: TextErrWriter(snap),
pResult: false,
wErr: snap,
}}
for n, tt := range tests {
t.Run(n, func(t *testing.T) {
predicate := func(bs) bool { return tt.pResult }
w := PredicateTextErrWriter(tt.Writer, tt.err, predicate)
_, err := w.Write(bs{})
req := require.New(t)
if tt.wErr != "" {
req.EqualError(err, tt.wErr)
return
}
req.NoError(err)
})
}
}
// TextErrWriter returns errText error on all Write calls.
func TextErrWriter(errText string) io.Writer {
return ErrWriter(errors.New(errText))
}
// ErrWriter returns err on all Write calls.
func ErrWriter(err error) io.Writer {
return PredicateErrWriter(nil, err, func(b []byte) bool { return true })
}
// PredicateTextErrWriter returns errText error on those Write calls for
// which the predicate returns true.
// The predicate gets passed the b to be written, so that can be used in
// its decision process.
func PredicateTextErrWriter(
w io.Writer, errText string, predicate func([]byte) bool,
) io.Writer {
return PredicateErrWriter(w, errors.New(errText), predicate)
}
// PredicateErrWriter returns err on those Write calls for which the
// predicate returns true.
// The predicate gets passed the b to be written, so that can be used in
// its decision process.
func PredicateErrWriter(
w io.Writer, err error, predicate func([]byte) bool,
) io.Writer {
return &predicateErrWriter{w, err, predicate}
}
type predicateErrWriter struct {
io.Writer
error
predicate func([]byte) bool
}
// Write implements the [io.Writer] interface.
func (w *predicateErrWriter) Write(b []byte) (int, error) {
if w.predicate(b) {
return 0, w.error
}
return w.Writer.Write(b)
}