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