1
1
package rules
2
2
3
3
import (
4
+ "fmt"
4
5
"testing"
5
6
6
7
"github.com/fabric8-services/fabric8-wit/actions/change"
@@ -23,6 +24,18 @@ type ActionStateToMetastateSuite struct {
23
24
gormtestsupport.DBTestSuite
24
25
}
25
26
27
+ func ArrayEquals (a []interface {}, b []interface {}) bool {
28
+ if len (a ) != len (b ) {
29
+ return false
30
+ }
31
+ for i , v := range a {
32
+ if v != b [i ] {
33
+ return false
34
+ }
35
+ }
36
+ return true
37
+ }
38
+
26
39
func (s * ActionStateToMetastateSuite ) TestContainsElement () {
27
40
s .T ().Run ("contains an element" , func (t * testing.T ) {
28
41
fxt := tf .NewTestFixture (t , s .DB , tf .CreateWorkItemEnvironment ())
@@ -80,12 +93,23 @@ func (s *ActionStateToMetastateSuite) TestRemoveElement() {
80
93
a = append (a , 2 )
81
94
a = append (a , 3 )
82
95
a = append (a , 2 )
83
- a = action .removeElement (a , 2 )
84
- require .Len (t , a , 3 )
85
- require .False (t , action .contains (a , 2 ))
86
96
a = action .removeElement (a , 1 )
87
- require .Len (t , a , 2 )
88
- require .False (t , action .contains (a , 1 ))
97
+ var expected []interface {}
98
+ expected = append (expected , 0 )
99
+ expected = append (expected , 2 )
100
+ expected = append (expected , 3 )
101
+ expected = append (expected , 2 )
102
+ require .Len (t , a , 4 )
103
+ require .True (t , ArrayEquals (expected , a ))
104
+ a = action .removeElement (a , 3 )
105
+ expected = []interface {}{}
106
+ expected = append (expected , 0 )
107
+ expected = append (expected , 2 )
108
+ expected = append (expected , 2 )
109
+ require .Len (t , a , 3 )
110
+ fmt .Println (expected )
111
+ fmt .Println (a )
112
+ require .True (t , ArrayEquals (expected , a ))
89
113
})
90
114
s .T ().Run ("removing a non-existing element" , func (t * testing.T ) {
91
115
fxt := tf .NewTestFixture (t , s .DB , tf .CreateWorkItemEnvironment ())
@@ -104,10 +128,70 @@ func (s *ActionStateToMetastateSuite) TestRemoveElement() {
104
128
a = append (a , 2 )
105
129
a = action .removeElement (a , 4 )
106
130
require .Len (t , a , 5 )
107
- require .True (t , action .contains (a , 0 ))
108
- require .True (t , action .contains (a , 1 ))
109
- require .True (t , action .contains (a , 2 ))
110
- require .True (t , action .contains (a , 3 ))
131
+ var expected []interface {}
132
+ expected = append (expected , 0 )
133
+ expected = append (expected , 1 )
134
+ expected = append (expected , 2 )
135
+ expected = append (expected , 3 )
136
+ expected = append (expected , 2 )
137
+ require .True (t , ArrayEquals (expected , a ))
138
+ })
139
+ s .T ().Run ("removing a duplicate element" , func (t * testing.T ) {
140
+ fxt := tf .NewTestFixture (t , s .DB , tf .CreateWorkItemEnvironment ())
141
+ action := ActionStateToMetaState {
142
+ Db : s .GormDB ,
143
+ Ctx : s .Ctx ,
144
+ UserID : & fxt .Identities [0 ].ID ,
145
+ }
146
+ // there is no other way of creating an []interface{}.
147
+ // but we have plenty of memory, so be it.
148
+ var a []interface {}
149
+ a = append (a , 0 )
150
+ a = append (a , 1 )
151
+ a = append (a , 2 )
152
+ a = append (a , 3 )
153
+ a = append (a , 2 )
154
+ a = action .removeElement (a , 2 )
155
+ var expected []interface {}
156
+ expected = append (expected , 0 )
157
+ expected = append (expected , 1 )
158
+ expected = append (expected , 3 )
159
+ require .Len (t , a , 3 )
160
+ require .True (t , ArrayEquals (expected , a ))
161
+ })
162
+ }
163
+
164
+ func (s * ActionStateToMetastateSuite ) TestDifference () {
165
+ s .T ().Run ("finding differences" , func (t * testing.T ) {
166
+ fxt := tf .NewTestFixture (t , s .DB , tf .CreateWorkItemEnvironment ())
167
+ action := ActionStateToMetaState {
168
+ Db : s .GormDB ,
169
+ Ctx : s .Ctx ,
170
+ UserID : & fxt .Identities [0 ].ID ,
171
+ }
172
+ // there is no other way of creating an []interface{}.
173
+ // but we have plenty of memory, so be it.
174
+ var a []interface {}
175
+ a = append (a , 0 )
176
+ a = append (a , 1 )
177
+ a = append (a , 2 )
178
+ a = append (a , 3 )
179
+ a = append (a , 2 )
180
+ var b []interface {}
181
+ b = append (b , 2 )
182
+ b = append (b , 3 )
183
+ b = append (b , 5 )
184
+ added , removed := action .difference (a , b )
185
+ require .Len (t , added , 1 )
186
+ require .Len (t , removed , 2 )
187
+ // wasting plenty more memory here
188
+ var expectedAdded []interface {}
189
+ expectedAdded = append (expectedAdded , 5 )
190
+ var expectedRemoved []interface {}
191
+ expectedRemoved = append (expectedRemoved , 0 )
192
+ expectedRemoved = append (expectedRemoved , 1 )
193
+ require .True (t , ArrayEquals (added , expectedAdded ))
194
+ require .True (t , ArrayEquals (removed , expectedRemoved ))
111
195
})
112
196
}
113
197
0 commit comments