diff options
author | Andrew Konchin <[email protected]> | 2025-06-02 19:34:54 +0300 |
---|---|---|
committer | Benoit Daloze <[email protected]> | 2025-06-02 21:54:48 +0200 |
commit | d6aa1714fed3e8b5ee8bede00a8853c338ff6092 (patch) | |
tree | ea863a44d9026f74b9a07375f401c3e5d8cb4bf5 /spec/ruby/library/stringio/readline_spec.rb | |
parent | 685c8ca9af892f562f64b54dbee73bb9a1999b90 (diff) |
Update to ruby/spec@4d2fc4d
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/13495
Diffstat (limited to 'spec/ruby/library/stringio/readline_spec.rb')
-rw-r--r-- | spec/ruby/library/stringio/readline_spec.rb | 160 |
1 files changed, 34 insertions, 126 deletions
diff --git a/spec/ruby/library/stringio/readline_spec.rb b/spec/ruby/library/stringio/readline_spec.rb index b16a16e23f..085360707f 100644 --- a/spec/ruby/library/stringio/readline_spec.rb +++ b/spec/ruby/library/stringio/readline_spec.rb @@ -1,150 +1,58 @@ require_relative '../../spec_helper' +require "stringio" require_relative 'fixtures/classes' +require_relative "shared/gets" +describe "StringIO#readline" do + describe "when passed [separator]" do + it_behaves_like :stringio_gets_separator, :readline -describe "StringIO#readline when passed [separator]" do - before :each do - @io = StringIO.new("this>is>an>example") - end - - it "returns the data read till the next occurrence of the passed separator" do - @io.readline(">").should == "this>" - @io.readline(">").should == "is>" - @io.readline(">").should == "an>" - @io.readline(">").should == "example" - end - - it "sets $_ to the read content" do - @io.readline(">") - $_.should == "this>" - @io.readline(">") - $_.should == "is>" - @io.readline(">") - $_.should == "an>" - @io.readline(">") - $_.should == "example" - end - - it "updates self's lineno by one" do - @io.readline(">") - @io.lineno.should eql(1) - - @io.readline(">") - @io.lineno.should eql(2) - - @io.readline(">") - @io.lineno.should eql(3) - end - - it "returns the next paragraph when the passed separator is an empty String" do - io = StringIO.new("this is\n\nan example") - io.readline("").should == "this is\n\n" - io.readline("").should == "an example" - end - - it "returns the remaining content starting at the current position when passed nil" do - io = StringIO.new("this is\n\nan example") - io.pos = 5 - io.readline(nil).should == "is\n\nan example" - end - - it "tries to convert the passed separator to a String using #to_str" do - obj = mock('to_str') - obj.should_receive(:to_str).and_return(">") - @io.readline(obj).should == "this>" - end -end - -describe "StringIO#readline when passed no argument" do - before :each do - @io = StringIO.new("this is\nan example\nfor StringIO#readline") - end - - it "returns the data read till the next occurrence of $/ or till eof" do - @io.readline.should == "this is\n" + it "raises an IOError if self is at the end" do + @io = StringIO.new("this>is>an>example") - begin - old_sep = $/ - suppress_warning {$/ = " "} - @io.readline.should == "an " - @io.readline.should == "example\nfor " - @io.readline.should == "StringIO#readline" - ensure - suppress_warning {$/ = old_sep} + @io.pos = 36 + -> { @io.readline(">") }.should raise_error(IOError) end end - it "sets $_ to the read content" do - @io.readline - $_.should == "this is\n" - @io.readline - $_.should == "an example\n" - @io.readline - $_.should == "for StringIO#readline" - end + describe "when passed [limit]" do + it_behaves_like :stringio_gets_limit, :readline - it "updates self's position" do - @io.readline - @io.pos.should eql(8) + it "raises an IOError if self is at the end" do + @io = StringIO.new("this>is>an>example") - @io.readline - @io.pos.should eql(19) - - @io.readline - @io.pos.should eql(40) + @io.pos = 36 + -> { @io.readline(3) }.should raise_error(IOError) + end end - it "updates self's lineno" do - @io.readline - @io.lineno.should eql(1) + describe "when passed [separator] and [limit]" do + it_behaves_like :stringio_gets_separator_and_limit, :readline - @io.readline - @io.lineno.should eql(2) + it "raises an IOError if self is at the end" do + @io = StringIO.new("this>is>an>example") - @io.readline - @io.lineno.should eql(3) - end - - it "raises an IOError if self is at the end" do - @io.pos = 40 - -> { @io.readline }.should raise_error(IOError) - end -end - -describe "StringIO#readline when in write-only mode" do - it "raises an IOError" do - io = StringIO.new(+"xyz", "w") - -> { io.readline }.should raise_error(IOError) - - io = StringIO.new("xyz") - io.close_read - -> { io.readline }.should raise_error(IOError) + @io.pos = 36 + -> { @io.readline(">", 3) }.should raise_error(IOError) + end end -end -describe "StringIO#readline when passed [chomp]" do - it "returns the data read without a trailing newline character" do - io = StringIO.new("this>is>an>example\n") - io.readline(chomp: true).should == "this>is>an>example" - end -end + describe "when passed no argument" do + it_behaves_like :stringio_gets_no_argument, :readline -describe "StringIO#readline when passed [limit]" do - before :each do - @io = StringIO.new("this>is>an>example") - end + it "raises an IOError if self is at the end" do + @io = StringIO.new("this>is>an>example") - it "returns the data read until the limit is met" do - io = StringIO.new("this>is>an>example\n") - io.readline(3).should == "thi" + @io.pos = 36 + -> { @io.readline }.should raise_error(IOError) + end end - it "returns a blank string when passed a limit of 0" do - @io.readline(0).should == "" + describe "when passed [chomp]" do + it_behaves_like :stringio_gets_chomp, :readline end - it "ignores it when the limit is negative" do - seen = [] - @io.readline(-4).should == "this>is>an>example" + describe "when in write-only mode" do + it_behaves_like :stringio_gets_write_only, :readline end end |