mirror of
https://github.com/ruby/ruby.git
synced 2025-09-22 03:53:58 +02:00
Add Integer.try_convert [Feature #15211]
This commit is contained in:
parent
eee709595c
commit
301d194ee3
Notes:
git
2021-07-16 17:50:23 +09:00
6 changed files with 83 additions and 3 deletions
|
@ -660,4 +660,21 @@ class TestInteger < Test::Unit::TestCase
|
|||
def o.fdiv(x); 1; end
|
||||
assert_equal(1.0, 1.fdiv(o))
|
||||
end
|
||||
|
||||
def test_try_convert
|
||||
assert_equal(1, Integer.try_convert(1))
|
||||
assert_equal(1, Integer.try_convert(1.0))
|
||||
assert_nil Integer.try_convert("1")
|
||||
o = Object.new
|
||||
assert_nil Integer.try_convert(o)
|
||||
def o.to_i; 1; end
|
||||
assert_nil Integer.try_convert(o)
|
||||
o = Object.new
|
||||
def o.to_int; 1; end
|
||||
assert_equal(1, Integer.try_convert(o))
|
||||
|
||||
o = Object.new
|
||||
def o.to_int; Object.new; end
|
||||
assert_raise_with_message(TypeError, /can't convert Object to Integer/) {Integer.try_convert(o)}
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue