aboutsummaryrefslogtreecommitdiffstats
path: root/utils/ioutil/common_test.go
blob: f5017f7faccdcd7e9e76bac614cbf13bb6790ca9 (plain) (blame)
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
package ioutil

import (
	"bytes"
	"io/ioutil"
	"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 := ioutil.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 := ioutil.ReadAll(r)
	c.Assert(err, IsNil)
	c.Assert(string(read), Equals, "1")

	c.Assert(r.Close(), IsNil)
	c.Assert(closer.called, Equals, 1)
}