compositing-array 1.0.13

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,1226 @@
1
+
2
+ require_relative '../lib/compositing-array.rb'
3
+
4
+ describe ::CompositingArray do
5
+
6
+ ################
7
+ # initialize #
8
+ ################
9
+
10
+ it 'can add initialize with an ancestor, inheriting its values and linking to it as a child' do
11
+
12
+ cascading_composite_array = ::CompositingArray.new
13
+
14
+ cascading_composite_array.instance_variable_get( :@parent_composite_object ).should == nil
15
+ cascading_composite_array.should == [ ]
16
+ cascading_composite_array.push( :A, :B, :C, :D )
17
+
18
+ sub_cascading_composite_array = ::CompositingArray.new( cascading_composite_array )
19
+ sub_cascading_composite_array.instance_variable_get( :@parent_composite_object ).should == cascading_composite_array
20
+ sub_cascading_composite_array.should == [ :A, :B, :C, :D ]
21
+
22
+ end
23
+
24
+ ##################################################################################################
25
+ # private #####################################################################################
26
+ ##################################################################################################
27
+
28
+ ##################################################
29
+ # update_corresponding_index_for_parent_change #
30
+ ##################################################
31
+
32
+ it 'can update tracked parent indices for parent insert/delete' do
33
+
34
+ cascading_composite_array = ::CompositingArray.new
35
+ cascading_composite_array.push( :A, :B )
36
+ sub_cascading_composite_array = ::CompositingArray.new( cascading_composite_array )
37
+
38
+ sub_cascading_composite_array.instance_eval do
39
+ @local_index_for_parent_index[ 0 ].should == 0
40
+ @local_index_for_parent_index[ 1 ].should == 1
41
+ @local_index_for_parent_index[ 2 ].should == nil
42
+ # insert 1 in parent before parent-1
43
+ update_corresponding_index_for_parent_change( 1, 1 )
44
+ @local_index_for_parent_index[ 0 ].should == 0
45
+ # no longer a parent-1 index (has to be set separately)
46
+ @local_index_for_parent_index[ 1 ].should == nil
47
+ # parent-1 is now parent-2
48
+ @local_index_for_parent_index[ 2 ].should == 2
49
+ @parent_and_interpolated_object_count.should == 3
50
+ end
51
+
52
+ end
53
+
54
+ #################################################
55
+ # update_corresponding_index_for_local_change #
56
+ #################################################
57
+
58
+ it 'can update tracked parent indices for local insert/delete' do
59
+
60
+ cascading_composite_array = ::CompositingArray.new
61
+ cascading_composite_array.push( :A, :B )
62
+ sub_cascading_composite_array = ::CompositingArray.new( cascading_composite_array )
63
+
64
+ sub_cascading_composite_array.instance_eval do
65
+ @local_index_for_parent_index[ 0 ].should == 0
66
+ @local_index_for_parent_index[ 1 ].should == 1
67
+ @local_index_for_parent_index[ 2 ].should == nil
68
+ # insert 1 before parent-1
69
+ update_corresponding_index_for_local_change( 1, 1 )
70
+ @local_index_for_parent_index[ 0 ].should == 0
71
+ # new index for parent-1 is 2
72
+ @local_index_for_parent_index[ 1 ].should == 2
73
+ @local_index_for_parent_index[ 2 ].should == nil
74
+ @parent_and_interpolated_object_count.should == 3
75
+ end
76
+
77
+ end
78
+
79
+ ###########################################
80
+ # update_as_sub_array_for_parent_insert #
81
+ ###########################################
82
+
83
+ it 'can handle updating itself as a sub-array when told an insert has occurred in parent' do
84
+
85
+ cascading_composite_array = ::CompositingArray.new
86
+ cascading_composite_array.push( :A, :B )
87
+ sub_cascading_composite_array = ::CompositingArray.new( cascading_composite_array )
88
+
89
+ sub_cascading_composite_array.instance_eval do
90
+ @local_index_for_parent_index[ 0 ].should == 0
91
+ @local_index_for_parent_index[ 1 ].should == 1
92
+ @local_index_for_parent_index[ 2 ].should == nil
93
+ # insert 1 before parent-1
94
+ update_as_sub_array_for_parent_insert( 1, :C )
95
+ @local_index_for_parent_index[ 0 ].should == 0
96
+ # new parent index parent-1 inserted for :C
97
+ @local_index_for_parent_index[ 1 ].should == 1
98
+ # new index for parent-1 is parent-2
99
+ @local_index_for_parent_index[ 2 ].should == 2
100
+ @parent_and_interpolated_object_count.should == 3
101
+ end
102
+
103
+ end
104
+
105
+ ########################################
106
+ # update_as_sub_array_for_parent_set #
107
+ ########################################
108
+
109
+ it 'can handle updating itself as a sub-array when told a set has occurred in parent' do
110
+
111
+ cascading_composite_array = ::CompositingArray.new
112
+ cascading_composite_array.push( :A, :B )
113
+ sub_cascading_composite_array = ::CompositingArray.new( cascading_composite_array )
114
+
115
+ sub_cascading_composite_array.instance_eval do
116
+ @local_index_for_parent_index[ 0 ].should == 0
117
+ @local_index_for_parent_index[ 1 ].should == 1
118
+ @local_index_for_parent_index[ 2 ].should == nil
119
+ # set for parent-1
120
+ update_as_sub_array_for_parent_set( 1, :C )
121
+ @local_index_for_parent_index[ 0 ].should == 0
122
+ @local_index_for_parent_index[ 1 ].should == 1
123
+ @local_index_for_parent_index[ 2 ].should == nil
124
+ @parent_and_interpolated_object_count.should == 2
125
+ end
126
+
127
+ end
128
+
129
+ ###########################################
130
+ # update_as_sub_array_for_parent_delete #
131
+ ###########################################
132
+
133
+ it 'can handle updating itself as a sub-array when told a delete has occurred in parent' do
134
+
135
+ cascading_composite_array = ::CompositingArray.new
136
+ cascading_composite_array.push( :A, :B )
137
+ sub_cascading_composite_array = ::CompositingArray.new( cascading_composite_array )
138
+
139
+ sub_cascading_composite_array.instance_eval do
140
+ @local_index_for_parent_index[ 0 ].should == 0
141
+ @local_index_for_parent_index[ 1 ].should == 1
142
+ @local_index_for_parent_index[ 2 ].should == nil
143
+ # delete parent-1
144
+ update_as_sub_array_for_parent_delete( 1 )
145
+ @local_index_for_parent_index[ 0 ].should == 0
146
+ @local_index_for_parent_index[ 1 ].should == 1
147
+ @local_index_for_parent_index[ 2 ].should == nil
148
+ @parent_and_interpolated_object_count.should == 1
149
+ end
150
+
151
+ end
152
+
153
+ ##################################################################################################
154
+ # public ######################################################################################
155
+ ##################################################################################################
156
+
157
+ #########
158
+ # []= #
159
+ #########
160
+
161
+ it 'can add elements' do
162
+
163
+ cascading_composite_array = ::CompositingArray.new
164
+ sub_cascading_composite_array = ::CompositingArray.new( cascading_composite_array )
165
+
166
+ cascading_composite_array[ 0 ] = :A
167
+ cascading_composite_array.should == [ :A ]
168
+ sub_cascading_composite_array.should == [ :A ]
169
+
170
+ cascading_composite_array[ 1 ] = :B
171
+ cascading_composite_array.should == [ :A, :B ]
172
+ sub_cascading_composite_array.should == [ :A, :B ]
173
+
174
+ sub_cascading_composite_array[ 0 ] = :C
175
+ cascading_composite_array.should == [ :A, :B ]
176
+ sub_cascading_composite_array.should == [ :C, :B ]
177
+
178
+ sub_cascading_composite_array[ 0 ] = :B
179
+ cascading_composite_array.should == [ :A, :B ]
180
+ sub_cascading_composite_array.should == [ :B, :B ]
181
+
182
+ sub_cascading_composite_array[ 2 ] = :C
183
+ cascading_composite_array.should == [ :A, :B ]
184
+ sub_cascading_composite_array.should == [ :B, :B, :C ]
185
+
186
+ cascading_composite_array[ 0 ] = :D
187
+ cascading_composite_array.should == [ :D, :B ]
188
+ sub_cascading_composite_array.should == [ :B, :B, :C ]
189
+
190
+ end
191
+
192
+ ############
193
+ # insert #
194
+ ############
195
+
196
+ it 'can insert elements' do
197
+
198
+ cascading_composite_array = ::CompositingArray.new
199
+ sub_cascading_composite_array = ::CompositingArray.new( cascading_composite_array )
200
+
201
+ cascading_composite_array.insert( 3, :D )
202
+ cascading_composite_array.should == [ nil, nil, nil, :D ]
203
+ sub_cascading_composite_array.should == [ nil, nil, nil, :D ]
204
+
205
+ cascading_composite_array.insert( 1, :B )
206
+ cascading_composite_array.should == [ nil, :B, nil, nil, :D ]
207
+ sub_cascading_composite_array.should == [ nil, :B, nil, nil, :D ]
208
+
209
+ cascading_composite_array.insert( 2, :C )
210
+ cascading_composite_array.should == [ nil, :B, :C, nil, nil, :D ]
211
+ sub_cascading_composite_array.should == [ nil, :B, :C, nil, nil, :D ]
212
+
213
+ sub_cascading_composite_array.insert( 0, :E )
214
+ cascading_composite_array.should == [ nil, :B, :C, nil, nil, :D ]
215
+ sub_cascading_composite_array.should == [ :E, nil, :B, :C, nil, nil, :D ]
216
+
217
+ sub_cascading_composite_array.insert( 4, :F )
218
+ cascading_composite_array.should == [ nil, :B, :C, nil, nil, :D ]
219
+ sub_cascading_composite_array.should == [ :E, nil, :B, :C, :F, nil, nil, :D ]
220
+
221
+ end
222
+
223
+ ##########
224
+ # push #
225
+ # << #
226
+ ##########
227
+
228
+ it 'can add elements' do
229
+
230
+ cascading_composite_array = ::CompositingArray.new
231
+ sub_cascading_composite_array = ::CompositingArray.new( cascading_composite_array )
232
+
233
+ cascading_composite_array << :A
234
+ cascading_composite_array.should == [ :A ]
235
+ sub_cascading_composite_array.should == [ :A ]
236
+
237
+ cascading_composite_array << :B
238
+ cascading_composite_array.should == [ :A, :B ]
239
+ sub_cascading_composite_array.should == [ :A, :B ]
240
+
241
+ sub_cascading_composite_array << :C
242
+ cascading_composite_array.should == [ :A, :B ]
243
+ sub_cascading_composite_array.should == [ :A, :B, :C ]
244
+
245
+ sub_cascading_composite_array << :B
246
+ cascading_composite_array.should == [ :A, :B ]
247
+ sub_cascading_composite_array.should == [ :A, :B, :C, :B ]
248
+
249
+ end
250
+
251
+ ############
252
+ # concat #
253
+ # + #
254
+ ############
255
+
256
+ it 'can add elements' do
257
+
258
+ # NOTE: this breaks + by causing it to modify the array like +=
259
+ # The alternative was worse.
260
+
261
+ cascading_composite_array = ::CompositingArray.new
262
+ sub_cascading_composite_array = ::CompositingArray.new( cascading_composite_array )
263
+
264
+ cascading_composite_array.concat( [ :A ] )
265
+ cascading_composite_array.should == [ :A ]
266
+ sub_cascading_composite_array.should == [ :A ]
267
+
268
+ cascading_composite_array += [ :B ]
269
+ cascading_composite_array.should == [ :A, :B ]
270
+ sub_cascading_composite_array.should == [ :A, :B ]
271
+
272
+ sub_cascading_composite_array.concat( [ :C ] )
273
+ cascading_composite_array.should == [ :A, :B ]
274
+ sub_cascading_composite_array.should == [ :A, :B, :C ]
275
+
276
+ sub_cascading_composite_array += [ :B ]
277
+ cascading_composite_array.should == [ :A, :B ]
278
+ sub_cascading_composite_array.should == [ :A, :B, :C, :B ]
279
+
280
+ end
281
+
282
+ ####################
283
+ # delete_objects #
284
+ ####################
285
+
286
+ it 'can delete multiple elements' do
287
+
288
+ cascading_composite_array = ::CompositingArray.new
289
+ sub_cascading_composite_array = ::CompositingArray.new( cascading_composite_array )
290
+
291
+ cascading_composite_array += [ :A, :B ]
292
+ cascading_composite_array.should == [ :A, :B ]
293
+ sub_cascading_composite_array.should == [ :A, :B ]
294
+
295
+ cascading_composite_array.delete_objects( :A, :B )
296
+ cascading_composite_array.should == [ ]
297
+ sub_cascading_composite_array.should == [ ]
298
+
299
+ sub_cascading_composite_array += [ :B, :C, :D ]
300
+ cascading_composite_array.should == [ ]
301
+ sub_cascading_composite_array.should == [ :B, :C, :D ]
302
+
303
+ sub_cascading_composite_array.delete_objects( :C, :B )
304
+ cascading_composite_array.should == [ ]
305
+ sub_cascading_composite_array.should == [ :D ]
306
+
307
+ end
308
+
309
+ #######
310
+ # - #
311
+ #######
312
+
313
+ it 'can exclude elements' do
314
+
315
+ cascading_composite_array = ::CompositingArray.new
316
+ sub_cascading_composite_array = ::CompositingArray.new( cascading_composite_array )
317
+
318
+ cascading_composite_array.push( :A )
319
+ cascading_composite_array.should == [ :A ]
320
+ sub_cascading_composite_array.should == [ :A ]
321
+
322
+ cascading_composite_array -= [ :A ]
323
+ cascading_composite_array.should == [ ]
324
+ sub_cascading_composite_array.should == [ ]
325
+
326
+ cascading_composite_array.push( :B )
327
+ cascading_composite_array.should == [ :B ]
328
+ sub_cascading_composite_array.should == [ :B ]
329
+
330
+ sub_cascading_composite_array.push( :C )
331
+ cascading_composite_array.should == [ :B ]
332
+ sub_cascading_composite_array.should == [ :B, :C ]
333
+
334
+ sub_cascading_composite_array -= [ :B ]
335
+ cascading_composite_array.should == [ :B ]
336
+ sub_cascading_composite_array.should == [ :C ]
337
+
338
+ end
339
+
340
+ ############
341
+ # delete #
342
+ ############
343
+
344
+ it 'can delete elements' do
345
+
346
+ cascading_composite_array = ::CompositingArray.new
347
+ sub_cascading_composite_array = ::CompositingArray.new( cascading_composite_array )
348
+
349
+ cascading_composite_array.push( :A )
350
+ cascading_composite_array.should == [ :A ]
351
+ sub_cascading_composite_array.should == [ :A ]
352
+
353
+ cascading_composite_array.delete( :A )
354
+ cascading_composite_array.should == [ ]
355
+ sub_cascading_composite_array.should == [ ]
356
+
357
+ cascading_composite_array.push( :B )
358
+ cascading_composite_array.should == [ :B ]
359
+ sub_cascading_composite_array.should == [ :B ]
360
+
361
+ sub_cascading_composite_array.push( :C )
362
+ cascading_composite_array.should == [ :B ]
363
+ sub_cascading_composite_array.should == [ :B, :C ]
364
+
365
+ sub_cascading_composite_array.delete( :B )
366
+ cascading_composite_array.should == [ :B ]
367
+ sub_cascading_composite_array.should == [ :C ]
368
+
369
+ end
370
+
371
+ ###############
372
+ # delete_at #
373
+ ###############
374
+
375
+ it 'can delete by indexes' do
376
+
377
+ cascading_composite_array = ::CompositingArray.new
378
+ sub_cascading_composite_array = ::CompositingArray.new( cascading_composite_array )
379
+
380
+ cascading_composite_array.push( :A )
381
+ cascading_composite_array.should == [ :A ]
382
+ sub_cascading_composite_array.should == [ :A ]
383
+
384
+ cascading_composite_array.delete_at( 0 )
385
+ cascading_composite_array.should == [ ]
386
+ sub_cascading_composite_array.should == [ ]
387
+
388
+ cascading_composite_array.push( :B )
389
+ cascading_composite_array.should == [ :B ]
390
+ sub_cascading_composite_array.should == [ :B ]
391
+
392
+ sub_cascading_composite_array.push( :C )
393
+ cascading_composite_array.should == [ :B ]
394
+ sub_cascading_composite_array.should == [ :B, :C ]
395
+
396
+ sub_cascading_composite_array.delete_at( 0 )
397
+ cascading_composite_array.should == [ :B ]
398
+ sub_cascading_composite_array.should == [ :C ]
399
+
400
+ end
401
+
402
+ #######################
403
+ # delete_at_indexes #
404
+ #######################
405
+
406
+ it 'can delete by indexes' do
407
+
408
+ cascading_composite_array = ::CompositingArray.new
409
+ sub_cascading_composite_array = ::CompositingArray.new( cascading_composite_array )
410
+
411
+ cascading_composite_array.push( :A, :B, :C )
412
+ cascading_composite_array.should == [ :A, :B, :C ]
413
+ sub_cascading_composite_array.should == [ :A, :B, :C ]
414
+
415
+ cascading_composite_array.delete_at_indexes( 0, 1 )
416
+ cascading_composite_array.should == [ :C ]
417
+ sub_cascading_composite_array.should == [ :C ]
418
+
419
+ sub_cascading_composite_array.push( :C, :B )
420
+ cascading_composite_array.should == [ :C ]
421
+ sub_cascading_composite_array.should == [ :C, :C, :B ]
422
+
423
+ sub_cascading_composite_array.delete_at_indexes( 0, 1, 2 )
424
+ cascading_composite_array.should == [ :C ]
425
+ sub_cascading_composite_array.should == [ ]
426
+
427
+ end
428
+
429
+ ###############
430
+ # delete_if #
431
+ ###############
432
+
433
+ it 'can delete by block' do
434
+
435
+ cascading_composite_array = ::CompositingArray.new
436
+ sub_cascading_composite_array = ::CompositingArray.new( cascading_composite_array )
437
+
438
+ cascading_composite_array.push( :A, :B, :C )
439
+ cascading_composite_array.should == [ :A, :B, :C ]
440
+ sub_cascading_composite_array.should == [ :A, :B, :C ]
441
+ cascading_composite_array.delete_if do |object|
442
+ object != :C
443
+ end
444
+ cascading_composite_array.should == [ :C ]
445
+ sub_cascading_composite_array.should == [ :C ]
446
+
447
+ sub_cascading_composite_array.push( :C, :B )
448
+ cascading_composite_array.should == [ :C ]
449
+ sub_cascading_composite_array.should == [ :C, :C, :B ]
450
+ sub_cascading_composite_array.delete_if do |object|
451
+ object != nil
452
+ end
453
+ sub_cascading_composite_array.should == [ ]
454
+ cascading_composite_array.should == [ :C ]
455
+
456
+ cascading_composite_array.delete_if.is_a?( Enumerator ).should == true
457
+
458
+ end
459
+
460
+ #############
461
+ # keep_if #
462
+ #############
463
+
464
+ it 'can keep by block' do
465
+
466
+ cascading_composite_array = ::CompositingArray.new
467
+ sub_cascading_composite_array = ::CompositingArray.new( cascading_composite_array )
468
+
469
+ cascading_composite_array.push( :A, :B, :C )
470
+ cascading_composite_array.should == [ :A, :B, :C ]
471
+ sub_cascading_composite_array.should == [ :A, :B, :C ]
472
+ cascading_composite_array.keep_if do |object|
473
+ object == :C
474
+ end
475
+ cascading_composite_array.should == [ :C ]
476
+ sub_cascading_composite_array.should == [ :C ]
477
+
478
+ sub_cascading_composite_array.push( :C, :B )
479
+ cascading_composite_array.should == [ :C ]
480
+ sub_cascading_composite_array.should == [ :C, :C, :B ]
481
+ sub_cascading_composite_array.keep_if do |object|
482
+ object == nil
483
+ end
484
+ cascading_composite_array.should == [ :C ]
485
+ sub_cascading_composite_array.should == [ ]
486
+
487
+ end
488
+
489
+ ##############
490
+ # compact! #
491
+ ##############
492
+
493
+ it 'can compact' do
494
+
495
+ cascading_composite_array = ::CompositingArray.new
496
+ sub_cascading_composite_array = ::CompositingArray.new( cascading_composite_array )
497
+
498
+ cascading_composite_array.push( :A, nil, :B, nil, :C, nil )
499
+ cascading_composite_array.should == [ :A, nil, :B, nil, :C, nil ]
500
+ sub_cascading_composite_array.should == [ :A, nil, :B, nil, :C, nil ]
501
+ cascading_composite_array.compact!
502
+ cascading_composite_array.should == [ :A, :B, :C ]
503
+ sub_cascading_composite_array.should == [ :A, :B, :C ]
504
+
505
+ sub_cascading_composite_array.push( nil, nil, :D )
506
+ cascading_composite_array.should == [ :A, :B, :C ]
507
+ sub_cascading_composite_array.should == [ :A, :B, :C, nil, nil, :D ]
508
+ sub_cascading_composite_array.compact!
509
+ cascading_composite_array.should == [ :A, :B, :C ]
510
+ sub_cascading_composite_array.should == [ :A, :B, :C, :D ]
511
+
512
+ end
513
+
514
+ ##############
515
+ # flatten! #
516
+ ##############
517
+
518
+ it 'can flatten' do
519
+
520
+ cascading_composite_array = ::CompositingArray.new
521
+ sub_cascading_composite_array = ::CompositingArray.new( cascading_composite_array )
522
+
523
+ cascading_composite_array.push( :A, [ :F_A, :F_B ], :B, [ :F_C ], :C, [ :F_D ], [ :F_E ] )
524
+ cascading_composite_array.should == [ :A, [ :F_A, :F_B ], :B, [ :F_C ], :C, [ :F_D ], [ :F_E ] ]
525
+ sub_cascading_composite_array.should == [ :A, [ :F_A, :F_B ], :B, [ :F_C ], :C, [ :F_D ], [ :F_E ] ]
526
+ cascading_composite_array.flatten!
527
+ cascading_composite_array.should == [ :A, :F_A, :F_B, :B, :F_C, :C, :F_D, :F_E ]
528
+ sub_cascading_composite_array.should == [ :A, :F_A, :F_B, :B, :F_C, :C, :F_D, :F_E ]
529
+
530
+ sub_cascading_composite_array.push( [ :F_F, :F_G ], :D, [ :F_H ] )
531
+ cascading_composite_array.should == [ :A, :F_A, :F_B, :B, :F_C, :C, :F_D, :F_E ]
532
+ sub_cascading_composite_array.should == [ :A, :F_A, :F_B, :B, :F_C, :C, :F_D, :F_E, [ :F_F, :F_G ], :D, [ :F_H ] ]
533
+ sub_cascading_composite_array.flatten!
534
+ cascading_composite_array.should == [ :A, :F_A, :F_B, :B, :F_C, :C, :F_D, :F_E ]
535
+ sub_cascading_composite_array.should == [ :A, :F_A, :F_B, :B, :F_C, :C, :F_D, :F_E, :F_F, :F_G, :D, :F_H ]
536
+
537
+ end
538
+
539
+ #############
540
+ # reject! #
541
+ #############
542
+
543
+ it 'can reject' do
544
+
545
+ cascading_composite_array = ::CompositingArray.new
546
+ sub_cascading_composite_array = ::CompositingArray.new( cascading_composite_array )
547
+
548
+ cascading_composite_array.push( :A, :B, :C )
549
+ cascading_composite_array.should == [ :A, :B, :C ]
550
+ sub_cascading_composite_array.should == [ :A, :B, :C ]
551
+ cascading_composite_array.reject! do |object|
552
+ object != :C
553
+ end
554
+ cascading_composite_array.should == [ :C ]
555
+ sub_cascading_composite_array.should == [ :C ]
556
+
557
+ sub_cascading_composite_array.push( :C, :B )
558
+ cascading_composite_array.should == [ :C ]
559
+ sub_cascading_composite_array.should == [ :C, :C, :B ]
560
+ sub_cascading_composite_array.reject! do |object|
561
+ object != nil
562
+ end
563
+ sub_cascading_composite_array.should == [ ]
564
+ cascading_composite_array.should == [ :C ]
565
+
566
+ cascading_composite_array.reject!.is_a?( Enumerator ).should == true
567
+
568
+ end
569
+
570
+ #############
571
+ # replace #
572
+ #############
573
+
574
+ it 'can replace self' do
575
+
576
+ cascading_composite_array = ::CompositingArray.new
577
+ sub_cascading_composite_array = ::CompositingArray.new( cascading_composite_array )
578
+
579
+ cascading_composite_array.push( :A, :B, :C )
580
+ cascading_composite_array.should == [ :A, :B, :C ]
581
+ sub_cascading_composite_array.should == [ :A, :B, :C ]
582
+ cascading_composite_array.replace( [ :D, :E, :F ] )
583
+ cascading_composite_array.should == [ :D, :E, :F ]
584
+ sub_cascading_composite_array.should == [ :D, :E, :F ]
585
+
586
+ cascading_composite_array.should == [ :D, :E, :F ]
587
+ sub_cascading_composite_array.should == [ :D, :E, :F ]
588
+ sub_cascading_composite_array.replace( [ :G, :H, :I ] )
589
+ cascading_composite_array.should == [ :D, :E, :F ]
590
+ sub_cascading_composite_array.should == [ :G, :H, :I ]
591
+
592
+ end
593
+
594
+ ##############
595
+ # reverse! #
596
+ ##############
597
+
598
+ it 'can reverse self' do
599
+
600
+ cascading_composite_array = ::CompositingArray.new
601
+ sub_cascading_composite_array = ::CompositingArray.new( cascading_composite_array )
602
+
603
+ cascading_composite_array.push( :A, :B, :C )
604
+ cascading_composite_array.should == [ :A, :B, :C ]
605
+ sub_cascading_composite_array.should == [ :A, :B, :C ]
606
+ cascading_composite_array.reverse!
607
+ cascading_composite_array.should == [ :C, :B, :A ]
608
+ sub_cascading_composite_array.should == [ :C, :B, :A ]
609
+
610
+ cascading_composite_array.should == [ :C, :B, :A ]
611
+ sub_cascading_composite_array.should == [ :C, :B, :A ]
612
+ sub_cascading_composite_array.reverse!
613
+ cascading_composite_array.should == [ :C, :B, :A ]
614
+ sub_cascading_composite_array.should == [ :A, :B, :C ]
615
+
616
+ end
617
+
618
+ #############
619
+ # rotate! #
620
+ #############
621
+
622
+ it 'can rotate self' do
623
+
624
+ cascading_composite_array = ::CompositingArray.new
625
+ sub_cascading_composite_array = ::CompositingArray.new( cascading_composite_array )
626
+
627
+ cascading_composite_array.push( :A, :B, :C )
628
+ cascading_composite_array.should == [ :A, :B, :C ]
629
+ sub_cascading_composite_array.should == [ :A, :B, :C ]
630
+
631
+ cascading_composite_array.rotate!
632
+ cascading_composite_array.should == [ :B, :C, :A ]
633
+ sub_cascading_composite_array.should == [ :B, :C, :A ]
634
+
635
+ cascading_composite_array.rotate!( -1 )
636
+ cascading_composite_array.should == [ :A, :B, :C ]
637
+ sub_cascading_composite_array.should == [ :A, :B, :C ]
638
+
639
+ sub_cascading_composite_array.rotate!( 2 )
640
+ cascading_composite_array.should == [ :A, :B, :C ]
641
+ sub_cascading_composite_array.should == [ :C, :A, :B ]
642
+
643
+ end
644
+
645
+ #############
646
+ # select! #
647
+ #############
648
+
649
+ it 'can keep by select' do
650
+
651
+ cascading_composite_array = ::CompositingArray.new
652
+ sub_cascading_composite_array = ::CompositingArray.new( cascading_composite_array )
653
+
654
+ cascading_composite_array.push( :A, :B, :C )
655
+ cascading_composite_array.should == [ :A, :B, :C ]
656
+ sub_cascading_composite_array.should == [ :A, :B, :C ]
657
+ cascading_composite_array.select! do |object|
658
+ object == :C
659
+ end
660
+ cascading_composite_array.should == [ :C ]
661
+ sub_cascading_composite_array.should == [ :C ]
662
+
663
+ sub_cascading_composite_array.push( :C, :B )
664
+ cascading_composite_array.should == [ :C ]
665
+ sub_cascading_composite_array.should == [ :C, :C, :B ]
666
+ sub_cascading_composite_array.select! do |object|
667
+ object == nil
668
+ end
669
+ cascading_composite_array.should == [ :C ]
670
+ sub_cascading_composite_array.should == [ ]
671
+
672
+ cascading_composite_array.select!.is_a?( Enumerator ).should == true
673
+
674
+ end
675
+
676
+ ##############
677
+ # shuffle! #
678
+ ##############
679
+
680
+ it 'can shuffle self' do
681
+
682
+ cascading_composite_array = ::CompositingArray.new
683
+ sub_cascading_composite_array = ::CompositingArray.new( cascading_composite_array )
684
+
685
+ cascading_composite_array.push( :A, :B, :C )
686
+ cascading_composite_array.should == [ :A, :B, :C ]
687
+ sub_cascading_composite_array.should == cascading_composite_array
688
+
689
+ prior_version = cascading_composite_array.dup
690
+ attempts = [ ]
691
+ 50.times do
692
+ cascading_composite_array.shuffle!
693
+ attempts.push( cascading_composite_array == prior_version )
694
+ prior_version = cascading_composite_array.dup
695
+ end
696
+ attempts_correct = attempts.select { |member| member == false }.count
697
+ ( attempts_correct >= 10 ).should == true
698
+ sub_cascading_composite_array.should == cascading_composite_array
699
+ first_shuffle_version = cascading_composite_array
700
+
701
+ cascading_composite_array.should == first_shuffle_version
702
+ attempts = [ ]
703
+ 50.times do
704
+ sub_cascading_composite_array.shuffle!
705
+ attempts.push( sub_cascading_composite_array == cascading_composite_array )
706
+ prior_version = cascading_composite_array.dup
707
+ end
708
+ attempts_correct = attempts.select { |member| member == false }.count
709
+ ( attempts_correct >= 10 ).should == true
710
+
711
+ end
712
+
713
+ ##############
714
+ # collect! #
715
+ # map! #
716
+ ##############
717
+
718
+ it 'can replace by collect/map' do
719
+
720
+ cascading_composite_array = ::CompositingArray.new
721
+ sub_cascading_composite_array = ::CompositingArray.new( cascading_composite_array )
722
+
723
+ cascading_composite_array.push( :A, :B, :C )
724
+ cascading_composite_array.should == [ :A, :B, :C ]
725
+ sub_cascading_composite_array.should == [ :A, :B, :C ]
726
+ cascading_composite_array.collect! do |object|
727
+ :C
728
+ end
729
+ cascading_composite_array.should == [ :C, :C, :C ]
730
+ sub_cascading_composite_array.should == [ :C, :C, :C ]
731
+
732
+ sub_cascading_composite_array.collect! do |object|
733
+ :A
734
+ end
735
+ cascading_composite_array.should == [ :C, :C, :C ]
736
+ sub_cascading_composite_array.should == [ :A, :A, :A ]
737
+
738
+ cascading_composite_array.collect!.is_a?( Enumerator ).should == true
739
+
740
+ end
741
+
742
+ ###########
743
+ # sort! #
744
+ ###########
745
+
746
+ it 'can replace by collect/map' do
747
+
748
+ cascading_composite_array = ::CompositingArray.new
749
+ sub_cascading_composite_array = ::CompositingArray.new( cascading_composite_array )
750
+
751
+ cascading_composite_array.push( :A, :B, :C )
752
+ cascading_composite_array.should == [ :A, :B, :C ]
753
+ sub_cascading_composite_array.should == [ :A, :B, :C ]
754
+ cascading_composite_array.sort! do |a, b|
755
+ if a < b
756
+ 1
757
+ elsif a > b
758
+ -1
759
+ elsif a == b
760
+ 0
761
+ end
762
+ end
763
+ cascading_composite_array.should == [ :C, :B, :A ]
764
+ sub_cascading_composite_array.should == [ :C, :B, :A ]
765
+
766
+ sub_cascading_composite_array.sort! do |a, b|
767
+ if a < b
768
+ -1
769
+ elsif a > b
770
+ 1
771
+ elsif a == b
772
+ 0
773
+ end
774
+ end
775
+ cascading_composite_array.should == [ :C, :B, :A ]
776
+ sub_cascading_composite_array.should == [ :A, :B, :C ]
777
+
778
+ cascading_composite_array.sort!
779
+ cascading_composite_array.should == [ :A, :B, :C ]
780
+ sub_cascading_composite_array.should == [ :A, :B, :C ]
781
+
782
+ end
783
+
784
+ ##############
785
+ # sort_by! #
786
+ ##############
787
+
788
+ it 'can replace by collect/map' do
789
+
790
+ cascading_composite_array = ::CompositingArray.new
791
+ sub_cascading_composite_array = ::CompositingArray.new( cascading_composite_array )
792
+
793
+ cascading_composite_array.push( :A, :B, :C )
794
+ cascading_composite_array.should == [ :A, :B, :C ]
795
+ sub_cascading_composite_array.should == [ :A, :B, :C ]
796
+ cascading_composite_array.sort_by! do |object|
797
+ case object
798
+ when :A
799
+ :B
800
+ when :B
801
+ :A
802
+ when :C
803
+ :C
804
+ end
805
+ end
806
+ cascading_composite_array.should == [ :B, :A, :C ]
807
+ sub_cascading_composite_array.should == [ :B, :A, :C ]
808
+
809
+ sub_cascading_composite_array.sort_by! do |object|
810
+ case object
811
+ when :A
812
+ :C
813
+ when :B
814
+ :B
815
+ when :C
816
+ :A
817
+ end
818
+ end
819
+ cascading_composite_array.should == [ :B, :A, :C ]
820
+ sub_cascading_composite_array.should == [ :C, :B, :A ]
821
+
822
+ cascading_composite_array.sort_by!.is_a?( Enumerator ).should == true
823
+
824
+ end
825
+
826
+ ###########
827
+ # uniq! #
828
+ ###########
829
+
830
+ it 'can remove non-unique elements' do
831
+
832
+ cascading_composite_array = ::CompositingArray.new
833
+ sub_cascading_composite_array = ::CompositingArray.new( cascading_composite_array )
834
+
835
+ cascading_composite_array.push( :A, :B, :C, :C, :C, :B, :A )
836
+ cascading_composite_array.should == [ :A, :B, :C, :C, :C, :B, :A ]
837
+ sub_cascading_composite_array.should == [ :A, :B, :C, :C, :C, :B, :A ]
838
+ cascading_composite_array.uniq!
839
+ cascading_composite_array.should == [ :A, :B, :C ]
840
+ sub_cascading_composite_array.should == [ :A, :B, :C ]
841
+
842
+ sub_cascading_composite_array.push( :C, :B )
843
+ cascading_composite_array.should == [ :A, :B, :C ]
844
+ sub_cascading_composite_array.should == [ :A, :B, :C, :C, :B ]
845
+ sub_cascading_composite_array.uniq!
846
+ cascading_composite_array.should == [ :A, :B, :C ]
847
+ sub_cascading_composite_array.should == [ :A, :B, :C ]
848
+
849
+ end
850
+
851
+ #############
852
+ # unshift #
853
+ #############
854
+
855
+ it 'can unshift onto the first element' do
856
+
857
+ cascading_composite_array = ::CompositingArray.new
858
+ sub_cascading_composite_array = ::CompositingArray.new( cascading_composite_array )
859
+
860
+ cascading_composite_array += :A
861
+ cascading_composite_array.should == [ :A ]
862
+ sub_cascading_composite_array.should == [ :A ]
863
+
864
+ cascading_composite_array.unshift( :B )
865
+ cascading_composite_array.should == [ :B, :A ]
866
+ sub_cascading_composite_array.should == [ :B, :A ]
867
+
868
+ sub_cascading_composite_array.unshift( :C )
869
+ cascading_composite_array.should == [ :B, :A ]
870
+ sub_cascading_composite_array.should == [ :C, :B, :A ]
871
+
872
+ end
873
+
874
+ #########
875
+ # pop #
876
+ #########
877
+
878
+ it 'can pop the final element' do
879
+
880
+ cascading_composite_array = ::CompositingArray.new
881
+ sub_cascading_composite_array = ::CompositingArray.new( cascading_composite_array )
882
+
883
+ cascading_composite_array += :A
884
+ cascading_composite_array.should == [ :A ]
885
+ sub_cascading_composite_array.should == [ :A ]
886
+
887
+ cascading_composite_array.pop.should == :A
888
+ cascading_composite_array.should == [ ]
889
+ sub_cascading_composite_array.should == [ ]
890
+
891
+ cascading_composite_array += :B
892
+ cascading_composite_array.should == [ :B ]
893
+ sub_cascading_composite_array.should == [ :B ]
894
+
895
+ sub_cascading_composite_array += :C
896
+ cascading_composite_array.should == [ :B ]
897
+ sub_cascading_composite_array.should == [ :B, :C ]
898
+ sub_cascading_composite_array.pop.should == :C
899
+ cascading_composite_array.should == [ :B ]
900
+ sub_cascading_composite_array.should == [ :B ]
901
+
902
+ end
903
+
904
+ ###########
905
+ # shift #
906
+ ###########
907
+
908
+ it 'can shift the first element' do
909
+
910
+ cascading_composite_array = ::CompositingArray.new
911
+ sub_cascading_composite_array = ::CompositingArray.new( cascading_composite_array )
912
+
913
+ cascading_composite_array += :A
914
+ cascading_composite_array.should == [ :A ]
915
+ sub_cascading_composite_array.should == [ :A ]
916
+
917
+ cascading_composite_array.shift.should == :A
918
+ cascading_composite_array.should == [ ]
919
+ sub_cascading_composite_array.should == [ ]
920
+
921
+ cascading_composite_array += :B
922
+ cascading_composite_array.should == [ :B ]
923
+ sub_cascading_composite_array.should == [ :B ]
924
+
925
+ sub_cascading_composite_array += :C
926
+ cascading_composite_array.should == [ :B ]
927
+ sub_cascading_composite_array.should == [ :B, :C ]
928
+ sub_cascading_composite_array.shift.should == :B
929
+ cascading_composite_array.should == [ :B ]
930
+ sub_cascading_composite_array.should == [ :C ]
931
+
932
+ end
933
+
934
+ ############
935
+ # slice! #
936
+ ############
937
+
938
+ it 'can slice elements' do
939
+
940
+ cascading_composite_array = ::CompositingArray.new
941
+ sub_cascading_composite_array = ::CompositingArray.new( cascading_composite_array )
942
+
943
+ cascading_composite_array += :A
944
+ cascading_composite_array.should == [ :A ]
945
+ sub_cascading_composite_array.should == [ :A ]
946
+
947
+ cascading_composite_array.slice!( 0, 1 ).should == [ :A ]
948
+ cascading_composite_array.should == [ ]
949
+ sub_cascading_composite_array.should == [ ]
950
+
951
+ cascading_composite_array += :B
952
+ cascading_composite_array.should == [ :B ]
953
+ sub_cascading_composite_array.should == [ :B ]
954
+
955
+ sub_cascading_composite_array += :C
956
+ cascading_composite_array.should == [ :B ]
957
+ sub_cascading_composite_array.should == [ :B, :C ]
958
+
959
+ sub_cascading_composite_array.slice!( 0, 1 ).should == [ :B ]
960
+ cascading_composite_array.should == [ :B ]
961
+ sub_cascading_composite_array.should == [ :C ]
962
+
963
+ end
964
+
965
+ ###########
966
+ # clear #
967
+ ###########
968
+
969
+ it 'can clear, causing present elements to be excluded' do
970
+
971
+ cascading_composite_array = ::CompositingArray.new
972
+ sub_cascading_composite_array = ::CompositingArray.new( cascading_composite_array )
973
+
974
+ cascading_composite_array += :A
975
+ cascading_composite_array.should == [ :A ]
976
+ sub_cascading_composite_array.should == [ :A ]
977
+
978
+ cascading_composite_array.clear
979
+ cascading_composite_array.should == [ ]
980
+ sub_cascading_composite_array.should == [ ]
981
+
982
+ cascading_composite_array += :B
983
+ cascading_composite_array.should == [ :B ]
984
+ sub_cascading_composite_array.should == [ :B ]
985
+
986
+ sub_cascading_composite_array += :C
987
+ cascading_composite_array.should == [ :B ]
988
+ sub_cascading_composite_array.should == [ :B, :C ]
989
+
990
+ sub_cascading_composite_array.clear
991
+ cascading_composite_array.should == [ :B ]
992
+ sub_cascading_composite_array.should == [ ]
993
+
994
+ end
995
+
996
+ ##################
997
+ # pre_set_hook #
998
+ ##################
999
+
1000
+ it 'has a hook that is called before setting a value; return value is used in place of object' do
1001
+
1002
+ class ::CompositingArray::SubMockPreSet < ::CompositingArray
1003
+
1004
+ def pre_set_hook( index, object, is_insert = false )
1005
+ return :some_other_value
1006
+ end
1007
+
1008
+ end
1009
+
1010
+ cascading_composite_array = ::CompositingArray::SubMockPreSet.new
1011
+
1012
+ cascading_composite_array.push( :some_value )
1013
+
1014
+ cascading_composite_array.should == [ :some_other_value ]
1015
+
1016
+ end
1017
+
1018
+ ###################
1019
+ # post_set_hook #
1020
+ ###################
1021
+
1022
+ it 'has a hook that is called after setting a value' do
1023
+
1024
+ class ::CompositingArray::SubMockPostSet < ::CompositingArray
1025
+
1026
+ def post_set_hook( index, object, is_insert = false )
1027
+ return :some_other_value
1028
+ end
1029
+
1030
+ end
1031
+
1032
+ cascading_composite_array = ::CompositingArray::SubMockPostSet.new
1033
+
1034
+ cascading_composite_array.push( :some_value ).should == [ :some_other_value ]
1035
+
1036
+ cascading_composite_array.should == [ :some_value ]
1037
+
1038
+ end
1039
+
1040
+ ##################
1041
+ # pre_get_hook #
1042
+ ##################
1043
+
1044
+ it 'has a hook that is called before getting a value; if return value is false, get does not occur' do
1045
+
1046
+ class ::CompositingArray::SubMockPreGet < ::CompositingArray
1047
+
1048
+ def pre_get_hook( index )
1049
+ return false
1050
+ end
1051
+
1052
+ end
1053
+
1054
+ cascading_composite_array = ::CompositingArray::SubMockPreGet.new
1055
+
1056
+ cascading_composite_array.push( :some_value )
1057
+ cascading_composite_array[ 0 ].should == nil
1058
+
1059
+ cascading_composite_array.should == [ :some_value ]
1060
+
1061
+ end
1062
+
1063
+ ###################
1064
+ # post_get_hook #
1065
+ ###################
1066
+
1067
+ it 'has a hook that is called after getting a value' do
1068
+
1069
+ class ::CompositingArray::SubMockPostGet < ::CompositingArray
1070
+
1071
+ def post_get_hook( index, object )
1072
+ return :some_other_value
1073
+ end
1074
+
1075
+ end
1076
+
1077
+ cascading_composite_array = ::CompositingArray::SubMockPostGet.new
1078
+
1079
+ cascading_composite_array.push( :some_value )
1080
+ cascading_composite_array[ 0 ].should == :some_other_value
1081
+
1082
+ cascading_composite_array.should == [ :some_value ]
1083
+
1084
+ end
1085
+
1086
+ #####################
1087
+ # pre_delete_hook #
1088
+ #####################
1089
+
1090
+ it 'has a hook that is called before deleting an index; if return value is false, delete does not occur' do
1091
+
1092
+ class ::CompositingArray::SubMockPreDelete < ::CompositingArray
1093
+
1094
+ def pre_delete_hook( index )
1095
+ return false
1096
+ end
1097
+
1098
+ end
1099
+
1100
+ cascading_composite_array = ::CompositingArray::SubMockPreDelete.new
1101
+
1102
+ cascading_composite_array.push( :some_value )
1103
+ cascading_composite_array.delete_at( 0 )
1104
+
1105
+ cascading_composite_array.should == [ :some_value ]
1106
+
1107
+ end
1108
+
1109
+ ######################
1110
+ # post_delete_hook #
1111
+ ######################
1112
+
1113
+ it 'has a hook that is called after deleting an index' do
1114
+
1115
+ class ::CompositingArray::SubMockPostDelete < ::CompositingArray
1116
+
1117
+ def post_delete_hook( index, object )
1118
+ return :some_other_value
1119
+ end
1120
+
1121
+ end
1122
+
1123
+ cascading_composite_array = ::CompositingArray::SubMockPostDelete.new
1124
+
1125
+ cascading_composite_array.push( :some_value )
1126
+ cascading_composite_array.delete_at( 0 ).should == :some_other_value
1127
+
1128
+ cascading_composite_array.should == [ ]
1129
+
1130
+ end
1131
+
1132
+ ########################
1133
+ # child_pre_set_hook #
1134
+ ########################
1135
+
1136
+ it 'has a hook that is called before setting a value that has been passed by a parent; return value is used in place of object' do
1137
+
1138
+ class ::CompositingArray::SubMockChildPreSet < ::CompositingArray
1139
+
1140
+ def child_pre_set_hook( index, object, is_insert = false )
1141
+ return :some_other_value
1142
+ end
1143
+
1144
+ end
1145
+
1146
+ cascading_composite_array = ::CompositingArray::SubMockChildPreSet.new
1147
+ sub_cascading_composite_array = ::CompositingArray::SubMockChildPreSet.new( cascading_composite_array )
1148
+ cascading_composite_array.push( :some_value )
1149
+
1150
+ sub_cascading_composite_array.should == [ :some_other_value ]
1151
+
1152
+ end
1153
+
1154
+ #########################
1155
+ # child_post_set_hook #
1156
+ #########################
1157
+
1158
+ it 'has a hook that is called after setting a value passed by a parent' do
1159
+
1160
+ class ::CompositingArray::SubMockChildPostSet < ::CompositingArray
1161
+
1162
+ def child_post_set_hook( index, object, is_insert = false )
1163
+ push( :some_other_value )
1164
+ end
1165
+
1166
+ end
1167
+
1168
+ cascading_composite_array = ::CompositingArray::SubMockChildPostSet.new
1169
+ sub_cascading_composite_array = ::CompositingArray::SubMockChildPostSet.new( cascading_composite_array )
1170
+ cascading_composite_array.push( :some_value )
1171
+
1172
+ cascading_composite_array.should == [ :some_value ]
1173
+ sub_cascading_composite_array.should == [ :some_value, :some_other_value ]
1174
+
1175
+ end
1176
+
1177
+ ###########################
1178
+ # child_pre_delete_hook #
1179
+ ###########################
1180
+
1181
+ it 'has a hook that is called before deleting an index that has been passed by a parent; if return value is false, delete does not occur' do
1182
+
1183
+ class ::CompositingArray::SubMockChildPreDelete < ::CompositingArray
1184
+
1185
+ def child_pre_delete_hook( index )
1186
+ false
1187
+ end
1188
+
1189
+ end
1190
+
1191
+ cascading_composite_array = ::CompositingArray::SubMockChildPreDelete.new
1192
+ sub_cascading_composite_array = ::CompositingArray::SubMockChildPreDelete.new( cascading_composite_array )
1193
+ cascading_composite_array.push( :some_value )
1194
+ cascading_composite_array.delete( :some_value )
1195
+
1196
+ cascading_composite_array.should == [ ]
1197
+ sub_cascading_composite_array.should == [ :some_value ]
1198
+
1199
+ end
1200
+
1201
+ ############################
1202
+ # child_post_delete_hook #
1203
+ ############################
1204
+
1205
+ it 'has a hook that is called after deleting an index passed by a parent' do
1206
+
1207
+ class ::CompositingArray::SubMockChildPostDelete < ::CompositingArray
1208
+
1209
+ def child_post_delete_hook( index, object )
1210
+ delete( :some_other_value )
1211
+ end
1212
+
1213
+ end
1214
+
1215
+ cascading_composite_array = ::CompositingArray::SubMockChildPostDelete.new
1216
+ sub_cascading_composite_array = ::CompositingArray::SubMockChildPostDelete.new( cascading_composite_array )
1217
+ cascading_composite_array.push( :some_value )
1218
+ sub_cascading_composite_array.push( :some_other_value )
1219
+ cascading_composite_array.delete( :some_value )
1220
+
1221
+ cascading_composite_array.should == [ ]
1222
+ sub_cascading_composite_array.should == [ ]
1223
+
1224
+ end
1225
+
1226
+ end