From a8ba3153af2cf2cb6e42118a516689379aa1d245 Mon Sep 17 00:00:00 2001 From: Herwin Date: Fri, 18 Jul 2025 09:33:00 +0200 Subject: [PATCH 1/3] Remove duplicated test for StringIO#readpartial The same test was at the bottom of the specs, grouped with the other exceptions --- library/stringio/readpartial_spec.rb | 5 ----- 1 file changed, 5 deletions(-) diff --git a/library/stringio/readpartial_spec.rb b/library/stringio/readpartial_spec.rb index f25cef4014..3c205ae945 100644 --- a/library/stringio/readpartial_spec.rb +++ b/library/stringio/readpartial_spec.rb @@ -10,11 +10,6 @@ @string.close unless @string.closed? end - it "raises IOError on closed stream" do - @string.close - -> { @string.readpartial(10) }.should raise_error(IOError) - end - it "reads at most the specified number of bytes" do # buffered read From b8faad86a8d0bf7b9ccfb407233c25761b5181ac Mon Sep 17 00:00:00 2001 From: Herwin Date: Fri, 18 Jul 2025 09:34:01 +0200 Subject: [PATCH 2/3] Remove superfluous newline from StringIO#readpartial specs --- library/stringio/readpartial_spec.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/library/stringio/readpartial_spec.rb b/library/stringio/readpartial_spec.rb index 3c205ae945..b1ae59ad21 100644 --- a/library/stringio/readpartial_spec.rb +++ b/library/stringio/readpartial_spec.rb @@ -11,7 +11,6 @@ end it "reads at most the specified number of bytes" do - # buffered read @string.read(1).should == 'S' # return only specified number, not the whole buffer From c506777278f08263ef3a00f189cde5fbc581cb16 Mon Sep 17 00:00:00 2001 From: Herwin Date: Fri, 18 Jul 2025 09:44:20 +0200 Subject: [PATCH 3/3] 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. --- core/io/readpartial_spec.rb | 5 +++++ library/stringio/readpartial_spec.rb | 24 ++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/core/io/readpartial_spec.rb b/core/io/readpartial_spec.rb index 2fcfaf5203..176c33cf9e 100644 --- a/core/io/readpartial_spec.rb +++ b/core/io/readpartial_spec.rb @@ -93,6 +93,11 @@ @rd.readpartial(0).should == "" end + it "raises IOError if the stream is closed and the length argument is 0" do + @rd.close + -> { @rd.readpartial(0) }.should raise_error(IOError, "closed stream") + end + it "clears and returns the given buffer if the length argument is 0" do buffer = +"existing content" @rd.readpartial(0, buffer).should == buffer diff --git a/library/stringio/readpartial_spec.rb b/library/stringio/readpartial_spec.rb index b1ae59ad21..988c552235 100644 --- a/library/stringio/readpartial_spec.rb +++ b/library/stringio/readpartial_spec.rb @@ -61,14 +61,34 @@ it "raises IOError if the stream is closed" do @string.close - -> { @string.readpartial(1) }.should raise_error(IOError) + -> { @string.readpartial(1) }.should raise_error(IOError, "not opened for reading") end it "raises ArgumentError if the negative argument is provided" do - -> { @string.readpartial(-1) }.should raise_error(ArgumentError) + -> { @string.readpartial(-1) }.should raise_error(ArgumentError, "negative length -1 given") end it "immediately returns an empty string if the length argument is 0" do @string.readpartial(0).should == "" end + + it "raises IOError if the stream is closed and the length argument is 0" do + @string.close + -> { @string.readpartial(0) }.should raise_error(IOError, "not opened for reading") + end + + it "clears and returns the given buffer if the length argument is 0" do + buffer = +"existing content" + @string.readpartial(0, buffer).should == buffer + buffer.should == "" + end + + version_is StringIO::VERSION, "3.1.2" do # ruby_version_is "3.4" + it "preserves the encoding of the given buffer" do + buffer = ''.encode(Encoding::ISO_8859_1) + @string.readpartial(10, buffer) + + buffer.encoding.should == Encoding::ISO_8859_1 + end + end end