diff options
Diffstat (limited to 'spec')
-rw-r--r-- | spec/ruby/core/enumerator/arithmetic_sequence/step_spec.rb | 2 | ||||
-rw-r--r-- | spec/ruby/core/numeric/shared/step.rb | 6 | ||||
-rw-r--r-- | spec/ruby/core/numeric/step_spec.rb | 90 | ||||
-rw-r--r-- | spec/ruby/core/range/step_spec.rb | 43 |
4 files changed, 92 insertions, 49 deletions
diff --git a/spec/ruby/core/enumerator/arithmetic_sequence/step_spec.rb b/spec/ruby/core/enumerator/arithmetic_sequence/step_spec.rb index 20a5cb6e7b..8b00fd4309 100644 --- a/spec/ruby/core/enumerator/arithmetic_sequence/step_spec.rb +++ b/spec/ruby/core/enumerator/arithmetic_sequence/step_spec.rb @@ -5,11 +5,9 @@ ruby_version_is "2.6" do it "returns the original value given to step method" do (1..10).step.step.should == 1 (1..10).step(3).step.should == 3 - (1..10).step(0).step.should == 0 1.step(10).step.should == 1 1.step(10, 3).step.should == 3 - 1.step(10, 0).step.should == 0 end end end diff --git a/spec/ruby/core/numeric/shared/step.rb b/spec/ruby/core/numeric/shared/step.rb index fac79b3e63..6c47cdeecb 100644 --- a/spec/ruby/core/numeric/shared/step.rb +++ b/spec/ruby/core/numeric/shared/step.rb @@ -261,8 +261,10 @@ describe :numeric_step, :shared => true 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) + ruby_version_is ""..."3.0" do + 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 end it "returns an #{step_enum_class} when not passed a block and self > stop" do diff --git a/spec/ruby/core/numeric/step_spec.rb b/spec/ruby/core/numeric/step_spec.rb index e9067864c8..2773000229 100644 --- a/spec/ruby/core/numeric/step_spec.rb +++ b/spec/ruby/core/numeric/step_spec.rb @@ -26,12 +26,14 @@ describe "Numeric#step" do step_enum_class = Enumerator::ArithmeticSequence end - it "returns an #{step_enum_class} when step is 0" do - 1.step(5, 0).should be_an_instance_of(step_enum_class) - end + 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) + 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 @@ -48,7 +50,7 @@ describe "Numeric#step" do end end - ruby_version_is "2.6" do + ruby_version_is "2.6"..."3.0" do it "is infinity when step is 0" do enum = 1.step(5, 0) enum.size.should == Float::INFINITY @@ -85,18 +87,20 @@ describe "Numeric#step" do end describe 'with keyword arguments' do - it "doesn't raise an error when step is 0" do - -> { 1.step(to: 5, by: 0) { break } }.should_not raise_error - end + 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 "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] + 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 @@ -106,12 +110,14 @@ describe "Numeric#step" do 1.step(by: 42).size.should == infinity_value end - it "should return infinity_value when step is 0" do - 1.step(to: 5, by: 0).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 + 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 @@ -146,12 +152,24 @@ describe "Numeric#step" do end describe 'with mixed arguments' do - it "doesn't raise an error when step is 0" do - -> { 1.step(5, by: 0) { break } }.should_not raise_error + 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 - it "doesn't raise an error when step is 0.0" do - -> { 1.step(2, by: 0.0) { break } }.should_not raise_error + 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 @@ -162,21 +180,25 @@ describe "Numeric#step" do -> { 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] + 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 - it "should return infinity_value when step is 0" do - 1.step(5, by: 0).size.should == infinity_value - end + 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 + 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 diff --git a/spec/ruby/core/range/step_spec.rb b/spec/ruby/core/range/step_spec.rb index d83868a0fb..e284551ab3 100644 --- a/spec/ruby/core/range/step_spec.rb +++ b/spec/ruby/core/range/step_spec.rb @@ -297,7 +297,7 @@ describe "Range#step" do it "yields Float values incremented by a Float step" do eval("(-2..)").step(1.5) { |x| break if x > 1.0; ScratchPad << x } - ScratchPad.recorded.should eql([-2.0, -0.5, 1.0])\ + ScratchPad.recorded.should eql([-2.0, -0.5, 1.0]) ScratchPad.record [] eval("(-2..)").step(1.5) { |x| break if x > 1.0; ScratchPad << x } @@ -371,20 +371,41 @@ describe "Range#step" do end describe "when no block is given" do + ruby_version_is "3.0" do + it "raises an ArgumentError if step is 0" do + -> { (-1..1).step(0) }.should raise_error(ArgumentError) + end + end + describe "returned Enumerator" do describe "size" do - it "raises a TypeError if step does not respond to #to_int" do - obj = mock("Range#step non-integer") - enum = (1..2).step(obj) - -> { enum.size }.should raise_error(TypeError) + ruby_version_is ""..."3.0" do + it "raises a TypeError if step does not respond to #to_int" do + obj = mock("Range#step non-integer") + enum = (1..2).step(obj) + -> { enum.size }.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") + enum = (1..2).step(obj) + + -> { enum.size }.should raise_error(TypeError) + end 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") - enum = (1..2).step(obj) + ruby_version_is "3.0" 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 - -> { enum.size }.should raise_error(TypeError) + 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 ""..."2.6" do @@ -404,7 +425,7 @@ describe "Range#step" do end end - ruby_version_is "2.6" do + ruby_version_is "2.6"..."3.0" 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 |