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/dup_spec.rb | 16 | ||||
| -rw-r--r-- | spec/ruby/core/numeric/fdiv_spec.rb | 1 | ||||
| -rw-r--r-- | spec/ruby/core/numeric/magnitude_spec.rb | 1 | ||||
| -rw-r--r-- | spec/ruby/core/numeric/quo_spec.rb | 12 | ||||
| -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 | 27 | ||||
| -rw-r--r-- | spec/ruby/core/numeric/step_spec.rb | 93 |
11 files changed, 93 insertions, 129 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/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/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/quo_spec.rb b/spec/ruby/core/numeric/quo_spec.rb index 5c952b11a9..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 @@ -20,7 +19,7 @@ describe "Numeric#quo" do -> { 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) + -> { (-bignum_value).quo(0) }.should raise_error(ZeroDivisionError) end it "calls #to_r to convert the object to a Rational" 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/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 ac2da40a3b..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| - -> { 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 9cde19a398..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| - -> { 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 fac79b3e63..977ec6de02 100644 --- a/spec/ruby/core/numeric/shared/step.rb +++ b/spec/ruby/core/numeric/shared/step.rb @@ -5,7 +5,7 @@ require_relative '../fixtures/classes' # 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 = -> x { ScratchPad << x } @@ -24,9 +24,9 @@ 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 - @step.call(1, 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 @@ -224,7 +224,7 @@ describe :numeric_step, :shared => true do 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 "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) @@ -256,14 +256,7 @@ describe :numeric_step, :shared => true do 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 - @step.call(1, 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 @step.call(1, 0, 2).should be_an_instance_of(step_enum_class) @@ -278,7 +271,7 @@ describe :numeric_step, :shared => true do 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 @step.call(1, 5, "foo").should be_an_instance_of(Enumerator) end @@ -294,7 +287,7 @@ describe :numeric_step, :shared => true do describe "returned Enumerator" do describe "size" do describe "when step is a String" do - describe "with self and stop as Fixnums" do + 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) @@ -317,7 +310,7 @@ describe :numeric_step, :shared => true do 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 @step.call(5, 10, 11).size.should == 1 @step.call(5, 10, 6).size.should == 1 @@ -334,7 +327,7 @@ describe :numeric_step, :shared => true do 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 @step.call(10, 5, -11).size.should == 1 @step.call(10, 5, -6).size.should == 1 diff --git a/spec/ruby/core/numeric/step_spec.rb b/spec/ruby/core/numeric/step_spec.rb index e9067864c8..1705fb1b4e 100644 --- a/spec/ruby/core/numeric/step_spec.rb +++ b/spec/ruby/core/numeric/step_spec.rb @@ -21,45 +21,10 @@ describe "Numeric#step" do 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) - -> { enum.size }.should raise_error(ArgumentError) - end - - it "raises an ArgumentError when step is 0.0" do - enum = 1.step(2, 0.0) - -> { 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 @@ -67,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 - -> { 1.step(to: 5, by: 0) { break } }.should_not raise_error - end - - it "doesn't raise an error when step is 0.0" do - -> { 1.step(to: 2, by: 0.0) { break } }.should_not raise_error - end - - it "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 @@ -106,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 @@ -146,12 +80,12 @@ describe "Numeric#step" do end describe 'with mixed arguments' do - it "doesn't raise an error when step is 0" do - -> { 1.step(5, by: 0) { break } }.should_not raise_error + 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 - -> { 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 @@ -162,22 +96,9 @@ describe "Numeric#step" do -> { 1.step(5, 1, by: 5) { break } }.should raise_error(ArgumentError) end - it "should loop over self when step is 0 or 0.0" do - 1.step(2, by: 0.0).take(5).should eql [1.0, 1.0, 1.0, 1.0, 1.0] - 1.step(2, by: 0).take(5).should eql [1, 1, 1, 1, 1] - 1.1.step(2, by: 0).take(5).should eql [1.1, 1.1, 1.1, 1.1, 1.1] - 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 |
