rational.c: try converting by to_int in Rational() (#3684)

[Bug #12485]
This commit is contained in:
Kenta Murata 2020-10-22 17:59:52 +09:00 committed by GitHub
parent 603fb940c0
commit d23d5c3130
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
Notes: git 2020-10-22 18:00:33 +09:00
Merged-By: mrkn <mrkn@ruby-lang.org>
2 changed files with 50 additions and 3 deletions

View file

@ -128,6 +128,13 @@ class Rational_Test < Test::Unit::TestCase
assert_raise(TypeError){Rational(Object.new, Object.new)}
assert_raise(TypeError){Rational(1, Object.new)}
bug12485 = '[ruby-core:75995] [Bug #12485]'
o = Object.new
def o.to_int; 1; end
assert_equal(1, Rational(o, 1), bug12485)
assert_equal(1, Rational(1, o), bug12485)
assert_equal(1, Rational(o, o), bug12485)
o = Object.new
def o.to_r; 1/42r; end
assert_equal(1/42r, Rational(o))
@ -834,6 +841,18 @@ class Rational_Test < Test::Unit::TestCase
assert_equal(nil, Rational(1, Object.new, exception: false))
}
bug12485 = '[ruby-core:75995] [Bug #12485]'
assert_nothing_raised(RuntimeError, bug12485) {
o = Object.new
def o.to_int; raise; end
assert_equal(nil, Rational(o, exception: false))
}
assert_nothing_raised(RuntimeError, bug12485) {
o = Object.new
def o.to_int; raise; end
assert_equal(nil, Rational(1, o, exception: false))
}
o = Object.new;
def o.to_r; raise; end
assert_nothing_raised(RuntimeError) {