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 167 168 169 170 171 172 173 174 175 176 177 178
|
package ioutil
import (
"bytes"
"context"
"io"
"strings"
"testing"
. "gopkg.in/check.v1"
)
func Test(t *testing.T) { TestingT(t) }
type CommonSuite struct{}
var _ = Suite(&CommonSuite{})
type closer struct {
called int
}
func (c *closer) Close() error {
c.called++
return nil
}
func (s *CommonSuite) TestNonEmptyReader_Empty(c *C) {
var buf bytes.Buffer
r, err := NonEmptyReader(&buf)
c.Assert(err, Equals, ErrEmptyReader)
c.Assert(r, IsNil)
}
func (s *CommonSuite) TestNonEmptyReader_NonEmpty(c *C) {
buf := bytes.NewBuffer([]byte("1"))
r, err := NonEmptyReader(buf)
c.Assert(err, IsNil)
c.Assert(r, NotNil)
read, err := io.ReadAll(r)
c.Assert(err, IsNil)
c.Assert(string(read), Equals, "1")
}
func (s *CommonSuite) TestNewReadCloser(c *C) {
buf := bytes.NewBuffer([]byte("1"))
closer := &closer{}
r := NewReadCloser(buf, closer)
read, err := io.ReadAll(r)
c.Assert(err, IsNil)
c.Assert(string(read), Equals, "1")
c.Assert(r.Close(), IsNil)
c.Assert(closer.called, Equals, 1)
}
func (s *CommonSuite) TestNewContextReader(c *C) {
buf := bytes.NewBuffer([]byte("12"))
ctx, close := context.WithCancel(context.Background())
r := NewContextReader(ctx, buf)
b := make([]byte, 1)
n, err := r.Read(b)
c.Assert(n, Equals, 1)
c.Assert(err, IsNil)
close()
n, err = r.Read(b)
c.Assert(n, Equals, 0)
c.Assert(err, NotNil)
}
func (s *CommonSuite) TestNewContextReadCloser(c *C) {
buf := NewReadCloser(bytes.NewBuffer([]byte("12")), &closer{})
ctx, close := context.WithCancel(context.Background())
r := NewContextReadCloser(ctx, buf)
b := make([]byte, 1)
n, err := r.Read(b)
c.Assert(n, Equals, 1)
c.Assert(err, IsNil)
close()
n, err = r.Read(b)
c.Assert(n, Equals, 0)
c.Assert(err, NotNil)
c.Assert(r.Close(), IsNil)
}
func (s *CommonSuite) TestNewContextWriter(c *C) {
buf := bytes.NewBuffer(nil)
ctx, close := context.WithCancel(context.Background())
r := NewContextWriter(ctx, buf)
n, err := r.Write([]byte("1"))
c.Assert(n, Equals, 1)
c.Assert(err, IsNil)
close()
n, err = r.Write([]byte("1"))
c.Assert(n, Equals, 0)
c.Assert(err, NotNil)
}
func (s *CommonSuite) TestNewContextWriteCloser(c *C) {
buf := NewWriteCloser(bytes.NewBuffer(nil), &closer{})
ctx, close := context.WithCancel(context.Background())
w := NewContextWriteCloser(ctx, buf)
n, err := w.Write([]byte("1"))
c.Assert(n, Equals, 1)
c.Assert(err, IsNil)
close()
n, err = w.Write([]byte("1"))
c.Assert(n, Equals, 0)
c.Assert(err, NotNil)
c.Assert(w.Close(), IsNil)
}
func (s *CommonSuite) TestNewWriteCloserOnError(c *C) {
buf := NewWriteCloser(bytes.NewBuffer(nil), &closer{})
ctx, close := context.WithCancel(context.Background())
var called error
w := NewWriteCloserOnError(NewContextWriteCloser(ctx, buf), func(err error) {
called = err
})
close()
w.Write(nil)
c.Assert(called, NotNil)
}
func (s *CommonSuite) TestNewReadCloserOnError(c *C) {
buf := NewReadCloser(bytes.NewBuffer(nil), &closer{})
ctx, close := context.WithCancel(context.Background())
var called error
w := NewReadCloserOnError(NewContextReadCloser(ctx, buf), func(err error) {
called = err
})
close()
w.Read(nil)
c.Assert(called, NotNil)
}
func ExampleCheckClose() {
// CheckClose is commonly used with named return values
f := func() (err error) {
// Get a io.ReadCloser
r := io.NopCloser(strings.NewReader("foo"))
// defer CheckClose call with an io.Closer and pointer to error
defer CheckClose(r, &err)
// ... work with r ...
// if err is not nil, CheckClose will assign any close errors to it
return err
}
err := f()
if err != nil {
panic(err)
}
}
|