-
Notifications
You must be signed in to change notification settings - Fork 5
/
readwritecloser.go
48 lines (39 loc) · 918 Bytes
/
readwritecloser.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
package mock
import "errors"
var (
READ_FAIL_ERROR error = errors.New("copy failed on read")
WRITE_FAIL_ERROR error = errors.New("copy failed on write")
CLOSE_FAIL_ERROR error = errors.New("close file failed")
)
func NewReadWriteCloser(readErr, writeErr, closeErr error) *MockReadWriteCloser {
return &MockReadWriteCloser{
ReadErr: readErr,
WriteErr: writeErr,
CloseErr: closeErr,
}
}
type MockReadWriteCloser struct {
BytesRead []byte
BytesWritten []byte
ReadErr error
WriteErr error
CloseErr error
}
func (r *MockReadWriteCloser) Read(p []byte) (n int, err error) {
if err = r.ReadErr; err == nil {
r.BytesRead = p
n = len(p)
}
return
}
func (r *MockReadWriteCloser) Close() (err error) {
err = r.CloseErr
return
}
func (r *MockReadWriteCloser) Write(p []byte) (n int, err error) {
if err = r.WriteErr; err != nil {
r.BytesWritten = p
n = len(p)
}
return
}