diff options
author | Santiago M. Mola <santi@mola.io> | 2016-11-04 16:12:01 +0100 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2016-11-04 16:12:01 +0100 |
commit | 07b8edee398163db2e61430414957df73aec7e20 (patch) | |
tree | 367dce24eb535374f2acd5466c9795cc000f3e68 /objects_test.go | |
parent | 03c4b83d745ecc86a2e3b795a72dfdef1218bab8 (diff) | |
download | go-git-07b8edee398163db2e61430414957df73aec7e20.tar.gz |
add Blobs, Trees and Objects iters. (#114)
* Now every object type as an iterator in Repository.
* old TreeIter is TreeWalker again, TreeIter now matches
the same behaviour as other iterators.
Diffstat (limited to 'objects_test.go')
-rw-r--r-- | objects_test.go | 79 |
1 files changed, 32 insertions, 47 deletions
diff --git a/objects_test.go b/objects_test.go index ab4aa30..ee77869 100644 --- a/objects_test.go +++ b/objects_test.go @@ -7,6 +7,7 @@ import ( "gopkg.in/src-d/go-git.v4/core" . "gopkg.in/check.v1" + "io" ) type ObjectsSuite struct { @@ -73,53 +74,6 @@ func (s *ObjectsSuite) TestParseTree(c *C) { c.Assert(count, Equals, 9) } -func (s *ObjectsSuite) TestBlobHash(c *C) { - o := &core.MemoryObject{} - o.SetType(core.BlobObject) - o.SetSize(3) - - writer, err := o.Writer() - c.Assert(err, IsNil) - defer func() { c.Assert(writer.Close(), IsNil) }() - - writer.Write([]byte{'F', 'O', 'O'}) - - blob := &Blob{} - c.Assert(blob.Decode(o), IsNil) - - c.Assert(blob.Size, Equals, int64(3)) - c.Assert(blob.Hash.String(), Equals, "d96c7efbfec2814ae0301ad054dc8d9fc416c9b5") - - reader, err := blob.Reader() - c.Assert(err, IsNil) - defer func() { c.Assert(reader.Close(), IsNil) }() - - data, err := ioutil.ReadAll(reader) - c.Assert(err, IsNil) - c.Assert(string(data), Equals, "FOO") -} - -func (s *ObjectsSuite) TestBlobDecodeEncodeIdempotent(c *C) { - var objects []*core.MemoryObject - for _, str := range []string{"foo", "foo\n"} { - obj := &core.MemoryObject{} - obj.Write([]byte(str)) - obj.SetType(core.BlobObject) - obj.Hash() - objects = append(objects, obj) - } - for _, object := range objects { - blob := &Blob{} - err := blob.Decode(object) - c.Assert(err, IsNil) - newObject := &core.MemoryObject{} - err = blob.Encode(newObject) - c.Assert(err, IsNil) - newObject.Hash() // Ensure Hash is pre-computed before deep comparison - c.Assert(newObject, DeepEquals, object) - } -} - func (s *ObjectsSuite) TestParseSignature(c *C) { cases := map[string]Signature{ `Foo Bar <foo@bar.com> 1257894000 +0100`: { @@ -169,6 +123,37 @@ func (s *ObjectsSuite) TestParseSignature(c *C) { } } +func (s *ObjectsSuite) TestObjectIter(c *C) { + iter, err := s.Repository.Objects() + c.Assert(err, IsNil) + + objects := []Object{} + iter.ForEach(func(o Object) error { + objects = append(objects, o) + return nil + }) + + c.Assert(len(objects) > 0, Equals, true) + iter.Close() + + iter, err = s.Repository.Objects() + c.Assert(err, IsNil) + + i := 0 + for { + o, err := iter.Next() + if err == io.EOF { + break + } + + c.Assert(err, IsNil) + c.Assert(o, DeepEquals, objects[i]) + i += 1 + } + + iter.Close() +} + func MustParseTime(value string) time.Time { t, _ := time.Parse("2006-01-02 15:04:05 -0700", value) return t |