1package bug
2
3import (
4 "github.com/MichaelMure/git-bug/identity"
5 "github.com/MichaelMure/git-bug/repository"
6 "github.com/stretchr/testify/assert"
7
8 "testing"
9 "time"
10)
11
12var (
13 rene = identity.NewIdentity("René Descartes", "rene@descartes.fr")
14 unix = time.Now().Unix()
15
16 createOp = NewCreateOp(rene, unix, "title", "message", nil)
17 setTitleOp = NewSetTitleOp(rene, unix, "title2", "title1")
18 addCommentOp = NewAddCommentOp(rene, unix, "message2", nil)
19 setStatusOp = NewSetStatusOp(rene, unix, ClosedStatus)
20 labelChangeOp = NewLabelChangeOperation(rene, unix, []Label{"added"}, []Label{"removed"})
21)
22
23func ExampleOperationIterator() {
24 b := NewBug()
25
26 // add operations
27
28 it := NewOperationIterator(b)
29
30 for it.Next() {
31 // do something with each operations
32 _ = it.Value()
33 }
34}
35
36func TestOpIterator(t *testing.T) {
37 mockRepo := repository.NewMockRepoForTest()
38
39 bug1 := NewBug()
40
41 // first pack
42 bug1.Append(createOp)
43 bug1.Append(setTitleOp)
44 bug1.Append(addCommentOp)
45 bug1.Append(setStatusOp)
46 bug1.Append(labelChangeOp)
47 err := bug1.Commit(mockRepo)
48 assert.NoError(t, err)
49
50 // second pack
51 bug1.Append(setTitleOp)
52 bug1.Append(setTitleOp)
53 bug1.Append(setTitleOp)
54 err = bug1.Commit(mockRepo)
55 assert.NoError(t, err)
56
57 // staging
58 bug1.Append(setTitleOp)
59 bug1.Append(setTitleOp)
60 bug1.Append(setTitleOp)
61
62 it := NewOperationIterator(bug1)
63
64 counter := 0
65 for it.Next() {
66 _ = it.Value()
67 counter++
68 }
69
70 if counter != 11 {
71 t.Fatalf("Wrong count of value iterated (%d instead of 8)", counter)
72 }
73}