summaryrefslogtreecommitdiff
path: root/spec/ruby/core/kernel/shared/sprintf.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/core/kernel/shared/sprintf.rb')
-rw-r--r--spec/ruby/core/kernel/shared/sprintf.rb91
1 files changed, 51 insertions, 40 deletions
diff --git a/spec/ruby/core/kernel/shared/sprintf.rb b/spec/ruby/core/kernel/shared/sprintf.rb
index 13dc6e97f0..fe77b5f45b 100644
--- a/spec/ruby/core/kernel/shared/sprintf.rb
+++ b/spec/ruby/core/kernel/shared/sprintf.rb
@@ -22,12 +22,13 @@ describe :kernel_sprintf, shared: true do
@method.call("%d", "112").should == "112"
@method.call("%d", "0127").should == "87"
@method.call("%d", "0xc4").should == "196"
+ @method.call("%d", "0").should == "0"
end
it "raises TypeError exception if cannot convert to Integer" do
-> {
@method.call("%b", Object.new)
- }.should raise_error(TypeError)
+ }.should.raise(TypeError)
end
["b", "B"].each do |f|
@@ -57,6 +58,11 @@ describe :kernel_sprintf, shared: true do
it "works well with large numbers" do
@method.call("%#{f}", 1234567890987654321).should == "1234567890987654321"
end
+
+ it "converts to the empty string if precision is 0 and value is 0" do
+ @method.call("%.#{f}", 0).should == ""
+ @method.call("%.0#{f}", 0).should == ""
+ end
end
end
@@ -116,7 +122,7 @@ describe :kernel_sprintf, shared: true do
it "raises TypeError exception if cannot convert to Float" do
-> {
@method.call("%f", Object.new)
- }.should raise_error(TypeError)
+ }.should.raise(TypeError)
end
{"e" => "e", "E" => "E"}.each_pair do |f, exp|
@@ -289,40 +295,24 @@ describe :kernel_sprintf, shared: true do
@method.call("%c", "a").should == "a"
end
- 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
+ it "displays only the first character if argument is a string of several characters" do
+ @method.call("%c", "abc").should == "a"
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
+ it "displays no characters if argument is an empty string" do
+ @method.call("%c", "").should == ""
end
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/)
+ }.should raise_consistent_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/)
+ }.should raise_consistent_error(TypeError, /no implicit conversion of nil into Integer/)
end
it "tries to convert argument to String with to_str" do
@@ -351,7 +341,7 @@ describe :kernel_sprintf, shared: true do
-> {
@method.call("%c", obj)
- }.should raise_error(TypeError, /can't convert BasicObject to String/)
+ }.should raise_consistent_error(TypeError, /can't convert BasicObject into String/)
end
it "raises TypeError if converting to Integer with to_int returns non-Integer" do
@@ -362,7 +352,7 @@ describe :kernel_sprintf, shared: true do
-> {
@method.call("%c", obj)
- }.should raise_error(TypeError, /can't convert BasicObject to Integer/)
+ }.should raise_consistent_error(TypeError, /can't convert BasicObject into Integer/)
end
end
@@ -372,6 +362,10 @@ describe :kernel_sprintf, shared: true do
obj.should_receive(:inspect).and_return("<inspect-result>")
@method.call("%p", obj).should == "<inspect-result>"
end
+
+ it "substitutes 'nil' for nil" do
+ @method.call("%p", nil).should == "nil"
+ end
end
describe "s" do
@@ -397,7 +391,7 @@ describe :kernel_sprintf, shared: true do
-> {
@method.call("%s", obj)
- }.should raise_error(NoMethodError)
+ }.should.raise(NoMethodError)
end
it "formats a partial substring without including omitted characters" do
@@ -450,12 +444,12 @@ describe :kernel_sprintf, shared: true do
it "alone raises an ArgumentError" do
-> {
@method.call("%")
- }.should raise_error(ArgumentError)
+ }.should.raise(ArgumentError)
end
it "is escaped by %" do
@method.call("%%").should == "%"
- @method.call("%%d", 10).should == "%d"
+ @method.call("%%d").should == "%d"
end
end
end
@@ -557,7 +551,7 @@ describe :kernel_sprintf, shared: true do
it "raises exception if argument number is bigger than actual arguments list" do
-> {
@method.call("%4$d", 1, 2, 3)
- }.should raise_error(ArgumentError)
+ }.should.raise(ArgumentError)
end
it "ignores '-' sign" do
@@ -568,7 +562,7 @@ describe :kernel_sprintf, shared: true do
it "raises ArgumentError exception when absolute and relative argument numbers are mixed" do
-> {
@method.call("%1$d %d", 1, 2)
- }.should raise_error(ArgumentError)
+ }.should.raise(ArgumentError)
end
end
@@ -818,7 +812,7 @@ describe :kernel_sprintf, shared: true do
it "raises ArgumentError when is mixed with width" do
-> {
@method.call("%*10d", 10, 112)
- }.should raise_error(ArgumentError)
+ }.should.raise(ArgumentError)
end
end
end
@@ -917,7 +911,7 @@ describe :kernel_sprintf, shared: true do
it "cannot be mixed with unnamed style" do
-> {
@method.call("%d %<foo>d", 1, foo: "123")
- }.should raise_error(ArgumentError)
+ }.should.raise(ArgumentError)
end
end
@@ -937,13 +931,30 @@ describe :kernel_sprintf, shared: true do
it "cannot be mixed with unnamed style" do
-> {
@method.call("%d %{foo}", 1, foo: "123")
- }.should raise_error(ArgumentError)
+ }.should.raise(ArgumentError)
+ end
+
+ it "respects Hash#default when there is no set key" do
+ @method.call("%{foo}", Hash.new(123)).should == "123"
+ @method.call("%{foo}", Hash.new { 123 }).should == "123"
end
- it "raises KeyError when there is no matching key" do
+ it "raises KeyError when Hash#default returns nil" do
-> {
@method.call("%{foo}", {})
- }.should raise_error(KeyError)
+ }.should.raise(KeyError, 'key{foo} not found')
+
+ -> {
+ @method.call("%{foo}", Hash.new(nil))
+ }.should.raise(KeyError, 'key{foo} not found')
+
+ -> {
+ @method.call("%{foo}", Hash.new { nil })
+ }.should.raise(KeyError, 'key{foo} not found')
+ end
+
+ it "accepts a nil value for an existing key" do
+ @method.call("%{foo}", { foo: nil }).should == ""
end
it "converts value to String with to_s" do
@@ -967,21 +978,21 @@ describe :kernel_sprintf, shared: true do
it "raises a KeyError" do
-> {
@method.call("%<foo>s", @object)
- }.should raise_error(KeyError)
+ }.should.raise(KeyError)
end
it "sets the Hash as the receiver of KeyError" do
-> {
@method.call("%<foo>s", @object)
- }.should raise_error(KeyError) { |err|
- err.receiver.should equal(@object)
+ }.should.raise(KeyError) { |err|
+ err.receiver.should.equal?(@object)
}
end
it "sets the unmatched key as the key of KeyError" do
-> {
@method.call("%<foo>s", @object)
- }.should raise_error(KeyError) { |err|
+ }.should.raise(KeyError) { |err|
err.key.to_s.should == 'foo'
}
end