thorero 0.9.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. data/History.txt +1 -0
  2. data/LICENSE +20 -0
  3. data/Manifest +45 -0
  4. data/Manifest.txt +29 -0
  5. data/README.txt +3 -0
  6. data/Rakefile +180 -0
  7. data/lib/extlib.rb +32 -0
  8. data/lib/extlib/assertions.rb +8 -0
  9. data/lib/extlib/blank.rb +42 -0
  10. data/lib/extlib/class.rb +175 -0
  11. data/lib/extlib/hash.rb +410 -0
  12. data/lib/extlib/hook.rb +366 -0
  13. data/lib/extlib/inflection.rb +141 -0
  14. data/lib/extlib/lazy_array.rb +106 -0
  15. data/lib/extlib/logger.rb +202 -0
  16. data/lib/extlib/mash.rb +143 -0
  17. data/lib/extlib/module.rb +37 -0
  18. data/lib/extlib/object.rb +165 -0
  19. data/lib/extlib/object_space.rb +13 -0
  20. data/lib/extlib/pathname.rb +5 -0
  21. data/lib/extlib/pooling.rb +233 -0
  22. data/lib/extlib/rubygems.rb +38 -0
  23. data/lib/extlib/simple_set.rb +39 -0
  24. data/lib/extlib/string.rb +132 -0
  25. data/lib/extlib/struct.rb +8 -0
  26. data/lib/extlib/tasks/release.rb +11 -0
  27. data/lib/extlib/time.rb +12 -0
  28. data/lib/extlib/version.rb +3 -0
  29. data/lib/extlib/virtual_file.rb +10 -0
  30. data/spec/blank_spec.rb +85 -0
  31. data/spec/hash_spec.rb +524 -0
  32. data/spec/hook_spec.rb +1198 -0
  33. data/spec/inflection_spec.rb +50 -0
  34. data/spec/lazy_array_spec.rb +896 -0
  35. data/spec/mash_spec.rb +244 -0
  36. data/spec/module_spec.rb +58 -0
  37. data/spec/object_space_spec.rb +9 -0
  38. data/spec/object_spec.rb +98 -0
  39. data/spec/pooling_spec.rb +486 -0
  40. data/spec/simple_set_spec.rb +26 -0
  41. data/spec/spec_helper.rb +8 -0
  42. data/spec/string_spec.rb +200 -0
  43. data/spec/struct_spec.rb +12 -0
  44. data/spec/time_spec.rb +16 -0
  45. data/spec/virtual_file_spec.rb +21 -0
  46. data/thorero.gemspec +147 -0
  47. metadata +146 -0
