diff options
Diffstat (limited to 'spec/ruby/core/numeric')
28 files changed, 319 insertions, 383 deletions
diff --git a/spec/ruby/core/numeric/abs2_spec.rb b/spec/ruby/core/numeric/abs2_spec.rb index 0e60cd0ae7..12866f9c47 100644 --- a/spec/ruby/core/numeric/abs2_spec.rb +++ b/spec/ruby/core/numeric/abs2_spec.rb @@ -18,7 +18,7 @@ describe "Numeric#abs2" do it "returns the square of the absolute value of self" do @numbers.each do |number| - number.abs2.should eql(number.abs ** 2) + number.abs2.should.eql?(number.abs ** 2) end end @@ -29,6 +29,6 @@ describe "Numeric#abs2" do end it "returns NaN when self is NaN" do - nan_value.abs2.nan?.should be_true + nan_value.abs2.nan?.should == true end end diff --git a/spec/ruby/core/numeric/clone_spec.rb b/spec/ruby/core/numeric/clone_spec.rb new file mode 100644 index 0000000000..1d06fdb050 --- /dev/null +++ b/spec/ruby/core/numeric/clone_spec.rb @@ -0,0 +1,30 @@ +require_relative '../../spec_helper' + +describe "Numeric#clone" do + it "returns self" do + value = 1 + value.clone.should.equal?(value) + + subclass = Class.new(Numeric) + value = subclass.new + value.clone.should.equal?(value) + end + + it "does not change frozen status" do + 1.clone.frozen?.should == true + end + + it "accepts optional keyword argument :freeze" do + value = 1 + value.clone(freeze: true).should.equal?(value) + end + + it "raises ArgumentError if passed freeze: false" do + -> { 1.clone(freeze: false) }.should.raise(ArgumentError, /can't unfreeze/) + end + + it "does not change frozen status if passed freeze: nil" do + value = 1 + value.clone(freeze: nil).should.equal?(value) + end +end diff --git a/spec/ruby/core/numeric/coerce_spec.rb b/spec/ruby/core/numeric/coerce_spec.rb index c0136b181e..9344d99ee6 100644 --- a/spec/ruby/core/numeric/coerce_spec.rb +++ b/spec/ruby/core/numeric/coerce_spec.rb @@ -25,7 +25,7 @@ describe "Numeric#coerce" do class << a; true; end # watch it explode - lambda { a.coerce(b) }.should raise_error(TypeError) + -> { a.coerce(b) }.should.raise(TypeError) end end @@ -38,22 +38,22 @@ describe "Numeric#coerce" do it "raise TypeError if they are instances of different classes and other does not respond to #to_f" do other = mock("numeric") - lambda { @obj.coerce(other) }.should raise_error(TypeError) + -> { @obj.coerce(other) }.should.raise(TypeError) end it "raises a TypeError when passed nil" do - lambda { @obj.coerce(nil) }.should raise_error(TypeError) + -> { @obj.coerce(nil) }.should.raise(TypeError) end it "raises a TypeError when passed a boolean" do - lambda { @obj.coerce(false) }.should raise_error(TypeError) + -> { @obj.coerce(false) }.should.raise(TypeError) end it "raises a TypeError when passed a Symbol" do - lambda { @obj.coerce(:symbol) }.should raise_error(TypeError) + -> { @obj.coerce(:symbol) }.should.raise(TypeError) end it "raises an ArgumentError when passed a non-numeric String" do - lambda { @obj.coerce("test") }.should raise_error(ArgumentError) + -> { @obj.coerce("test") }.should.raise(ArgumentError) end end diff --git a/spec/ruby/core/numeric/comparison_spec.rb b/spec/ruby/core/numeric/comparison_spec.rb index 4b4d52501a..b0a9390cc0 100644 --- a/spec/ruby/core/numeric/comparison_spec.rb +++ b/spec/ruby/core/numeric/comparison_spec.rb @@ -26,22 +26,22 @@ describe "Numeric#<=>" do end it "is called when instances are compared with #<" do - (@a < @b).should be_false + (@a < @b).should == false ScratchPad.recorded.should == :numeric_comparison end it "is called when instances are compared with #<=" do - (@a <= @b).should be_false + (@a <= @b).should == false ScratchPad.recorded.should == :numeric_comparison end it "is called when instances are compared with #>" do - (@a > @b).should be_true + (@a > @b).should == true ScratchPad.recorded.should == :numeric_comparison end it "is called when instances are compared with #>=" do - (@a >= @b).should be_true + (@a >= @b).should == true ScratchPad.recorded.should == :numeric_comparison end end diff --git a/spec/ruby/core/numeric/div_spec.rb b/spec/ruby/core/numeric/div_spec.rb index 0017311487..a17961850c 100644 --- a/spec/ruby/core/numeric/div_spec.rb +++ b/spec/ruby/core/numeric/div_spec.rb @@ -15,8 +15,8 @@ describe "Numeric#div" do end it "raises ZeroDivisionError for 0" do - lambda { @obj.div(0) }.should raise_error(ZeroDivisionError) - lambda { @obj.div(0.0) }.should raise_error(ZeroDivisionError) - lambda { @obj.div(Complex(0,0)) }.should raise_error(ZeroDivisionError) + -> { @obj.div(0) }.should.raise(ZeroDivisionError) + -> { @obj.div(0.0) }.should.raise(ZeroDivisionError) + -> { @obj.div(Complex(0,0)) }.should.raise(ZeroDivisionError) end end diff --git a/spec/ruby/core/numeric/dup_spec.rb b/spec/ruby/core/numeric/dup_spec.rb new file mode 100644 index 0000000000..c158c618de --- /dev/null +++ b/spec/ruby/core/numeric/dup_spec.rb @@ -0,0 +1,16 @@ +require_relative '../../spec_helper' + +describe "Numeric#dup" do + it "returns self" do + value = 1 + value.dup.should.equal?(value) + + subclass = Class.new(Numeric) + value = subclass.new + value.dup.should.equal?(value) + end + + it "does not change frozen status" do + 1.dup.frozen?.should == true + end +end diff --git a/spec/ruby/core/numeric/eql_spec.rb b/spec/ruby/core/numeric/eql_spec.rb index b33e00e51f..80c58caef4 100644 --- a/spec/ruby/core/numeric/eql_spec.rb +++ b/spec/ruby/core/numeric/eql_spec.rb @@ -7,16 +7,16 @@ describe "Numeric#eql?" do end it "returns false if self's and other's types don't match" do - @obj.should_not eql(1) - @obj.should_not eql(-1.5) - @obj.should_not eql(bignum_value) - @obj.should_not eql(:sym) + @obj.should_not.eql?(1) + @obj.should_not.eql?(-1.5) + @obj.should_not.eql?(bignum_value) + @obj.should_not.eql?(:sym) end it "returns the result of calling self#== with other when self's and other's types match" do other = NumericSpecs::Subclass.new @obj.should_receive(:==).with(other).and_return("result", nil) - @obj.should eql(other) - @obj.should_not eql(other) + @obj.should.eql?(other) + @obj.should_not.eql?(other) end end diff --git a/spec/ruby/core/numeric/fdiv_spec.rb b/spec/ruby/core/numeric/fdiv_spec.rb index 907e5d343c..2c22c6a86a 100644 --- a/spec/ruby/core/numeric/fdiv_spec.rb +++ b/spec/ruby/core/numeric/fdiv_spec.rb @@ -1,5 +1,4 @@ require_relative '../../spec_helper' -require_relative 'shared/quo' describe "Numeric#fdiv" do it "coerces self with #to_f" do @@ -19,7 +18,7 @@ describe "Numeric#fdiv" do end it "returns a Float" do - bignum_value.fdiv(Float::MAX).should be_an_instance_of(Float) + bignum_value.fdiv(Float::MAX).should.instance_of?(Float) end it "returns Infinity if other is 0" do @@ -27,6 +26,6 @@ describe "Numeric#fdiv" do end it "returns NaN if other is NaN" do - 3334.fdiv(nan_value).nan?.should be_true + 3334.fdiv(nan_value).nan?.should == true end end diff --git a/spec/ruby/core/numeric/finite_spec.rb b/spec/ruby/core/numeric/finite_spec.rb index a4df23602b..2c18c89466 100644 --- a/spec/ruby/core/numeric/finite_spec.rb +++ b/spec/ruby/core/numeric/finite_spec.rb @@ -1,10 +1,8 @@ require_relative '../../spec_helper' -ruby_version_is "2.4" do - describe "Numeric#finite?" do - it "returns true by default" do - o = mock_numeric("finite") - o.finite?.should be_true - end +describe "Numeric#finite?" do + it "returns true by default" do + o = mock_numeric("finite") + o.finite?.should == true end end diff --git a/spec/ruby/core/numeric/i_spec.rb b/spec/ruby/core/numeric/i_spec.rb index 621ecc09ec..f5fb99dfd3 100644 --- a/spec/ruby/core/numeric/i_spec.rb +++ b/spec/ruby/core/numeric/i_spec.rb @@ -2,7 +2,7 @@ require_relative '../../spec_helper' describe "Numeric#i" do it "returns a Complex object" do - 34.i.should be_an_instance_of(Complex) + 34.i.should.instance_of?(Complex) end it "sets the real part to 0" do diff --git a/spec/ruby/core/numeric/infinite_spec.rb b/spec/ruby/core/numeric/infinite_spec.rb index 7ed2f6d125..3ea7825c8c 100644 --- a/spec/ruby/core/numeric/infinite_spec.rb +++ b/spec/ruby/core/numeric/infinite_spec.rb @@ -1,10 +1,8 @@ require_relative '../../spec_helper' -ruby_version_is "2.4" do - describe "Numeric#infinite?" do - it "returns nil by default" do - o = mock_numeric("infinite") - o.infinite?.should == nil - end +describe "Numeric#infinite?" do + it "returns nil by default" do + o = mock_numeric("infinite") + o.infinite?.should == nil end end diff --git a/spec/ruby/core/numeric/integer_spec.rb b/spec/ruby/core/numeric/integer_spec.rb index 5d4bf00360..adbac4d7aa 100644 --- a/spec/ruby/core/numeric/integer_spec.rb +++ b/spec/ruby/core/numeric/integer_spec.rb @@ -3,6 +3,6 @@ require_relative 'fixtures/classes' describe "Numeric#integer?" do it "returns false" do - NumericSpecs::Subclass.new.integer?.should == false + NumericSpecs::Subclass.new.should_not.integer? end end diff --git a/spec/ruby/core/numeric/magnitude_spec.rb b/spec/ruby/core/numeric/magnitude_spec.rb index 7a3290b036..1371dff21f 100644 --- a/spec/ruby/core/numeric/magnitude_spec.rb +++ b/spec/ruby/core/numeric/magnitude_spec.rb @@ -1,3 +1,4 @@ +require_relative "../../spec_helper" require_relative 'shared/abs' describe "Numeric#magnitude" do diff --git a/spec/ruby/core/numeric/negative_spec.rb b/spec/ruby/core/numeric/negative_spec.rb index da464a9094..f2d8a847da 100644 --- a/spec/ruby/core/numeric/negative_spec.rb +++ b/spec/ruby/core/numeric/negative_spec.rb @@ -4,22 +4,22 @@ require_relative 'fixtures/classes' describe "Numeric#negative?" do describe "on positive numbers" do it "returns false" do - 1.negative?.should be_false - 0.1.negative?.should be_false + 1.negative?.should == false + 0.1.negative?.should == false end end describe "on zero" do it "returns false" do - 0.negative?.should be_false - 0.0.negative?.should be_false + 0.negative?.should == false + 0.0.negative?.should == false end end describe "on negative numbers" do it "returns true" do - -1.negative?.should be_true - -0.1.negative?.should be_true + -1.negative?.should == true + -0.1.negative?.should == true end end end @@ -31,11 +31,11 @@ describe "Numeric#negative?" do it "returns true if self is less than 0" do @obj.should_receive(:<).with(0).and_return(true) - @obj.negative?.should == true + @obj.should.negative? end it "returns false if self is greater than 0" do @obj.should_receive(:<).with(0).and_return(false) - @obj.negative?.should == false + @obj.should_not.negative? end end diff --git a/spec/ruby/core/numeric/polar_spec.rb b/spec/ruby/core/numeric/polar_spec.rb index b594e408b2..0695d7afb2 100644 --- a/spec/ruby/core/numeric/polar_spec.rb +++ b/spec/ruby/core/numeric/polar_spec.rb @@ -17,7 +17,7 @@ describe "Numeric#polar" do it "returns a two-element Array" do @numbers.each do |number| - number.polar.should be_an_instance_of(Array) + number.polar.should.instance_of?(Array) number.polar.size.should == 2 end end @@ -44,7 +44,7 @@ describe "Numeric#polar" do it "returns [NaN, NaN] if self is NaN" do nan_value.polar.size.should == 2 - nan_value.polar.first.nan?.should be_true - nan_value.polar.last.nan?.should be_true + nan_value.polar.first.nan?.should == true + nan_value.polar.last.nan?.should == true end end diff --git a/spec/ruby/core/numeric/positive_spec.rb b/spec/ruby/core/numeric/positive_spec.rb index 8f98fbfa26..7c8d15cd9f 100644 --- a/spec/ruby/core/numeric/positive_spec.rb +++ b/spec/ruby/core/numeric/positive_spec.rb @@ -4,22 +4,22 @@ require_relative 'fixtures/classes' describe "Numeric#positive?" do describe "on positive numbers" do it "returns true" do - 1.positive?.should be_true - 0.1.positive?.should be_true + 1.positive?.should == true + 0.1.positive?.should == true end end describe "on zero" do it "returns false" do - 0.positive?.should be_false - 0.0.positive?.should be_false + 0.positive?.should == false + 0.0.positive?.should == false end end describe "on negative numbers" do it "returns false" do - -1.positive?.should be_false - -0.1.positive?.should be_false + -1.positive?.should == false + -0.1.positive?.should == false end end end @@ -31,11 +31,11 @@ describe "Numeric#positive?" do it "returns true if self is greater than 0" do @obj.should_receive(:>).with(0).and_return(true) - @obj.positive?.should == true + @obj.should.positive? end it "returns false if self is less than 0" do @obj.should_receive(:>).with(0).and_return(false) - @obj.positive?.should == false + @obj.should_not.positive? end end diff --git a/spec/ruby/core/numeric/quo_spec.rb b/spec/ruby/core/numeric/quo_spec.rb index bf05cf678d..66ff019231 100644 --- a/spec/ruby/core/numeric/quo_spec.rb +++ b/spec/ruby/core/numeric/quo_spec.rb @@ -1,14 +1,13 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' -require_relative 'shared/quo' describe "Numeric#quo" do it "returns the result of self divided by the given Integer as a Rational" do - 5.quo(2).should eql(Rational(5,2)) + 5.quo(2).should.eql?(Rational(5,2)) end it "returns the result of self divided by the given Float as a Float" do - 2.quo(2.5).should eql(0.8) + 2.quo(2.5).should.eql?(0.8) end it "returns the result of self divided by the given Bignum as a Float" do @@ -16,11 +15,11 @@ describe "Numeric#quo" do end it "raises a ZeroDivisionError when the given Integer is 0" do - lambda { 0.quo(0) }.should raise_error(ZeroDivisionError) - lambda { 10.quo(0) }.should raise_error(ZeroDivisionError) - lambda { -10.quo(0) }.should raise_error(ZeroDivisionError) - lambda { bignum_value.quo(0) }.should raise_error(ZeroDivisionError) - lambda { -bignum_value.quo(0) }.should raise_error(ZeroDivisionError) + -> { 0.quo(0) }.should.raise(ZeroDivisionError) + -> { 10.quo(0) }.should.raise(ZeroDivisionError) + -> { -10.quo(0) }.should.raise(ZeroDivisionError) + -> { bignum_value.quo(0) }.should.raise(ZeroDivisionError) + -> { (-bignum_value).quo(0) }.should.raise(ZeroDivisionError) end it "calls #to_r to convert the object to a Rational" do @@ -34,16 +33,16 @@ describe "Numeric#quo" do obj = NumericSpecs::Subclass.new obj.should_receive(:to_r).and_return(1) - lambda { obj.quo(19) }.should raise_error(TypeError) + -> { obj.quo(19) }.should.raise(TypeError) end it "raises a TypeError when given a non-Integer" do - lambda { + -> { (obj = mock('x')).should_not_receive(:to_int) 13.quo(obj) - }.should raise_error(TypeError) - lambda { 13.quo("10") }.should raise_error(TypeError) - lambda { 13.quo(:symbol) }.should raise_error(TypeError) + }.should.raise(TypeError) + -> { 13.quo("10") }.should.raise(TypeError) + -> { 13.quo(:symbol) }.should.raise(TypeError) end it "returns the result of calling self#/ with other" do @@ -52,4 +51,13 @@ describe "Numeric#quo" do obj.quo(19).should == 1.quo(20) end + + it "raises a ZeroDivisionError if the given argument is zero and not a Float" do + -> { 1.quo(0) }.should.raise(ZeroDivisionError) + end + + it "returns infinity if the given argument is zero and is a Float" do + (1.quo(0.0)).to_s.should == 'Infinity' + (-1.quo(0.0)).to_s.should == '-Infinity' + end end diff --git a/spec/ruby/core/numeric/real_spec.rb b/spec/ruby/core/numeric/real_spec.rb index 8c318dce0f..09d482a691 100644 --- a/spec/ruby/core/numeric/real_spec.rb +++ b/spec/ruby/core/numeric/real_spec.rb @@ -16,7 +16,7 @@ describe "Numeric#real" do it "returns self" do @numbers.each do |number| if number.to_f.nan? - number.real.nan?.should be_true + number.real.nan?.should == true else number.real.should == number end @@ -25,13 +25,13 @@ describe "Numeric#real" do it "raises an ArgumentError if given any arguments" do @numbers.each do |number| - lambda { number.real(number) }.should raise_error(ArgumentError) + -> { number.real(number) }.should.raise(ArgumentError) end end end describe "Numeric#real?" do it "returns true" do - NumericSpecs::Subclass.new.real?.should == true + NumericSpecs::Subclass.new.should.real? end end diff --git a/spec/ruby/core/numeric/remainder_spec.rb b/spec/ruby/core/numeric/remainder_spec.rb index 1e2f5f3a96..bdf7358b21 100644 --- a/spec/ruby/core/numeric/remainder_spec.rb +++ b/spec/ruby/core/numeric/remainder_spec.rb @@ -6,13 +6,14 @@ describe "Numeric#remainder" do @obj = NumericSpecs::Subclass.new @result = mock("Numeric#% result") @other = mock("Passed Object") + @other.should_receive(:coerce).with(@obj).and_return([@obj, @other]) end it "returns the result of calling self#% with other if self is 0" do @obj.should_receive(:%).with(@other).and_return(@result) @result.should_receive(:==).with(0).and_return(true) - @obj.remainder(@other).should equal(@result) + @obj.remainder(@other).should.equal?(@result) end it "returns the result of calling self#% with other if self and other are greater than 0" do @@ -24,7 +25,7 @@ describe "Numeric#remainder" do @obj.should_receive(:>).with(0).and_return(true) @other.should_receive(:<).with(0).and_return(false) - @obj.remainder(@other).should equal(@result) + @obj.remainder(@other).should.equal?(@result) end it "returns the result of calling self#% with other if self and other are less than 0" do @@ -36,7 +37,7 @@ describe "Numeric#remainder" do @obj.should_receive(:>).with(0).and_return(false) - @obj.remainder(@other).should equal(@result) + @obj.remainder(@other).should.equal?(@result) end it "returns the result of calling self#% with other - other if self is greater than 0 and other is less than 0" do diff --git a/spec/ruby/core/numeric/shared/conj.rb b/spec/ruby/core/numeric/shared/conj.rb index 6d5197ecab..1a661fbbe8 100644 --- a/spec/ruby/core/numeric/shared/conj.rb +++ b/spec/ruby/core/numeric/shared/conj.rb @@ -14,7 +14,7 @@ describe :numeric_conj, shared: true do it "returns self" do @numbers.each do |number| - number.send(@method).should equal(number) + number.send(@method).should.equal?(number) end end end diff --git a/spec/ruby/core/numeric/shared/imag.rb b/spec/ruby/core/numeric/shared/imag.rb index 11daf0af55..605a23d8c6 100644 --- a/spec/ruby/core/numeric/shared/imag.rb +++ b/spec/ruby/core/numeric/shared/imag.rb @@ -19,8 +19,8 @@ describe :numeric_imag, shared: true do end it "raises an ArgumentError if given any arguments" do - @numbers.each do |number| - lambda { number.send(@method, number) }.should raise_error(ArgumentError) - end + @numbers.each do |number| + -> { number.send(@method, number) }.should.raise(ArgumentError) + end end end diff --git a/spec/ruby/core/numeric/shared/quo.rb b/spec/ruby/core/numeric/shared/quo.rb deleted file mode 100644 index 2392636fe7..0000000000 --- a/spec/ruby/core/numeric/shared/quo.rb +++ /dev/null @@ -1,7 +0,0 @@ -describe :numeric_quo_18, shared: true do - it "returns the result of calling self#/ with other" do - obj = mock_numeric('numeric') - obj.should_receive(:/).with(19).and_return(:result) - obj.send(@method, 19).should == :result - end -end diff --git a/spec/ruby/core/numeric/shared/rect.rb b/spec/ruby/core/numeric/shared/rect.rb index 047d6556e1..32d03e45d2 100644 --- a/spec/ruby/core/numeric/shared/rect.rb +++ b/spec/ruby/core/numeric/shared/rect.rb @@ -14,7 +14,7 @@ describe :numeric_rect, shared: true do it "returns an Array" do @numbers.each do |number| - number.send(@method).should be_an_instance_of(Array) + number.send(@method).should.instance_of?(Array) end end @@ -25,24 +25,24 @@ describe :numeric_rect, shared: true do end it "returns self as the first element" do - @numbers.each do |number| - if Float === number and number.nan? - number.send(@method).first.nan?.should be_true - else - number.send(@method).first.should == number - end - end + @numbers.each do |number| + if Float === number and number.nan? + number.send(@method).first.nan?.should == true + else + number.send(@method).first.should == number + end + end end it "returns 0 as the last element" do - @numbers.each do |number| - number.send(@method).last.should == 0 - end + @numbers.each do |number| + number.send(@method).last.should == 0 + end end it "raises an ArgumentError if given any arguments" do - @numbers.each do |number| - lambda { number.send(@method, number) }.should raise_error(ArgumentError) - end + @numbers.each do |number| + -> { number.send(@method, number) }.should.raise(ArgumentError) + end end end diff --git a/spec/ruby/core/numeric/shared/step.rb b/spec/ruby/core/numeric/shared/step.rb index 066f499dc5..66b8c1af0d 100644 --- a/spec/ruby/core/numeric/shared/step.rb +++ b/spec/ruby/core/numeric/shared/step.rb @@ -2,18 +2,18 @@ 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_args var must contain a Proc that transforms +# 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 +describe :numeric_step, shared: true do before :each do ScratchPad.record [] - @prc = lambda { |x| ScratchPad << x } + @prc = -> x { ScratchPad << x } end it "defaults to step = 1" do - 1.send(@method, *@step_args.call(5), &@prc) - ScratchPad.recorded.should eql [1, 2, 3, 4, 5] + @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 @@ -24,41 +24,41 @@ describe :numeric_step, :shared => true 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 - 1.send(@method, *@step_args.call(5, 1)) { |x| x.should be_an_instance_of(Fixnum) } + describe "when self, stop and step are Integers" do + it "yields only Integers" do + @step.call(1, 5, 1) { |x| x.should.instance_of?(Integer) } end describe "with a positive step" do it "yields while increasing self by step until stop is reached" do - 1.send(@method, *@step_args.call(5, 1), &@prc) - ScratchPad.recorded.should eql [1, 2, 3, 4, 5] + @step.call(1, 5, 1, &@prc) + ScratchPad.recorded.should.eql? [1, 2, 3, 4, 5] end it "yields once when self equals stop" do - 1.send(@method, *@step_args.call(1, 1), &@prc) - ScratchPad.recorded.should eql [1] + @step.call(1, 1, 1, &@prc) + ScratchPad.recorded.should.eql? [1] end it "does not yield when self is greater than stop" do - 2.send(@method, *@step_args.call(1, 1), &@prc) - ScratchPad.recorded.should eql [] + @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 - 5.send(@method, *@step_args.call(1, -1), &@prc) - ScratchPad.recorded.should eql [5, 4, 3, 2, 1] + @step.call(5, 1, -1, &@prc) + ScratchPad.recorded.should.eql? [5, 4, 3, 2, 1] end it "yields once when self equals stop" do - 5.send(@method, *@step_args.call(5, -1), &@prc) - ScratchPad.recorded.should eql [5] + @step.call(5, 5, -1, &@prc) + ScratchPad.recorded.should.eql? [5] end it "does not yield when self is less than stop" do - 1.send(@method, *@step_args.call(5, -1), &@prc) + @step.call(1, 5, -1, &@prc) ScratchPad.recorded.should == [] end end @@ -66,156 +66,157 @@ describe :numeric_step, :shared => true do 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 - 1.5.send(@method, *@step_args.call(5, 1)) { |x| x.should be_an_instance_of(Float) } + @step.call(1.5, 5, 1) { |x| x.should.instance_of?(Float) } end it "yields Floats even if only stop is a Float" do - 1.send(@method, *@step_args.call(5.0, 1)) { |x| x.should be_an_instance_of(Float) } + @step.call(1, 5.0, 1) { |x| x.should.instance_of?(Float) } end it "yields Floats even if only step is a Float" do - 1.send(@method, *@step_args.call(5, 1.0)) { |x| x.should be_an_instance_of(Float) } + @step.call(1, 5, 1.0) { |x| x.should.instance_of?(Float) } end describe "with a positive step" do it "yields while increasing self by step while < stop" do - 1.5.send(@method, *@step_args.call(5, 1), &@prc) - ScratchPad.recorded.should eql [1.5, 2.5, 3.5, 4.5] + @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 - 1.5.send(@method, *@step_args.call(1.5, 1), &@prc) - ScratchPad.recorded.should eql [1.5] + @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 - 2.5.send(@method, *@step_args.call(1.5, 1), &@prc) + @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: - 1.0.send(@method, *@step_args.call(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] + @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 - 5.send(@method, *@step_args.call(1.5, -1), &@prc) - ScratchPad.recorded.should eql [5.0, 4.0, 3.0, 2.0] + @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 - 1.5.send(@method, *@step_args.call(1.5, -1), &@prc) - ScratchPad.recorded.should eql [1.5] + @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 - 1.send(@method, *@step_args.call(5, -1.5), &@prc) + @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: - -1.0.send(@method, *@step_args.call(-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] + @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 - 42.send(@method, *@step_args.call(100, infinity_value), &@prc) - ScratchPad.recorded.should eql [42.0] + @step.call(42, 100, infinity_value, &@prc) + ScratchPad.recorded.should.eql? [42.0] end it "yields once when stop is Infinity" do - 42.send(@method, *@step_args.call(infinity_value, infinity_value), &@prc) - ScratchPad.recorded.should eql [42.0] + @step.call(42, infinity_value, infinity_value, &@prc) + ScratchPad.recorded.should.eql? [42.0] end it "yields once when self equals stop" do - 42.send(@method, *@step_args.call(42, infinity_value), &@prc) - ScratchPad.recorded.should eql [42.0] + @step.call(42, 42, infinity_value, &@prc) + ScratchPad.recorded.should.eql? [42.0] end it "yields once when self and stop are Infinity" do - (infinity_value).send(@method, *@step_args.call(infinity_value, infinity_value), &@prc) + # @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 - 100.send(@method, *@step_args.call(42, infinity_value), &@prc) + @step.call(100, 42, infinity_value, &@prc) ScratchPad.recorded.should == [] end it "does not yield when stop is -Infinity" do - 42.send(@method, *@step_args.call(-infinity_value, infinity_value), &@prc) + @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 - 42.send(@method, *@step_args.call(6, -infinity_value), &@prc) - ScratchPad.recorded.should eql [42.0] + @step.call(42, 6, -infinity_value, &@prc) + ScratchPad.recorded.should.eql? [42.0] end it "yields once if stop is -Infinity" do - 42.send(@method, *@step_args.call(-infinity_value, -infinity_value), &@prc) - ScratchPad.recorded.should eql [42.0] + @step.call(42, -infinity_value, -infinity_value, &@prc) + ScratchPad.recorded.should.eql? [42.0] end it "yields once when self equals stop" do - 42.send(@method, *@step_args.call(42, -infinity_value), &@prc) - ScratchPad.recorded.should eql [42.0] + @step.call(42, 42, -infinity_value, &@prc) + ScratchPad.recorded.should.eql? [42.0] end it "yields once when self and stop are Infinity" do - (infinity_value).send(@method, *@step_args.call(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 - 42.send(@method, *@step_args.call(100, -infinity_value), &@prc) + @step.call(42, 100, -infinity_value, &@prc) ScratchPad.recorded.should == [] end it "does not yield when stop is Infinity" do - 42.send(@method, *@step_args.call(infinity_value, -infinity_value), &@prc) + @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 - (infinity_value).send(@method, *@step_args.call(infinity_value, 1), &@prc) + @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 - (-infinity_value).send(@method, *@step_args.call(infinity_value, -1), &@prc) + @step.call(-infinity_value, infinity_value, -1, &@prc) ScratchPad.recorded.should == [] end it "does not yield when self is positive infinity" do - infinity_value.send(@method, *@step_args.call(infinity_value, -1), &@prc) + @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 - (-infinity_value).send(@method, *@step_args.call(-infinity_value, 1), &@prc) + @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 - (-infinity_value).send(@method, *@step_args.call(-infinity_value, -1), &@prc) + @step.call(-infinity_value, -infinity_value, -1, &@prc) ScratchPad.recorded.should == [] end end @@ -223,80 +224,62 @@ describe :numeric_step, :shared => true do end describe "when step is a String" do - error = nil - ruby_version_is ""..."2.4" do - error = ArgumentError - end - ruby_version_is "2.4"..."2.5" do - error = TypeError - end - ruby_version_is "2.5" do - error = ArgumentError - end - - describe "with self and stop as Fixnums" do - it "raises an #{error} when step is a numeric representation" do - lambda { 1.send(@method, *@step_args.call(5, "1")) {} }.should raise_error(error) - lambda { 1.send(@method, *@step_args.call(5, "0.1")) {} }.should raise_error(error) - lambda { 1.send(@method, *@step_args.call(5, "1/3")) {} }.should raise_error(error) + describe "with self and stop as Integers" do + it "raises an ArgumentError when step is a numeric representation" do + -> { @step.call(1, 5, "1") {} }.should.raise(ArgumentError) + -> { @step.call(1, 5, "0.1") {} }.should.raise(ArgumentError) + -> { @step.call(1, 5, "1/3") {} }.should.raise(ArgumentError) end - it "raises an #{error} with step as an alphanumeric string" do - lambda { 1.send(@method, *@step_args.call(5, "foo")) {} }.should raise_error(error) + it "raises an ArgumentError with step as an alphanumeric string" do + -> { @step.call(1, 5, "foo") {} }.should.raise(ArgumentError) end end describe "with self and stop as Floats" do - it "raises an #{error} when step is a numeric representation" do - lambda { 1.1.send(@method, *@step_args.call(5.1, "1")) {} }.should raise_error(error) - lambda { 1.1.send(@method, *@step_args.call(5.1, "0.1")) {} }.should raise_error(error) - lambda { 1.1.send(@method, *@step_args.call(5.1, "1/3")) {} }.should raise_error(error) + it "raises an ArgumentError when step is a numeric representation" do + -> { @step.call(1.1, 5.1, "1") {} }.should.raise(ArgumentError) + -> { @step.call(1.1, 5.1, "0.1") {} }.should.raise(ArgumentError) + -> { @step.call(1.1, 5.1, "1/3") {} }.should.raise(ArgumentError) end - it "raises an #{error} with step as an alphanumeric string" do - lambda { 1.1.send(@method, *@step_args.call(5.1, "foo")) {} }.should raise_error(error) + it "raises an ArgumentError with step as an alphanumeric string" do + -> { @step.call(1.1, 5.1, "foo") {} }.should.raise(ArgumentError) end end end it "does not rescue ArgumentError exceptions" do - lambda { 1.send(@method, *@step_args.call(2)) { raise ArgumentError, "" }}.should raise_error(ArgumentError) + -> { @step.call(1, 2) { raise ArgumentError, "" }}.should.raise(ArgumentError) end it "does not rescue TypeError exceptions" do - lambda { 1.send(@method, *@step_args.call(2)) { raise TypeError, "" } }.should raise_error(TypeError) + -> { @step.call(1, 2) { raise TypeError, "" } }.should.raise(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 - 1.send(@method, *@step_args.call(2, 0)).should be_an_instance_of(step_enum_class) - end + step_enum_class = Enumerator::ArithmeticSequence it "returns an #{step_enum_class} when not passed a block and self > stop" do - 1.send(@method, *@step_args.call(0, 2)).should be_an_instance_of(step_enum_class) + @step.call(1, 0, 2).should.instance_of?(step_enum_class) end 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) + @step.call(1, 2, 3).should.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] + @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 + describe "with self and stop as Integers" do it "returns an Enumerator" do - 1.send(@method, *@step_args.call(5, "foo")).should be_an_instance_of(Enumerator) + @step.call(1, 5, "foo").should.instance_of?(Enumerator) end end describe "with self and stop as Floats" do it "returns an Enumerator" do - 1.1.send(@method, *@step_args.call(5.1, "foo")).should be_an_instance_of(Enumerator) + @step.call(1.1, 5.1, "foo").should.instance_of?(Enumerator) end end end @@ -304,132 +287,121 @@ describe :numeric_step, :shared => true do describe "returned Enumerator" do describe "size" do describe "when step is a String" do - error = nil - ruby_version_is ""..."2.4" do - error = ArgumentError - end - ruby_version_is "2.4"..."2.5" do - error = TypeError - end - ruby_version_is "2.5" do - error = ArgumentError - end - - describe "with self and stop as Fixnums" do - it "raises an #{error} when step is a numeric representation" do - lambda { 1.send(@method, *@step_args.call(5, "1")).size }.should raise_error(error) - lambda { 1.send(@method, *@step_args.call(5, "0.1")).size }.should raise_error(error) - lambda { 1.send(@method, *@step_args.call(5, "1/3")).size }.should raise_error(error) + describe "with self and stop as Integers" do + it "raises an ArgumentError when step is a numeric representation" do + -> { @step.call(1, 5, "1").size }.should.raise(ArgumentError) + -> { @step.call(1, 5, "0.1").size }.should.raise(ArgumentError) + -> { @step.call(1, 5, "1/3").size }.should.raise(ArgumentError) end - it "raises an #{error} with step as an alphanumeric string" do - lambda { 1.send(@method, *@step_args.call(5, "foo")).size }.should raise_error(error) + it "raises an ArgumentError with step as an alphanumeric string" do + -> { @step.call(1, 5, "foo").size }.should.raise(ArgumentError) end end describe "with self and stop as Floats" do - it "raises an #{error} when step is a numeric representation" do - lambda { 1.1.send(@method, *@step_args.call(5.1, "1")).size }.should raise_error(error) - lambda { 1.1.send(@method, *@step_args.call(5.1, "0.1")).size }.should raise_error(error) - lambda { 1.1.send(@method, *@step_args.call(5.1, "1/3")).size }.should raise_error(error) + it "raises an ArgumentError when step is a numeric representation" do + -> { @step.call(1.1, 5.1, "1").size }.should.raise(ArgumentError) + -> { @step.call(1.1, 5.1, "0.1").size }.should.raise(ArgumentError) + -> { @step.call(1.1, 5.1, "1/3").size }.should.raise(ArgumentError) end - it "raises an #{error} with step as an alphanumeric string" do - lambda { 1.1.send(@method, *@step_args.call(5.1, "foo")).size }.should raise_error(error) + it "raises an ArgumentError with step as an alphanumeric string" do + -> { @step.call(1.1, 5.1, "foo").size }.should.raise(ArgumentError) end end end - describe "when self, stop and step are Fixnums and step is positive" do + describe "when self, stop and step are Integers and step is positive" do it "returns the difference between self and stop divided by the number of steps" do - 5.send(@method, *@step_args.call(10, 11)).size.should == 1 - 5.send(@method, *@step_args.call(10, 6)).size.should == 1 - 5.send(@method, *@step_args.call(10, 5)).size.should == 2 - 5.send(@method, *@step_args.call(10, 4)).size.should == 2 - 5.send(@method, *@step_args.call(10, 2)).size.should == 3 - 5.send(@method, *@step_args.call(10, 1)).size.should == 6 - 5.send(@method, *@step_args.call(10)).size.should == 6 - 10.send(@method, *@step_args.call(10, 1)).size.should == 1 + @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 - 11.send(@method, *@step_args.call(10, 1)).size.should == 0 + @step.call(11, 10, 1).size.should == 0 end end - describe "when self, stop and step are Fixnums and step is negative" do + describe "when self, stop and step are Integers and step is negative" do it "returns the difference between self and stop divided by the number of steps" do - 10.send(@method, *@step_args.call(5, -11)).size.should == 1 - 10.send(@method, *@step_args.call(5, -6)).size.should == 1 - 10.send(@method, *@step_args.call(5, -5)).size.should == 2 - 10.send(@method, *@step_args.call(5, -4)).size.should == 2 - 10.send(@method, *@step_args.call(5, -2)).size.should == 3 - 10.send(@method, *@step_args.call(5, -1)).size.should == 6 - 10.send(@method, *@step_args.call(10, -1)).size.should == 1 + @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 - 10.send(@method, *@step_args.call(11, -1)).size.should == 0 + @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 - 5.send(@method, *@step_args.call(10, 11.0)).size.should == 1 - 5.send(@method, *@step_args.call(10, 6.0)).size.should == 1 - 5.send(@method, *@step_args.call(10, 5.0)).size.should == 2 - 5.send(@method, *@step_args.call(10, 4.0)).size.should == 2 - 5.send(@method, *@step_args.call(10, 2.0)).size.should == 3 - 5.send(@method, *@step_args.call(10, 0.5)).size.should == 11 - 5.send(@method, *@step_args.call(10, 1.0)).size.should == 6 - 5.send(@method, *@step_args.call(10.5)).size.should == 6 - 10.send(@method, *@step_args.call(10, 1.0)).size.should == 1 + @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 - 10.send(@method, *@step_args.call(5.5)).size.should == 0 - 11.send(@method, *@step_args.call(10, 1.0)).size.should == 0 - 11.send(@method, *@step_args.call(10, 1.5)).size.should == 0 - 10.send(@method, *@step_args.call(5, infinity_value)).size.should == 0 + @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 - 5.send(@method, *@step_args.call(10, infinity_value)).size.should == 1 + @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 - 10.send(@method, *@step_args.call(5, -11.0)).size.should == 1 - 10.send(@method, *@step_args.call(5, -6.0)).size.should == 1 - 10.send(@method, *@step_args.call(5, -5.0)).size.should == 2 - 10.send(@method, *@step_args.call(5, -4.0)).size.should == 2 - 10.send(@method, *@step_args.call(5, -2.0)).size.should == 3 - 10.send(@method, *@step_args.call(5, -0.5)).size.should == 11 - 10.send(@method, *@step_args.call(5, -1.0)).size.should == 6 - 10.send(@method, *@step_args.call(10, -1.0)).size.should == 1 + @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 - 10.send(@method, *@step_args.call(11, -1.0)).size.should == 0 - 10.send(@method, *@step_args.call(11, -1.5)).size.should == 0 - 5.send(@method, *@step_args.call(10, -infinity_value)).size.should == 0 + @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 - 10.send(@method, *@step_args.call(5, -infinity_value)).size.should == 1 + @step.call(10, 5, -infinity_value).size.should == 1 end end end describe "when stop is not passed" do it "returns infinity_value" do - 1.send(@method, *@step_args.call()).size.should == infinity_value + @step.call(1).size.should == infinity_value end end describe "when stop is nil" do it "returns infinity_value" do - 1.send(@method, *@step_args.call(nil, 5)).size.should == infinity_value + @step.call(1, nil, 5).size.should == infinity_value end end end diff --git a/spec/ruby/core/numeric/singleton_method_added_spec.rb b/spec/ruby/core/numeric/singleton_method_added_spec.rb index c39e9d7b32..327bb5662a 100644 --- a/spec/ruby/core/numeric/singleton_method_added_spec.rb +++ b/spec/ruby/core/numeric/singleton_method_added_spec.rb @@ -18,24 +18,24 @@ describe "Numeric#singleton_method_added" do end it "raises a TypeError when trying to define a singleton method on a Numeric" do - lambda do + -> do a = NumericSpecs::Subclass.new def a.test; end - end.should raise_error(TypeError) + end.should.raise(TypeError) - lambda do + -> do a = 1 def a.test; end - end.should raise_error(TypeError) + end.should.raise(TypeError) - lambda do + -> do a = 1.5 def a.test; end - end.should raise_error(TypeError) + end.should.raise(TypeError) - lambda do + -> do a = bignum_value def a.test; end - end.should raise_error(TypeError) + end.should.raise(TypeError) end end diff --git a/spec/ruby/core/numeric/step_spec.rb b/spec/ruby/core/numeric/step_spec.rb index 12369a47a2..6896009eb6 100644 --- a/spec/ruby/core/numeric/step_spec.rb +++ b/spec/ruby/core/numeric/step_spec.rb @@ -6,61 +6,25 @@ 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) + -> { 1.step(5, 0) {} }.should.raise(ArgumentError) end it "raises an ArgumentError when step is 0.0" do - lambda { 1.step(2, 0.0) {} }.should raise_error(ArgumentError) + -> { 1.step(2, 0.0) {} }.should.raise(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 } + @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 - ruby_version_is "2.6" 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 - - 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 + step_enum_class = Enumerator::ArithmeticSequence describe "returned #{step_enum_class}" do describe "size" do - 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 - end - - 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 - end - it "defaults to an infinite size" do enum = 1.step enum.size.should == Float::INFINITY @@ -68,38 +32,15 @@ describe "Numeric#step" do end 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 + 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 - 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 @@ -107,14 +48,6 @@ 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 - - it "should return infinity_value when step is 0.0" do - 1.step(to: 2, by: 0.0).size.should == infinity_value - 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 @@ -135,64 +68,51 @@ describe "Numeric#step" do end before :all do - # This lambda transforms a positional step method args into - # keyword arguments. + # 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]} + @step = -> receiver, *args, &block do + kw_args = { to: args[0] } kw_args[:by] = args[1] if args.size == 2 - [kw_args] + receiver.step(**kw_args, &block) 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 + it " raises an ArgumentError when step is 0" do + -> { 1.step(5, by: 0) { break } }.should.raise(ArgumentError) 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 + it "raises an ArgumentError when step is 0.0" do + -> { 1.step(2, by: 0.0) { break } }.should.raise(ArgumentError) end it "raises a ArgumentError when limit and to are defined" do - lambda { 1.step(5, 1, to: 5) { break } }.should raise_error(ArgumentError) + -> { 1.step(5, 1, to: 5) { break } }.should.raise(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] + -> { 1.step(5, 1, by: 5) { break } }.should.raise(ArgumentError) 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 + @step = -> receiver, *args, &block do if args.size == 2 - [args[0], {by: args[1]}] + receiver.step(args[0], by: args[1], &block) else - args + receiver.step(*args, &block) end end end diff --git a/spec/ruby/core/numeric/to_c_spec.rb b/spec/ruby/core/numeric/to_c_spec.rb index 75245a612e..70b7a9dd0c 100644 --- a/spec/ruby/core/numeric/to_c_spec.rb +++ b/spec/ruby/core/numeric/to_c_spec.rb @@ -22,7 +22,7 @@ describe "Numeric#to_c" do it "returns a Complex object" do @numbers.each do |number| - number.to_c.should be_an_instance_of(Complex) + number.to_c.should.instance_of?(Complex) end end @@ -30,7 +30,7 @@ describe "Numeric#to_c" do @numbers.each do |number| real = number.to_c.real if Float === number and number.nan? - real.nan?.should be_true + real.nan?.should == true else real.should == number end diff --git a/spec/ruby/core/numeric/zero_spec.rb b/spec/ruby/core/numeric/zero_spec.rb index 9de71d1dc9..0fb7619bcd 100644 --- a/spec/ruby/core/numeric/zero_spec.rb +++ b/spec/ruby/core/numeric/zero_spec.rb @@ -8,11 +8,11 @@ describe "Numeric#zero?" do it "returns true if self is 0" do @obj.should_receive(:==).with(0).and_return(true) - @obj.zero?.should == true + @obj.should.zero? end it "returns false if self is not 0" do @obj.should_receive(:==).with(0).and_return(false) - @obj.zero?.should == false + @obj.should_not.zero? end end |
