1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
|
require_relative '../../spec_helper'
describe "Range#step" do
before :each do
ScratchPad.record []
end
it "returns self" do
r = 1..2
r.step { }.should equal(r)
end
ruby_version_is ""..."3.4" do
it "calls #to_int to coerce step to an Integer" do
obj = mock("Range#step")
obj.should_receive(:to_int).and_return(1)
(1..2).step(obj) { |x| ScratchPad << x }
ScratchPad.recorded.should eql([1, 2])
end
it "raises a TypeError if step does not respond to #to_int" do
obj = mock("Range#step non-integer")
-> { (1..2).step(obj) { } }.should raise_error(TypeError)
end
it "raises a TypeError if #to_int does not return an Integer" do
obj = mock("Range#step non-integer")
obj.should_receive(:to_int).and_return("1")
-> { (1..2).step(obj) { } }.should raise_error(TypeError)
end
it "raises a TypeError if the first element does not respond to #succ" do
obj = mock("Range#step non-comparable")
obj.should_receive(:<=>).with(obj).and_return(1)
-> { (obj..obj).step { |x| x } }.should raise_error(TypeError)
end
end
ruby_version_is "3.4" do
it "calls #coerce to coerce step to an Integer" do
obj = mock("Range#step")
obj.should_receive(:coerce).at_least(:once).and_return([1, 2])
(1..3).step(obj) { |x| ScratchPad << x }
ScratchPad.recorded.should eql([1, 3])
end
it "raises a TypeError if step does not respond to #coerce" do
obj = mock("Range#step non-coercible")
-> { (1..2).step(obj) { } }.should raise_error(TypeError)
end
end
it "raises an ArgumentError if step is 0" do
-> { (-1..1).step(0) { |x| x } }.should raise_error(ArgumentError)
end
it "raises an ArgumentError if step is 0.0" do
-> { (-1..1).step(0.0) { |x| x } }.should raise_error(ArgumentError)
end
ruby_version_is "3.4" do
it "does not raise an ArgumentError if step is 0 for non-numeric ranges" do
t = Time.utc(2023, 2, 24)
-> { (t..t+1).step(0) { break } }.should_not raise_error(ArgumentError)
end
end
ruby_version_is ""..."3.4" do
it "raises an ArgumentError if step is negative" do
-> { (-1..1).step(-2) { |x| x } }.should raise_error(ArgumentError)
end
end
describe "with inclusive end" do
describe "and Integer values" do
it "yields Integer values incremented by 1 and less than or equal to end when not passed a step" do
(-2..2).step { |x| ScratchPad << x }
ScratchPad.recorded.should eql([-2, -1, 0, 1, 2])
end
it "yields Integer values incremented by an Integer step" do
(-5..5).step(2) { |x| ScratchPad << x }
ScratchPad.recorded.should eql([-5, -3, -1, 1, 3, 5])
end
it "yields Float values incremented by a Float step" do
(-2..2).step(1.5) { |x| ScratchPad << x }
ScratchPad.recorded.should eql([-2.0, -0.5, 1.0])
end
ruby_version_is "3.4" do
it "does not iterate if step is negative for forward range" do
(-1..1).step(-1) { |x| ScratchPad << x }
ScratchPad.recorded.should eql([])
end
it "iterates backward if step is negative for backward range" do
(1..-1).step(-1) { |x| ScratchPad << x }
ScratchPad.recorded.should eql([1, 0, -1])
end
end
end
describe "and Float values" do
it "yields Float values incremented by 1 and less than or equal to end when not passed a step" do
(-2.0..2.0).step { |x| ScratchPad << x }
ScratchPad.recorded.should eql([-2.0, -1.0, 0.0, 1.0, 2.0])
end
it "yields Float values incremented by an Integer step" do
(-5.0..5.0).step(2) { |x| ScratchPad << x }
ScratchPad.recorded.should eql([-5.0, -3.0, -1.0, 1.0, 3.0, 5.0])
end
it "yields Float values incremented by a Float step" do
(-1.0..1.0).step(0.5) { |x| ScratchPad << x }
ScratchPad.recorded.should eql([-1.0, -0.5, 0.0, 0.5, 1.0])
end
it "returns Float values of 'step * n + begin <= end'" do
(1.0..6.4).step(1.8) { |x| ScratchPad << x }
(1.0..12.7).step(1.3) { |x| ScratchPad << x }
ScratchPad.recorded.should eql([1.0, 2.8, 4.6, 6.4, 1.0, 2.3, 3.6,
4.9, 6.2, 7.5, 8.8, 10.1, 11.4, 12.7])
end
it "handles infinite values at either end" do
(-Float::INFINITY..0.0).step(2) { |x| ScratchPad << x; break if ScratchPad.recorded.size == 3 }
ScratchPad.recorded.should eql([-Float::INFINITY, -Float::INFINITY, -Float::INFINITY])
ScratchPad.record []
(0.0..Float::INFINITY).step(2) { |x| ScratchPad << x; break if ScratchPad.recorded.size == 3 }
ScratchPad.recorded.should eql([0.0, 2.0, 4.0])
end
end
describe "and Integer, Float values" do
it "yields Float values incremented by 1 and less than or equal to end when not passed a step" do
(-2..2.0).step { |x| ScratchPad << x }
ScratchPad.recorded.should eql([-2.0, -1.0, 0.0, 1.0, 2.0])
end
it "yields Float values incremented by an Integer step" do
(-5..5.0).step(2) { |x| ScratchPad << x }
ScratchPad.recorded.should eql([-5.0, -3.0, -1.0, 1.0, 3.0, 5.0])
end
it "yields Float values incremented by a Float step" do
(-1..1.0).step(0.5) { |x| ScratchPad << x }
ScratchPad.recorded.should eql([-1.0, -0.5, 0.0, 0.5, 1.0])
end
end
describe "and Float, Integer values" do
it "yields Float values incremented by 1 and less than or equal to end when not passed a step" do
(-2.0..2).step { |x| ScratchPad << x }
ScratchPad.recorded.should eql([-2.0, -1.0, 0.0, 1.0, 2.0])
end
it "yields Float values incremented by an Integer step" do
(-5.0..5).step(2) { |x| ScratchPad << x }
ScratchPad.recorded.should eql([-5.0, -3.0, -1.0, 1.0, 3.0, 5.0])
end
it "yields Float values incremented by a Float step" do
(-1.0..1).step(0.5) { |x| ScratchPad << x }
ScratchPad.recorded.should eql([-1.0, -0.5, 0.0, 0.5, 1.0])
end
end
describe "and String values" do
it "yields String values incremented by #succ and less than or equal to end when not passed a step" do
("A".."E").step { |x| ScratchPad << x }
ScratchPad.recorded.should == ["A", "B", "C", "D", "E"]
end
it "yields String values incremented by #succ called Integer step times" do
("A".."G").step(2) { |x| ScratchPad << x }
ScratchPad.recorded.should == ["A", "C", "E", "G"]
end
it "raises a TypeError when passed a Float step" do
-> { ("A".."G").step(2.0) { } }.should raise_error(TypeError)
end
ruby_version_is ""..."3.4" do
it "calls #succ on begin and each element returned by #succ" do
obj = mock("Range#step String start")
obj.should_receive(:<=>).exactly(3).times.and_return(-1, -1, -1, 0)
obj.should_receive(:succ).exactly(2).times.and_return(obj)
(obj..obj).step { |x| ScratchPad << x }
ScratchPad.recorded.should == [obj, obj, obj]
end
end
ruby_version_is "3.4" do
it "yields String values adjusted by step and less than or equal to end" do
("A".."AAA").step("A") { |x| ScratchPad << x }
ScratchPad.recorded.should == ["A", "AA", "AAA"]
end
it "raises a TypeError when passed an incompatible type step" do
-> { ("A".."G").step([]) { } }.should raise_error(TypeError)
end
it "calls #+ on begin and each element returned by #+" do
start = mock("Range#step String start")
stop = mock("Range#step String stop")
mid1 = mock("Range#step String mid1")
mid2 = mock("Range#step String mid2")
step = mock("Range#step String step")
# Deciding on the direction of iteration
start.should_receive(:<=>).with(stop).at_least(:twice).and_return(-1)
# Deciding whether the step moves iteration in the right direction
start.should_receive(:<=>).with(mid1).and_return(-1)
# Iteration 1
start.should_receive(:+).at_least(:once).with(step).and_return(mid1)
# Iteration 2
mid1.should_receive(:<=>).with(stop).and_return(-1)
mid1.should_receive(:+).with(step).and_return(mid2)
# Iteration 3
mid2.should_receive(:<=>).with(stop).and_return(0)
(start..stop).step(step) { |x| ScratchPad << x }
ScratchPad.recorded.should == [start, mid1, mid2]
end
it "iterates backward if the step is decreasing values, and the range is backward" do
start = mock("Range#step String start")
stop = mock("Range#step String stop")
mid1 = mock("Range#step String mid1")
mid2 = mock("Range#step String mid2")
step = mock("Range#step String step")
# Deciding on the direction of iteration
start.should_receive(:<=>).with(stop).at_least(:twice).and_return(1)
# Deciding whether the step moves iteration in the right direction
start.should_receive(:<=>).with(mid1).and_return(1)
# Iteration 1
start.should_receive(:+).at_least(:once).with(step).and_return(mid1)
# Iteration 2
mid1.should_receive(:<=>).with(stop).and_return(1)
mid1.should_receive(:+).with(step).and_return(mid2)
# Iteration 3
mid2.should_receive(:<=>).with(stop).and_return(0)
(start..stop).step(step) { |x| ScratchPad << x }
ScratchPad.recorded.should == [start, mid1, mid2]
end
it "does no iteration of the direction of the range and of the step don't match" do
start = mock("Range#step String start")
stop = mock("Range#step String stop")
mid1 = mock("Range#step String mid1")
mid2 = mock("Range#step String mid2")
step = mock("Range#step String step")
# Deciding on the direction of iteration: stop > start
start.should_receive(:<=>).with(stop).at_least(:twice).and_return(1)
# Deciding whether the step moves iteration in the right direction
# start + step < start, the direction is opposite to the range's
start.should_receive(:+).with(step).and_return(mid1)
start.should_receive(:<=>).with(mid1).and_return(-1)
(start..stop).step(step) { |x| ScratchPad << x }
ScratchPad.recorded.should == []
end
end
end
end
describe "with exclusive end" do
describe "and Integer values" do
it "yields Integer values incremented by 1 and less than end when not passed a step" do
(-2...2).step { |x| ScratchPad << x }
ScratchPad.recorded.should eql([-2, -1, 0, 1])
end
it "yields Integer values incremented by an Integer step" do
(-5...5).step(2) { |x| ScratchPad << x }
ScratchPad.recorded.should eql([-5, -3, -1, 1, 3])
end
it "yields Float values incremented by a Float step" do
(-2...2).step(1.5) { |x| ScratchPad << x }
ScratchPad.recorded.should eql([-2.0, -0.5, 1.0])
end
end
describe "and Float values" do
it "yields Float values incremented by 1 and less than end when not passed a step" do
(-2.0...2.0).step { |x| ScratchPad << x }
ScratchPad.recorded.should eql([-2.0, -1.0, 0.0, 1.0])
end
it "yields Float values incremented by an Integer step" do
(-5.0...5.0).step(2) { |x| ScratchPad << x }
ScratchPad.recorded.should eql([-5.0, -3.0, -1.0, 1.0, 3.0])
end
it "yields Float values incremented by a Float step" do
(-1.0...1.0).step(0.5) { |x| ScratchPad << x }
ScratchPad.recorded.should eql([-1.0, -0.5, 0.0, 0.5])
end
it "returns Float values of 'step * n + begin < end'" do
(1.0...6.4).step(1.8) { |x| ScratchPad << x }
ScratchPad.recorded.should eql([1.0, 2.8, 4.6])
end
it "correctly handles values near the upper limit" do # https://bugs.ruby-lang.org/issues/16612
(1.0...55.6).step(18.2) { |x| ScratchPad << x }
ScratchPad.recorded.should eql([1.0, 19.2, 37.4, 55.599999999999994])
(1.0...55.6).step(18.2).size.should == 4
end
it "handles infinite values at either end" do
(-Float::INFINITY...0.0).step(2) { |x| ScratchPad << x; break if ScratchPad.recorded.size == 3 }
ScratchPad.recorded.should eql([-Float::INFINITY, -Float::INFINITY, -Float::INFINITY])
ScratchPad.record []
(0.0...Float::INFINITY).step(2) { |x| ScratchPad << x; break if ScratchPad.recorded.size == 3 }
ScratchPad.recorded.should eql([0.0, 2.0, 4.0])
end
end
describe "and Integer, Float values" do
it "yields Float values incremented by 1 and less than end when not passed a step" do
(-2...2.0).step { |x| ScratchPad << x }
ScratchPad.recorded.should eql([-2.0, -1.0, 0.0, 1.0])
end
it "yields Float values incremented by an Integer step" do
(-5...5.0).step(2) { |x| ScratchPad << x }
ScratchPad.recorded.should eql([-5.0, -3.0, -1.0, 1.0, 3.0])
end
it "yields an Float and then Float values incremented by a Float step" do
(-1...1.0).step(0.5) { |x| ScratchPad << x }
ScratchPad.recorded.should eql([-1.0, -0.5, 0.0, 0.5])
end
end
describe "and Float, Integer values" do
it "yields Float values incremented by 1 and less than end when not passed a step" do
(-2.0...2).step { |x| ScratchPad << x }
ScratchPad.recorded.should eql([-2.0, -1.0, 0.0, 1.0])
end
it "yields Float values incremented by an Integer step" do
(-5.0...5).step(2) { |x| ScratchPad << x }
ScratchPad.recorded.should eql([-5.0, -3.0, -1.0, 1.0, 3.0])
end
it "yields Float values incremented by a Float step" do
(-1.0...1).step(0.5) { |x| ScratchPad << x }
ScratchPad.recorded.should eql([-1.0, -0.5, 0.0, 0.5])
end
end
describe "and String values" do
ruby_version_is ""..."3.4" do
it "yields String values incremented by #succ and less than or equal to end when not passed a step" do
("A"..."E").step { |x| ScratchPad << x }
ScratchPad.recorded.should == ["A", "B", "C", "D"]
end
it "yields String values incremented by #succ called Integer step times" do
("A"..."G").step(2) { |x| ScratchPad << x }
ScratchPad.recorded.should == ["A", "C", "E"]
end
it "raises a TypeError when passed a Float step" do
-> { ("A"..."G").step(2.0) { } }.should raise_error(TypeError)
end
end
ruby_version_is "3.4" do
it "yields String values adjusted by step and less than or equal to end" do
("A"..."AAA").step("A") { |x| ScratchPad << x }
ScratchPad.recorded.should == ["A", "AA"]
end
it "raises a TypeError when passed an incompatible type step" do
-> { ("A".."G").step([]) { } }.should raise_error(TypeError)
end
end
end
end
describe "with an endless range" do
describe "and Integer values" do
it "yield Integer values incremented by 1 when not passed a step" do
(-2..).step { |x| break if x > 2; ScratchPad << x }
ScratchPad.recorded.should eql([-2, -1, 0, 1, 2])
ScratchPad.record []
(-2...).step { |x| break if x > 2; ScratchPad << x }
ScratchPad.recorded.should eql([-2, -1, 0, 1, 2])
end
it "yields Integer values incremented by an Integer step" do
(-5..).step(2) { |x| break if x > 3; ScratchPad << x }
ScratchPad.recorded.should eql([-5, -3, -1, 1, 3])
ScratchPad.record []
(-5...).step(2) { |x| break if x > 3; ScratchPad << x }
ScratchPad.recorded.should eql([-5, -3, -1, 1, 3])
end
it "yields Float values incremented by a Float step" do
(-2..).step(1.5) { |x| break if x > 1.0; ScratchPad << x }
ScratchPad.recorded.should eql([-2.0, -0.5, 1.0])
ScratchPad.record []
(-2..).step(1.5) { |x| break if x > 1.0; ScratchPad << x }
ScratchPad.recorded.should eql([-2.0, -0.5, 1.0])
end
end
describe "and Float values" do
it "yields Float values incremented by 1 and less than end when not passed a step" do
(-2.0..).step { |x| break if x > 1.5; ScratchPad << x }
ScratchPad.recorded.should eql([-2.0, -1.0, 0.0, 1.0])
ScratchPad.record []
(-2.0...).step { |x| break if x > 1.5; ScratchPad << x }
ScratchPad.recorded.should eql([-2.0, -1.0, 0.0, 1.0])
end
it "yields Float values incremented by an Integer step" do
(-5.0..).step(2) { |x| break if x > 3.5; ScratchPad << x }
ScratchPad.recorded.should eql([-5.0, -3.0, -1.0, 1.0, 3.0])
ScratchPad.record []
(-5.0...).step(2) { |x| break if x > 3.5; ScratchPad << x }
ScratchPad.recorded.should eql([-5.0, -3.0, -1.0, 1.0, 3.0])
end
it "yields Float values incremented by a Float step" do
(-1.0..).step(0.5) { |x| break if x > 0.6; ScratchPad << x }
ScratchPad.recorded.should eql([-1.0, -0.5, 0.0, 0.5])
ScratchPad.record []
(-1.0...).step(0.5) { |x| break if x > 0.6; ScratchPad << x }
ScratchPad.recorded.should eql([-1.0, -0.5, 0.0, 0.5])
end
it "handles infinite values at the start" do
(-Float::INFINITY..).step(2) { |x| ScratchPad << x; break if ScratchPad.recorded.size == 3 }
ScratchPad.recorded.should eql([-Float::INFINITY, -Float::INFINITY, -Float::INFINITY])
ScratchPad.record []
(-Float::INFINITY...).step(2) { |x| ScratchPad << x; break if ScratchPad.recorded.size == 3 }
ScratchPad.recorded.should eql([-Float::INFINITY, -Float::INFINITY, -Float::INFINITY])
end
end
describe "and String values" do
it "yields String values incremented by #succ and less than or equal to end when not passed a step" do
('A'..).step { |x| break if x > "D"; ScratchPad << x }
ScratchPad.recorded.should == ["A", "B", "C", "D"]
ScratchPad.record []
('A'...).step { |x| break if x > "D"; ScratchPad << x }
ScratchPad.recorded.should == ["A", "B", "C", "D"]
end
it "yields String values incremented by #succ called Integer step times" do
('A'..).step(2) { |x| break if x > "F"; ScratchPad << x }
ScratchPad.recorded.should == ["A", "C", "E"]
ScratchPad.record []
('A'...).step(2) { |x| break if x > "F"; ScratchPad << x }
ScratchPad.recorded.should == ["A", "C", "E"]
end
it "raises a TypeError when passed a Float step" do
-> { ('A'..).step(2.0) { } }.should raise_error(TypeError)
-> { ('A'...).step(2.0) { } }.should raise_error(TypeError)
end
ruby_version_is "3.4" do
it "yields String values adjusted by step" do
('A'..).step("A") { |x| break if x > "AAA"; ScratchPad << x }
ScratchPad.recorded.should == ["A", "AA", "AAA"]
ScratchPad.record []
('A'...).step("A") { |x| break if x > "AAA"; ScratchPad << x }
ScratchPad.recorded.should == ["A", "AA", "AAA"]
end
it "raises a TypeError when passed an incompatible type step" do
-> { ('A'..).step([]) { } }.should raise_error(TypeError)
-> { ('A'...).step([]) { } }.should raise_error(TypeError)
end
end
end
end
describe "when no block is given" do
it "raises an ArgumentError if step is 0" do
-> { (-1..1).step(0) }.should raise_error(ArgumentError)
end
describe "returned Enumerator" do
describe "size" do
ruby_version_is ""..."3.4" do
it "raises a TypeError if step does not respond to #to_int" do
obj = mock("Range#step non-integer")
-> { (1..2).step(obj) }.should raise_error(TypeError)
end
it "raises a TypeError if #to_int does not return an Integer" do
obj = mock("Range#step non-integer")
obj.should_receive(:to_int).and_return("1")
-> { (1..2).step(obj) }.should raise_error(TypeError)
end
end
ruby_version_is "3.4" do
it "does not raise if step is incompatible" do
obj = mock("Range#step non-integer")
-> { (1..2).step(obj) }.should_not raise_error
end
end
it "returns the ceil of range size divided by the number of steps" do
(1..10).step(4).size.should == 3
(1..10).step(3).size.should == 4
(1..10).step(2).size.should == 5
(1..10).step(1).size.should == 10
(-5..5).step(2).size.should == 6
(1...10).step(4).size.should == 3
(1...10).step(3).size.should == 3
(1...10).step(2).size.should == 5
(1...10).step(1).size.should == 9
(-5...5).step(2).size.should == 5
end
it "returns the ceil of range size divided by the number of steps even if step is negative" do
(-1..1).step(-1).size.should == 0
(1..-1).step(-1).size.should == 3
end
it "returns the correct number of steps when one of the arguments is a float" do
(-1..1.0).step(0.5).size.should == 5
(-1.0...1.0).step(0.5).size.should == 4
end
it "returns the range size when there's no step_size" do
(-2..2).step.size.should == 5
(-2.0..2.0).step.size.should == 5
(-2..2.0).step.size.should == 5
(-2.0..2).step.size.should == 5
(1.0..6.4).step(1.8).size.should == 4
(1.0..12.7).step(1.3).size.should == 10
(-2...2).step.size.should == 4
(-2.0...2.0).step.size.should == 4
(-2...2.0).step.size.should == 4
(-2.0...2).step.size.should == 4
(1.0...6.4).step(1.8).size.should == 3
end
ruby_version_is ""..."3.4" do
it "returns nil with begin and end are String" do
("A".."E").step(2).size.should == nil
("A"..."E").step(2).size.should == nil
("A".."E").step.size.should == nil
("A"..."E").step.size.should == nil
end
it "return nil and not raises a TypeError if the first element does not respond to #succ" do
obj = mock("Range#step non-comparable")
obj.should_receive(:<=>).with(obj).and_return(1)
enum = (obj..obj).step
-> { enum.size }.should_not raise_error
enum.size.should == nil
end
end
ruby_version_is "3.4" do
it "returns nil with begin and end are String" do
("A".."E").step("A").size.should == nil
("A"..."E").step("A").size.should == nil
end
it "return nil and not raises a TypeError if the first element is not of compatible type" do
obj = mock("Range#step non-comparable")
obj.should_receive(:<=>).with(obj).and_return(1)
enum = (obj..obj).step(obj)
-> { enum.size }.should_not raise_error
enum.size.should == nil
end
end
end
# We use .take below to ensure the enumerator works
# because that's an Enumerable method and so it uses the Enumerator behavior
# not just a method overridden in Enumerator::ArithmeticSequence.
describe "type" do
context "when both begin and end are numerics" do
it "returns an instance of Enumerator::ArithmeticSequence" do
(1..10).step.class.should == Enumerator::ArithmeticSequence
(1..10).step(3).take(4).should == [1, 4, 7, 10]
end
end
context "when begin is not defined and end is numeric" do
it "returns an instance of Enumerator::ArithmeticSequence" do
(..10).step.class.should == Enumerator::ArithmeticSequence
end
end
context "when range is endless" do
it "returns an instance of Enumerator::ArithmeticSequence when begin is numeric" do
(1..).step.class.should == Enumerator::ArithmeticSequence
(1..).step(2).take(3).should == [1, 3, 5]
end
ruby_version_is ""..."3.4" do
it "returns an instance of Enumerator when begin is not numeric" do
("a"..).step.class.should == Enumerator
("a"..).step(2).take(3).should == %w[a c e]
end
end
ruby_version_is "3.4" do
it "returns an instance of Enumerator when begin is not numeric" do
("a"..).step("a").class.should == Enumerator
("a"..).step("a").take(3).should == %w[a aa aaa]
end
end
end
context "when range is beginless and endless" do
ruby_version_is ""..."3.4" do
it "returns an instance of Enumerator" do
Range.new(nil, nil).step.class.should == Enumerator
end
end
ruby_version_is "3.4" do
it "raises an ArgumentError" do
-> { Range.new(nil, nil).step(1) }.should raise_error(ArgumentError)
end
end
end
context "when begin and end are not numerics" do
ruby_version_is ""..."3.4" do
it "returns an instance of Enumerator" do
("a".."z").step.class.should == Enumerator
("a".."z").step(3).take(4).should == %w[a d g j]
end
end
ruby_version_is "3.4" do
it "returns an instance of Enumerator" do
("a".."z").step("a").class.should == Enumerator
("a".."z").step("a").take(4).should == %w[a aa aaa aaaa]
end
end
end
end
end
end
end
|