-
Notifications
You must be signed in to change notification settings - Fork 185
/
writer.go
62 lines (52 loc) · 1.12 KB
/
writer.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
package testutil
import (
"github.com/gookit/goutil/errorx"
)
// TestWriter struct, useful for testing
type TestWriter struct {
Buffer
// ErrOnWrite return error on write, useful for testing
ErrOnWrite bool
// ErrOnFlush return error on flush, useful for testing
ErrOnFlush bool
// ErrOnClose return error on close, useful for testing
ErrOnClose bool
}
// NewTestWriter instance
func NewTestWriter() *TestWriter {
return &TestWriter{}
}
// SetErrOnWrite method
func (w *TestWriter) SetErrOnWrite() {
w.ErrOnWrite = true
}
// SetErrOnFlush method
func (w *TestWriter) SetErrOnFlush() {
w.ErrOnFlush = true
}
// SetErrOnClose method
func (w *TestWriter) SetErrOnClose() {
w.ErrOnClose = true
}
// Flush implements
func (w *TestWriter) Flush() error {
if w.ErrOnFlush {
return errorx.Raw("flush error")
}
w.Reset()
return nil
}
// Close implements
func (w *TestWriter) Close() error {
if w.ErrOnClose {
return errorx.Raw("close error")
}
return nil
}
// Write implements
func (w *TestWriter) Write(p []byte) (n int, err error) {
if w.ErrOnWrite {
return 0, errorx.Raw("write error")
}
return w.Buffer.Write(p)
}