From 11a4a83b3797b69d0a1cbcf3239951ff487ce86b Mon Sep 17 00:00:00 2001 From: Aaron Jacobs Date: Fri, 27 Feb 2015 16:06:55 +1100 Subject: [PATCH] HelloFSTest.OpenAndRead --- samples/hello_fs_test.go | 58 ++++++++++++++++++++++++++++++++++------ 1 file changed, 50 insertions(+), 8 deletions(-) diff --git a/samples/hello_fs_test.go b/samples/hello_fs_test.go index 4846785..52e1248 100644 --- a/samples/hello_fs_test.go +++ b/samples/hello_fs_test.go @@ -4,6 +4,7 @@ package samples_test import ( + "io" "io/ioutil" "log" "os" @@ -198,16 +199,57 @@ func (t *HelloFSTest) ReadFile_World() { ExpectEq("Hello, world!", string(slice)) } -func (t *HelloFSTest) Read_Hello() { - AssertTrue(false, "TODO") -} +func (t *HelloFSTest) OpenAndRead() { + var buf []byte = make([]byte, 1024) + var n int + var off int64 + var err error -func (t *HelloFSTest) Read_Dir() { - AssertTrue(false, "TODO") -} + // Open the file. + f, err := os.Open(path.Join(t.mfs.Dir(), "hello")) + defer func() { + if f != nil { + ExpectEq(nil, f.Close()) + } + }() -func (t *HelloFSTest) Read_World() { - AssertTrue(false, "TODO") + AssertEq(nil, err) + + // Seeking shouldn't affect the random access reads below. + _, err = f.Seek(7, 0) + AssertEq(nil, err) + + // Random access reads + n, err = f.ReadAt(buf[:2], 0) + AssertEq(nil, err) + ExpectEq(2, n) + ExpectEq("He", string(buf[:n])) + + n, err = f.ReadAt(buf[:2], int64(len("Hel"))) + AssertEq(nil, err) + ExpectEq(2, n) + ExpectEq("lo", string(buf[:n])) + + n, err = f.ReadAt(buf[:3], int64(len("Hello, wo"))) + AssertEq(nil, err) + ExpectEq(3, n) + ExpectEq("rld", string(buf[:n])) + + // Read beyond end. + n, err = f.ReadAt(buf[:3], int64(len("Hello, world"))) + AssertEq(io.EOF, err) + ExpectEq(1, n) + ExpectEq("!", string(buf[:n])) + + // Seek then read the rest. + off, err = f.Seek(int64(len("Hel")), 0) + AssertEq(nil, err) + AssertEq(len("Hel"), off) + + n, err = io.ReadFull(f, buf[:len("lo, world!")]) + AssertEq(nil, err) + ExpectEq(len("lo, world!"), n) + ExpectEq("lo, world!", string(buf[:n])) } func (t *HelloFSTest) Open_NonExistent() {