aboutsummaryrefslogtreecommitdiffstats
path: root/bug/operation_iterator_test.go
blob: 2865d25de6c86d6c8f3d1d564d988b31c34244d1 (plain) (blame)
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
package bug

import (
	"github.com/MichaelMure/git-bug/identity"
	"github.com/MichaelMure/git-bug/repository"
	"github.com/stretchr/testify/assert"

	"testing"
	"time"
)

var (
// Beware, don't those test data in multi-repo situation !
// As an example, the Identity would be considered commited after a commit
// in one repo,
// rene = identity.NewIdentity("René Descartes", "rene@descartes.fr")
// unix = time.Now().Unix()

// createOp      = NewCreateOp(rene, unix, "title", "message", nil)
// setTitleOp    = NewSetTitleOp(rene, unix, "title2", "title1")
// addCommentOp  = NewAddCommentOp(rene, unix, "message2", nil)
// setStatusOp   = NewSetStatusOp(rene, unix, ClosedStatus)
// labelChangeOp = NewLabelChangeOperation(rene, unix, []Label{"added"}, []Label{"removed"})
)

func ExampleOperationIterator() {
	b := NewBug()

	// add operations

	it := NewOperationIterator(b)

	for it.Next() {
		// do something with each operations
		_ = it.Value()
	}
}

func TestOpIterator(t *testing.T) {
	mockRepo := repository.NewMockRepoForTest()

	rene := identity.NewIdentity("René Descartes", "rene@descartes.fr")
	unix := time.Now().Unix()

	createOp := NewCreateOp(rene, unix, "title", "message", nil)
	setTitleOp := NewSetTitleOp(rene, unix, "title2", "title1")
	addCommentOp := NewAddCommentOp(rene, unix, "message2", nil)
	setStatusOp := NewSetStatusOp(rene, unix, ClosedStatus)
	labelChangeOp := NewLabelChangeOperation(rene, unix, []Label{"added"}, []Label{"removed"})

	bug1 := NewBug()

	// first pack
	bug1.Append(createOp)
	bug1.Append(setTitleOp)
	bug1.Append(addCommentOp)
	bug1.Append(setStatusOp)
	bug1.Append(labelChangeOp)
	err := bug1.Commit(mockRepo)
	assert.NoError(t, err)

	// second pack
	bug1.Append(setTitleOp)
	bug1.Append(setTitleOp)
	bug1.Append(setTitleOp)
	err = bug1.Commit(mockRepo)
	assert.NoError(t, err)

	// staging
	bug1.Append(setTitleOp)
	bug1.Append(setTitleOp)
	bug1.Append(setTitleOp)

	it := NewOperationIterator(bug1)

	counter := 0
	for it.Next() {
		_ = it.Value()
		counter++
	}

	if counter != 11 {
		t.Fatalf("Wrong count of value iterated (%d instead of 8)", counter)
	}
}