Skip to content

Commit 555a495

Browse files
aardvark179eregon
authored andcommitted
Add tests for formatting nil with width and precision.
1 parent 89ac167 commit 555a495

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

core/kernel/shared/sprintf.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,10 @@ def obj.to_i; 10; end
320320
@method.call("%s", "abc").should == "abc"
321321
end
322322

323+
it "substitutes '' for nil" do
324+
@method.call("%s", nil).should == ""
325+
end
326+
323327
it "converts argument to string with to_s" do
324328
obj = mock("string")
325329
obj.should_receive(:to_s).and_return("abc")
@@ -348,6 +352,28 @@ def obj.to_str
348352
Kernel.format("%-3.3s", "hello").should == "hel"
349353
end
350354

355+
it "formats string with width" do
356+
@method.call("%6s", "abc").should == " abc"
357+
@method.call("%6s", "abcdefg").should == "abcdefg"
358+
end
359+
360+
it "formats string with width and precision" do
361+
@method.call("%4.6s", "abc").should == " abc"
362+
@method.call("%4.6s", "abcdefg").should == "abcdef"
363+
end
364+
365+
it "formats nli with width" do
366+
@method.call("%6s", nil).should == " "
367+
end
368+
369+
it "formats nli with precision" do
370+
@method.call("%.6s", nil).should == ""
371+
end
372+
373+
it "formats nil with width and precision" do
374+
@method.call("%4.6s", nil).should == " "
375+
end
376+
351377
it "formats multibyte string with precision" do
352378
Kernel.format("%.2s", "été").should == "ét"
353379
end

0 commit comments

Comments
 (0)