1447
|
1 |
"{ Package: 'stx:goodies/regression' }"
|
1062
|
2 |
|
|
3 |
"{ NameSpace: RegressionTests }"
|
|
4 |
|
|
5 |
TestCase subclass:#CECollectionExtensionTest
|
|
6 |
instanceVariableNames:''
|
|
7 |
classVariableNames:''
|
|
8 |
poolDictionaries:''
|
|
9 |
category:'tests-Regression-CollectionExtensions'
|
|
10 |
!
|
|
11 |
|
|
12 |
|
|
13 |
!CECollectionExtensionTest methodsFor:'as yet unclassified'!
|
|
14 |
|
|
15 |
testDetectIfOne
|
|
16 |
| element result |
|
|
17 |
result := #(1 2 3) detect: [:each | each = 2] ifOne: [:theOne | element := theOne ].
|
|
18 |
self assert: element = 2.
|
|
19 |
self assert: result = 2.
|
|
20 |
|
|
21 |
element := nil.
|
|
22 |
result := #(1 2 3) detect: [:each | each = 4] ifOne: [:theOne | element := theOne ].
|
|
23 |
self assert: element isNil.
|
|
24 |
self assert: result isNil.
|
|
25 |
!
|
|
26 |
|
|
27 |
testDetectIfOneIfNone
|
|
28 |
| element |
|
|
29 |
#(1 2 3) detect: [:each | each = 2] ifOne: [:theOne | element := theOne ] ifNone: [element := nil].
|
|
30 |
self assert: element = 2.
|
|
31 |
|
|
32 |
#(1 2 3) detect: [:each | each = 4] ifOne: [:theOne | element := theOne ] ifNone: [element := nil].
|
|
33 |
self assert: element isNil.
|
|
34 |
! !
|
|
35 |
|
|
36 |
!CECollectionExtensionTest methodsFor:'collect as set'!
|
|
37 |
|
|
38 |
testCollectAsSet
|
|
39 |
"self debug: #testCollectAsSet"
|
1634
|
40 |
|
1062
|
41 |
self assert: ((#() collectAsSet: [:each | each odd]) = Set new).
|
|
42 |
self assert: (#(1 2 3 4 5 6) collectAsSet: [:each | each odd])
|
|
43 |
= (Set with: true with: false).
|
1634
|
44 |
self assert: (#(1 3 5 7 9 11) collectAsSet: [:each | each odd])
|
1062
|
45 |
= (Set with: true).
|
1634
|
46 |
|
1062
|
47 |
self assert: (#(1 2 3 4 5 4 3 2 1) collectAsSet: [:each | each]) = (1 to: 5) asSet.
|
1634
|
48 |
|
|
49 |
|
1062
|
50 |
!
|
|
51 |
|
|
52 |
testCollectAsSetUsingSymbol
|
|
53 |
"self debug: #testCollectAsSetUsingSymbol"
|
1634
|
54 |
|
1062
|
55 |
self assert: ((#() collectAsSet: #odd) = Set new).
|
|
56 |
self assert: (#(1 2 3 4 5 6) collectAsSet: #odd)
|
|
57 |
= (Set with: true with: false).
|
1634
|
58 |
self assert: (#(1 3 5 7 9 11) collectAsSet: #odd)
|
1062
|
59 |
= (Set with: true).
|
|
60 |
! !
|
|
61 |
|
|
62 |
!CECollectionExtensionTest methodsFor:'flat collect'!
|
|
63 |
|
|
64 |
testFlatCollect
|
|
65 |
"self debug: #testFlatCollect"
|
1634
|
66 |
|
|
67 |
|
1062
|
68 |
!
|
|
69 |
|
|
70 |
testFlatCollectArray
|
|
71 |
"self debug: #testFlatCollectArray"
|
1634
|
72 |
|
1062
|
73 |
self assert: ((#((1 2) (3 4) (5 3)) flatCollect: [ :each | each ]) = #(1 2 3 4 5 3)).
|
1634
|
74 |
self assert: ((#((1 2) (2 3) (1 3 4)) flatCollect: [:each | each]) = #(1 2 2 3 1 3 4)).
|
|
75 |
|
|
76 |
self assert: ((#((1 2) (2 3) () ()) flatCollect: [:each | each]) = #(1 2 2 3)).
|
1062
|
77 |
|
|
78 |
self assert: ((#((1 2) (2 3) (1 3 4)) flatCollect: [:each| Array with: each])
|
|
79 |
= #(#(1 2) #(2 3) #(1 3 4))).
|
1634
|
80 |
|
1062
|
81 |
self assert: ((#((1 2) (2 3) (1 3 4)) flatCollect: [:each| Set with: each])
|
1634
|
82 |
= #(#(1 2) #(2 3) #(1 3 4))).
|
|
83 |
|
1062
|
84 |
!
|
|
85 |
|
|
86 |
testFlatCollectSet
|
1635
|
87 |
"self debug: #testFlatCollectSet"
|
1634
|
88 |
|
1635
|
89 |
self assert: ((#((1 2) (1 2) (1 3 4)) asSet flatCollect: [:each | each]) = #(1 1 2 3 4) asSet).
|
|
90 |
self assert: ((#() asSet flatCollect: [:each | each]) = #() asSet).
|
1062
|
91 |
|
1635
|
92 |
self assert: ((#((1 2) () (1 3 4)) asSet flatCollect: [:each | each]) = #(1 1 2 3 4) asSet).
|
|
93 |
self assert: ((#((1 2) #((99)) (1 3 4)) asSet flatCollect: [:each | each])
|
|
94 |
= #(1 1 2 3 4 (99)) asSet).
|
|
95 |
self assert: ((#((1 2) #(()) (1 3 4)) asSet flatCollect: [:each | each])
|
|
96 |
= #(1 1 2 3 4 ()) asSet).
|
|
97 |
|
|
98 |
"Modified: / 11-07-2017 / 19:51:47 / mawalch"
|
1062
|
99 |
! !
|
|
100 |
|
|
101 |
!CECollectionExtensionTest methodsFor:'flatten'!
|
|
102 |
|
|
103 |
testDeepFlatten
|
|
104 |
|
|
105 |
self assert: #(1 2 3)
|
|
106 |
equals: #((1) (2) (3)) deepFlatten.
|
|
107 |
self assert: #(1 2 3 1 2)
|
|
108 |
equals: #((1 2) (3 1 2)) deepFlatten.
|
|
109 |
!
|
|
110 |
|
|
111 |
testDeepFlattenIsRecursive
|
|
112 |
|
|
113 |
self assert: #(1 2 4 5 3)
|
|
114 |
equals: #((1 2) ((4 5) 3)) deepFlatten.
|
|
115 |
!
|
|
116 |
|
|
117 |
testDeepFlattenOnFlatCollection
|
|
118 |
|
|
119 |
self assert: #(1 2 4)
|
|
120 |
equals: #(1 2 4) deepFlatten.
|
|
121 |
self assert: #(5 3)
|
|
122 |
equals: #(5 ((3))) deepFlatten.
|
|
123 |
!
|
|
124 |
|
|
125 |
testDeepFlattenOnString
|
|
126 |
"don't flatten strings"
|
|
127 |
|
|
128 |
self assert: #(a b c d e)
|
|
129 |
equals: #((a b) ((c d) e)) deepFlatten.
|
|
130 |
self assert: #('foo' 'bar' 'zorg')
|
|
131 |
equals: #(('foo' ('bar')) 'zorg') deepFlatten
|
|
132 |
!
|
|
133 |
|
|
134 |
testFlatten
|
|
135 |
|
|
136 |
self assert: #(1 2 3)
|
|
137 |
equals: #((1) (2) (3)) flatten.
|
|
138 |
self assert: #(1 2 3 1 2)
|
|
139 |
equals: #((1 2) (3 1 2)) flatten.
|
|
140 |
self assert: #(a b (c d))
|
|
141 |
equals: #((a b) ((c d))) flatten.
|
|
142 |
self should: [ #((1 2) 3) flatten ] raise: Error.
|
|
143 |
! !
|
|
144 |
|
|
145 |
!CECollectionExtensionTest methodsFor:'groupedBy'!
|
|
146 |
|
|
147 |
testGroupedBy
|
|
148 |
"self debug: #testGroupedBy"
|
1634
|
149 |
|
1062
|
150 |
| res |
|
1634
|
151 |
res := #(1 2 3 4 5) asOrderedCollection
|
1062
|
152 |
groupedBy: [:each | each odd].
|
|
153 |
self assert: (res at: true) = #(1 3 5) asOrderedCollection.
|
|
154 |
self assert: (res at: false) = #(2 4) asOrderedCollection
|
|
155 |
!
|
|
156 |
|
|
157 |
testGroupedByArray
|
|
158 |
"self debug: #testGroupedByArray"
|
1634
|
159 |
|
1062
|
160 |
| res |
|
|
161 |
res := #(1 2 3 4 5) groupedBy: [:each | each odd].
|
|
162 |
self assert: (res at: true) = #(1 3 5).
|
|
163 |
self assert: (res at: false) = #(2 4)
|
|
164 |
!
|
|
165 |
|
|
166 |
testGroupedBySet
|
|
167 |
"self debug: #testGroupedBySet"
|
1634
|
168 |
|
1062
|
169 |
| res |
|
|
170 |
res := #(1 2 3 4 5 3 4 5) asSet groupedBy: [:each | each odd].
|
|
171 |
self assert: (res at: true) = #(1 3 5) asSet.
|
|
172 |
self assert: (res at: false) = #(2 4) asSet
|
|
173 |
! !
|
|
174 |
|
|
175 |
!CECollectionExtensionTest methodsFor:'symbol - value'!
|
|
176 |
|
|
177 |
testSymbolInPlaceOfBlock
|
|
178 |
"self debug: #testSymbolInPlaceOfBlock"
|
1634
|
179 |
|
1062
|
180 |
self assert: (#(1 2 3 4) collect: #odd) = #(true false true false).
|
|
181 |
self assert: (#(1 2 3 4) select: #odd) = #(1 3).
|
|
182 |
! !
|
|
183 |
|
|
184 |
!CECollectionExtensionTest class methodsFor:'documentation'!
|
|
185 |
|
|
186 |
version
|
|
187 |
^ '$Header$'
|
|
188 |
!
|
|
189 |
|
|
190 |
version_CVS
|
|
191 |
^ '$Header$'
|
|
192 |
! !
|
1635
|
193 |
|