1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
This commit is contained in:
Benoit Daloze 2022-08-29 15:36:29 +02:00
parent a319d3cfdc
commit 4ee1a68776
32 changed files with 471 additions and 50 deletions

View file

@ -293,13 +293,13 @@ describe :kernel_sprintf, shared: true do
it "raises ArgumentError if argument is a string of several characters" do
-> {
@method.call("%c", "abc")
}.should raise_error(ArgumentError)
}.should raise_error(ArgumentError, /%c requires a character/)
end
it "raises ArgumentError if argument is an empty string" do
-> {
@method.call("%c", "")
}.should raise_error(ArgumentError)
}.should raise_error(ArgumentError, /%c requires a character/)
end
end
@ -313,9 +313,56 @@ describe :kernel_sprintf, shared: true do
end
end
it "supports Unicode characters" do
@method.call("%c", 1286).should == "Ԇ"
@method.call("%c", "ش").should == "ش"
it "raises TypeError if argument is not String or Integer and cannot be converted to them" do
-> {
@method.call("%c", [])
}.should raise_error(TypeError, /no implicit conversion of Array into Integer/)
end
it "raises TypeError if argument is nil" do
-> {
@method.call("%c", nil)
}.should raise_error(TypeError, /no implicit conversion from nil to integer/)
end
it "tries to convert argument to String with to_str" do
obj = BasicObject.new
def obj.to_str
"a"
end
@method.call("%c", obj).should == "a"
end
it "tries to convert argument to Integer with to_int" do
obj = BasicObject.new
def obj.to_int
90
end
@method.call("%c", obj).should == "Z"
end
it "raises TypeError if converting to String with to_str returns non-String" do
obj = BasicObject.new
def obj.to_str
:foo
end
-> {
@method.call("%c", obj)
}.should raise_error(TypeError, /can't convert BasicObject to String/)
end
it "raises TypeError if converting to Integer with to_int returns non-Integer" do
obj = BasicObject.new
def obj.to_str
:foo
end
-> {
@method.call("%c", obj)
}.should raise_error(TypeError, /can't convert BasicObject to String/)
end
end
@ -374,11 +421,11 @@ describe :kernel_sprintf, shared: true do
@method.call("%4.6s", "abcdefg").should == "abcdef"
end
it "formats nli with width" do
it "formats nil with width" do
@method.call("%6s", nil).should == " "
end
it "formats nli with precision" do
it "formats nil with precision" do
@method.call("%.6s", nil).should == ""
end
@ -939,4 +986,8 @@ describe :kernel_sprintf, shared: true do
}
end
end
it "does not raise error when passed more arguments than needed" do
sprintf("%s %d %c", "string", 2, "c", []).should == "string 2 c"
end
end