Skip to content

Commit 3b51ece

Browse files
committed
StringIO: Add spec for printf and polish a bit the existing ones
1 parent aaadee3 commit 3b51ece

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

spec/ruby/library/stringio/open_spec.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,14 @@
167167
io.should equal(ret)
168168
end
169169

170-
it "sets the mode to read-write" do
170+
it "sets the mode to read-write (r+)" do
171171
io = StringIO.open("example")
172172
io.closed_read?.should be_false
173173
io.closed_write?.should be_false
174+
175+
io = StringIO.new("example")
176+
io.printf("%d", 123)
177+
io.string.should == "123mple"
174178
end
175179

176180
it "tries to convert the passed Object to a String using #to_str" do
@@ -195,10 +199,14 @@
195199
io.should equal(ret)
196200
end
197201

198-
it "sets the mode to read-write" do
202+
it "sets the mode to read-write (r+)" do
199203
io = StringIO.open
200204
io.closed_read?.should be_false
201205
io.closed_write?.should be_false
206+
207+
io = StringIO.new("example")
208+
io.printf("%d", 123)
209+
io.string.should == "123mple"
202210
end
203211

204212
it "uses an empty String as the StringIO backend" do

spec/ruby/library/stringio/printf_spec.rb

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44

55
describe "StringIO#printf" do
66
before :each do
7-
@io = StringIO.new('example')
7+
@io = StringIO.new()
88
end
99

1010
it "returns nil" do
1111
@io.printf("%d %04x", 123, 123).should be_nil
1212
end
1313

1414
it "pads self with \\000 when the current position is after the end" do
15-
@io.pos = 10
15+
@io.pos = 3
1616
@io.printf("%d", 123)
17-
@io.string.should == "example\000\000\000123"
17+
@io.string.should == "\000\000\000123"
1818
end
1919

2020
it "performs format conversion" do
@@ -39,6 +39,27 @@
3939
end
4040
end
4141

42+
describe "StringIO#printf when in read-write mode" do
43+
before :each do
44+
@io = StringIO.new("example", "r+")
45+
end
46+
47+
it "starts from the beginning" do
48+
@io.printf("%s", "abcdefghijk")
49+
@io.string.should == "abcdefghijk"
50+
end
51+
52+
it "does not truncate existing string" do
53+
@io.printf("%s", "abc")
54+
@io.string.should == "abcmple"
55+
end
56+
57+
it "correctly updates self's position" do
58+
@io.printf("%s", "abc")
59+
@io.pos.should eql(3)
60+
end
61+
end
62+
4263
describe "StringIO#printf when in append mode" do
4364
before :each do
4465
@io = StringIO.new("example", "a")

0 commit comments

Comments
 (0)