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