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
|
require_relative '../../../spec_helper'
require_relative '../fixtures/classes'
# Describes Numeric#step shared specs between different argument styles.
# To be able to do it, the @step ivar must contain a Proc that transforms
# the step call arguments passed as positional arguments to the style of
# arguments pretended to test.
describe :numeric_step, :shared => true do
before :each do
ScratchPad.record []
@prc = -> x { ScratchPad << x }
end
it "defaults to step = 1" do
@step.call(1, 5, &@prc)
ScratchPad.recorded.should eql [1, 2, 3, 4, 5]
end
it "defaults to an infinite limit with a step size of 1 for Integers" do
1.step.first(5).should == [1, 2, 3, 4, 5]
end
it "defaults to an infinite limit with a step size of 1.0 for Floats" do
1.0.step.first(5).should == [1.0, 2.0, 3.0, 4.0, 5.0]
end
describe "when self, stop and step are Fixnums" do
it "yields only Fixnums" do
@step.call(1, 5, 1) { |x| x.should be_an_instance_of(Fixnum) }
end
describe "with a positive step" do
it "yields while increasing self by step until stop is reached" do
@step.call(1, 5, 1, &@prc)
ScratchPad.recorded.should eql [1, 2, 3, 4, 5]
end
it "yields once when self equals stop" do
@step.call(1, 1, 1, &@prc)
ScratchPad.recorded.should eql [1]
end
it "does not yield when self is greater than stop" do
@step.call(2, 1, 1, &@prc)
ScratchPad.recorded.should eql []
end
end
describe "with a negative step" do
it "yields while decreasing self by step until stop is reached" do
@step.call(5, 1, -1, &@prc)
ScratchPad.recorded.should eql [5, 4, 3, 2, 1]
end
it "yields once when self equals stop" do
@step.call(5, 5, -1, &@prc)
ScratchPad.recorded.should eql [5]
end
it "does not yield when self is less than stop" do
@step.call(1, 5, -1, &@prc)
ScratchPad.recorded.should == []
end
end
end
describe "when at least one of self, stop or step is a Float" do
it "yields Floats even if only self is a Float" do
@step.call(1.5, 5, 1) { |x| x.should be_an_instance_of(Float) }
end
it "yields Floats even if only stop is a Float" do
@step.call(1, 5.0, 1) { |x| x.should be_an_instance_of(Float) }
end
it "yields Floats even if only step is a Float" do
@step.call(1, 5, 1.0) { |x| x.should be_an_instance_of(Float) }
end
describe "with a positive step" do
it "yields while increasing self by step while < stop" do
@step.call(1.5, 5, 1, &@prc)
ScratchPad.recorded.should eql [1.5, 2.5, 3.5, 4.5]
end
it "yields once when self equals stop" do
@step.call(1.5, 1.5, 1, &@prc)
ScratchPad.recorded.should eql [1.5]
end
it "does not yield when self is greater than stop" do
@step.call(2.5, 1.5, 1, &@prc)
ScratchPad.recorded.should == []
end
it "is careful about not yielding a value greater than limit" do
# As 9*1.3+1.0 == 12.700000000000001 > 12.7, we test:
@step.call(1.0, 12.7, 1.3, &@prc)
ScratchPad.recorded.should eql [1.0, 2.3, 3.6, 4.9, 6.2, 7.5, 8.8, 10.1, 11.4, 12.7]
end
end
describe "with a negative step" do
it "yields while decreasing self by step while self > stop" do
@step.call(5, 1.5, -1, &@prc)
ScratchPad.recorded.should eql [5.0, 4.0, 3.0, 2.0]
end
it "yields once when self equals stop" do
@step.call(1.5, 1.5, -1, &@prc)
ScratchPad.recorded.should eql [1.5]
end
it "does not yield when self is less than stop" do
@step.call(1, 5, -1.5, &@prc)
ScratchPad.recorded.should == []
end
it "is careful about not yielding a value smaller than limit" do
# As -9*1.3-1.0 == -12.700000000000001 < -12.7, we test:
@step.call(-1.0, -12.7, -1.3, &@prc)
ScratchPad.recorded.should eql [-1.0, -2.3, -3.6, -4.9, -6.2, -7.5, -8.8, -10.1, -11.4, -12.7]
end
end
describe "with a positive Infinity step" do
it "yields once if self < stop" do
@step.call(42, 100, infinity_value, &@prc)
ScratchPad.recorded.should eql [42.0]
end
it "yields once when stop is Infinity" do
@step.call(42, infinity_value, infinity_value, &@prc)
ScratchPad.recorded.should eql [42.0]
end
it "yields once when self equals stop" do
@step.call(42, 42, infinity_value, &@prc)
ScratchPad.recorded.should eql [42.0]
end
it "yields once when self and stop are Infinity" do
# @step.call(infinity_value, infinity_value, infinity_value, &@prc)
@step.call(infinity_value, infinity_value, infinity_value, &@prc)
ScratchPad.recorded.should == [infinity_value]
end
it "does not yield when self > stop" do
@step.call(100, 42, infinity_value, &@prc)
ScratchPad.recorded.should == []
end
it "does not yield when stop is -Infinity" do
@step.call(42, -infinity_value, infinity_value, &@prc)
ScratchPad.recorded.should == []
end
end
describe "with a negative Infinity step" do
it "yields once if self > stop" do
@step.call(42, 6, -infinity_value, &@prc)
ScratchPad.recorded.should eql [42.0]
end
it "yields once if stop is -Infinity" do
@step.call(42, -infinity_value, -infinity_value, &@prc)
ScratchPad.recorded.should eql [42.0]
end
it "yields once when self equals stop" do
@step.call(42, 42, -infinity_value, &@prc)
ScratchPad.recorded.should eql [42.0]
end
it "yields once when self and stop are Infinity" do
@step.call(infinity_value, infinity_value, -infinity_value, &@prc)
ScratchPad.recorded.should == [infinity_value]
end
it "does not yield when self > stop" do
@step.call(42, 100, -infinity_value, &@prc)
ScratchPad.recorded.should == []
end
it "does not yield when stop is Infinity" do
@step.call(42, infinity_value, -infinity_value, &@prc)
ScratchPad.recorded.should == []
end
end
describe "with a Infinity stop and a positive step" do
it "does not yield when self is infinity" do
@step.call(infinity_value, infinity_value, 1, &@prc)
ScratchPad.recorded.should == []
end
end
describe "with a Infinity stop and a negative step" do
it "does not yield when self is negative infinity" do
@step.call(-infinity_value, infinity_value, -1, &@prc)
ScratchPad.recorded.should == []
end
it "does not yield when self is positive infinity" do
@step.call(infinity_value, infinity_value, -1, &@prc)
ScratchPad.recorded.should == []
end
end
describe "with a negative Infinity stop and a positive step" do
it "does not yield when self is negative infinity" do
@step.call(-infinity_value, -infinity_value, 1, &@prc)
ScratchPad.recorded.should == []
end
end
describe "with a negative Infinity stop and a negative step" do
it "does not yield when self is negative infinity" do
@step.call(-infinity_value, -infinity_value, -1, &@prc)
ScratchPad.recorded.should == []
end
end
end
describe "when step is a String" do
describe "with self and stop as Fixnums" do
it "raises an ArgumentError when step is a numeric representation" do
-> { @step.call(1, 5, "1") {} }.should raise_error(ArgumentError)
-> { @step.call(1, 5, "0.1") {} }.should raise_error(ArgumentError)
-> { @step.call(1, 5, "1/3") {} }.should raise_error(ArgumentError)
end
it "raises an ArgumentError with step as an alphanumeric string" do
-> { @step.call(1, 5, "foo") {} }.should raise_error(ArgumentError)
end
end
describe "with self and stop as Floats" do
it "raises an ArgumentError when step is a numeric representation" do
-> { @step.call(1.1, 5.1, "1") {} }.should raise_error(ArgumentError)
-> { @step.call(1.1, 5.1, "0.1") {} }.should raise_error(ArgumentError)
-> { @step.call(1.1, 5.1, "1/3") {} }.should raise_error(ArgumentError)
end
it "raises an ArgumentError with step as an alphanumeric string" do
-> { @step.call(1.1, 5.1, "foo") {} }.should raise_error(ArgumentError)
end
end
end
it "does not rescue ArgumentError exceptions" do
-> { @step.call(1, 2) { raise ArgumentError, "" }}.should raise_error(ArgumentError)
end
it "does not rescue TypeError exceptions" do
-> { @step.call(1, 2) { raise TypeError, "" } }.should raise_error(TypeError)
end
describe "when no block is given" do
step_enum_class = Enumerator
ruby_version_is "2.6" do
step_enum_class = Enumerator::ArithmeticSequence
end
it "returns an #{step_enum_class} when step is 0" do
@step.call(1, 2, 0).should be_an_instance_of(step_enum_class)
end
it "returns an #{step_enum_class} when not passed a block and self > stop" do
@step.call(1, 0, 2).should be_an_instance_of(step_enum_class)
end
it "returns an #{step_enum_class} when not passed a block and self < stop" do
@step.call(1, 2, 3).should be_an_instance_of(step_enum_class)
end
it "returns an #{step_enum_class} that uses the given step" do
@step.call(0, 5, 2).to_a.should eql [0, 2, 4]
end
describe "when step is a String" do
describe "with self and stop as Fixnums" do
it "returns an Enumerator" do
@step.call(1, 5, "foo").should be_an_instance_of(Enumerator)
end
end
describe "with self and stop as Floats" do
it "returns an Enumerator" do
@step.call(1.1, 5.1, "foo").should be_an_instance_of(Enumerator)
end
end
end
describe "returned Enumerator" do
describe "size" do
describe "when step is a String" do
describe "with self and stop as Fixnums" do
it "raises an ArgumentError when step is a numeric representation" do
-> { @step.call(1, 5, "1").size }.should raise_error(ArgumentError)
-> { @step.call(1, 5, "0.1").size }.should raise_error(ArgumentError)
-> { @step.call(1, 5, "1/3").size }.should raise_error(ArgumentError)
end
it "raises an ArgumentError with step as an alphanumeric string" do
-> { @step.call(1, 5, "foo").size }.should raise_error(ArgumentError)
end
end
describe "with self and stop as Floats" do
it "raises an ArgumentError when step is a numeric representation" do
-> { @step.call(1.1, 5.1, "1").size }.should raise_error(ArgumentError)
-> { @step.call(1.1, 5.1, "0.1").size }.should raise_error(ArgumentError)
-> { @step.call(1.1, 5.1, "1/3").size }.should raise_error(ArgumentError)
end
it "raises an ArgumentError with step as an alphanumeric string" do
-> { @step.call(1.1, 5.1, "foo").size }.should raise_error(ArgumentError)
end
end
end
describe "when self, stop and step are Fixnums and step is positive" do
it "returns the difference between self and stop divided by the number of steps" do
@step.call(5, 10, 11).size.should == 1
@step.call(5, 10, 6).size.should == 1
@step.call(5, 10, 5).size.should == 2
@step.call(5, 10, 4).size.should == 2
@step.call(5, 10, 2).size.should == 3
@step.call(5, 10, 1).size.should == 6
@step.call(5, 10).size.should == 6
@step.call(10, 10, 1).size.should == 1
end
it "returns 0 if value > limit" do
@step.call(11, 10, 1).size.should == 0
end
end
describe "when self, stop and step are Fixnums and step is negative" do
it "returns the difference between self and stop divided by the number of steps" do
@step.call(10, 5, -11).size.should == 1
@step.call(10, 5, -6).size.should == 1
@step.call(10, 5, -5).size.should == 2
@step.call(10, 5, -4).size.should == 2
@step.call(10, 5, -2).size.should == 3
@step.call(10, 5, -1).size.should == 6
@step.call(10, 10, -1).size.should == 1
end
it "returns 0 if value < limit" do
@step.call(10, 11, -1).size.should == 0
end
end
describe "when self, stop or step is a Float" do
describe "and step is positive" do
it "returns the difference between self and stop divided by the number of steps" do
@step.call(5, 10, 11.0).size.should == 1
@step.call(5, 10, 6.0).size.should == 1
@step.call(5, 10, 5.0).size.should == 2
@step.call(5, 10, 4.0).size.should == 2
@step.call(5, 10, 2.0).size.should == 3
@step.call(5, 10, 0.5).size.should == 11
@step.call(5, 10, 1.0).size.should == 6
@step.call(5, 10.5).size.should == 6
@step.call(10, 10, 1.0).size.should == 1
end
it "returns 0 if value > limit" do
@step.call(10, 5.5).size.should == 0
@step.call(11, 10, 1.0).size.should == 0
@step.call(11, 10, 1.5).size.should == 0
@step.call(10, 5, infinity_value).size.should == 0
end
it "returns 1 if step is infinity_value" do
@step.call(5, 10, infinity_value).size.should == 1
end
end
describe "and step is negative" do
it "returns the difference between self and stop divided by the number of steps" do
@step.call(10, 5, -11.0).size.should == 1
@step.call(10, 5, -6.0).size.should == 1
@step.call(10, 5, -5.0).size.should == 2
@step.call(10, 5, -4.0).size.should == 2
@step.call(10, 5, -2.0).size.should == 3
@step.call(10, 5, -0.5).size.should == 11
@step.call(10, 5, -1.0).size.should == 6
@step.call(10, 10, -1.0).size.should == 1
end
it "returns 0 if value < limit" do
@step.call(10, 11, -1.0).size.should == 0
@step.call(10, 11, -1.5).size.should == 0
@step.call(5, 10, -infinity_value).size.should == 0
end
it "returns 1 if step is infinity_value" do
@step.call(10, 5, -infinity_value).size.should == 1
end
end
end
describe "when stop is not passed" do
it "returns infinity_value" do
@step.call(1).size.should == infinity_value
end
end
describe "when stop is nil" do
it "returns infinity_value" do
@step.call(1, nil, 5).size.should == infinity_value
end
end
end
end
end
end
|