aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/tdewolff/parse/buffer/reader_test.go
blob: 73600ec1883e13c3901d0ba6ff534510a6baae38 (plain)
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
package buffer // import "github.com/tdewolff/parse/buffer"

import (
	"bytes"
	"fmt"
	"io"
	"testing"

	"github.com/tdewolff/test"
)

func TestReader(t *testing.T) {
	s := []byte("abcde")
	r := NewReader(s)
	test.Bytes(t, r.Bytes(), s, "reader must return bytes stored")

	buf := make([]byte, 3)
	n, err := r.Read(buf)
	test.T(t, err, nil, "error")
	test.That(t, n == 3, "first read must read 3 characters")
	test.Bytes(t, buf, []byte("abc"), "first read must match 'abc'")

	n, err = r.Read(buf)
	test.T(t, err, nil, "error")
	test.That(t, n == 2, "second read must read 2 characters")
	test.Bytes(t, buf[:n], []byte("de"), "second read must match 'de'")

	n, err = r.Read(buf)
	test.T(t, err, io.EOF, "error")
	test.That(t, n == 0, "third read must read 0 characters")

	n, err = r.Read(nil)
	test.T(t, err, nil, "error")
	test.That(t, n == 0, "read to nil buffer must return 0 characters read")

	r.Reset()
	n, err = r.Read(buf)
	test.T(t, err, nil, "error")
	test.That(t, n == 3, "read after reset must read 3 characters")
	test.Bytes(t, buf, []byte("abc"), "read after reset must match 'abc'")
}

func ExampleNewReader() {
	r := NewReader([]byte("Lorem ipsum"))
	w := &bytes.Buffer{}
	io.Copy(w, r)
	fmt.Println(w.String())
	// Output: Lorem ipsum
}