diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2016-09-25 23:58:59 +0200 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2016-09-25 23:58:59 +0200 |
commit | b9c0a09435392913c0054382500c805cd7cb596b (patch) | |
tree | d5a4bebff33b02215b25515ab769f277c0c07bb9 /formats/objfile/writer_test.go | |
parent | 859775d320d574979c63a114de1437e3c5d9114c (diff) | |
download | go-git-b9c0a09435392913c0054382500c805cd7cb596b.tar.gz |
formats: objfile idomatic reader/writer
Diffstat (limited to 'formats/objfile/writer_test.go')
-rw-r--r-- | formats/objfile/writer_test.go | 51 |
1 files changed, 31 insertions, 20 deletions
diff --git a/formats/objfile/writer_test.go b/formats/objfile/writer_test.go index ab5a5bf..18bba79 100644 --- a/formats/objfile/writer_test.go +++ b/formats/objfile/writer_test.go @@ -16,54 +16,65 @@ var _ = Suite(&SuiteWriter{}) func (s *SuiteWriter) TestWriteObjfile(c *C) { for k, fixture := range objfileFixtures { + buffer := bytes.NewBuffer(nil) + com := fmt.Sprintf("test %d: ", k) hash := core.NewHash(fixture.hash) content, _ := base64.StdEncoding.DecodeString(fixture.content) - buffer := new(bytes.Buffer) // Write the data out to the buffer - testWriter(c, buffer, hash, fixture.t, content, com) + testWriter(c, buffer, hash, fixture.t, content) // Read the data back in from the buffer to be sure it matches testReader(c, buffer, hash, fixture.t, content, com) } } -func testWriter(c *C, dest io.Writer, hash core.Hash, typ core.ObjectType, content []byte, com string) { - length := int64(len(content)) - w, err := NewWriter(dest, typ, length) +func testWriter(c *C, dest io.Writer, hash core.Hash, t core.ObjectType, content []byte) { + size := int64(len(content)) + w := NewWriter(dest) + + err := w.WriteHeader(t, size) c.Assert(err, IsNil) - c.Assert(w.Type(), Equals, typ) - c.Assert(w.Size(), Equals, length) + written, err := io.Copy(w, bytes.NewReader(content)) c.Assert(err, IsNil) - c.Assert(written, Equals, length) - c.Assert(w.Size(), Equals, int64(len(content))) - c.Assert(w.Hash(), Equals, hash) // Test Hash() before close + c.Assert(written, Equals, size) + + c.Assert(w.Hash(), Equals, hash) c.Assert(w.Close(), IsNil) - c.Assert(w.Hash(), Equals, hash) // Test Hash() after close - _, err = w.Write([]byte{1}) - c.Assert(err, Equals, ErrClosed) } func (s *SuiteWriter) TestWriteOverflow(c *C) { - w, err := NewWriter(new(bytes.Buffer), core.BlobObject, 8) + buf := bytes.NewBuffer(nil) + w := NewWriter(buf) + + err := w.WriteHeader(core.BlobObject, 8) c.Assert(err, IsNil) - _, err = w.Write([]byte("1234")) + + n, err := w.Write([]byte("1234")) c.Assert(err, IsNil) - _, err = w.Write([]byte("56789")) + c.Assert(n, Equals, 4) + + n, err = w.Write([]byte("56789")) c.Assert(err, Equals, ErrOverflow) + c.Assert(n, Equals, 4) } func (s *SuiteWriter) TestNewWriterInvalidType(c *C) { - var t core.ObjectType - _, err := NewWriter(new(bytes.Buffer), t, 8) + buf := bytes.NewBuffer(nil) + w := NewWriter(buf) + + err := w.WriteHeader(core.InvalidObject, 8) c.Assert(err, Equals, core.ErrInvalidType) } func (s *SuiteWriter) TestNewWriterInvalidSize(c *C) { - _, err := NewWriter(new(bytes.Buffer), core.BlobObject, -1) + buf := bytes.NewBuffer(nil) + w := NewWriter(buf) + + err := w.WriteHeader(core.BlobObject, -1) c.Assert(err, Equals, ErrNegativeSize) - _, err = NewWriter(new(bytes.Buffer), core.BlobObject, -1651860) + err = w.WriteHeader(core.BlobObject, -1651860) c.Assert(err, Equals, ErrNegativeSize) } |