extlib 0.9.2

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of extlib might be problematic. Click here for more details.

@@ -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,882 @@
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 #eql?' do
223
+ @lazy_array.should respond_to(:eql?)
224
+ end
225
+
226
+ describe '#eql?' do
227
+ it_should_return_plain(:eql?)
228
+
229
+ it 'should return true if for the same lazy array' do
230
+ @lazy_array.object_id.should == @lazy_array.object_id
231
+ @lazy_array.entries.should == @lazy_array.entries
232
+ @lazy_array.should be_eql(@lazy_array)
233
+ end
234
+
235
+ it 'should return true for duplicate lazy arrays' do
236
+ dup = @lazy_array.dup
237
+ dup.should be_kind_of(LazyArray)
238
+ dup.object_id.should_not == @lazy_array.object_id
239
+ dup.should be_eql(@lazy_array)
240
+ end
241
+
242
+ it 'should return false for different lazy arrays' do
243
+ @lazy_array.should_not be_eql(@other)
244
+ end
245
+ end
246
+
247
+ it 'should provide #fetch' do
248
+ @lazy_array.should respond_to(:fetch)
249
+ end
250
+
251
+ describe '#fetch' do
252
+ it_should_return_plain(:fetch)
253
+
254
+ it 'should lookup the entry with an index' do
255
+ @lazy_array.fetch(0).should == @nancy
256
+ end
257
+
258
+ it 'should throw an IndexError exception if the index is outside the array' do
259
+ lambda { @lazy_array.fetch(99) }.should raise_error(IndexError)
260
+ end
261
+
262
+ it 'should substitute the default if the index is outside the array' do
263
+ entry = 'cow'
264
+ @lazy_array.fetch(99, entry).object_id.should == entry.object_id
265
+ end
266
+
267
+ it 'should substitute the value returned by the default block if the index is outside the array' do
268
+ entry = 'cow'
269
+ @lazy_array.fetch(99) { entry }.object_id.should == entry.object_id
270
+ end
271
+ end
272
+
273
+ it 'should provide #first' do
274
+ @lazy_array.should respond_to(:first)
275
+ end
276
+
277
+ describe '#first' do
278
+ it_should_return_plain(:first)
279
+
280
+ describe 'with no arguments' do
281
+ it 'should return the first entry in the lazy array' do
282
+ @lazy_array.first.should == @nancy
283
+ end
284
+ end
285
+
286
+ describe 'with number of results specified' do
287
+ it 'should return an Array ' do
288
+ array = @lazy_array.first(2)
289
+ array.class.should == Array
290
+ array.should == [ @nancy, @bessie ]
291
+ end
292
+ end
293
+ end
294
+
295
+ it 'should provide #index' do
296
+ @lazy_array.should respond_to(:index)
297
+ end
298
+
299
+ describe '#index' do
300
+ it_should_return_plain(:index)
301
+
302
+ it 'should return an Integer' do
303
+ @lazy_array.index(@nancy).should be_kind_of(Integer)
304
+ end
305
+
306
+ it 'should return the index for the first matching entry in the lazy array' do
307
+ @lazy_array.index(@nancy).should == 0
308
+ end
309
+ end
310
+
311
+ it 'should provide #insert' do
312
+ @lazy_array.should respond_to(:insert)
313
+ end
314
+
315
+ describe '#insert' do
316
+ it_should_return_self(:insert)
317
+
318
+ it 'should return self' do
319
+ @lazy_array.insert(1, @steve).object_id.should == @lazy_array.object_id
320
+ end
321
+
322
+ it 'should insert the entry at index in the lazy array' do
323
+ @lazy_array.insert(1, @steve)
324
+ @lazy_array.should == [ @nancy, @steve, @bessie ]
325
+ end
326
+ end
327
+
328
+ it 'should provide #last' do
329
+ @lazy_array.should respond_to(:last)
330
+ end
331
+
332
+ describe '#last' do
333
+ it_should_return_plain(:last)
334
+
335
+ describe 'with no arguments' do
336
+ it 'should return the last entry in the lazy array' do
337
+ @lazy_array.last.should == @bessie
338
+ end
339
+ end
340
+
341
+ describe 'with number of results specified' do
342
+ it 'should return an Array' do
343
+ array = @lazy_array.last(2)
344
+ array.class.should == Array
345
+ array.should == [ @nancy, @bessie ]
346
+ end
347
+ end
348
+ end
349
+
350
+ it 'should provide #length' do
351
+ @lazy_array.should respond_to(:length)
352
+ end
353
+
354
+ describe '#length' do
355
+ it_should_return_plain(:length)
356
+
357
+ it 'should return an Integer' do
358
+ @lazy_array.length.should be_kind_of(Integer)
359
+ end
360
+
361
+ it 'should return the length of the lazy array' do
362
+ @lazy_array.length.should == 2
363
+ end
364
+ end
365
+
366
+ it 'should provide #loaded?' do
367
+ @lazy_array.should respond_to(:loaded?)
368
+ end
369
+
370
+ describe '#loaded?' do
371
+ it 'should return true for an initialized lazy array' do
372
+ @lazy_array.at(0) # initialize the array
373
+ @lazy_array.should be_loaded
374
+ end
375
+
376
+ it 'should return false for an uninitialized lazy array' do
377
+ uninitialized = LazyArray.new
378
+ uninitialized.should_not be_loaded
379
+ end
380
+ end
381
+
382
+ it 'should provide #partition' do
383
+ @lazy_array.should respond_to(:partition)
384
+ end
385
+
386
+ describe '#partition' do
387
+ describe 'return value' do
388
+ before do
389
+ @array = @lazy_array.partition { |e| e == @nancy }
390
+ end
391
+
392
+ it 'should be an Array' do
393
+ @array.should be_kind_of(Array)
394
+ end
395
+
396
+ it 'should have two entries' do
397
+ @array.length.should == 2
398
+ end
399
+
400
+ describe 'first entry' do
401
+ before do
402
+ @true_results = @array.first
403
+ end
404
+
405
+ it 'should be an Array' do
406
+ @true_results.class.should == Array
407
+ end
408
+
409
+ it 'should have one entry' do
410
+ @true_results.length.should == 1
411
+ end
412
+
413
+ it 'should contain the entry the block returned true for' do
414
+ @true_results.should == [ @nancy ]
415
+ end
416
+ end
417
+
418
+ describe 'second entry' do
419
+ before do
420
+ @false_results = @array.last
421
+ end
422
+
423
+ it 'should be an Array' do
424
+ @false_results.class.should == Array
425
+ end
426
+
427
+ it 'should have one entry' do
428
+ @false_results.length.should == 1
429
+ end
430
+
431
+ it 'should contain the entry the block returned true for' do
432
+ @false_results.should == [ @bessie ]
433
+ end
434
+ end
435
+ end
436
+ end
437
+
438
+ it 'should provide #pop' do
439
+ @lazy_array.should respond_to(:pop)
440
+ end
441
+
442
+ describe '#pop' do
443
+ it_should_return_plain(:pop)
444
+
445
+ it 'should remove the last entry' do
446
+ @lazy_array.pop.should == @bessie
447
+ @lazy_array.should == [ @nancy ]
448
+ end
449
+ end
450
+
451
+ it 'should provide #push' do
452
+ @lazy_array.should respond_to(:push)
453
+ end
454
+
455
+ describe '#push' do
456
+ it_should_return_self(:push)
457
+
458
+ it 'should return self' do
459
+ @lazy_array.push(@steve).object_id.should == @lazy_array.object_id
460
+ end
461
+
462
+ it 'should append an entry' do
463
+ @lazy_array.push(@steve)
464
+ @lazy_array.should == [ @nancy, @bessie, @steve ]
465
+ end
466
+ end
467
+
468
+ it 'should provide #reject' do
469
+ @lazy_array.should respond_to(:reject)
470
+ end
471
+
472
+ describe '#reject' do
473
+ it_should_return_plain(:reject)
474
+
475
+ it 'should return an Array with entries that did not match the block' do
476
+ rejected = @lazy_array.reject { |entry| false }
477
+ rejected.class.should == Array
478
+ rejected.should == [ @nancy, @bessie ]
479
+ end
480
+
481
+ it 'should return an empty Array if entries matched the block' do
482
+ rejected = @lazy_array.reject { |entry| true }
483
+ rejected.class.should == Array
484
+ rejected.should == []
485
+ end
486
+ end
487
+
488
+ it 'should provide #reject!' do
489
+ @lazy_array.should respond_to(:reject!)
490
+ end
491
+
492
+ describe '#reject!' do
493
+ it_should_return_self(:reject!)
494
+
495
+ it 'should return self if entries matched the block' do
496
+ @lazy_array.reject! { |entry| true }.object_id.should == @lazy_array.object_id
497
+ end
498
+
499
+ it 'should return nil if no entries matched the block' do
500
+ @lazy_array.reject! { |entry| false }.should be_nil
501
+ end
502
+
503
+ it 'should remove entries that matched the block' do
504
+ @lazy_array.reject! { |entry| true }
505
+ @lazy_array.should be_empty
506
+ end
507
+
508
+ it 'should not remove entries that did not match the block' do
509
+ @lazy_array.reject! { |entry| false }
510
+ @lazy_array.should == [ @nancy, @bessie ]
511
+ end
512
+ end
513
+
514
+ it 'should provide #replace' do
515
+ @lazy_array.should respond_to(:replace)
516
+ end
517
+
518
+ describe '#replace' do
519
+ it_should_return_self(:replace)
520
+
521
+ it 'should return self' do
522
+ @lazy_array.replace(@other).object_id.should == @lazy_array.object_id
523
+ end
524
+
525
+ it 'should replace itself with the other object' do
526
+ replaced = @lazy_array.replace(@other)
527
+ replaced.should == @other
528
+ end
529
+
530
+ it 'should be loaded afterwards' do
531
+ @lazy_array.should_not be_loaded
532
+ @lazy_array.should_not_receive(:lazy_load)
533
+
534
+ replaced = @lazy_array.replace(@other)
535
+ replaced.should be_loaded
536
+ end
537
+ end
538
+
539
+ it 'should provide #reverse' do
540
+ @lazy_array.should respond_to(:reverse)
541
+ end
542
+
543
+ describe '#reverse' do
544
+ it_should_return_plain(:reverse)
545
+
546
+ it 'should return an Array with reversed entries' do
547
+ reversed = @lazy_array.reverse
548
+ reversed.class.should == Array
549
+ reversed.should == @lazy_array.entries.reverse
550
+ end
551
+ end
552
+
553
+ it 'should provide #reverse!' do
554
+ @lazy_array.should respond_to(:reverse!)
555
+ end
556
+
557
+ describe '#reverse!' do
558
+ it_should_return_self(:reverse!)
559
+
560
+ it 'should return self' do
561
+ @lazy_array.reverse!.object_id.should == @lazy_array.object_id
562
+ end
563
+
564
+ it 'should reverse the order of entries in the lazy array inline' do
565
+ entries = @lazy_array.entries
566
+ @lazy_array.reverse!
567
+ @lazy_array.entries.should == entries.reverse
568
+ end
569
+ end
570
+
571
+ it 'should provide #reverse_each' do
572
+ @lazy_array.should respond_to(:reverse_each)
573
+ end
574
+
575
+ describe '#reverse_each' do
576
+ it_should_return_self(:reverse_each)
577
+
578
+ it 'should return self' do
579
+ @lazy_array.reverse_each { |entry| }.object_id.should == @lazy_array.object_id
580
+ end
581
+
582
+ it 'should iterate through the lazy array in reverse' do
583
+ entries = []
584
+ @lazy_array.reverse_each { |entry| entries << entry }
585
+ entries.should == @lazy_array.entries.reverse
586
+ end
587
+ end
588
+
589
+ it 'should provide #rindex' do
590
+ @lazy_array.should respond_to(:rindex)
591
+ end
592
+
593
+ describe '#rindex' do
594
+ it_should_return_plain(:rindex)
595
+
596
+ it 'should return an Integer' do
597
+ @lazy_array.rindex(@nancy).should be_kind_of(Integer)
598
+ end
599
+
600
+ it 'should return the index for the last matching entry in the lazy array' do
601
+ @lazy_array.rindex(@nancy).should == 0
602
+ end
603
+ end
604
+
605
+ it 'should provide #select' do
606
+ @lazy_array.should respond_to(:select)
607
+ end
608
+
609
+ describe '#select' do
610
+ it_should_return_plain(:select)
611
+
612
+ it 'should return an Array with entries that matched the block' do
613
+ selected = @lazy_array.select { |entry| true }
614
+ selected.class.should == Array
615
+ selected.should == @lazy_array.entries
616
+ end
617
+
618
+ it 'should return an empty Array if no entries matched the block' do
619
+ selected = @lazy_array.select { |entry| false }
620
+ selected.class.should == Array
621
+ selected.should be_empty
622
+ end
623
+ end
624
+
625
+ it 'should provide #shift' do
626
+ @lazy_array.should respond_to(:shift)
627
+ end
628
+
629
+ describe '#shift' do
630
+ it_should_return_plain(:shift)
631
+
632
+ it 'should remove the first entry' do
633
+ @lazy_array.shift.should == @nancy
634
+ @lazy_array.should == [ @bessie ]
635
+ end
636
+ end
637
+
638
+ it 'should provide #slice' do
639
+ @lazy_array.should respond_to(:slice)
640
+ end
641
+
642
+ describe '#slice' do
643
+ it_should_return_plain(:slice)
644
+
645
+ describe 'with an index' do
646
+ it 'should not modify the lazy array' do
647
+ @lazy_array.slice(0)
648
+ @lazy_array.size.should == 2
649
+ end
650
+ end
651
+
652
+ describe 'with a start and length' do
653
+ it 'should return an Array' do
654
+ sliced = @lazy_array.slice(0, 1)
655
+ sliced.class.should == Array
656
+ sliced.should == [ @nancy ]
657
+ end
658
+
659
+ it 'should not modify the lazy array' do
660
+ @lazy_array.slice(0, 1)
661
+ @lazy_array.size.should == 2
662
+ end
663
+ end
664
+
665
+ describe 'with a Range' do
666
+ it 'should return an Array' do
667
+ sliced = @lazy_array.slice(0..1)
668
+ sliced.class.should == Array
669
+ sliced.should == [ @nancy, @bessie ]
670
+ end
671
+
672
+ it 'should not modify the lazy array' do
673
+ @lazy_array.slice(0..1)
674
+ @lazy_array.size.should == 2
675
+ end
676
+ end
677
+ end
678
+
679
+ it 'should provide #slice!' do
680
+ @lazy_array.should respond_to(:slice!)
681
+ end
682
+
683
+ describe '#slice!' do
684
+ it_should_return_plain(:slice!)
685
+
686
+ describe 'with an index' do
687
+ it 'should modify the lazy array' do
688
+ @lazy_array.slice!(0)
689
+ @lazy_array.size.should == 1
690
+ end
691
+ end
692
+
693
+ describe 'with a start and length' do
694
+ it 'should return an Array' do
695
+ sliced = @lazy_array.slice!(0, 1)
696
+ sliced.class.should == Array
697
+ sliced.should == [ @nancy ]
698
+ end
699
+
700
+ it 'should modify the lazy array' do
701
+ @lazy_array.slice!(0, 1)
702
+ @lazy_array.size.should == 1
703
+ end
704
+ end
705
+
706
+ describe 'with a Range' do
707
+ it 'should return an Array' do
708
+ sliced = @lazy_array.slice(0..1)
709
+ sliced.class.should == Array
710
+ sliced.should == [ @nancy, @bessie ]
711
+ end
712
+
713
+ it 'should modify the lazy array' do
714
+ @lazy_array.slice!(0..1)
715
+ @lazy_array.size.should == 0
716
+ end
717
+ end
718
+ end
719
+
720
+ it 'should provide #sort' do
721
+ @lazy_array.should respond_to(:sort)
722
+ end
723
+
724
+ describe '#sort' do
725
+ it_should_return_plain(:sort)
726
+
727
+ it 'should return an Array' do
728
+ sorted = @lazy_array.sort { |a,b| a <=> b }
729
+ sorted.class.should == Array
730
+ end
731
+
732
+ it 'should sort the entries' do
733
+ sorted = @lazy_array.sort { |a,b| a <=> b }
734
+ sorted.entries.should == @lazy_array.entries.reverse
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_self(:sort!)
744
+
745
+ it 'should return self' do
746
+ @lazy_array.sort! { |a,b| 0 }.object_id.should == @lazy_array.object_id
747
+ end
748
+
749
+ it 'should sort the LazyArray in place' do
750
+ original_entries = @lazy_array.entries
751
+ @lazy_array.length.should == 2
752
+ @lazy_array.sort! { |a,b| a <=> b }
753
+ @lazy_array.length.should == 2
754
+ @lazy_array.entries.should == original_entries.reverse
755
+ end
756
+ end
757
+
758
+ it 'should provide #to_a' do
759
+ @lazy_array.should respond_to(:to_a)
760
+ end
761
+
762
+ describe '#to_a' do
763
+ it_should_return_plain(:to_a)
764
+
765
+ it 'should return an Array' do
766
+ @lazy_array.to_a.class.should == Array
767
+ end
768
+ end
769
+
770
+ it 'should provide #to_ary' do
771
+ @lazy_array.should respond_to(:to_ary)
772
+ end
773
+
774
+ describe '#to_ary' do
775
+ it_should_return_plain(:to_ary)
776
+
777
+ it 'should return an Array' do
778
+ @lazy_array.to_ary.class.should == Array
779
+ end
780
+ end
781
+
782
+ it 'should provide #to_proc' do
783
+ @lazy_array.should respond_to(:to_proc)
784
+ end
785
+
786
+ describe '#to_proc' do
787
+ it 'should return a Prox' do
788
+ @lazy_array.to_proc.class.should == Proc
789
+ end
790
+
791
+ it 'should return the proc supplied to load_with' do
792
+ proc = lambda { |a| }
793
+ @lazy_array.load_with(&proc)
794
+ @lazy_array.to_proc.object_id.should == proc.object_id
795
+ end
796
+ end
797
+
798
+ it 'should provide #unload' do
799
+ @lazy_array.should respond_to(:unload)
800
+ end
801
+
802
+ describe '#unload' do
803
+ it 'should return self' do
804
+ @lazy_array.unload.object_id.should == @lazy_array.object_id
805
+ end
806
+
807
+ it 'should make the lazy array become empty' do
808
+ @lazy_array.should_not be_empty
809
+ @lazy_array.load_with {} # ensure it's not lazy-loaded by be_empty
810
+ @lazy_array.unload.should be_empty
811
+ end
812
+
813
+ it 'should not be loaded afterwards' do
814
+ @lazy_array.should_not be_loaded
815
+ unloaded = @lazy_array.unload
816
+ unloaded.should_not be_loaded
817
+ end
818
+ end
819
+
820
+ it 'should provide #unshift' do
821
+ @lazy_array.should respond_to(:unshift)
822
+ end
823
+
824
+ describe '#unshift' do
825
+ it_should_return_self(:unshift)
826
+
827
+ it 'should return self' do
828
+ @lazy_array.unshift(@steve).object_id.should == @lazy_array.object_id
829
+ end
830
+
831
+ it 'should prepend an entry' do
832
+ @lazy_array.unshift(@steve)
833
+ @lazy_array.should == [ @steve, @nancy, @bessie ]
834
+ end
835
+ end
836
+
837
+ it 'should provide #values_at' do
838
+ @lazy_array.should respond_to(:values_at)
839
+ end
840
+
841
+ describe '#values_at' do
842
+ it_should_return_plain(:values_at)
843
+
844
+ it 'should return an Array' do
845
+ values = @lazy_array.values_at(0)
846
+ values.class.should == Array
847
+ end
848
+
849
+ it 'should return an Array of the entries at the index' do
850
+ @lazy_array.values_at(0).entries.should == [ @nancy ]
851
+ end
852
+ end
853
+
854
+ describe 'a method mixed into Array' do
855
+ before :all do
856
+ class Array
857
+ def group_by(&block)
858
+ groups = []
859
+ each do |entry|
860
+ value = yield(entry)
861
+ if(last_group = groups.last) && last_group.first == value
862
+ last_group.last << entry
863
+ else
864
+ groups << [ value, [ entry ] ]
865
+ end
866
+ end
867
+ groups
868
+ end
869
+ end
870
+ end
871
+
872
+ it 'should delegate to the Array' do
873
+ @lazy_array.group_by { |e| e.length }.should == [ [ 5, %w[ nancy ] ], [ 6, %w[ bessie ] ] ]
874
+ end
875
+ end
876
+
877
+ describe 'an unknown method' do
878
+ it 'should raise an exception' do
879
+ lambda { @lazy_array.unknown }.should raise_error(NoMethodError)
880
+ end
881
+ end
882
+ end