diff options
Diffstat (limited to 'spec/ruby/core/kernel/shared/sprintf.rb')
-rw-r--r-- | spec/ruby/core/kernel/shared/sprintf.rb | 85 |
1 files changed, 74 insertions, 11 deletions
diff --git a/spec/ruby/core/kernel/shared/sprintf.rb b/spec/ruby/core/kernel/shared/sprintf.rb index 84d472b0d1..13dc6e97f0 100644 --- a/spec/ruby/core/kernel/shared/sprintf.rb +++ b/spec/ruby/core/kernel/shared/sprintf.rb @@ -289,21 +289,80 @@ describe :kernel_sprintf, shared: true do @method.call("%c", "a").should == "a" end - it "raises ArgumentError if argument is a string of several characters" do + ruby_version_is ""..."3.2" do + it "raises ArgumentError if argument is a string of several characters" do + -> { + @method.call("%c", "abc") + }.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, /%c requires a character/) + end + end + + ruby_version_is "3.2" do + it "displays only the first character if argument is a string of several characters" do + @method.call("%c", "abc").should == "a" + end + + it "displays no characters if argument is an empty string" do + @method.call("%c", "").should == "" + end + end + + it "raises TypeError if argument is not String or Integer and cannot be converted to them" do -> { - @method.call("%c", "abc") - }.should raise_error(ArgumentError) + @method.call("%c", []) + }.should raise_error(TypeError, /no implicit conversion of Array into Integer/) end - it "raises ArgumentError if argument is an empty string" do + it "raises TypeError if argument is nil" do -> { - @method.call("%c", "") - }.should raise_error(ArgumentError) + @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 "supports Unicode characters" do - @method.call("%c", 1286).should == "Ԇ" - @method.call("%c", "ش").should == "ش" + 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_int + :foo + end + + -> { + @method.call("%c", obj) + }.should raise_error(TypeError, /can't convert BasicObject to Integer/) end end @@ -362,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 @@ -927,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 |