Skip to content

Commit

Permalink
Merge pull request #2261 from herwinw/stringio_specs
Browse files Browse the repository at this point in the history
Refresh specs for StringIO
  • Loading branch information
herwinw committed Oct 12, 2024
2 parents 5cfcd42 + b3bfedd commit fc8f213
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 5 deletions.
4 changes: 4 additions & 0 deletions spec/library/stringio/each_line_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@
describe "StringIO#each_line when passed chomp" do
it_behaves_like :stringio_each_chomp, :each_line
end

describe "StringIO#each_line when passed limit" do
it_behaves_like :stringio_each_limit, :each_line
end
8 changes: 8 additions & 0 deletions spec/library/stringio/each_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,11 @@
describe "StringIO#each when passed chomp" do
it_behaves_like :stringio_each_chomp, :each
end

describe "StringIO#each when passed chomp" do
it_behaves_like :stringio_each_separator_and_chomp, :each
end

describe "StringIO#each when passed limit" do
it_behaves_like :stringio_each_limit, :each
end
64 changes: 59 additions & 5 deletions spec/library/stringio/shared/each.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,24 @@
seen.should == ["2 1 2 1 2"]
end

it "yields each paragraph when passed an empty String as separator" do
seen = []
io = StringIO.new("para1\n\npara2\n\n\npara3")
io.send(@method, "") {|s| seen << s}
seen.should == ["para1\n\n", "para2\n\n", "para3"]
version_is StringIO::VERSION, ""..."3.0.4" do #ruby_version_is ""..."3.2" do
it "yields each paragraph with two separation characters when passed an empty String as separator" do
seen = []
io = StringIO.new("para1\n\npara2\n\n\npara3")
io.send(@method, "") {|s| seen << s}
seen.should == ["para1\n\n", "para2\n\n", "para3"]
end
end

version_is StringIO::VERSION, "3.0.4" do #ruby_version_is "3.2" do
it "yields each paragraph with all separation characters when passed an empty String as separator" do
seen = []
io = StringIO.new("para1\n\npara2\n\n\npara3")
io.send(@method, "") {|s| seen << s}
NATFIXME 'it yields each paragraph with all separation characters when passed an empty String as separator', exception: SpecFailedException do
seen.should == ["para1\n\n", "para2\n\n\n", "para3"]
end
end
end
end

Expand Down Expand Up @@ -112,4 +125,45 @@
io.send(@method, chomp: true) {|s| seen << s }
seen.should == ["a b \rc d e", "1 2 3 4 5", "the end"]
end

it "returns each line with removed newline characters when called without block" do
seen = []
io = StringIO.new("a b \rc d e\n1 2 3 4 5\r\nthe end")
enum = io.send(@method, chomp: true)
enum.each {|s| seen << s }
NATFIXME 'it returns each line with removed newline characters when called without block', exception: SpecFailedException do
seen.should == ["a b \rc d e", "1 2 3 4 5", "the end"]
end
end
end

describe :stringio_each_separator_and_chomp, shared: true do
it "yields each line with removed separator to the passed block" do
seen = []
io = StringIO.new("a b \nc d e|1 2 3 4 5\n|the end")
io.send(@method, "|", chomp: true) {|s| seen << s }
seen.should == ["a b \nc d e", "1 2 3 4 5\n", "the end"]
end

it "returns each line with removed separator when called without block" do
seen = []
io = StringIO.new("a b \nc d e|1 2 3 4 5\n|the end")
enum = io.send(@method, "|", chomp: true)
enum.each {|s| seen << s }
NATFIXME 'it returns each line with removed separator when called without block', exception: SpecFailedException do
seen.should == ["a b \nc d e", "1 2 3 4 5\n", "the end"]
end
end
end

describe :stringio_each_limit, shared: true do
before :each do
@io = StringIO.new("a b c d e\n1 2 3 4 5")
end

it "returns the data read until the limit is met" do
seen = []
@io.send(@method, 4) { |s| seen << s }
seen.should == ["a b ", "c d ", "e\n", "1 2 ", "3 4 ", "5"]
end
end
26 changes: 26 additions & 0 deletions spec/library/stringio/shared/read.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,24 @@
buffer.should == "example"
end

ruby_version_is ""..."3.4" do
it "does not preserve the encoding of the given buffer" do
buffer = ''.encode(Encoding::ISO_8859_1)
@io.send(@method, 7, buffer)

buffer.encoding.should_not == Encoding::ISO_8859_1
end
end

ruby_version_is "3.4" do
it "preserves the encoding of the given buffer" do
buffer = ''.encode(Encoding::ISO_8859_1)
@io.send(@method, 7, buffer)

buffer.encoding.should == Encoding::ISO_8859_1
end
end

it "tries to convert the passed buffer Object to a String using #to_str" do
obj = mock("to_str")
obj.should_receive(:to_str).and_return(buffer = +"")
Expand Down Expand Up @@ -89,6 +107,14 @@
@io.send(@method)
@io.pos.should eql(7)
end

it "correctly update the current position in bytes when multi-byte characters are used" do
@io.print("example\u03A3") # Overwrite the original string with 8 characters containing 9 bytes.
@io.send(@method)
NATFIXME 'it correctly update the current position in bytes when multi-byte characters are used', exception: SpecFailedException do
@io.pos.should eql(9)
end
end
end

describe :stringio_read_nil, shared: true do
Expand Down

0 comments on commit fc8f213

Please sign in to comment.