forked from tdewolff/test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.go
52 lines (42 loc) · 1.04 KB
/
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
package test
import "errors"
// ErrPlain is the default error that is returned for functions in this package.
var ErrPlain = errors.New("error")
////////////////
// ErrorReader implements an io.Reader that will do N successive reads before it returns ErrPlain.
type ErrorReader struct {
n int
}
// NewErrorReader returns a new ErrorReader.
func NewErrorReader(n int) *ErrorReader {
return &ErrorReader{n}
}
// Read implements the io.Reader interface.
func (r *ErrorReader) Read(b []byte) (n int, err error) {
if len(b) == 0 {
return 0, nil
}
if r.n == 0 {
return 0, ErrPlain
}
r.n--
b[0] = '.'
return 1, nil
}
////////////////
// ErrorWriter implements an io.Writer that will do N successive writes before it returns ErrPlain.
type ErrorWriter struct {
n int
}
// NewErrorWriter returns a new ErrorWriter.
func NewErrorWriter(n int) *ErrorWriter {
return &ErrorWriter{n}
}
// Write implements the io.Writer interface.
func (w *ErrorWriter) Write(b []byte) (n int, err error) {
if w.n == 0 {
return 0, ErrPlain
}
w.n--
return len(b), nil
}