diff options
Diffstat (limited to 'spec/ruby/core/integer/divide_spec.rb')
| -rw-r--r-- | spec/ruby/core/integer/divide_spec.rb | 53 |
1 files changed, 45 insertions, 8 deletions
diff --git a/spec/ruby/core/integer/divide_spec.rb b/spec/ruby/core/integer/divide_spec.rb index a878c4668c..e75432fd83 100644 --- a/spec/ruby/core/integer/divide_spec.rb +++ b/spec/ruby/core/integer/divide_spec.rb @@ -12,6 +12,17 @@ describe "Integer#/" do it "supports dividing negative numbers" do (-1 / 10).should == -1 + (-1 / 10**10).should == -1 + (-1 / 10**20).should == -1 + end + + it "preservers sign correctly" do + (4 / 3).should == 1 + (4 / -3).should == -2 + (-4 / 3).should == -2 + (-4 / -3).should == 1 + (0 / -3).should == 0 + (0 / 3).should == 0 end it "returns result the same class as the argument" do @@ -21,7 +32,7 @@ describe "Integer#/" do end it "raises a ZeroDivisionError if the given argument is zero and not a Float" do - -> { 1 / 0 }.should raise_error(ZeroDivisionError) + -> { 1 / 0 }.should.raise(ZeroDivisionError) end it "does NOT raise ZeroDivisionError if the given argument is zero and is a Float" do @@ -35,9 +46,9 @@ describe "Integer#/" do end it "raises a TypeError when given a non-Integer" do - -> { 13 / mock('10') }.should raise_error(TypeError) - -> { 13 / "10" }.should raise_error(TypeError) - -> { 13 / :symbol }.should raise_error(TypeError) + -> { 13 / mock('10') }.should.raise(TypeError) + -> { 13 / "10" }.should.raise(TypeError) + -> { 13 / :symbol }.should.raise(TypeError) end end @@ -58,6 +69,15 @@ describe "Integer#/" do ((10**50) / -(10**40 + 1)).should == -10000000000 end + it "preservers sign correctly" do + (4 / bignum_value).should == 0 + (4 / -bignum_value).should == -1 + (-4 / bignum_value).should == -1 + (-4 / -bignum_value).should == 0 + (0 / bignum_value).should == 0 + (0 / -bignum_value).should == 0 + end + it "returns self divided by Float" do not_supported_on :opal do (bignum_value(88) / 4294967295.0).should be_close(4294967297.0, TOLERANCE) @@ -77,13 +97,30 @@ describe "Integer#/" do end it "raises a ZeroDivisionError if other is zero and not a Float" do - -> { @bignum / 0 }.should raise_error(ZeroDivisionError) + -> { @bignum / 0 }.should.raise(ZeroDivisionError) end it "raises a TypeError when given a non-numeric" do - -> { @bignum / mock('10') }.should raise_error(TypeError) - -> { @bignum / "2" }.should raise_error(TypeError) - -> { @bignum / :symbol }.should raise_error(TypeError) + -> { @bignum / mock('10') }.should.raise(TypeError) + -> { @bignum / "2" }.should.raise(TypeError) + -> { @bignum / :symbol }.should.raise(TypeError) end end + + it "coerces the RHS and calls #coerce" do + obj = mock("integer plus") + obj.should_receive(:coerce).with(6).and_return([6, 3]) + (6 / obj).should == 2 + end + + it "coerces the RHS and calls #coerce even if it's private" do + obj = Object.new + class << obj + private def coerce(n) + [n, 3] + end + end + + (6 / obj).should == 2 + end end |
