From deff9e0a41eca43f832314219241c9a63cf8007e Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Fri, 13 Jul 2018 16:48:55 +0200 Subject: add basic tests for Bug and OperationIterator --- bug/bug.go | 27 ++++++++++++++++++++++++--- bug/operation_iterator.go | 6 +++--- bug/operation_pack.go | 6 +++++- 3 files changed, 32 insertions(+), 7 deletions(-) (limited to 'bug') 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() } -- cgit