summaryrefslogtreecommitdiff
path: root/spec/ruby/core/float
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/core/float')
-rw-r--r--spec/ruby/core/float/divide_spec.rb3
-rw-r--r--spec/ruby/core/float/fixtures/classes.rb4
-rw-r--r--spec/ruby/core/float/gt_spec.rb3
-rw-r--r--spec/ruby/core/float/gte_spec.rb3
-rw-r--r--spec/ruby/core/float/lt_spec.rb3
-rw-r--r--spec/ruby/core/float/lte_spec.rb3
-rw-r--r--spec/ruby/core/float/minus_spec.rb3
-rw-r--r--spec/ruby/core/float/multiply_spec.rb3
-rw-r--r--spec/ruby/core/float/plus_spec.rb3
-rw-r--r--spec/ruby/core/float/shared/arithmetic_exception_in_coerce.rb34
-rw-r--r--spec/ruby/core/float/shared/comparison_exception_in_coerce.rb36
11 files changed, 98 insertions, 0 deletions
diff --git a/spec/ruby/core/float/divide_spec.rb b/spec/ruby/core/float/divide_spec.rb
index 0acd7b20b4..34d6ef45e1 100644
--- a/spec/ruby/core/float/divide_spec.rb
+++ b/spec/ruby/core/float/divide_spec.rb
@@ -1,7 +1,10 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/coerce.rb', __FILE__)
+require File.expand_path('../shared/arithmetic_exception_in_coerce', __FILE__)
describe "Float#/" do
+ it_behaves_like :float_arithmetic_exception_in_coerce, :/
+
it "returns self divided by other" do
(5.75 / -2).should be_close(-2.875,TOLERANCE)
(451.0 / 9.3).should be_close(48.494623655914,TOLERANCE)
diff --git a/spec/ruby/core/float/fixtures/classes.rb b/spec/ruby/core/float/fixtures/classes.rb
new file mode 100644
index 0000000000..2d80184e7d
--- /dev/null
+++ b/spec/ruby/core/float/fixtures/classes.rb
@@ -0,0 +1,4 @@
+module FloatSpecs
+ class CoerceError < StandardError
+ end
+end
diff --git a/spec/ruby/core/float/gt_spec.rb b/spec/ruby/core/float/gt_spec.rb
index 9725c6acd7..96ea805f90 100644
--- a/spec/ruby/core/float/gt_spec.rb
+++ b/spec/ruby/core/float/gt_spec.rb
@@ -1,6 +1,9 @@
require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../shared/comparison_exception_in_coerce', __FILE__)
describe "Float#>" do
+ it_behaves_like :float_comparison_exception_in_coerce, :>
+
it "returns true if self is greater than other" do
(1.5 > 1).should == true
(2.5 > 3).should == false
diff --git a/spec/ruby/core/float/gte_spec.rb b/spec/ruby/core/float/gte_spec.rb
index 2c14651dd7..1903308763 100644
--- a/spec/ruby/core/float/gte_spec.rb
+++ b/spec/ruby/core/float/gte_spec.rb
@@ -1,6 +1,9 @@
require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../shared/comparison_exception_in_coerce', __FILE__)
describe "Float#>=" do
+ it_behaves_like :float_comparison_exception_in_coerce, :>=
+
it "returns true if self is greater than or equal to other" do
(5.2 >= 5.2).should == true
(9.71 >= 1).should == true
diff --git a/spec/ruby/core/float/lt_spec.rb b/spec/ruby/core/float/lt_spec.rb
index e2e43b0fb7..d83fee0d4a 100644
--- a/spec/ruby/core/float/lt_spec.rb
+++ b/spec/ruby/core/float/lt_spec.rb
@@ -1,6 +1,9 @@
require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../shared/comparison_exception_in_coerce', __FILE__)
describe "Float#<" do
+ it_behaves_like :float_comparison_exception_in_coerce, :<
+
it "returns true if self is less than other" do
(71.3 < 91.8).should == true
(192.6 < -500).should == false
diff --git a/spec/ruby/core/float/lte_spec.rb b/spec/ruby/core/float/lte_spec.rb
index e2e44b2257..3d5f830633 100644
--- a/spec/ruby/core/float/lte_spec.rb
+++ b/spec/ruby/core/float/lte_spec.rb
@@ -1,6 +1,9 @@
require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../shared/comparison_exception_in_coerce', __FILE__)
describe "Float#<=" do
+ it_behaves_like :float_comparison_exception_in_coerce, :>=
+
it "returns true if self is less than or equal to other" do
(2.0 <= 3.14159).should == true
(-2.7183 <= -24).should == false
diff --git a/spec/ruby/core/float/minus_spec.rb b/spec/ruby/core/float/minus_spec.rb
index d5c0d863ed..49a81e2fc8 100644
--- a/spec/ruby/core/float/minus_spec.rb
+++ b/spec/ruby/core/float/minus_spec.rb
@@ -1,6 +1,9 @@
require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../shared/arithmetic_exception_in_coerce', __FILE__)
describe "Float#-" do
+ it_behaves_like :float_arithmetic_exception_in_coerce, :-
+
it "returns self minus other" do
(9_237_212.5280 - 5_280).should be_close(9231932.528, TOLERANCE)
(2_560_496.1691 - bignum_value).should be_close(-9223372036852215808.000, TOLERANCE)
diff --git a/spec/ruby/core/float/multiply_spec.rb b/spec/ruby/core/float/multiply_spec.rb
index 14680534c4..f13beb7641 100644
--- a/spec/ruby/core/float/multiply_spec.rb
+++ b/spec/ruby/core/float/multiply_spec.rb
@@ -1,6 +1,9 @@
require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../shared/arithmetic_exception_in_coerce', __FILE__)
describe "Float#*" do
+ it_behaves_like :float_arithmetic_exception_in_coerce, :*
+
it "returns self multiplied by other" do
(4923.98221 * 2).should be_close(9847.96442, TOLERANCE)
(6712.5 * 0.25).should be_close(1678.125, TOLERANCE)
diff --git a/spec/ruby/core/float/plus_spec.rb b/spec/ruby/core/float/plus_spec.rb
index a49124d303..743ac5f618 100644
--- a/spec/ruby/core/float/plus_spec.rb
+++ b/spec/ruby/core/float/plus_spec.rb
@@ -1,6 +1,9 @@
require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../shared/arithmetic_exception_in_coerce', __FILE__)
describe "Float#+" do
+ it_behaves_like :float_arithmetic_exception_in_coerce, :+
+
it "returns self plus other" do
(491.213 + 2).should be_close(493.213, TOLERANCE)
(9.99 + bignum_value).should be_close(9223372036854775808.000, TOLERANCE)
diff --git a/spec/ruby/core/float/shared/arithmetic_exception_in_coerce.rb b/spec/ruby/core/float/shared/arithmetic_exception_in_coerce.rb
new file mode 100644
index 0000000000..0d9f32af16
--- /dev/null
+++ b/spec/ruby/core/float/shared/arithmetic_exception_in_coerce.rb
@@ -0,0 +1,34 @@
+require File.expand_path('../../fixtures/classes', __FILE__)
+
+describe :float_arithmetic_exception_in_coerce, shared: true do
+ ruby_version_is ""..."2.5" do
+ it "rescues exception (StandardError and subclasses) raised in other#coerce and raises TypeError" do
+ b = mock("numeric with failed #coerce")
+ b.should_receive(:coerce).and_raise(FloatSpecs::CoerceError)
+
+ # e.g. 1.0 > b
+ -> { 1.0.send(@method, b) }.should raise_error(TypeError, /MockObject can't be coerced into Float/)
+ end
+
+ it "does not rescue Exception and StandardError siblings raised in other#coerce" do
+ [Exception, NoMemoryError].each do |exception|
+ b = mock("numeric with failed #coerce")
+ b.should_receive(:coerce).and_raise(exception)
+
+ # e.g. 1.0 > b
+ -> { 1.0.send(@method, b) }.should raise_error(exception)
+ end
+ end
+ end
+
+ ruby_version_is "2.5" do
+ it "does not rescue exception raised in other#coerce" do
+ b = mock("numeric with failed #coerce")
+ b.should_receive(:coerce).and_raise(FloatSpecs::CoerceError)
+
+ # e.g. 1.0 > b
+ -> { 1.0.send(@method, b) }.should raise_error(FloatSpecs::CoerceError)
+ end
+ end
+end
+
diff --git a/spec/ruby/core/float/shared/comparison_exception_in_coerce.rb b/spec/ruby/core/float/shared/comparison_exception_in_coerce.rb
new file mode 100644
index 0000000000..688e5dc0d1
--- /dev/null
+++ b/spec/ruby/core/float/shared/comparison_exception_in_coerce.rb
@@ -0,0 +1,36 @@
+require File.expand_path('../../fixtures/classes', __FILE__)
+
+describe :float_comparison_exception_in_coerce, shared: true do
+ ruby_version_is ""..."2.5" do
+ it "rescues exception (StandardError and subclasses) raised in other#coerce and raises ArgumentError" do
+ b = mock("numeric with failed #coerce")
+ b.should_receive(:coerce).and_raise(FloatSpecs::CoerceError)
+
+ # e.g. 1.0 > b
+ -> {
+ -> { 1.0.send(@method, b) }.should raise_error(ArgumentError, /comparison of Float with MockObject failed/)
+ }.should complain(/Numerical comparison operators will no more rescue exceptions of #coerce/)
+ end
+
+ it "does not rescue Exception and StandardError siblings raised in other#coerce" do
+ [Exception, NoMemoryError].each do |exception|
+ b = mock("numeric with failed #coerce")
+ b.should_receive(:coerce).and_raise(exception)
+
+ # e.g. 1.0 > b
+ -> { 1.0.send(@method, b) }.should raise_error(exception)
+ end
+ end
+ end
+
+ ruby_version_is "2.5" do
+ it "does not rescue exception raised in other#coerce" do
+ b = mock("numeric with failed #coerce")
+ b.should_receive(:coerce).and_raise(FloatSpecs::CoerceError)
+
+ # e.g. 1.0 > b
+ -> { 1.0.send(@method, b) }.should raise_error(FloatSpecs::CoerceError)
+ end
+ end
+end
+