1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66888 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
eregon 2019-01-20 20:38:57 +00:00
parent 58573c33e4
commit 6204e0804b
147 changed files with 2728 additions and 21522 deletions

View file

@ -99,6 +99,65 @@ describe :kernel_integer, shared: true do
it "raises a FloatDomainError when passed Infinity" do
lambda { Integer(infinity_value) }.should raise_error(FloatDomainError)
end
ruby_version_is "2.6" do
describe "when passed exception: false" do
describe "and to_i returns a value that is not an Integer" do
it "swallows an error" do
obj = mock("object")
obj.should_receive(:to_i).and_return("1")
Integer(obj, exception: false).should == nil
end
end
describe "and no to_int or to_i methods exist" do
it "swallows an error" do
obj = mock("object")
Integer(obj, exception: false).should == nil
end
end
describe "and to_int returns nil and no to_i exists" do
it "swallows an error" do
obj = mock("object")
obj.should_receive(:to_i).and_return(nil)
Integer(obj, exception: false).should == nil
end
end
ruby_bug "#15525", "2.6"..."2.6.1" do
describe "and passed NaN" do
it "swallows an error" do
Integer(nan_value, exception: false).should == nil
end
end
describe "and passed Infinity" do
it "swallows an error" do
Integer(infinity_value, exception: false).should == nil
end
end
end
describe "and passed nil" do
it "swallows an error" do
Integer(nil, exception: false).should == nil
end
end
describe "and passed a String that contains numbers" do
it "normally parses it and returns an Integer" do
Integer("42", exception: false).should == 42
end
end
describe "and passed a String that can't be converted to an Integer" do
it "swallows an error" do
Integer("abc", exception: false).should == nil
end
end
end
end
end
describe "Integer() given a String", shared: true do
@ -189,6 +248,34 @@ describe "Integer() given a String", shared: true do
lambda { Integer("") }.should raise_error(ArgumentError)
end
ruby_version_is "2.6" do
describe "when passed exception: false" do
describe "and multiple leading -s" do
it "swallows an error" do
Integer("---1", exception: false).should == nil
end
end
describe "and multiple trailing -s" do
it "swallows an error" do
Integer("1---", exception: false).should == nil
end
end
describe "and an argument that contains a period" do
it "swallows an error" do
Integer("0.0", exception: false).should == nil
end
end
describe "and an empty string" do
it "swallows an error" do
Integer("", exception: false).should == nil
end
end
end
end
it "parses the value as 0 if the string consists of a single zero character" do
Integer("0").should == 0
end
@ -508,6 +595,24 @@ describe "Integer() given a String and base", shared: true do
lambda { Integer(98, 15) }.should raise_error(ArgumentError)
end
end
ruby_version_is "2.6" do
describe "when passed exception: false" do
describe "and valid argument" do
it "returns an Integer number" do
Integer("100", 10, exception: false).should == 100
Integer("100", 2, exception: false).should == 4
end
end
describe "and invalid argument" do
it "swallows an error" do
Integer("999", 2, exception: false).should == nil
Integer("abc", 10, exception: false).should == nil
end
end
end
end
end
describe :kernel_Integer, shared: true do