@@ -0,0 +1,50 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
2
+
3
+ describe Extlib::Inflection do
4
+
5
+ it 'should pluralize a word' do
6
+ 'car'.plural.should == 'cars'
7
+ Extlib::Inflection.pluralize('car').should == 'cars'
8
+ end
9
+
10
+ it 'should singularize a word' do
11
+ "cars".singular.should == "car"
12
+ Extlib::Inflection.singularize('cars').should == 'car'
13
+ end
14
+
15
+ it 'should classify an underscored name' do
16
+ Extlib::Inflection.classify('data_mapper').should == 'DataMapper'
17
+ end
18
+
19
+ it 'should camelize an underscored name' do
20
+ Extlib::Inflection.camelize('data_mapper').should == 'DataMapper'
21
+ end
22
+
23
+ it 'should underscore a camelized name' do
24
+ Extlib::Inflection.underscore('DataMapper').should == 'data_mapper'
25
+ end
26
+
27
+ it 'should humanize names' do
28
+ Extlib::Inflection.humanize('employee_salary').should == 'Employee salary'
29
+ Extlib::Inflection.humanize('author_id').should == 'Author'
30
+ end
31
+
32
+ it 'should demodulize a module name' do
33
+ Extlib::Inflection.demodulize('DataMapper::Inflector').should == 'Inflector'
34
+ end
35
+
36
+ it 'should tableize a name (underscore with last word plural)' do
37
+ Extlib::Inflection.tableize('fancy_category').should == 'fancy_categories'
38
+ Extlib::Inflection.tableize('FancyCategory').should == 'fancy_categories'
39
+ end
40
+
41
+ it 'should create a fk name from a class name' do
42
+ Extlib::Inflection.foreign_key('Message').should == 'message_id'
43
+ Extlib::Inflection.foreign_key('Admin::Post').should == 'post_id'
44
+ end
45
+
46
+
47
+
48
+
49
+
50
+ end
@@ -0,0 +1,896 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
2
+
3
+ describe LazyArray do
4
+ def self.it_should_return_self(method)
5
+ it 'should delegate to the array and return self' do
6
+ LazyArray::RETURN_SELF.should include(method)
7
+ end
8
+ end
9
+
10
+ def self.it_should_return_plain(method)
11
+ it 'should delegate to the array and return the results directly' do
12
+ LazyArray::RETURN_SELF.should_not include(method)
13
+ end
14
+ end
15
+
16
+ before do
17
+ @nancy = 'nancy'
18
+ @bessie = 'bessie'
19
+ @steve = 'steve'
20
+
21
+ @lazy_array = LazyArray.new
22
+ @lazy_array.load_with { |la| la.push(@nancy, @bessie) }
23
+
24
+ @other = LazyArray.new
25
+ @other.load_with { |la| la.push(@steve) }
26
+ end
27
+
28
+ it 'should provide #at' do
29
+ @lazy_array.should respond_to(:at)
30
+ end
31
+
32
+ describe '#at' do
33
+ it_should_return_plain(:at)
34
+
35
+ it 'should lookup the entry by index' do
36
+ @lazy_array.at(0).should == @nancy
37
+ end
38
+ end
39
+
40
+ it 'should provide #clear' do
41
+ @lazy_array.should respond_to(:clear)
42
+ end
43
+
44
+ describe '#clear' do
45
+ it_should_return_self(:clear)
46
+
47
+ it 'should return self' do
48
+ @lazy_array.clear.object_id.should == @lazy_array.object_id
49
+ end
50
+
51
+ it 'should make the lazy array become empty' do
52
+ @lazy_array.clear.should be_empty
53
+ end
54
+
55
+ it 'should be loaded afterwards' do
56
+ @lazy_array.should_not be_loaded
57
+ @lazy_array.should_not_receive(:lazy_load)
58
+
59
+ cleared = @lazy_array.clear
60
+ cleared.should be_loaded
61
+ end
62
+ end
63
+
64
+ it 'should provide #collect!' do
65
+ @lazy_array.should respond_to(:collect!)
66
+ end
67
+
68
+ describe '#collect!' do
69
+ it_should_return_self(:collect!)
70
+
71
+ it 'should return self' do
72
+ @lazy_array.collect! { |entry| entry }.object_id.should == @lazy_array.object_id
73
+ end
74
+
75
+ it 'should iterate over the lazy array' do
76
+ entries = []
77
+ @lazy_array.collect! { |entry| entries << entry; entry }
78
+ entries.should == @lazy_array.entries
79
+ end
80
+
81
+ it 'should update the lazy array with the result of the block' do
82
+ @lazy_array.collect! { |entry| @steve }.entries.should == [ @steve, @steve ]
83
+ end
84
+ end
85
+
86
+ it 'should provide #concat' do
87
+ @lazy_array.should respond_to(:concat)
88
+ end
89
+
90
+ describe '#concat' do
91
+ it_should_return_self(:concat)
92
+
93
+ it 'should return self' do
94
+ @lazy_array.concat(@other).object_id.should == @lazy_array.object_id
95
+ end
96
+
97
+ it 'should concatenate another lazy array with #concat' do
98
+ concatenated = @lazy_array.concat(@other)
99
+ concatenated.should == [ @nancy, @bessie, @steve ]
100
+ end
101
+ end
102
+
103
+ it 'should provide #delete' do
104
+ @lazy_array.should respond_to(:delete)
105
+ end
106
+
107
+ describe '#delete' do
108
+ it_should_return_plain(:delete)
109
+
110
+ it 'should delete the matching entry from the lazy array' do
111
+ @lazy_array.entries.should == [ @nancy, @bessie ]
112
+ @lazy_array.delete(@nancy).should == @nancy
113
+ @lazy_array.entries.should == [ @bessie ]
114
+ end
115
+
116
+ it 'should use the passed-in block when no entry was removed' do
117
+ @lazy_array.entries.should == [ @nancy, @bessie ]
118
+ @lazy_array.delete(@steve) { @steve }.should == @steve
119
+ @lazy_array.entries.should == [ @nancy, @bessie ]
120
+ end
121
+ end
122
+
123
+ it 'should provide #delete_at' do
124
+ @lazy_array.should respond_to(:delete_at)
125
+ end
126
+
127
+ describe '#delete_at' do
128
+ it_should_return_plain(:delete_at)
129
+
130
+ it 'should delete the entry from the lazy array with the index' do
131
+ @lazy_array.entries.should == [ @nancy, @bessie ]
132
+ @lazy_array.delete_at(0).should == @nancy
133
+ @lazy_array.entries.should == [ @bessie ]
134
+ end
135
+ end
136
+
137
+ it 'should provide #dup' do
138
+ @lazy_array.should respond_to(:dup)
139
+ end
140
+
141
+ describe '#dup' do
142
+ it_should_return_plain(:dup)
143
+
144
+ it 'should copy the original array' do
145
+ dup = @lazy_array.dup
146
+ dup.entries.should == @lazy_array.entries
147
+ end
148
+
149
+ it 'should copy the original load proc' do
150
+ dup = @lazy_array.dup
151
+ dup.to_proc.object_id.should == @lazy_array.to_proc.object_id
152
+ end
153
+ end
154
+
155
+ it 'should provide #each' do
156
+ @lazy_array.should respond_to(:each)
157
+ end
158
+
159
+ describe '#each' do
160
+ it_should_return_self(:each)
161
+
162
+ it 'should return self' do
163
+ @lazy_array.each { |entry| }.object_id.should == @lazy_array.object_id
164
+ end
165
+
166
+ it 'should iterate over the lazy array entries' do
167
+ entries = []
168
+ @lazy_array.each { |entry| entries << entry }
169
+ entries.should == @lazy_array.entries
170
+ end
171
+ end
172
+
173
+ it 'should provide #each_index' do
174
+ @lazy_array.should respond_to(:each_index)
175
+ end
176
+
177
+ describe '#each_index' do
178
+ it_should_return_self(:each_index)
179
+
180
+ it 'should return self' do
181
+ @lazy_array.each_index { |entry| }.object_id.should == @lazy_array.object_id
182
+ end
183
+
184
+ it 'should iterate over the lazy array by index' do
185
+ indexes = []
186
+ @lazy_array.each_index { |index| indexes << index }
187
+ indexes.should == [ 0, 1 ]
188
+ end
189
+ end
190
+
191
+ it 'should provide #empty?' do
192
+ @lazy_array.should respond_to(:empty?)
193
+ end
194
+
195
+ describe '#empty?' do
196
+ it_should_return_plain(:empty?)
197
+
198
+ it 'should return true if the lazy array has entries' do
199
+ @lazy_array.length.should == 2
200
+ @lazy_array.empty?.should be_false
201
+ end
202
+
203
+ it 'should return false if the lazy array has no entries' do
204
+ @lazy_array.clear
205
+ @lazy_array.length.should == 0
206
+ @lazy_array.empty?.should be_true
207
+ end
208
+ end
209
+
210
+ it 'should provide #entries' do
211
+ @lazy_array.should respond_to(:entries)
212
+ end
213
+
214
+ describe '#entries' do
215
+ it_should_return_plain(:entries)
216
+
217
+ it 'should return an Array' do
218
+ @lazy_array.entries.class.should == Array
219
+ end
220
+ end
221
+
222
+ it 'should provide #freeze' do
223
+ @lazy_array.should respond_to(:freeze)
224
+ end
225
+
226
+ describe '#freeze' do
227
+ it_should_return_self(:freeze)
228
+
229
+ it 'should freeze the underlying array' do
230
+ @lazy_array.should_not be_frozen
231
+ @lazy_array.instance_variable_get('@array').should_not be_frozen
232
+
233
+ @lazy_array.freeze
234
+
235
+ @lazy_array.should be_frozen
236
+ @lazy_array.instance_variable_get('@array').should be_frozen
237
+ end
238
+ end
239
+
240
+ it 'should provide #eql?' do
241
+ @lazy_array.should respond_to(:eql?)
242
+ end
243
+
244
+ describe '#eql?' do
245
+ it_should_return_plain(:eql?)
246
+
247
+ it 'should return true if for the same lazy array' do
248
+ @lazy_array.object_id.should == @lazy_array.object_id
249
+ @lazy_array.entries.should == @lazy_array.entries
250
+ @lazy_array.should be_eql(@lazy_array)
251
+ end
252
+
253
+ it 'should return true for duplicate lazy arrays' do
254
+ dup = @lazy_array.dup
255
+ dup.should be_kind_of(LazyArray)
256
+ dup.object_id.should_not == @lazy_array.object_id
257
+ dup.should be_eql(@lazy_array)
258
+ end
259
+
260
+ it 'should return false for different lazy arrays' do
261
+ @lazy_array.should_not be_eql(@other)
262
+ end
263
+ end
264
+
265
+ it 'should provide #fetch' do
266
+ @lazy_array.should respond_to(:fetch)
267
+ end
268
+
269
+ describe '#fetch' do
270
+ it_should_return_plain(:fetch)
271
+
272
+ it 'should lookup the entry with an index' do
273
+ @lazy_array.fetch(0).should == @nancy
274
+ end
275
+
276
+ it 'should throw an IndexError exception if the index is outside the array' do
277
+ lambda { @lazy_array.fetch(99) }.should raise_error(IndexError)
278
+ end
279
+
280
+ it 'should substitute the default if the index is outside the array' do
281
+ entry = 'cow'
282
+ @lazy_array.fetch(99, entry).object_id.should == entry.object_id
283
+ end
284
+
285
+ it 'should substitute the value returned by the default block if the index is outside the array' do
286
+ entry = 'cow'
287
+ @lazy_array.fetch(99) { entry }.object_id.should == entry.object_id
288
+ end
289
+ end
290
+
291
+ it 'should provide #first' do
292
+ @lazy_array.should respond_to(:first)
293
+ end
294
+
295
+ describe '#first' do
296
+ it_should_return_plain(:first)
297
+
298
+ describe 'with no arguments' do
299
+ it 'should return the first entry in the lazy array' do
300
+ @lazy_array.first.should == @nancy
301
+ end
302
+ end
303
+
304
+ describe 'with number of results specified' do
305
+ it 'should return an Array ' do
306
+ array = @lazy_array.first(2)
307
+ array.class.should == Array
308
+ array.should == [ @nancy, @bessie ]
309
+ end
310
+ end
311
+ end
312
+
313
+ it 'should provide #index' do
314
+ @lazy_array.should respond_to(:index)
315
+ end
316
+
317
+ describe '#index' do
318
+ it_should_return_plain(:index)
319
+
320
+ it 'should return an Integer' do
321
+ @lazy_array.index(@nancy).should be_kind_of(Integer)
322
+ end
323
+
324
+ it 'should return the index for the first matching entry in the lazy array' do
325
+ @lazy_array.index(@nancy).should == 0
326
+ end
327
+ end
328
+
329
+ it 'should provide #insert' do
330
+ @lazy_array.should respond_to(:insert)
331
+ end
332
+
333
+ describe '#insert' do
334
+ it_should_return_self(:insert)
335
+
336
+ it 'should return self' do
337
+ @lazy_array.insert(1, @steve).object_id.should == @lazy_array.object_id
338
+ end
339
+
340
+ it 'should insert the entry at index in the lazy array' do
341
+ @lazy_array.insert(1, @steve)
342
+ @lazy_array.should == [ @nancy, @steve, @bessie ]
343
+ end
344
+ end
345
+
346
+ it 'should provide #last' do
347
+ @lazy_array.should respond_to(:last)
348
+ end
349
+
350
+ describe '#last' do
351
+ it_should_return_plain(:last)
352
+
353
+ describe 'with no arguments' do
354
+ it 'should return the last entry in the lazy array' do
355
+ @lazy_array.last.should == @bessie
356
+ end
357
+ end
358
+
359
+ describe 'with number of results specified' do
360
+ it 'should return an Array' do
361
+ array = @lazy_array.last(2)
362
+ array.class.should == Array
363
+ array.should == [ @nancy, @bessie ]
364
+ end
365
+ end
366
+ end
367
+
368
+ it 'should provide #length' do
369
+ @lazy_array.should respond_to(:length)
370
+ end
371
+
372
+ describe '#length' do
373
+ it_should_return_plain(:length)
374
+
375
+ it 'should return an Integer' do
376
+ @lazy_array.length.should be_kind_of(Integer)
377
+ end
378
+
379
+ it 'should return the length of the lazy array' do
380
+ @lazy_array.length.should == 2
381
+ end
382
+ end
383
+
384
+ it 'should provide #loaded?' do
385
+ @lazy_array.should respond_to(:loaded?)
386
+ end
387
+
388
+ describe '#loaded?' do
389
+ it 'should return true for an initialized lazy array' do
390
+ @lazy_array.at(0) # initialize the array
391
+ @lazy_array.should be_loaded
392
+ end
393
+
394
+ it 'should return false for an uninitialized lazy array' do
395
+ uninitialized = LazyArray.new
396
+ uninitialized.should_not be_loaded
397
+ end
398
+ end
399
+
400
+ it 'should provide #partition' do
401
+ @lazy_array.should respond_to(:partition)
402
+ end
403
+
404
+ describe '#partition' do
405
+ describe 'return value' do
406
+ before do
407
+ @array = @lazy_array.partition { |e| e == @nancy }
408
+ end
409
+
410
+ it 'should be an Array' do
411
+ @array.should be_kind_of(Array)
412
+ end
413
+
414
+ it 'should have two entries' do
415
+ @array.length.should == 2
416
+ end
417
+
418
+ describe 'first entry' do
419
+ before do
420
+ @true_results = @array.first
421
+ end
422
+
423
+ it 'should be an Array' do
424
+ @true_results.class.should == Array
425
+ end
426
+
427
+ it 'should have one entry' do
428
+ @true_results.length.should == 1
429
+ end
430
+
431
+ it 'should contain the entry the block returned true for' do
432
+ @true_results.should == [ @nancy ]
433
+ end
434
+ end
435
+
436
+ describe 'second entry' do
437
+ before do
438
+ @false_results = @array.last
439
+ end
440
+
441
+ it 'should be an Array' do
442
+ @false_results.class.should == Array
443
+ end
444
+
445
+ it 'should have one entry' do
446
+ @false_results.length.should == 1
447
+ end
448
+
449
+ it 'should contain the entry the block returned true for' do
450
+ @false_results.should == [ @bessie ]
451
+ end
452
+ end
453
+ end
454
+ end
455
+
456
+ it 'should provide #pop' do
457
+ @lazy_array.should respond_to(:pop)
458
+ end
459
+
460
+ describe '#pop' do
461
+ it_should_return_plain(:pop)
462
+
463
+ it 'should remove the last entry' do
464
+ @lazy_array.pop.should == @bessie
465
+ @lazy_array.should == [ @nancy ]
466
+ end
467
+ end
468
+
469
+ it 'should provide #push' do
470
+ @lazy_array.should respond_to(:push)
471
+ end
472
+
473
+ describe '#push' do
474
+ it_should_return_self(:push)
475
+
476
+ it 'should return self' do
477
+ @lazy_array.push(@steve).object_id.should == @lazy_array.object_id
478
+ end
479
+
480
+ it 'should append an entry' do
481
+ @lazy_array.push(@steve)
482
+ @lazy_array.should == [ @nancy, @bessie, @steve ]
483
+ end
484
+ end
485
+
486
+ it 'should provide #reject' do
487
+ @lazy_array.should respond_to(:reject)
488
+ end
489
+
490
+ describe '#reject' do
491
+ it_should_return_plain(:reject)
492
+
493
+ it 'should return an Array with entries that did not match the block' do
494
+ rejected = @lazy_array.reject { |entry| false }
495
+ rejected.class.should == Array
496
+ rejected.should == [ @nancy, @bessie ]
497
+ end
498
+
499
+ it 'should return an empty Array if entries matched the block' do
500
+ rejected = @lazy_array.reject { |entry| true }
501
+ rejected.class.should == Array
502
+ rejected.should == []
503
+ end
504
+ end
505
+
506
+ it 'should provide #reject!' do
507
+ @lazy_array.should respond_to(:reject!)
508
+ end
509
+
510
+ describe '#reject!' do
511
+ it_should_return_self(:reject!)
512
+
513
+ it 'should return self if entries matched the block' do
514
+ @lazy_array.reject! { |entry| true }.object_id.should == @lazy_array.object_id
515
+ end
516
+
517
+ it 'should return nil if no entries matched the block' do
518
+ @lazy_array.reject! { |entry| false }.should be_nil
519
+ end
520
+
521
+ it 'should remove entries that matched the block' do
522
+ @lazy_array.reject! { |entry| true }
523
+ @lazy_array.should be_empty
524
+ end
525
+
526
+ it 'should not remove entries that did not match the block' do
527
+ @lazy_array.reject! { |entry| false }
528
+ @lazy_array.should == [ @nancy, @bessie ]
529
+ end
530
+ end
531
+
532
+ it 'should provide #replace' do
533
+ @lazy_array.should respond_to(:replace)
534
+ end
535
+
536
+ describe '#replace' do
537
+ it_should_return_self(:replace)
538
+
539
+ it 'should return self' do
540
+ @lazy_array.replace(@other).object_id.should == @lazy_array.object_id
541
+ end
542
+
543
+ it 'should replace itself with the other object' do
544
+ replaced = @lazy_array.replace(@other)
545
+ replaced.should == @other
546
+ end
547
+
548
+ it 'should be loaded afterwards' do
549
+ @lazy_array.should_not be_loaded
550
+ @lazy_array.should_not_receive(:lazy_load)
551
+
552
+ replaced = @lazy_array.replace(@other)
553
+ replaced.should be_loaded
554
+ end
555
+ end
556
+
557
+ it 'should provide #reverse' do
558
+ @lazy_array.should respond_to(:reverse)
559
+ end
560
+
561
+ describe '#reverse' do
562
+ it_should_return_plain(:reverse)
563
+
564
+ it 'should return an Array with reversed entries' do
565
+ reversed = @lazy_array.reverse
566
+ reversed.class.should == Array
567
+ reversed.should == @lazy_array.entries.reverse
568
+ end
569
+ end
570
+
571
+ it 'should provide #reverse!' do
572
+ @lazy_array.should respond_to(:reverse!)
573
+ end
574
+
575
+ describe '#reverse!' do
576
+ it_should_return_self(:reverse!)
577
+
578
+ it 'should return self' do
579
+ @lazy_array.reverse!.object_id.should == @lazy_array.object_id
580
+ end
581
+
582
+ it 'should reverse the order of entries in the lazy array inline' do
583
+ entries = @lazy_array.entries
584
+ @lazy_array.reverse!
585
+ @lazy_array.entries.should == entries.reverse
586
+ end
587
+ end
588
+
589
+ it 'should provide #reverse_each' do
590
+ @lazy_array.should respond_to(:reverse_each)
591
+ end
592
+
593
+ describe '#reverse_each' do
594
+ it_should_return_self(:reverse_each)
595
+
596
+ it 'should return self' do
597
+ @lazy_array.reverse_each { |entry| }.object_id.should == @lazy_array.object_id
598
+ end
599
+
600
+ it 'should iterate through the lazy array in reverse' do
601
+ entries = []
602
+ @lazy_array.reverse_each { |entry| entries << entry }
603
+ entries.should == @lazy_array.entries.reverse
604
+ end
605
+ end
606
+
607
+ it 'should provide #rindex' do
608
+ @lazy_array.should respond_to(:rindex)
609
+ end
610
+
611
+ describe '#rindex' do
612
+ it_should_return_plain(:rindex)
613
+
614
+ it 'should return an Integer' do
615
+ @lazy_array.rindex(@nancy).should be_kind_of(Integer)
616
+ end
617
+
618
+ it 'should return the index for the last matching entry in the lazy array' do
619
+ @lazy_array.rindex(@nancy).should == 0
620
+ end
621
+ end
622
+
623
+ it 'should provide #select' do
624
+ @lazy_array.should respond_to(:select)
625
+ end
626
+
627
+ describe '#select' do
628
+ it_should_return_plain(:select)
629
+
630
+ it 'should return an Array with entries that matched the block' do
631
+ selected = @lazy_array.select { |entry| true }
632
+ selected.class.should == Array
633
+ selected.should == @lazy_array.entries
634
+ end
635
+
636
+ it 'should return an empty Array if no entries matched the block' do
637
+ selected = @lazy_array.select { |entry| false }
638
+ selected.class.should == Array
639
+ selected.should be_empty
640
+ end
641
+ end
642
+
643
+ it 'should provide #shift' do
644
+ @lazy_array.should respond_to(:shift)
645
+ end
646
+
647
+ describe '#shift' do
648
+ it_should_return_plain(:shift)
649
+
650
+ it 'should remove the first entry' do
651
+ @lazy_array.shift.should == @nancy
652
+ @lazy_array.should == [ @bessie ]
653
+ end
654
+ end
655
+
656
+ it 'should provide #slice' do
657
+ @lazy_array.should respond_to(:slice)
658
+ end
659
+
660
+ describe '#slice' do
661
+ it_should_return_plain(:slice)
662
+
663
+ describe 'with an index' do
664
+ it 'should not modify the lazy array' do
665
+ @lazy_array.slice(0)
666
+ @lazy_array.size.should == 2
667
+ end
668
+ end
669
+
670
+ describe 'with a start and length' do
671
+ it 'should return an Array' do
672
+ sliced = @lazy_array.slice(0, 1)
673
+ sliced.class.should == Array
674
+ sliced.should == [ @nancy ]
675
+ end
676
+
677
+ it 'should not modify the lazy array' do
678
+ @lazy_array.slice(0, 1)
679
+ @lazy_array.size.should == 2
680
+ end
681
+ end
682
+
683
+ describe 'with a Range' do
684
+ it 'should return an Array' do
685
+ sliced = @lazy_array.slice(0..1)
686
+ sliced.class.should == Array
687
+ sliced.should == [ @nancy, @bessie ]
688
+ end
689
+
690
+ it 'should not modify the lazy array' do
691
+ @lazy_array.slice(0..1)
692
+ @lazy_array.size.should == 2
693
+ end
694
+ end
695
+ end
696
+
697
+ it 'should provide #slice!' do
698
+ @lazy_array.should respond_to(:slice!)
699
+ end
700
+
701
+ describe '#slice!' do
702
+ it_should_return_plain(:slice!)
703
+
704
+ describe 'with an index' do
705
+ it 'should modify the lazy array' do
706
+ @lazy_array.slice!(0)
707
+ @lazy_array.size.should == 1
708
+ end
709
+ end
710
+
711
+ describe 'with a start and length' do
712
+ it 'should return an Array' do
713
+ sliced = @lazy_array.slice!(0, 1)
714
+ sliced.class.should == Array
715
+ sliced.should == [ @nancy ]
716
+ end
717
+
718
+ it 'should modify the lazy array' do
719
+ @lazy_array.slice!(0, 1)
720
+ @lazy_array.size.should == 1
721
+ end
722
+ end
723
+
724
+ describe 'with a Range' do
725
+ it 'should return an Array' do
726
+ sliced = @lazy_array.slice(0..1)
727
+ sliced.class.should == Array
728
+ sliced.should == [ @nancy, @bessie ]
729
+ end
730
+
731
+ it 'should modify the lazy array' do
732
+ @lazy_array.slice!(0..1)
733
+ @lazy_array.size.should == 0
734
+ end
735
+ end
736
+ end
737
+
738
+ it 'should provide #sort' do
739
+ @lazy_array.should respond_to(:sort)
740
+ end
741
+
742
+ describe '#sort' do
743
+ it_should_return_plain(:sort)
744
+
745
+ it 'should return an Array' do
746
+ sorted = @lazy_array.sort { |a,b| a <=> b }
747
+ sorted.class.should == Array
748
+ end
749
+
750
+ it 'should sort the entries' do
751
+ sorted = @lazy_array.sort { |a,b| a <=> b }
752
+ sorted.entries.should == @lazy_array.entries.reverse
753
+ end
754
+ end
755
+
756
+ it 'should provide #sort!' do
757
+ @lazy_array.should respond_to(:sort!)
758
+ end
759
+
760
+ describe '#sort!' do
761
+ it_should_return_self(:sort!)
762
+
763
+ it 'should return self' do
764
+ @lazy_array.sort! { |a,b| 0 }.object_id.should == @lazy_array.object_id
765
+ end
766
+
767
+ it 'should sort the LazyArray in place' do
768
+ original_entries = @lazy_array.entries
769
+ @lazy_array.length.should == 2
770
+ @lazy_array.sort! { |a,b| a <=> b }
771
+ @lazy_array.length.should == 2
772
+ @lazy_array.entries.should == original_entries.reverse
773
+ end
774
+ end
775
+
776
+ it 'should provide #to_a' do
777
+ @lazy_array.should respond_to(:to_a)
778
+ end
779
+
780
+ describe '#to_a' do
781
+ it_should_return_plain(:to_a)
782
+
783
+ it 'should return an Array' do
784
+ @lazy_array.to_a.class.should == Array
785
+ end
786
+ end
787
+
788
+ it 'should provide #to_ary' do
789
+ @lazy_array.should respond_to(:to_ary)
790
+ end
791
+
792
+ describe '#to_ary' do
793
+ it_should_return_plain(:to_ary)
794
+
795
+ it 'should return an Array' do
796
+ @lazy_array.to_ary.class.should == Array
797
+ end
798
+ end
799
+
800
+ it 'should provide #to_proc' do
801
+ @lazy_array.should respond_to(:to_proc)
802
+ end
803
+
804
+ describe '#to_proc' do
805
+ it 'should return a Prox' do
806
+ @lazy_array.to_proc.class.should == Proc
807
+ end
808
+
809
+ it 'should return the proc supplied to load_with' do
810
+ proc = lambda { |a| }
811
+ @lazy_array.load_with(&proc)
812
+ @lazy_array.to_proc.object_id.should == proc.object_id
813
+ end
814
+ end
815
+
816
+ it 'should provide #unload' do
817
+ @lazy_array.should respond_to(:unload)
818
+ end
819
+
820
+ describe '#unload' do
821
+ it 'should return self' do
822
+ @lazy_array.unload.object_id.should == @lazy_array.object_id
823
+ end
824
+
825
+ it 'should make the lazy array become empty' do
826
+ @lazy_array.should_not be_empty
827
+ @lazy_array.load_with {} # ensure it's not lazy-loaded by be_empty
828
+ @lazy_array.unload.should be_empty
829
+ end
830
+
831
+ it 'should not be loaded afterwards' do
832
+ @lazy_array.should_not be_loaded
833
+ unloaded = @lazy_array.unload
834
+ unloaded.should_not be_loaded
835
+ end
836
+ end
837
+
838
+ it 'should provide #unshift' do
839
+ @lazy_array.should respond_to(:unshift)
840
+ end
841
+
842
+ describe '#unshift' do
843
+ it_should_return_self(:unshift)
844
+
845
+ it 'should return self' do
846
+ @lazy_array.unshift(@steve).object_id.should == @lazy_array.object_id
847
+ end
848
+
849
+ it 'should prepend an entry' do
850
+ @lazy_array.unshift(@steve)
851
+ @lazy_array.should == [ @steve, @nancy, @bessie ]
852
+ end
853
+ end
854
+
855
+ it 'should provide #values_at' do
856
+ @lazy_array.should respond_to(:values_at)
857
+ end
858
+
859
+ describe '#values_at' do
860
+ it_should_return_plain(:values_at)
861
+
862
+ it 'should return an Array' do
863
+ values = @lazy_array.values_at(0)
864
+ values.class.should == Array
865
+ end
866
+
867
+ it 'should return an Array of the entries at the index' do
868
+ @lazy_array.values_at(0).entries.should == [ @nancy ]
869
+ end
870
+ end
871
+
872
+ describe 'a method mixed into Array' do
873
+ before :all do
874
+ module Enumerable
875
+ def group_by(&block)
876
+ groups = {}
877
+ each do |entry|
878
+ value = yield(entry)
879
+ (groups[value] ||= []).push(entry)
880
+ end
881
+ groups
882
+ end
883
+ end
884
+ end
885
+
886
+ it 'should delegate to the Array' do
887
+ @lazy_array.group_by { |e| e.length }.should == { 5 => %w[ nancy ], 6 => %w[ bessie ] }
888
+ end
889
+ end
890
+
891
+ describe 'an unknown method' do
892
+ it 'should raise an exception' do
893
+ lambda { @lazy_array.unknown }.should raise_error(NoMethodError)
894
+ end
895
+ end
896
+ end