1 // Copyright 2010 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
15 var testOutput = []byte("0123456789abcdefghijklmnopqrstuvwxy")
16 var testInput = []byte("012\n345\n678\n9ab\ncde\nfgh\nijk\nlmn\nopq\nrst\nuvw\nxy")
17 var testInputrn = []byte("012\r\n345\r\n678\r\n9ab\r\ncde\r\nfgh\r\nijk\r\nlmn\r\nopq\r\nrst\r\nuvw\r\nxy\r\n\n\r\n")
19 // TestReader wraps a []byte and returns reads of a specific length.
20 type testReader struct {
25 func (t *testReader) Read(buf []byte) (n int, err os.Error) {
41 func testLineReader(t *testing.T, input []byte) {
42 for stride := 1; stride < len(input); stride++ {
44 reader := testReader{input, stride}
45 l := NewReader(&reader, len(input)+1)
47 line, isPrefix, err := l.ReadLine()
48 if len(line) > 0 && err != nil {
49 t.Errorf("ReadLine returned both data and error: %s", err)
52 t.Errorf("ReadLine returned prefix")
56 t.Fatalf("Got unknown error: %s", err)
60 if want := testOutput[done : done+len(line)]; !bytes.Equal(want, line) {
61 t.Errorf("Bad line at stride %d: want: %x got: %x", stride, want, line)
65 if done != len(testOutput) {
66 t.Error("ReadLine didn't return everything")
71 func TestReader(t *testing.T) {
72 testLineReader(t, testInput)
73 testLineReader(t, testInputrn)
76 func TestLineTooLong(t *testing.T) {
77 buf := bytes.NewBuffer([]byte("aaabbbcc\n"))
78 l := NewReader(buf, 3)
79 line, isPrefix, err := l.ReadLine()
80 if !isPrefix || !bytes.Equal(line, []byte("aaa")) || err != nil {
81 t.Errorf("bad result for first line: %x %s", line, err)
83 line, isPrefix, err = l.ReadLine()
84 if !isPrefix || !bytes.Equal(line, []byte("bbb")) || err != nil {
85 t.Errorf("bad result for second line: %x", line)
87 line, isPrefix, err = l.ReadLine()
88 if isPrefix || !bytes.Equal(line, []byte("cc")) || err != nil {
89 t.Errorf("bad result for third line: %x", line)
93 func TestReadAfterLines(t *testing.T) {
95 restData := "line2\nline 3\n"
96 inbuf := bytes.NewBuffer([]byte(line1 + "\n" + restData))
97 outbuf := new(bytes.Buffer)
98 maxLineLength := len(line1) + len(restData)/2
99 l := NewReader(inbuf, maxLineLength)
100 line, isPrefix, err := l.ReadLine()
101 if isPrefix || err != nil || string(line) != line1 {
102 t.Errorf("bad result for first line: isPrefix=%v err=%v line=%q", isPrefix, err, string(line))
104 n, err := io.Copy(outbuf, l)
105 if int(n) != len(restData) || err != nil {
106 t.Errorf("bad result for Read: n=%d err=%v", n, err)
108 if outbuf.String() != restData {
109 t.Errorf("bad result for Read: got %q; expected %q", outbuf.String(), restData)
113 func TestReadEmptyBuffer(t *testing.T) {
114 l := NewReader(bytes.NewBuffer(nil), 10)
115 line, isPrefix, err := l.ReadLine()
117 t.Errorf("expected EOF from ReadLine, got '%s' %t %s", line, isPrefix, err)
121 func TestLinesAfterRead(t *testing.T) {
122 l := NewReader(bytes.NewBuffer([]byte("foo")), 10)
123 _, err := ioutil.ReadAll(l)
129 line, isPrefix, err := l.ReadLine()
131 t.Errorf("expected EOF from ReadLine, got '%s' %t %s", line, isPrefix, err)