summaryrefslogtreecommitdiff
path: root/src/pkg/fmt/scan_test.go
diff options
context:
space:
mode:
authorRob Pike <r@golang.org>2010-06-24 15:24:25 -0700
committerRob Pike <r@golang.org>2010-06-24 15:24:25 -0700
commit8a8fa410a043d8d95a79881cc7777930da113f9f (patch)
tree0c3b269fbccf0375ab1de922e9f0d845cb283e53 /src/pkg/fmt/scan_test.go
parentc3a14037297827d36b77a49cf0cbd0849295e79a (diff)
downloadgolang-8a8fa410a043d8d95a79881cc7777930da113f9f.tar.gz
fmt.Scan: fix handling of EOFs.
Fixes issue 876. R=rsc CC=golang-dev http://codereview.appspot.com/1675048
Diffstat (limited to 'src/pkg/fmt/scan_test.go')
-rw-r--r--src/pkg/fmt/scan_test.go42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/pkg/fmt/scan_test.go b/src/pkg/fmt/scan_test.go
index f195c0317..1e0319836 100644
--- a/src/pkg/fmt/scan_test.go
+++ b/src/pkg/fmt/scan_test.go
@@ -493,3 +493,45 @@ func TestScanlnWithMiddleNewline(t *testing.T) {
t.Errorf("expected newline error scanning string with extra newline, got: %s", err)
}
}
+
+// Special Reader that counts reads at end of file.
+type eofCounter struct {
+ reader *strings.Reader
+ eofCount int
+}
+
+func (ec *eofCounter) Read(b []byte) (n int, err os.Error) {
+ n, err = ec.reader.Read(b)
+ if n == 0 {
+ ec.eofCount++
+ }
+ return
+}
+
+// Verify that when we scan, we see at most EOF once per call to a Scan function,
+// and then only when it's really an EOF
+func TestEOF(t *testing.T) {
+ ec := &eofCounter{strings.NewReader("123\n"), 0}
+ var a int
+ n, err := Fscanln(ec, &a)
+ if err != nil {
+ t.Error("unexpected error", err)
+ }
+ if n != 1 {
+ t.Error("expected to scan one item, got", n)
+ }
+ if ec.eofCount != 0 {
+ t.Error("expected zero EOFs", ec.eofCount)
+ ec.eofCount = 0 // reset for next test
+ }
+ n, err = Fscanln(ec, &a)
+ if err == nil {
+ t.Error("expected error scanning empty string")
+ }
+ if n != 0 {
+ t.Error("expected to scan zero items, got", n)
+ }
+ if ec.eofCount != 1 {
+ t.Error("expected one EOF, got", ec.eofCount)
+ }
+}