mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Add version guards for Enumerator::ArithmeticSequence
* And keep specs for older versions. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64411 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
5032f6377b
commit
f3b347e109
3 changed files with 77 additions and 27 deletions
|
@ -258,19 +258,24 @@ describe :numeric_step, :shared => true do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "when no block is given" do
|
describe "when no block is given" do
|
||||||
it "returns an Enumerator when step is 0" do
|
step_enum_class = Enumerator
|
||||||
1.send(@method, *@step_args.call(2, 0)).should be_an_instance_of(Enumerator::ArithmeticSequence)
|
ruby_version_is "2.6" do
|
||||||
|
step_enum_class = Enumerator::ArithmeticSequence
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns an Enumerator when not passed a block and self > stop" do
|
it "returns an #{step_enum_class} when step is 0" do
|
||||||
1.send(@method, *@step_args.call(0, 2)).should be_an_instance_of(Enumerator::ArithmeticSequence)
|
1.send(@method, *@step_args.call(2, 0)).should be_an_instance_of(step_enum_class)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns an Enumerator when not passed a block and self < stop" do
|
it "returns an #{step_enum_class} when not passed a block and self > stop" do
|
||||||
1.send(@method, *@step_args.call(2, 3)).should be_an_instance_of(Enumerator::ArithmeticSequence)
|
1.send(@method, *@step_args.call(0, 2)).should be_an_instance_of(step_enum_class)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns an Enumerator that uses the given step" do
|
it "returns an #{step_enum_class} when not passed a block and self < stop" do
|
||||||
|
1.send(@method, *@step_args.call(2, 3)).should be_an_instance_of(step_enum_class)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns an #{step_enum_class} that uses the given step" do
|
||||||
0.send(@method, *@step_args.call(5, 2)).to_a.should eql [0, 2, 4]
|
0.send(@method, *@step_args.call(5, 2)).to_a.should eql [0, 2, 4]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -22,24 +22,43 @@ describe "Numeric#step" do
|
||||||
it_behaves_like :numeric_step, :step
|
it_behaves_like :numeric_step, :step
|
||||||
|
|
||||||
describe "when no block is given" do
|
describe "when no block is given" do
|
||||||
it "returns an Enumerator when step is 0" do
|
step_enum_class = Enumerator
|
||||||
1.step(5, 0).should be_an_instance_of(Enumerator::ArithmeticSequence)
|
ruby_version_is "2.6" do
|
||||||
|
step_enum_class = Enumerator::ArithmeticSequence
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns an Enumerator when step is 0.0" do
|
it "returns an #{step_enum_class} when step is 0" do
|
||||||
1.step(2, 0.0).should be_an_instance_of(Enumerator::ArithmeticSequence)
|
1.step(5, 0).should be_an_instance_of(step_enum_class)
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "returned Enumerator" do
|
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
|
||||||
describe "size" do
|
describe "size" do
|
||||||
it "raises an ArgumentError when step is 0" do
|
ruby_version_is ""..."2.6" do
|
||||||
enum = 1.step(5, 0)
|
it "raises an ArgumentError when step is 0" do
|
||||||
enum.size.should == Float::INFINITY
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
it "raises an ArgumentError when step is 0.0" do
|
ruby_version_is "2.6" do
|
||||||
enum = 1.step(2, 0.0)
|
it "is infinity when step is 0" do
|
||||||
enum.size.should == Float::INFINITY
|
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
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,13 +1,18 @@
|
||||||
require_relative '../../spec_helper'
|
require_relative '../../spec_helper'
|
||||||
|
|
||||||
describe "Range#step" do
|
describe "Range#step" do
|
||||||
|
step_enum_class = Enumerator
|
||||||
|
ruby_version_is "2.6" do
|
||||||
|
step_enum_class = Enumerator::ArithmeticSequence
|
||||||
|
end
|
||||||
|
|
||||||
before :each do
|
before :each do
|
||||||
ScratchPad.record []
|
ScratchPad.record []
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns an arithmetic sequence when no block is given" do
|
it "returns an #{step_enum_class} when no block is given" do
|
||||||
enum = (1..10).step(4)
|
enum = (1..10).step(4)
|
||||||
enum.should be_an_instance_of(Enumerator::ArithmeticSequence)
|
enum.should be_an_instance_of(step_enum_class)
|
||||||
enum.to_a.should eql([1, 5, 9])
|
enum.to_a.should eql([1, 5, 9])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -263,7 +268,7 @@ describe "Range#step" do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "when no block is given" do
|
describe "when no block is given" do
|
||||||
describe "returned Enumerator::ArithmeticSequence" do
|
describe "returned #{step_enum_class}" do
|
||||||
describe "size" do
|
describe "size" do
|
||||||
it "raises a TypeError if step does not respond to #to_int" do
|
it "raises a TypeError if step does not respond to #to_int" do
|
||||||
obj = mock("Range#step non-integer")
|
obj = mock("Range#step non-integer")
|
||||||
|
@ -279,9 +284,28 @@ describe "Range#step" do
|
||||||
lambda { enum.size }.should raise_error(TypeError)
|
lambda { enum.size }.should raise_error(TypeError)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns Float::INFINITY for zero step" do
|
ruby_version_is ""..."2.6" do
|
||||||
(-1..1).step(0).size.should == Float::INFINITY
|
it "raises an ArgumentError if step is 0" do
|
||||||
(-1..1).step(0.0).size.should == Float::INFINITY
|
enum = (-1..1).step(0)
|
||||||
|
lambda { enum.size }.should raise_error(ArgumentError)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "raises an ArgumentError if step is 0.0" do
|
||||||
|
enum = (-1..1).step(0.0)
|
||||||
|
lambda { enum.size }.should raise_error(ArgumentError)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "raises an ArgumentError if step is negative" do
|
||||||
|
enum = (-1..1).step(-2)
|
||||||
|
lambda { enum.size }.should raise_error(ArgumentError)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
ruby_version_is "2.6" do
|
||||||
|
it "returns Float::INFINITY for zero step" do
|
||||||
|
(-1..1).step(0).size.should == Float::INFINITY
|
||||||
|
(-1..1).step(0.0).size.should == Float::INFINITY
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns the ceil of range size divided by the number of steps" do
|
it "returns the ceil of range size divided by the number of steps" do
|
||||||
|
@ -297,9 +321,11 @@ describe "Range#step" do
|
||||||
(-5...5).step(2).size.should == 5
|
(-5...5).step(2).size.should == 5
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns the ceil of range size divided by the number of steps even if step is negative" do
|
ruby_version_is "2.6" do
|
||||||
(-1..1).step(-1).size.should == 0
|
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 == 3
|
(-1..1).step(-1).size.should == 0
|
||||||
|
(1..-1).step(-1).size.should == 3
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns the correct number of steps when one of the arguments is a float" do
|
it "returns the correct number of steps when one of the arguments is a float" do
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue