mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Check the result of to_int in Kernel#Integer
[ruby-core:85813] [Bug #14552] * object.c (rb_convert_to_integer): Check the result of to_int in Kernel#Integer * test/ruby/test_integer.rb: add tests. * spec/ruby/core/kernel/Integer_spec.rb: fix examples. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62581 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
6549f20ebf
commit
cc4be225a8
3 changed files with 24 additions and 6 deletions
2
object.c
2
object.c
|
@ -3132,7 +3132,7 @@ rb_convert_to_integer(VALUE val, int base)
|
|||
rb_raise(rb_eArgError, "base specified for non string value");
|
||||
}
|
||||
tmp = convert_type(val, "Integer", "to_int", FALSE);
|
||||
if (NIL_P(tmp)) {
|
||||
if (!RB_INTEGER_TYPE_P(tmp)) {
|
||||
return rb_to_integer(val, "to_i");
|
||||
}
|
||||
return tmp;
|
||||
|
|
|
@ -10,11 +10,18 @@ describe :kernel_integer, shared: true do
|
|||
Integer(100).should == 100
|
||||
end
|
||||
|
||||
it "uncritically return the value of to_int even if it is not an Integer" do
|
||||
it "raises a TypeError when to_int returns not-an-Integer object and to_i returns nil" do
|
||||
obj = mock("object")
|
||||
obj.should_receive(:to_int).and_return("1")
|
||||
obj.should_not_receive(:to_i)
|
||||
Integer(obj).should == "1"
|
||||
obj.should_receive(:to_i).and_return(nil)
|
||||
lambda { Integer(obj) }.should raise_error(TypeError)
|
||||
end
|
||||
|
||||
it "return a result of to_i when to_int does not return an Integer" do
|
||||
obj = mock("object")
|
||||
obj.should_receive(:to_int).and_return("1")
|
||||
obj.should_receive(:to_i).and_return(42)
|
||||
Integer(obj).should == 42
|
||||
end
|
||||
|
||||
it "raises a TypeError when passed nil" do
|
||||
|
@ -45,9 +52,9 @@ describe :kernel_integer, shared: true do
|
|||
|
||||
it "returns the value of to_int if the result is a Bignum" do
|
||||
obj = mock("object")
|
||||
obj.should_receive(:to_int).and_return(2e100)
|
||||
obj.should_receive(:to_int).and_return(2 * 10**100)
|
||||
obj.should_not_receive(:to_i)
|
||||
Integer(obj).should == 2e100
|
||||
Integer(obj).should == 2 * 10**100
|
||||
end
|
||||
|
||||
it "calls to_i on an object whose to_int returns nil" do
|
||||
|
|
|
@ -92,6 +92,17 @@ class TestInteger < Test::Unit::TestCase
|
|||
assert_equal(2 ** 50, Integer(2.0 ** 50))
|
||||
assert_raise(TypeError) { Integer(nil) }
|
||||
|
||||
bug14552 = '[ruby-core:85813]'
|
||||
obj = Object.new
|
||||
def obj.to_int; "str"; end
|
||||
assert_raise(TypeError, bug14552) { Integer(obj) }
|
||||
def obj.to_i; 42; end
|
||||
assert_equal(42, Integer(obj), bug14552)
|
||||
|
||||
obj = Object.new
|
||||
def obj.to_i; "str"; end
|
||||
assert_raise(TypeError) { Integer(obj) }
|
||||
|
||||
bug6192 = '[ruby-core:43566]'
|
||||
assert_raise(Encoding::CompatibilityError, bug6192) {Integer("0".encode("utf-16be"))}
|
||||
assert_raise(Encoding::CompatibilityError, bug6192) {Integer("0".encode("utf-16le"))}
|
||||
|
|
Loading…
Reference in a new issue