1
0
Fork 0
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:
mrkn 2018-02-26 07:31:10 +00:00
parent 6549f20ebf
commit cc4be225a8
3 changed files with 24 additions and 6 deletions

View file

@ -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