1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
package storer
import (
"fmt"
"testing"
. "gopkg.in/check.v1"
"srcd.works/go-git.v4/plumbing"
)
func Test(t *testing.T) { TestingT(t) }
type ObjectSuite struct {
Objects []plumbing.EncodedObject
Hash []plumbing.Hash
}
var _ = Suite(&ObjectSuite{})
func (s *ObjectSuite) SetUpSuite(c *C) {
s.Objects = []plumbing.EncodedObject{
s.buildObject([]byte("foo")),
s.buildObject([]byte("bar")),
}
for _, o := range s.Objects {
s.Hash = append(s.Hash, o.Hash())
}
}
func (s *ObjectSuite) TestMultiObjectIterNext(c *C) {
expected := []plumbing.EncodedObject{
&plumbing.MemoryObject{},
&plumbing.MemoryObject{},
&plumbing.MemoryObject{},
&plumbing.MemoryObject{},
&plumbing.MemoryObject{},
&plumbing.MemoryObject{},
}
iter := NewMultiEncodedObjectIter([]EncodedObjectIter{
NewEncodedObjectSliceIter(expected[0:2]),
NewEncodedObjectSliceIter(expected[2:4]),
NewEncodedObjectSliceIter(expected[4:5]),
})
var i int
iter.ForEach(func(o plumbing.EncodedObject) error {
c.Assert(o, Equals, expected[i])
i++
return nil
})
iter.Close()
}
func (s *ObjectSuite) buildObject(content []byte) plumbing.EncodedObject {
o := &plumbing.MemoryObject{}
o.Write(content)
return o
}
func (s *ObjectSuite) TestObjectLookupIter(c *C) {
var count int
storage := &MockObjectStorage{s.Objects}
i := NewEncodedObjectLookupIter(storage, plumbing.CommitObject, s.Hash)
err := i.ForEach(func(o plumbing.EncodedObject) error {
c.Assert(o, NotNil)
c.Assert(o.Hash().String(), Equals, s.Hash[count].String())
count++
return nil
})
c.Assert(err, IsNil)
i.Close()
}
func (s *ObjectSuite) TestObjectSliceIter(c *C) {
var count int
i := NewEncodedObjectSliceIter(s.Objects)
err := i.ForEach(func(o plumbing.EncodedObject) error {
c.Assert(o, NotNil)
c.Assert(o.Hash().String(), Equals, s.Hash[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) {
i := NewEncodedObjectSliceIter(s.Objects)
var count = 0
err := i.ForEach(func(o plumbing.EncodedObject) error {
c.Assert(o, NotNil)
c.Assert(o.Hash().String(), Equals, s.Hash[count].String())
count++
return ErrStop
})
c.Assert(count, Equals, 1)
c.Assert(err, IsNil)
}
func (s *ObjectSuite) TestObjectSliceIterError(c *C) {
i := NewEncodedObjectSliceIter([]plumbing.EncodedObject{
s.buildObject([]byte("foo")),
})
err := i.ForEach(func(plumbing.EncodedObject) error {
return fmt.Errorf("a random error")
})
c.Assert(err, NotNil)
}
type MockObjectStorage struct {
db []plumbing.EncodedObject
}
func (o *MockObjectStorage) NewEncodedObject() plumbing.EncodedObject {
return nil
}
func (o *MockObjectStorage) SetEncodedObject(obj plumbing.EncodedObject) (plumbing.Hash, error) {
return plumbing.ZeroHash, nil
}
func (o *MockObjectStorage) EncodedObject(t plumbing.ObjectType, h plumbing.Hash) (plumbing.EncodedObject, error) {
for _, o := range o.db {
if o.Hash() == h {
return o, nil
}
}
return nil, plumbing.ErrObjectNotFound
}
func (o *MockObjectStorage) IterEncodedObjects(t plumbing.ObjectType) (EncodedObjectIter, error) {
return nil, nil
}
func (o *MockObjectStorage) Begin() Transaction {
return nil
}
|