mirror of
https://github.com/golang/go
synced 2024-11-18 01:54:45 -07:00
bufio: test for exact error value in TestNegativeEOFReader and TestLargeReader
CL 225357 added tests for Scanner not panicking on bad readers. CL 225557 created a named error value that is returned instead. CL 237739 documents that the bufio.ErrBadReadCount is returned when bufio.Scanner is used with an invalid io.Reader. This suggests we wouldn't want that behavior to be able to change without a test noticing it, so modify the tests to check for the exact error value instead of just any non-nil one. For #38053. Change-Id: I4b0b8eb6804ebfe2c768505ddb94f0b1017fcf8b Reviewed-on: https://go-review.googlesource.com/c/go/+/238217 Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
parent
340efd3608
commit
dea6d928f6
@ -558,8 +558,8 @@ func (r *negativeEOFReader) Read(p []byte) (int, error) {
|
|||||||
return -1, io.EOF
|
return -1, io.EOF
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test that the scanner doesn't panic on a reader that returns a
|
// Test that the scanner doesn't panic and returns ErrBadReadCount
|
||||||
// negative count of bytes read (issue 38053).
|
// on a reader that returns a negative count of bytes read (issue 38053).
|
||||||
func TestNegativeEOFReader(t *testing.T) {
|
func TestNegativeEOFReader(t *testing.T) {
|
||||||
r := negativeEOFReader(10)
|
r := negativeEOFReader(10)
|
||||||
scanner := NewScanner(&r)
|
scanner := NewScanner(&r)
|
||||||
@ -571,8 +571,8 @@ func TestNegativeEOFReader(t *testing.T) {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if scanner.Err() == nil {
|
if got, want := scanner.Err(), ErrBadReadCount; got != want {
|
||||||
t.Error("scanner.Err returned nil, expected an error")
|
t.Errorf("scanner.Err: got %v, want %v", got, want)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -584,11 +584,13 @@ func (largeReader) Read(p []byte) (int, error) {
|
|||||||
return len(p) + 1, nil
|
return len(p) + 1, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test that the scanner doesn't panic and returns ErrBadReadCount
|
||||||
|
// on a reader that returns an impossibly large count of bytes read (issue 38053).
|
||||||
func TestLargeReader(t *testing.T) {
|
func TestLargeReader(t *testing.T) {
|
||||||
scanner := NewScanner(largeReader{})
|
scanner := NewScanner(largeReader{})
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
}
|
}
|
||||||
if scanner.Err() == nil {
|
if got, want := scanner.Err(), ErrBadReadCount; got != want {
|
||||||
t.Error("scanner.Err returned nil, expected an error")
|
t.Errorf("scanner.Err: got %v, want %v", got, want)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user