diff options
-rw-r--r-- | complex.c | 7 | ||||
-rw-r--r-- | test/ruby/test_complex.rb | 37 |
2 files changed, 21 insertions, 23 deletions
@@ -2323,8 +2323,11 @@ nucomp_convert(VALUE klass, VALUE a1, VALUE a2, int raise) return a1; /* should raise exception for consistency */ if (!k_numeric_p(a1)) { - if (!raise) - return rb_protect(to_complex, a1, NULL); + if (!raise) { + a1 = rb_protect(to_complex, a1, NULL); + rb_set_errinfo(Qnil); + return a1; + } return to_complex(a1); } } diff --git a/test/ruby/test_complex.rb b/test/ruby/test_complex.rb index 5cd17d9205..f2a0f5b5ee 100644 --- a/test/ruby/test_complex.rb +++ b/test/ruby/test_complex.rb @@ -981,30 +981,16 @@ class Complex_Test < Test::Unit::TestCase end def test_Complex_without_exception - assert_nothing_raised(ArgumentError){ - assert_equal(nil, Complex('5x', exception: false)) - } - assert_nothing_raised(ArgumentError){ - assert_equal(nil, Complex(nil, exception: false)) - } - assert_nothing_raised(ArgumentError){ - assert_equal(nil, Complex(Object.new, exception: false)) - } - assert_nothing_raised(ArgumentError){ - assert_equal(nil, Complex(1, nil, exception: false)) - } - assert_nothing_raised(ArgumentError){ - assert_equal(nil, Complex(1, Object.new, exception: false)) - } + assert_complex_without_exception('5x') + assert_complex_without_exception(nil) + assert_complex_without_exception(Object.new) + assert_complex_without_exception(1, nil) + assert_complex_without_exception(1, Object.new) o = Object.new def o.to_c; raise; end - assert_nothing_raised(ArgumentError){ - assert_equal(nil, Complex(o, exception: false)) - } - assert_nothing_raised(ArgumentError){ - assert_equal(nil, Complex(1, o, exception: false)) - } + assert_complex_without_exception(o) + assert_complex_without_exception(1, o) end def test_respond @@ -1263,4 +1249,13 @@ class Complex_Test < Test::Unit::TestCase Complex.polar(1, obj) end end + + private + + def assert_complex_without_exception(*args) + assert_nothing_raised(ArgumentError) do + assert_nil(Complex(*args, exception: false)) + assert_nil($!) + end + end end |