1
0
mirror of https://github.com/golang/go synced 2024-11-21 20:04:44 -07:00

encoding/line: fix line returned after EOF

Fixes #1509.

R=r
CC=golang-dev
https://golang.org/cl/4167045
This commit is contained in:
Adam Langley 2011-02-15 09:52:20 -05:00
parent e6ee0d2492
commit 5311d915f4
2 changed files with 26 additions and 0 deletions

View File

@ -105,6 +105,9 @@ func (l *Reader) ReadLine() (line []byte, isPrefix bool, err os.Error) {
l.buf = l.buf[:oldLen+n] l.buf = l.buf[:oldLen+n]
if readErr != nil { if readErr != nil {
l.err = readErr l.err = readErr
if len(l.buf) == 0 {
return nil, false, readErr
}
} }
} }
panic("unreachable") panic("unreachable")

View File

@ -7,6 +7,7 @@ package line
import ( import (
"bytes" "bytes"
"io" "io"
"io/ioutil"
"os" "os"
"testing" "testing"
) )
@ -108,3 +109,25 @@ func TestReadAfterLines(t *testing.T) {
t.Errorf("bad result for Read: got %q; expected %q", outbuf.String(), restData) t.Errorf("bad result for Read: got %q; expected %q", outbuf.String(), restData)
} }
} }
func TestReadEmptyBuffer(t *testing.T) {
l := NewReader(bytes.NewBuffer(nil), 10)
line, isPrefix, err := l.ReadLine()
if err != os.EOF {
t.Errorf("expected EOF from ReadLine, got '%s' %t %s", line, isPrefix, err)
}
}
func TestLinesAfterRead(t *testing.T) {
l := NewReader(bytes.NewBuffer([]byte("foo")), 10)
_, err := ioutil.ReadAll(l)
if err != nil {
t.Error(err)
return
}
line, isPrefix, err := l.ReadLine()
if err != os.EOF {
t.Errorf("expected EOF from ReadLine, got '%s' %t %s", line, isPrefix, err)
}
}