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
|
require_relative '../../spec_helper'
require_relative 'fixtures/classes'
require_relative 'shared/step'
describe "Numeric#step" do
describe 'with positional args' do
it "raises an ArgumentError when step is 0" do
-> { 1.step(5, 0) {} }.should raise_error(ArgumentError)
end
it "raises an ArgumentError when step is 0.0" do
-> { 1.step(2, 0.0) {} }.should raise_error(ArgumentError)
end
before :all do
# This lambda definition limits to return the arguments it receives.
# It's needed to test numeric_step behaviour with positional arguments.
@step = -> receiver, *args, &block { receiver.step(*args, &block) }
end
it_behaves_like :numeric_step, :step
describe "when no block is given" do
step_enum_class = Enumerator::ArithmeticSequence
ruby_version_is ""..."3.0" do
it "returns an #{step_enum_class} when step is 0" do
1.step(5, 0).should be_an_instance_of(step_enum_class)
end
it "returns an #{step_enum_class} when step is 0.0" do
1.step(2, 0.0).should be_an_instance_of(step_enum_class)
end
end
describe "returned #{step_enum_class}" do
describe "size" do
ruby_version_is ""..."3.0" do
it "is infinity when step is 0" do
enum = 1.step(5, 0)
enum.size.should == Float::INFINITY
end
it "is infinity when step is 0.0" do
enum = 1.step(2, 0.0)
enum.size.should == Float::INFINITY
end
end
it "defaults to an infinite size" do
enum = 1.step
enum.size.should == Float::INFINITY
end
end
describe "type" do
it "returns an instance of Enumerator::ArithmeticSequence" do
1.step(10).class.should == Enumerator::ArithmeticSequence
end
end
end
end
end
describe 'with keyword arguments' do
ruby_version_is ""..."3.0" do
it "doesn't raise an error when step is 0" do
-> { 1.step(to: 5, by: 0) { break } }.should_not raise_error
end
it "doesn't raise an error when step is 0.0" do
-> { 1.step(to: 2, by: 0.0) { break } }.should_not raise_error
end
it "should loop over self when step is 0 or 0.0" do
1.step(to: 2, by: 0.0).take(5).should eql [1.0, 1.0, 1.0, 1.0, 1.0]
1.step(to: 2, by: 0).take(5).should eql [1, 1, 1, 1, 1]
1.1.step(to: 2, by: 0).take(5).should eql [1.1, 1.1, 1.1, 1.1, 1.1]
end
end
describe "when no block is given" do
describe "returned Enumerator" do
describe "size" do
it "should return infinity_value when limit is nil" do
1.step(by: 42).size.should == infinity_value
end
ruby_version_is ""..."3.0" do
it "should return infinity_value when step is 0" do
1.step(to: 5, by: 0).size.should == infinity_value
end
it "should return infinity_value when step is 0.0" do
1.step(to: 2, by: 0.0).size.should == infinity_value
end
end
it "should return infinity_value when ascending towards a limit of Float::INFINITY" do
1.step(to: Float::INFINITY, by: 42).size.should == infinity_value
end
it "should return infinity_value when descending towards a limit of -Float::INFINITY" do
1.step(to: -Float::INFINITY, by: -42).size.should == infinity_value
end
it "should return 1 when the both limit and step are Float::INFINITY" do
1.step(to: Float::INFINITY, by: Float::INFINITY).size.should == 1
end
it "should return 1 when the both limit and step are -Float::INFINITY" do
1.step(to: -Float::INFINITY, by: -Float::INFINITY).size.should == 1
end
end
end
end
before :all do
# This lambda transforms a positional step method args into keyword arguments.
# It's needed to test numeric_step behaviour with keyword arguments.
@step = -> receiver, *args, &block do
kw_args = { to: args[0] }
kw_args[:by] = args[1] if args.size == 2
receiver.step(**kw_args, &block)
end
end
it_behaves_like :numeric_step, :step
end
describe 'with mixed arguments' do
ruby_version_is ""..."3.0" do
it "doesn't raise an error when step is 0" do
-> { 1.step(5, by: 0) { break } }.should_not raise_error
end
it "doesn't raise an error when step is 0.0" do
-> { 1.step(2, by: 0.0) { break } }.should_not raise_error
end
end
ruby_version_is "3.0" do
it " raises an ArgumentError when step is 0" do
-> { 1.step(5, by: 0) { break } }.should raise_error(ArgumentError)
end
it "raises an ArgumentError when step is 0.0" do
-> { 1.step(2, by: 0.0) { break } }.should raise_error(ArgumentError)
end
end
it "raises a ArgumentError when limit and to are defined" do
-> { 1.step(5, 1, to: 5) { break } }.should raise_error(ArgumentError)
end
it "raises a ArgumentError when step and by are defined" do
-> { 1.step(5, 1, by: 5) { break } }.should raise_error(ArgumentError)
end
ruby_version_is ""..."3.0" do
it "should loop over self when step is 0 or 0.0" do
1.step(2, by: 0.0).take(5).should eql [1.0, 1.0, 1.0, 1.0, 1.0]
1.step(2, by: 0).take(5).should eql [1, 1, 1, 1, 1]
1.1.step(2, by: 0).take(5).should eql [1.1, 1.1, 1.1, 1.1, 1.1]
end
end
describe "when no block is given" do
describe "returned Enumerator" do
describe "size" do
ruby_version_is ""..."3.0" do
it "should return infinity_value when step is 0" do
1.step(5, by: 0).size.should == infinity_value
end
it "should return infinity_value when step is 0.0" do
1.step(2, by: 0.0).size.should == infinity_value
end
end
end
end
end
before :all do
# This lambda definition transforms a positional step method args into
# a mix of positional and keyword arguments.
# It's needed to test numeric_step behaviour with positional mixed with
# keyword arguments.
@step = -> receiver, *args, &block do
if args.size == 2
receiver.step(args[0], by: args[1], &block)
else
receiver.step(*args, &block)
end
end
end
it_behaves_like :numeric_step, :step
end
end
|