mirror of
https://github.com/golang/go
synced 2024-11-12 06:30:21 -07:00
bufio: add some tests
R=golang-dev, adg CC=golang-dev https://golang.org/cl/7927044
This commit is contained in:
parent
5060dded0f
commit
dc71ace282
@ -7,6 +7,7 @@ package bufio_test
|
||||
import (
|
||||
. "bufio"
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
@ -434,9 +435,12 @@ func TestWriteErrors(t *testing.T) {
|
||||
t.Errorf("Write hello to %v: %v", w, e)
|
||||
continue
|
||||
}
|
||||
e = buf.Flush()
|
||||
if e != w.expect {
|
||||
t.Errorf("Flush %v: got %v, wanted %v", w, e, w.expect)
|
||||
// Two flushes, to verify the error is sticky.
|
||||
for i := 0; i < 2; i++ {
|
||||
e = buf.Flush()
|
||||
if e != w.expect {
|
||||
t.Errorf("Flush %d/2 %v: got %v, wanted %v", i+1, w, e, w.expect)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -962,6 +966,43 @@ func TestNegativeRead(t *testing.T) {
|
||||
b.Read(make([]byte, 100))
|
||||
}
|
||||
|
||||
var errFake = errors.New("fake error")
|
||||
|
||||
type errorThenGoodReader struct {
|
||||
didErr bool
|
||||
nread int
|
||||
}
|
||||
|
||||
func (r *errorThenGoodReader) Read(p []byte) (int, error) {
|
||||
r.nread++
|
||||
if !r.didErr {
|
||||
r.didErr = true
|
||||
return 0, errFake
|
||||
}
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
func TestReaderClearError(t *testing.T) {
|
||||
r := &errorThenGoodReader{}
|
||||
b := NewReader(r)
|
||||
buf := make([]byte, 1)
|
||||
if _, err := b.Read(nil); err != nil {
|
||||
t.Fatalf("1st nil Read = %v; want nil", err)
|
||||
}
|
||||
if _, err := b.Read(buf); err != errFake {
|
||||
t.Fatalf("1st Read = %v; want errFake", err)
|
||||
}
|
||||
if _, err := b.Read(nil); err != nil {
|
||||
t.Fatalf("2nd nil Read = %v; want nil", err)
|
||||
}
|
||||
if _, err := b.Read(buf); err != nil {
|
||||
t.Fatalf("3rd Read with buffer = %v; want nil", err)
|
||||
}
|
||||
if r.nread != 2 {
|
||||
t.Errorf("num reads = %d; want 2", r.nread)
|
||||
}
|
||||
}
|
||||
|
||||
// An onlyReader only implements io.Reader, no matter what other methods the underlying implementation may have.
|
||||
type onlyReader struct {
|
||||
r io.Reader
|
||||
|
Loading…
Reference in New Issue
Block a user