diff options
Diffstat (limited to 'spec/ruby/core/numeric')
| -rw-r--r-- | spec/ruby/core/numeric/clone_spec.rb | 30 | ||||
| -rw-r--r-- | spec/ruby/core/numeric/coerce_spec.rb | 12 | ||||
| -rw-r--r-- | spec/ruby/core/numeric/div_spec.rb | 6 | ||||
| -rw-r--r-- | spec/ruby/core/numeric/dup_spec.rb | 16 | ||||
| -rw-r--r-- | spec/ruby/core/numeric/fdiv_spec.rb | 1 | ||||
| -rw-r--r-- | spec/ruby/core/numeric/integer_spec.rb | 2 | ||||
| -rw-r--r-- | spec/ruby/core/numeric/magnitude_spec.rb | 1 | ||||
| -rw-r--r-- | spec/ruby/core/numeric/negative_spec.rb | 4 | ||||
| -rw-r--r-- | spec/ruby/core/numeric/positive_spec.rb | 4 | ||||
| -rw-r--r-- | spec/ruby/core/numeric/quo_spec.rb | 28 | ||||
| -rw-r--r-- | spec/ruby/core/numeric/real_spec.rb | 4 | ||||
| -rw-r--r-- | spec/ruby/core/numeric/remainder_spec.rb | 3 | ||||
| -rw-r--r-- | spec/ruby/core/numeric/shared/imag.rb | 6 | ||||
| -rw-r--r-- | spec/ruby/core/numeric/shared/quo.rb | 7 | ||||
| -rw-r--r-- | spec/ruby/core/numeric/shared/rect.rb | 26 | ||||
| -rw-r--r-- | spec/ruby/core/numeric/shared/step.rb | 270 | ||||
| -rw-r--r-- | spec/ruby/core/numeric/singleton_method_added_spec.rb | 8 | ||||
| -rw-r--r-- | spec/ruby/core/numeric/step_spec.rb | 120 | ||||
| -rw-r--r-- | spec/ruby/core/numeric/zero_spec.rb | 4 |
19 files changed, 250 insertions, 302 deletions
diff --git a/spec/ruby/core/numeric/clone_spec.rb b/spec/ruby/core/numeric/clone_spec.rb new file mode 100644 index 0000000000..423cec85dd --- /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_error(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..4c4416d30b 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_error(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_error(TypeError) end it "raises a TypeError when passed nil" do - lambda { @obj.coerce(nil) }.should raise_error(TypeError) + -> { @obj.coerce(nil) }.should raise_error(TypeError) end it "raises a TypeError when passed a boolean" do - lambda { @obj.coerce(false) }.should raise_error(TypeError) + -> { @obj.coerce(false) }.should raise_error(TypeError) end it "raises a TypeError when passed a Symbol" do - lambda { @obj.coerce(:symbol) }.should raise_error(TypeError) + -> { @obj.coerce(:symbol) }.should raise_error(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_error(ArgumentError) end end diff --git a/spec/ruby/core/numeric/div_spec.rb b/spec/ruby/core/numeric/div_spec.rb index 0017311487..53917b84c9 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_error(ZeroDivisionError) + -> { @obj.div(0.0) }.should raise_error(ZeroDivisionError) + -> { @obj.div(Complex(0,0)) }.should raise_error(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..189a7ef44d --- /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/fdiv_spec.rb b/spec/ruby/core/numeric/fdiv_spec.rb index 907e5d343c..e97fa77f79 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 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..9c6f95fd87 100644 --- a/spec/ruby/core/numeric/negative_spec.rb +++ b/spec/ruby/core/numeric/negative_spec.rb @@ -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/positive_spec.rb b/spec/ruby/core/numeric/positive_spec.rb index 8f98fbfa26..3b831b4d34 100644 --- a/spec/ruby/core/numeric/positive_spec.rb +++ b/spec/ruby/core/numeric/positive_spec.rb @@ -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..6e3ce7a374 100644 --- a/spec/ruby/core/numeric/quo_spec.rb +++ b/spec/ruby/core/numeric/quo_spec.rb @@ -1,6 +1,5 @@ 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 @@ -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_error(ZeroDivisionError) + -> { 10.quo(0) }.should raise_error(ZeroDivisionError) + -> { -10.quo(0) }.should raise_error(ZeroDivisionError) + -> { bignum_value.quo(0) }.should raise_error(ZeroDivisionError) + -> { (-bignum_value).quo(0) }.should raise_error(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_error(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) + -> { 13.quo("10") }.should raise_error(TypeError) + -> { 13.quo(:symbol) }.should raise_error(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_error(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..2d2499bcce 100644 --- a/spec/ruby/core/numeric/real_spec.rb +++ b/spec/ruby/core/numeric/real_spec.rb @@ -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_error(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..674fa22d8e 100644 --- a/spec/ruby/core/numeric/remainder_spec.rb +++ b/spec/ruby/core/numeric/remainder_spec.rb @@ -6,6 +6,9 @@ describe "Numeric#remainder" do @obj = NumericSpecs::Subclass.new @result = mock("Numeric#% result") @other = mock("Passed Object") + ruby_version_is "3.3" do + @other.should_receive(:coerce).with(@obj).and_return([@obj, @other]) + end end it "returns the result of calling self#% with other if self is 0" do diff --git a/spec/ruby/core/numeric/shared/imag.rb b/spec/ruby/core/numeric/shared/imag.rb index 11daf0af55..4f117e243a 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_error(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..120a69b1c4 100644 --- a/spec/ruby/core/numeric/shared/rect.rb +++ b/spec/ruby/core/numeric/shared/rect.rb @@ -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 be_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_error(ArgumentError) + end end end diff --git a/spec/ruby/core/numeric/shared/step.rb b/spec/ruby/core/numeric/shared/step.rb index 5dcaa4b253..977ec6de02 100644 --- a/spec/ruby/core/numeric/shared/step.rb +++ b/spec/ruby/core/numeric/shared/step.rb @@ -2,17 +2,17 @@ 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) + @step.call(1, 5, &@prc) ScratchPad.recorded.should eql [1, 2, 3, 4, 5] end @@ -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 be_an_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) + @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) + @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) + @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) + @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) + @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 be_an_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 be_an_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 be_an_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) + @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) + @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) + @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) + @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) + @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) + @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) + @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) + @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) + @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) + @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) + @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) + @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,77 +224,62 @@ describe :numeric_step, :shared => true do end describe "when step is a String" do - error = nil - 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_error(ArgumentError) + -> { @step.call(1, 5, "0.1") {} }.should raise_error(ArgumentError) + -> { @step.call(1, 5, "1/3") {} }.should raise_error(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_error(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_error(ArgumentError) + -> { @step.call(1.1, 5.1, "0.1") {} }.should raise_error(ArgumentError) + -> { @step.call(1.1, 5.1, "1/3") {} }.should raise_error(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_error(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_error(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_error(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 be_an_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 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] + @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 be_an_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 be_an_instance_of(Enumerator) end end end @@ -301,129 +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"..."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_error(ArgumentError) + -> { @step.call(1, 5, "0.1").size }.should raise_error(ArgumentError) + -> { @step.call(1, 5, "1/3").size }.should raise_error(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_error(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_error(ArgumentError) + -> { @step.call(1.1, 5.1, "0.1").size }.should raise_error(ArgumentError) + -> { @step.call(1.1, 5.1, "1/3").size }.should raise_error(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_error(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..2091398e5d 100644 --- a/spec/ruby/core/numeric/singleton_method_added_spec.rb +++ b/spec/ruby/core/numeric/singleton_method_added_spec.rb @@ -18,22 +18,22 @@ 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) - lambda do + -> do a = 1 def a.test; end end.should raise_error(TypeError) - lambda do + -> do a = 1.5 def a.test; end end.should raise_error(TypeError) - lambda do + -> do a = bignum_value def a.test; end end.should raise_error(TypeError) diff --git a/spec/ruby/core/numeric/step_spec.rb b/spec/ruby/core/numeric/step_spec.rb index 12369a47a2..1705fb1b4e 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_error(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_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 } + @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_error(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_error(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_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] + -> { 1.step(5, 1, by: 5) { break } }.should raise_error(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/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 |
