-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.go
51 lines (41 loc) · 790 Bytes
/
reader.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
package mock
import "io"
func NewReader() *reader {
return &reader{hasEOF: true}
}
func NewReadCloser() *readCloser {
return &readCloser{
reader: NewReader(),
closer: NewCloser(),
}
}
type ReadCase struct {
Data string
Err error
}
type reader struct {
cases []ReadCase
hasEOF bool // if reader should return io.EOF when there is nothing left to read
}
type readCloser struct {
*reader
*closer
}
func (r *reader) ReaderHasEOF(hasEOF bool) {
r.hasEOF = hasEOF
}
func (r *reader) AddReadCase(c ReadCase) {
r.cases = append(r.cases, c)
}
func (r *reader) Read(p []byte) (n int, err error) {
if len(r.cases) == 0 {
if r.hasEOF {
return 0, io.EOF
} else {
return 0, nil
}
}
c := r.cases[0]
r.cases = r.cases[1:]
return copy(p, []byte(c.Data)), c.Err
}