aboutsummaryrefslogtreecommitdiffstats
path: root/bug
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2018-07-13 16:48:55 +0200
committerMichael Muré <batolettre@gmail.com>2018-07-13 16:48:55 +0200
commitdeff9e0a41eca43f832314219241c9a63cf8007e (patch)
tree215e07e7a77972cee50268603eeb3777de583e8b /bug
parent078545538e6e6bf7a050fe6602a42a61fb5203e9 (diff)
downloadgit-bug-deff9e0a41eca43f832314219241c9a63cf8007e.tar.gz
add basic tests for Bug and OperationIterator
Diffstat (limited to 'bug')
-rw-r--r--bug/bug.go27
-rw-r--r--bug/operation_iterator.go6
-rw-r--r--bug/operation_pack.go6
3 files changed, 32 insertions, 7 deletions
diff --git a/bug/bug.go b/bug/bug.go
index 807f614f..b36246e3 100644
--- a/bug/bug.go
+++ b/bug/bug.go
@@ -37,7 +37,7 @@ func NewBug() (*Bug, error) {
// IsValid check if the Bug data is valid
func (bug *Bug) IsValid() bool {
// non-empty
- if len(bug.Packs) == 0 {
+ if len(bug.Packs) == 0 && bug.Staging.IsEmpty() {
return false
}
@@ -48,9 +48,16 @@ func (bug *Bug) IsValid() bool {
}
}
+ // check if Staging is valid if needed
+ if !bug.Staging.IsEmpty() {
+ if !bug.Staging.IsValid() {
+ return false
+ }
+ }
+
// The very first Op should be a CREATE
- firstOp := bug.Packs[0].Operations[0]
- if firstOp.OpType() != CREATE {
+ firstOp := bug.firstOp()
+ if firstOp == nil || firstOp.OpType() != CREATE {
return false
}
@@ -82,3 +89,17 @@ func (bug *Bug) Commit() {
func (bug *Bug) HumanId() string {
return bug.Id.String()
}
+
+func (bug *Bug) firstOp() Operation {
+ for _, pack := range bug.Packs {
+ for _, op := range pack.Operations {
+ return op
+ }
+ }
+
+ if !bug.Staging.IsEmpty() {
+ return bug.Staging.Operations[0]
+ }
+
+ return nil
+}
diff --git a/bug/operation_iterator.go b/bug/operation_iterator.go
index a576ddd6..fe001d45 100644
--- a/bug/operation_iterator.go
+++ b/bug/operation_iterator.go
@@ -16,7 +16,7 @@ func NewOperationIterator(bug *Bug) *OperationIterator {
func (it *OperationIterator) Next() bool {
// Special case of the staging area
- if it.packIndex == len(it.bug.Packs)+1 {
+ if it.packIndex == len(it.bug.Packs) {
pack := it.bug.Staging
it.opIndex++
return it.opIndex < len(pack.Operations)
@@ -39,7 +39,7 @@ func (it *OperationIterator) Next() bool {
it.packIndex++
// Special case of the non-empty staging area
- if it.packIndex == len(it.bug.Packs)+1 && len(it.bug.Staging.Operations) > 0 {
+ if it.packIndex == len(it.bug.Packs) && len(it.bug.Staging.Operations) > 0 {
return true
}
@@ -48,7 +48,7 @@ func (it *OperationIterator) Next() bool {
func (it *OperationIterator) Value() Operation {
// Special case of the staging area
- if it.packIndex == len(it.bug.Packs)+1 {
+ if it.packIndex == len(it.bug.Packs) {
pack := it.bug.Staging
if it.opIndex >= len(pack.Operations) {
diff --git a/bug/operation_pack.go b/bug/operation_pack.go
index e3d64e72..21376b9c 100644
--- a/bug/operation_pack.go
+++ b/bug/operation_pack.go
@@ -15,6 +15,10 @@ func (opp *OperationPack) Append(op Operation) {
opp.Operations = append(opp.Operations, op)
}
+func (opp *OperationPack) IsEmpty() bool {
+ return len(opp.Operations) == 0
+}
+
func (opp *OperationPack) IsValid() bool {
- return len(opp.Operations) > 0
+ return !opp.IsEmpty()
}