2018-03-04 10:09:32 -05:00
|
|
|
require_relative '../../spec_helper'
|
|
|
|
require_relative 'fixtures/classes'
|
|
|
|
require_relative 'shared/step'
|
2017-05-07 08:04:49 -04:00
|
|
|
|
|
|
|
describe "Numeric#step" do
|
|
|
|
|
|
|
|
describe 'with positional args' do
|
|
|
|
it "raises an ArgumentError when step is 0" do
|
|
|
|
lambda { 1.step(5, 0) {} }.should raise_error(ArgumentError)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an ArgumentError when step is 0.0" do
|
|
|
|
lambda { 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_args = ->(*args) { args }
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like :numeric_step, :step
|
|
|
|
|
|
|
|
describe "when no block is given" do
|
2018-08-17 07:22:30 -04:00
|
|
|
step_enum_class = Enumerator
|
|
|
|
ruby_version_is "2.6" do
|
|
|
|
step_enum_class = Enumerator::ArithmeticSequence
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
|
2018-08-17 07:22:30 -04:00
|
|
|
it "returns an #{step_enum_class} when step is 0" do
|
|
|
|
1.step(5, 0).should be_an_instance_of(step_enum_class)
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
|
2018-08-17 07:22:30 -04:00
|
|
|
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
|
|
|
|
|
|
|
|
describe "returned #{step_enum_class}" do
|
2017-05-07 08:04:49 -04:00
|
|
|
describe "size" do
|
2018-08-17 07:22:30 -04:00
|
|
|
ruby_version_is ""..."2.6" do
|
|
|
|
it "raises an ArgumentError when step is 0" do
|
|
|
|
enum = 1.step(5, 0)
|
|
|
|
lambda { enum.size }.should raise_error(ArgumentError)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an ArgumentError when step is 0.0" do
|
|
|
|
enum = 1.step(2, 0.0)
|
|
|
|
lambda { enum.size }.should raise_error(ArgumentError)
|
|
|
|
end
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
|
2018-08-17 07:22:30 -04:00
|
|
|
ruby_version_is "2.6" 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
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
2019-02-21 10:38:59 -05:00
|
|
|
|
|
|
|
it "defaults to an infinite size" do
|
|
|
|
enum = 1.step
|
|
|
|
enum.size.should == Float::INFINITY
|
|
|
|
end
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
2019-01-20 15:38:57 -05:00
|
|
|
|
|
|
|
describe "type" do
|
|
|
|
ruby_version_is ""..."2.6" do
|
|
|
|
it "returns an instance of Enumerator" do
|
|
|
|
1.step(10).class.should == Enumerator
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
ruby_version_is "2.6" do
|
|
|
|
it "returns an instance of Enumerator::ArithmeticSequence" do
|
|
|
|
1.step(10).class.should == Enumerator::ArithmeticSequence
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'with keyword arguments' do
|
|
|
|
it "doesn't raise an error when step is 0" do
|
|
|
|
lambda { 1.step(to: 5, by: 0) { break } }.should_not raise_error
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't raise an error when step is 0.0" do
|
|
|
|
lambda { 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
|
|
|
|
|
|
|
|
describe "when no block is given" do
|
|
|
|
describe "returned Enumerator" do
|
|
|
|
describe "size" do
|
2017-07-27 08:10:41 -04:00
|
|
|
it "should return infinity_value when limit is nil" do
|
|
|
|
1.step(by: 42).size.should == infinity_value
|
|
|
|
end
|
|
|
|
|
2017-05-07 08:04:49 -04:00
|
|
|
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
|
|
|
|
|
2017-07-27 08:10:41 -04:00
|
|
|
it "should return infinity_value when ascending towards a limit of Float::INFINITY" do
|
2017-05-07 08:04:49 -04:00
|
|
|
1.step(to: Float::INFINITY, by: 42).size.should == infinity_value
|
|
|
|
end
|
|
|
|
|
2019-02-07 11:35:33 -05:00
|
|
|
it "should return infinity_value when descending towards a limit of -Float::INFINITY" do
|
2017-07-27 08:10:41 -04:00
|
|
|
1.step(to: -Float::INFINITY, by: -42).size.should == infinity_value
|
|
|
|
end
|
|
|
|
|
2017-05-07 08:04:49 -04:00
|
|
|
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
|
2017-07-27 08:10:41 -04:00
|
|
|
|
|
|
|
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
|
2017-05-07 08:04:49 -04:00
|
|
|
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_args = ->(*args) do
|
|
|
|
kw_args = {to: args[0]}
|
|
|
|
kw_args[:by] = args[1] if args.size == 2
|
|
|
|
[kw_args]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
it_behaves_like :numeric_step, :step
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'with mixed arguments' do
|
|
|
|
it "doesn't raise an error when step is 0" do
|
|
|
|
lambda { 1.step(5, by: 0) { break } }.should_not raise_error
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't raise an error when step is 0.0" do
|
|
|
|
lambda { 1.step(2, by: 0.0) { break } }.should_not raise_error
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises a ArgumentError when limit and to are defined" do
|
|
|
|
lambda { 1.step(5, 1, to: 5) { break } }.should raise_error(ArgumentError)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises a ArgumentError when step and by are defined" do
|
|
|
|
lambda { 1.step(5, 1, by: 5) { break } }.should raise_error(ArgumentError)
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
describe "when no block is given" do
|
|
|
|
describe "returned Enumerator" do
|
|
|
|
describe "size" 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
|
|
|
|
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_args = ->(*args) do
|
|
|
|
if args.size == 2
|
|
|
|
[args[0], {by: args[1]}]
|
|
|
|
else
|
|
|
|
args
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
it_behaves_like :numeric_step, :step
|
|
|
|
end
|
|
|
|
end
|