-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader_test.go
166 lines (146 loc) · 3.55 KB
/
reader_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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright 2017 The Mellium Contributors.
// Use of this source code is governed by the BSD 2-clause
// license that can be found in the LICENSE file.
package reader_test
import (
"errors"
"io"
"io/ioutil"
"net"
"strconv"
"strings"
"testing"
"time"
"mellium.im/reader"
)
func TestError(t *testing.T) {
err := errors.New("Oops")
er := reader.Error(err)
if _, e := er.Read(nil); e != err {
t.Fatalf("Expected original error but got %v", e)
}
}
func TestBefore(t *testing.T) {
n := 0
r := reader.Before(strings.NewReader("Send these, the homeless, tempest-tost to me"), func() error {
n++
return nil
})
for i := 0; i < 3; i++ {
p := make([]byte, 4)
_, err := r.Read(p)
switch {
case err != nil:
t.Fatalf("Read returned unexpected error: %v", err)
case n > 1:
t.Fatal("Before func executed more than once!")
}
}
}
func TestBeforeError(t *testing.T) {
in := "I had a lover's quarrel with the world."
r := strings.NewReader(in)
errOops := errors.New("Oops")
before := reader.Before(r, func() error {
return errOops
})
if _, err := ioutil.ReadAll(before); err != errOops {
t.Fatalf("Unexpected error returned, want=%v, got=%v", errOops, err)
}
b, err := ioutil.ReadAll(r)
switch {
case err != nil:
t.Fatalf("Unexpected error returned from inner reader: %v", err)
case string(b) != in:
t.Fatalf("Failed to read expected value from inner reader, was some of it already read? want='%s', got='%s'", in, string(b))
}
}
var errOops = errors.New("oops")
type afterTestData struct {
p []byte
n int
err error
}
var afterTestCases = [...]struct {
r io.Reader
f func(int, error) (int, error)
data []afterTestData
}{
0: {
r: reader.Error(io.EOF),
data: []afterTestData{
{p: nil, n: 0, err: io.EOF},
{p: nil, n: 0, err: io.EOF},
},
},
1: {
r: reader.Error(io.EOF),
f: func(n int, err error) (int, error) {
return 2, errOops
},
data: []afterTestData{
{p: nil, n: 2, err: errOops},
{p: nil, n: 2, err: errOops},
},
},
2: {
r: strings.NewReader("test"),
data: []afterTestData{
{p: make([]byte, 4), n: 4, err: nil},
{p: nil, n: 0, err: io.EOF},
},
},
}
func TestAfter(t *testing.T) {
for i, tc := range afterTestCases {
t.Run(strconv.Itoa(i), func(t *testing.T) {
ar := reader.After(tc.r, tc.f)
for c, d := range tc.data {
n, err := ar.Read(d.p)
if n != d.n {
t.Errorf("Wrong number of bytes on read %d: want=%d, got=%d", c, d.n, n)
}
if err != d.err {
t.Errorf("Wrong error on read %d: want=%q, got=%q", c, d.err, err)
}
}
})
}
}
type panicConn struct{}
func (panicConn) Read(b []byte) (n int, err error) {
panic("reader: not implemented")
}
func (panicConn) Write(b []byte) (n int, err error) {
panic("reader: not implemented")
}
func (panicConn) Close() error {
panic("reader: not implemented")
}
func (panicConn) LocalAddr() net.Addr {
panic("reader: not implemented")
}
func (panicConn) RemoteAddr() net.Addr {
panic("reader: not implemented")
}
func (panicConn) SetDeadline(t time.Time) error {
panic("reader: not implemented")
}
func (panicConn) SetReadDeadline(t time.Time) error {
panic("reader: not implemented")
}
func (panicConn) SetWriteDeadline(t time.Time) error {
panic("reader: not implemented")
}
func TestConn(t *testing.T) {
c := reader.Conn(panicConn{}, reader.Func(func(_ []byte) (int, error) {
return 10, nil
}))
n, err := c.Read(nil)
switch {
case n != 10:
t.Errorf("Got wrong number from conn read, got=%d, want=10", n)
case err != nil:
t.Errorf("Did not expect error from Conn's new read method, got: %v", err)
}
}