Skip to content

Commit

Permalink
Added removeElement() test.
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelkleinhenz committed Aug 27, 2018
1 parent 35e6034 commit d867145
Showing 1 changed file with 93 additions and 9 deletions.
102 changes: 93 additions & 9 deletions actions/rules/action_state_to_metastate_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rules

import (
"fmt"
"testing"

"github.com/fabric8-services/fabric8-wit/actions/change"
Expand All @@ -23,6 +24,18 @@ type ActionStateToMetastateSuite struct {
gormtestsupport.DBTestSuite
}

func ArrayEquals(a []interface{}, b []interface{}) bool {
if len(a) != len(b) {
return false
}
for i, v := range a {
if v != b[i] {
return false
}
}
return true
}

func (s *ActionStateToMetastateSuite) TestContainsElement() {
s.T().Run("contains an element", func(t *testing.T) {
fxt := tf.NewTestFixture(t, s.DB, tf.CreateWorkItemEnvironment())
Expand Down Expand Up @@ -80,12 +93,23 @@ func (s *ActionStateToMetastateSuite) TestRemoveElement() {
a = append(a, 2)
a = append(a, 3)
a = append(a, 2)
a = action.removeElement(a, 2)
require.Len(t, a, 3)
require.False(t, action.contains(a, 2))
a = action.removeElement(a, 1)
require.Len(t, a, 2)
require.False(t, action.contains(a, 1))
var expected []interface{}
expected = append(expected, 0)
expected = append(expected, 2)
expected = append(expected, 3)
expected = append(expected, 2)
require.Len(t, a, 4)
require.True(t, ArrayEquals(expected, a))
a = action.removeElement(a, 3)
expected = []interface{}{}
expected = append(expected, 0)
expected = append(expected, 2)
expected = append(expected, 2)
require.Len(t, a, 3)
fmt.Println(expected)
fmt.Println(a)
require.True(t, ArrayEquals(expected, a))
})
s.T().Run("removing a non-existing element", func(t *testing.T) {
fxt := tf.NewTestFixture(t, s.DB, tf.CreateWorkItemEnvironment())
Expand All @@ -104,10 +128,70 @@ func (s *ActionStateToMetastateSuite) TestRemoveElement() {
a = append(a, 2)
a = action.removeElement(a, 4)
require.Len(t, a, 5)
require.True(t, action.contains(a, 0))
require.True(t, action.contains(a, 1))
require.True(t, action.contains(a, 2))
require.True(t, action.contains(a, 3))
var expected []interface{}
expected = append(expected, 0)
expected = append(expected, 1)
expected = append(expected, 2)
expected = append(expected, 3)
expected = append(expected, 2)
require.True(t, ArrayEquals(expected, a))
})
s.T().Run("removing a duplicate element", func(t *testing.T) {
fxt := tf.NewTestFixture(t, s.DB, tf.CreateWorkItemEnvironment())
action := ActionStateToMetaState{
Db: s.GormDB,
Ctx: s.Ctx,
UserID: &fxt.Identities[0].ID,
}
// there is no other way of creating an []interface{}.
// but we have plenty of memory, so be it.
var a []interface{}
a = append(a, 0)
a = append(a, 1)
a = append(a, 2)
a = append(a, 3)
a = append(a, 2)
a = action.removeElement(a, 2)
var expected []interface{}
expected = append(expected, 0)
expected = append(expected, 1)
expected = append(expected, 3)
require.Len(t, a, 3)
require.True(t, ArrayEquals(expected, a))
})
}

func (s *ActionStateToMetastateSuite) TestDifference() {
s.T().Run("finding differences", func(t *testing.T) {
fxt := tf.NewTestFixture(t, s.DB, tf.CreateWorkItemEnvironment())
action := ActionStateToMetaState{
Db: s.GormDB,
Ctx: s.Ctx,
UserID: &fxt.Identities[0].ID,
}
// there is no other way of creating an []interface{}.
// but we have plenty of memory, so be it.
var a []interface{}
a = append(a, 0)
a = append(a, 1)
a = append(a, 2)
a = append(a, 3)
a = append(a, 2)
var b []interface{}
b = append(b, 2)
b = append(b, 3)
b = append(b, 5)
added, removed := action.difference(a, b)
require.Len(t, added, 1)
require.Len(t, removed, 2)
// wasting plenty more memory here
var expectedAdded []interface{}
expectedAdded = append(expectedAdded, 5)
var expectedRemoved []interface{}
expectedRemoved = append(expectedRemoved, 0)
expectedRemoved = append(expectedRemoved, 1)
require.True(t, ArrayEquals(added, expectedAdded))
require.True(t, ArrayEquals(removed, expectedRemoved))
})
}

Expand Down

0 comments on commit d867145

Please sign in to comment.