aboutsummaryrefslogtreecommitdiffstats
path: root/core/object_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'core/object_test.go')
-rw-r--r--core/object_test.go129
1 files changed, 129 insertions, 0 deletions
diff --git a/core/object_test.go b/core/object_test.go
index c9f4f2c..08c5dd2 100644
--- a/core/object_test.go
+++ b/core/object_test.go
@@ -6,6 +6,45 @@ type ObjectSuite struct{}
var _ = Suite(&ObjectSuite{})
+func (s *ObjectSuite) TestObjectTypeString(c *C) {
+ c.Assert(CommitObject.String(), Equals, "commit")
+ c.Assert(TreeObject.String(), Equals, "tree")
+ c.Assert(BlobObject.String(), Equals, "blob")
+ c.Assert(TagObject.String(), Equals, "tag")
+ c.Assert(REFDeltaObject.String(), Equals, "ref-delta")
+ c.Assert(OFSDeltaObject.String(), Equals, "ofs-delta")
+ c.Assert(AnyObject.String(), Equals, "any")
+ c.Assert(ObjectType(42).String(), Equals, "unknown")
+}
+
+func (s *ObjectSuite) TestObjectTypeBytes(c *C) {
+ c.Assert(CommitObject.Bytes(), DeepEquals, []byte("commit"))
+}
+
+func (s *ObjectSuite) TestObjectTypeValid(c *C) {
+ c.Assert(CommitObject.Valid(), Equals, true)
+ c.Assert(ObjectType(42).Valid(), Equals, false)
+}
+
+func (s *ObjectSuite) TestParseObjectType(c *C) {
+ for s, e := range map[string]ObjectType{
+ "commit": CommitObject,
+ "tree": TreeObject,
+ "blob": BlobObject,
+ "tag": TagObject,
+ "ref-delta": REFDeltaObject,
+ "ofs-delta": OFSDeltaObject,
+ } {
+ t, err := ParseObjectType(s)
+ c.Assert(err, IsNil)
+ c.Assert(e, Equals, t)
+ }
+
+ t, err := ParseObjectType("foo")
+ c.Assert(err, Equals, ErrInvalidType)
+ c.Assert(t, Equals, InvalidObject)
+}
+
func (s *ObjectSuite) TestMultiObjectIterNext(c *C) {
expected := []Object{
&MemoryObject{},
@@ -28,4 +67,94 @@ func (s *ObjectSuite) TestMultiObjectIterNext(c *C) {
i++
return nil
})
+
+ iter.Close()
+}
+
+func (s *ObjectSuite) TestObjectLookupIter(c *C) {
+ h := []Hash{
+ NewHash("0920f02906615b285040767a67c5cb30fe0f5e2c"),
+ NewHash("4921e391f1128010a2d957f8db15c5e729ccf94a"),
+ }
+
+ var count int
+
+ i := NewObjectLookupIter(&MockObjectStorage{}, CommitObject, h)
+ err := i.ForEach(func(o Object) error {
+ c.Assert(o, NotNil)
+ c.Assert(o.Hash().String(), Equals, h[count].String())
+ count++
+ return nil
+ })
+
+ c.Assert(err, IsNil)
+ i.Close()
+}
+
+func (s *ObjectSuite) TestObjectSliceIter(c *C) {
+ h := []Hash{
+ NewHash("0920f02906615b285040767a67c5cb30fe0f5e2c"),
+ NewHash("4921e391f1128010a2d957f8db15c5e729ccf94a"),
+ }
+
+ var count int
+
+ i := NewObjectSliceIter([]Object{
+ &MemoryObject{h: h[0]}, &MemoryObject{h: h[1]},
+ })
+
+ err := i.ForEach(func(o Object) error {
+ c.Assert(o, NotNil)
+ c.Assert(o.Hash().String(), Equals, h[count].String())
+ count++
+ return nil
+ })
+
+ c.Assert(count, Equals, 2)
+ c.Assert(err, IsNil)
+ c.Assert(i.series, HasLen, 0)
+}
+
+func (s *ObjectSuite) TestObjectSliceIterStop(c *C) {
+ h := []Hash{
+ NewHash("0920f02906615b285040767a67c5cb30fe0f5e2c"),
+ NewHash("4921e391f1128010a2d957f8db15c5e729ccf94a"),
+ }
+
+ i := NewObjectSliceIter([]Object{
+ &MemoryObject{h: h[0]}, &MemoryObject{h: h[1]},
+ })
+
+ var count = 0
+ err := i.ForEach(func(o Object) error {
+ c.Assert(o, NotNil)
+ c.Assert(o.Hash().String(), Equals, h[count].String())
+ count++
+ return ErrStop
+ })
+
+ c.Assert(count, Equals, 1)
+ c.Assert(err, IsNil)
+}
+
+type MockObjectStorage struct{}
+
+func (o *MockObjectStorage) NewObject() Object {
+ return nil
+}
+
+func (o *MockObjectStorage) Set(obj Object) (Hash, error) {
+ return ZeroHash, nil
+}
+
+func (o *MockObjectStorage) Get(t ObjectType, h Hash) (Object, error) {
+ return &MemoryObject{h: h}, nil
+}
+
+func (o *MockObjectStorage) Iter(t ObjectType) (ObjectIter, error) {
+ return nil, nil
+}
+
+func (o *MockObjectStorage) Begin() TxObjectStorage {
+ return nil
}