Skip to content

Commit 7863a1b

Browse files
herwinwandrykonchin
authored andcommitted
Additional tests for StringIO#readpartial
* Encoding of buffer argument * Exception message validation (except for EOFError) * Calling with length of 0 on a closed stream should raise an error. Added this to IO specs as well.
1 parent 9320f5e commit 7863a1b

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

core/io/readpartial_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@
9393
@rd.readpartial(0).should == ""
9494
end
9595

96+
it "raises IOError if the stream is closed and the length argument is 0" do
97+
@rd.close
98+
-> { @rd.readpartial(0) }.should raise_error(IOError, "closed stream")
99+
end
100+
96101
it "clears and returns the given buffer if the length argument is 0" do
97102
buffer = +"existing content"
98103
@rd.readpartial(0, buffer).should == buffer

library/stringio/readpartial_spec.rb

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,34 @@
6161

6262
it "raises IOError if the stream is closed" do
6363
@string.close
64-
-> { @string.readpartial(1) }.should raise_error(IOError)
64+
-> { @string.readpartial(1) }.should raise_error(IOError, "not opened for reading")
6565
end
6666

6767
it "raises ArgumentError if the negative argument is provided" do
68-
-> { @string.readpartial(-1) }.should raise_error(ArgumentError)
68+
-> { @string.readpartial(-1) }.should raise_error(ArgumentError, "negative length -1 given")
6969
end
7070

7171
it "immediately returns an empty string if the length argument is 0" do
7272
@string.readpartial(0).should == ""
7373
end
74+
75+
it "raises IOError if the stream is closed and the length argument is 0" do
76+
@string.close
77+
-> { @string.readpartial(0) }.should raise_error(IOError, "not opened for reading")
78+
end
79+
80+
it "clears and returns the given buffer if the length argument is 0" do
81+
buffer = +"existing content"
82+
@string.readpartial(0, buffer).should == buffer
83+
buffer.should == ""
84+
end
85+
86+
version_is StringIO::VERSION, "3.1.2" do # ruby_version_is "3.4"
87+
it "preserves the encoding of the given buffer" do
88+
buffer = ''.encode(Encoding::ISO_8859_1)
89+
@string.readpartial(10, buffer)
90+
91+
buffer.encoding.should == Encoding::ISO_8859_1
92+
end
93+
end
7494
end

0 commit comments

Comments
 (0)