diff options
author | Benoit Daloze <[email protected]> | 2022-06-26 14:50:14 +0200 |
---|---|---|
committer | Benoit Daloze <[email protected]> | 2022-06-26 14:50:14 +0200 |
commit | d3d5ef0cca160fca538c7f556c5a6e08df5847e6 (patch) | |
tree | 57358b4b9cdd6f429d0383005ac393cb74dd3bff /spec/ruby/core/io/shared | |
parent | f616e816372d14e605879d2e43c7fbdda29ef837 (diff) |
Update to ruby/spec@ab32a1a
Diffstat (limited to 'spec/ruby/core/io/shared')
-rw-r--r-- | spec/ruby/core/io/shared/each.rb | 48 | ||||
-rw-r--r-- | spec/ruby/core/io/shared/readlines.rb | 133 |
2 files changed, 137 insertions, 44 deletions
diff --git a/spec/ruby/core/io/shared/each.rb b/spec/ruby/core/io/shared/each.rb index 91766fbe03..badf8985e0 100644 --- a/spec/ruby/core/io/shared/each.rb +++ b/spec/ruby/core/io/shared/each.rb @@ -161,6 +161,54 @@ describe :io_each, shared: true do @io.send(@method, chomp: true) { |s| ScratchPad << s } ScratchPad.recorded.should == IOSpecs.lines_without_newline_characters end + + ruby_version_is "3.0" do + it "raises exception when options passed as Hash" do + -> { + @io.send(@method, { chomp: true }) { |s| } + }.should raise_error(TypeError) + + -> { + @io.send(@method, "\n", 1, { chomp: true }) { |s| } + }.should raise_error(ArgumentError, "wrong number of arguments (given 3, expected 0..2)") + end + end + end + + describe "when passed chomp and a separator" do + it "yields each line without separator to the passed block" do + @io.send(@method, " ", chomp: true) { |s| ScratchPad << s } + ScratchPad.recorded.should == IOSpecs.lines_space_separator_without_trailing_spaces + end + end + + describe "when passed chomp and empty line as a separator" do + it "yields each paragraph without trailing new line characters" do + @io.send(@method, "", 1024, chomp: true) { |s| ScratchPad << s } + ScratchPad.recorded.should == IOSpecs.paragraphs_without_trailing_new_line_characters + end + end + + describe "when passed chomp and nil as a separator" do + it "yields self's content without trailing new line character" do + @io.pos = 100 + @io.send(@method, nil, chomp: true) { |s| ScratchPad << s } + ScratchPad.recorded.should == ["qui a linha cinco.\nHere is line six."] + end + end + + describe "when passed chomp, nil as a separator, and a limit" do + it "yields each line of limit size without truncating trailing new line character" do + # 43 - is a size of the 1st paragraph in the file + @io.send(@method, nil, 43, chomp: true) { |s| ScratchPad << s } + + ScratchPad.recorded.should == [ + "Voici la ligne une.\nQui è la linea due.\n\n\n", + "Aquí está la línea tres.\n" + "Hier ist Zeile ", + "vier.\n\nEstá aqui a linha cinco.\nHere is li", + "ne six.\n" + ] + end end end diff --git a/spec/ruby/core/io/shared/readlines.rb b/spec/ruby/core/io/shared/readlines.rb index 52b20364ef..479452b71c 100644 --- a/spec/ruby/core/io/shared/readlines.rb +++ b/spec/ruby/core/io/shared/readlines.rb @@ -78,6 +78,14 @@ describe :io_readlines_options_19, shared: true do result = IO.send(@method, @name, -2, &@object) (result ? result : ScratchPad.recorded).should == IOSpecs.lines end + + ruby_bug "#18767", ""..."3.3" do + describe "when passed limit" do + it "raises ArgumentError when passed 0 as a limit" do + -> { IO.send(@method, @name, 0, &@object) }.should raise_error(ArgumentError) + end + end + end end describe "when the object is a String" do @@ -92,31 +100,35 @@ describe :io_readlines_options_19, shared: true do end end - describe "when the object is a Hash" do - it "uses the value as the options hash" do - result = IO.send(@method, @name, mode: "r", &@object) - (result ? result : ScratchPad.recorded).should == IOSpecs.lines + describe "when the object is an options Hash" do + ruby_version_is "3.0" do + it "raises TypeError exception" do + -> { + IO.send(@method, @name, { chomp: true }, &@object) + }.should raise_error(TypeError) + end end end - end - describe "when passed name, object, object" do - describe "when the first object is an Integer" do - it "uses the second object as an options Hash" do - -> do - IO.send(@method, @filename, 10, mode: "w", &@object) - end.should raise_error(IOError) - end + describe "when the object is neither Integer nor String" do + it "raises TypeError exception" do + obj = mock("not io readlines limit") - it "calls #to_hash to convert the second object to a Hash" do - options = mock("io readlines options Hash") - options.should_receive(:to_hash).and_return({ mode: "w" }) - -> do - IO.send(@method, @filename, 10, **options, &@object) - end.should raise_error(IOError) + -> { + IO.send(@method, @name, obj, &@object) + }.should raise_error(TypeError) end end + end + describe "when passed name, keyword arguments" do + it "uses the keyword arguments as options" do + result = IO.send(@method, @name, mode: "r", &@object) + (result ? result : ScratchPad.recorded).should == IOSpecs.lines + end + end + + describe "when passed name, object, object" do describe "when the first object is a String" do it "uses the second object as a limit if it is an Integer" do result = IO.send(@method, @name, " ", 10, &@object) @@ -129,32 +141,18 @@ describe :io_readlines_options_19, shared: true do result = IO.send(@method, @name, " ", limit, &@object) (result ? result : ScratchPad.recorded).should == IOSpecs.lines_space_separator_limit end - - it "uses the second object as an options Hash" do - -> do - IO.send(@method, @filename, " ", mode: "w", &@object) - end.should raise_error(IOError) - end - - it "calls #to_hash to convert the second object to a Hash" do - options = mock("io readlines options Hash") - options.should_receive(:to_hash).and_return({ mode: "w" }) - -> do - IO.send(@method, @filename, " ", **options, &@object) - end.should raise_error(IOError) - end end describe "when the first object is not a String or Integer" do it "calls #to_str to convert the object to a String" do sep = mock("io readlines separator") sep.should_receive(:to_str).at_least(1).and_return(" ") - result = IO.send(@method, @name, sep, 10, mode: "r", &@object) + result = IO.send(@method, @name, sep, 10, &@object) (result ? result : ScratchPad.recorded).should == IOSpecs.lines_space_separator_limit end it "uses the second object as a limit if it is an Integer" do - result = IO.send(@method, @name, " ", 10, mode: "r", &@object) + result = IO.send(@method, @name, " ", 10, &@object) (result ? result : ScratchPad.recorded).should == IOSpecs.lines_space_separator_limit end @@ -164,24 +162,59 @@ describe :io_readlines_options_19, shared: true do result = IO.send(@method, @name, " ", limit, &@object) (result ? result : ScratchPad.recorded).should == IOSpecs.lines_space_separator_limit end + end + + describe "when the second object is neither Integer nor String" do + it "raises TypeError exception" do + obj = mock("not io readlines limit") + + -> { + IO.send(@method, @name, " ", obj, &@object) + }.should raise_error(TypeError) + end + end + + describe "when the second object is an options Hash" do + ruby_version_is "3.0" do + it "raises TypeError exception" do + -> { + IO.send(@method, @name, "", { chomp: true }, &@object) + }.should raise_error(TypeError) + end + end + end + end + + describe "when passed name, object, keyword arguments" do + describe "when the first object is an Integer" do + it "uses the keyword arguments as options" do + -> do + IO.send(@method, @filename, 10, mode: "w", &@object) + end.should raise_error(IOError) + end + end - it "uses the second object as an options Hash" do + describe "when the first object is a String" do + it "uses the keyword arguments as options" do -> do IO.send(@method, @filename, " ", mode: "w", &@object) end.should raise_error(IOError) end + end + + describe "when the first object is not a String or Integer" do + it "uses the keyword arguments as options" do + sep = mock("io readlines separator") + sep.should_receive(:to_str).at_least(1).and_return(" ") - it "calls #to_hash to convert the second object to a Hash" do - options = mock("io readlines options Hash") - options.should_receive(:to_hash).and_return({ mode: "w" }) -> do - IO.send(@method, @filename, " ", **options, &@object) + IO.send(@method, @filename, sep, mode: "w", &@object) end.should raise_error(IOError) end end end - describe "when passed name, separator, limit, options" do + describe "when passed name, separator, limit, keyword arguments" do it "calls #to_path to convert the name object" do name = mock("io name to_path") name.should_receive(:to_path).and_return(@name) @@ -203,12 +236,24 @@ describe :io_readlines_options_19, shared: true do (result ? result : ScratchPad.recorded).should == IOSpecs.lines_space_separator_limit end - it "calls #to_hash to convert the options object" do - options = mock("io readlines options Hash") - options.should_receive(:to_hash).and_return({ mode: "w" }) + it "uses the keyword arguments as options" do -> do - IO.send(@method, @filename, " ", 10, **options, &@object) + IO.send(@method, @filename, " ", 10, mode: "w", &@object) end.should raise_error(IOError) end + + describe "when passed chomp, nil as a separator, and a limit" do + it "yields each line of limit size without truncating trailing new line character" do + # 43 - is a size of the 1st paragraph in the file + result = IO.send(@method, @name, nil, 43, chomp: true, &@object) + + (result ? result : ScratchPad.recorded).should == [ + "Voici la ligne une.\nQui è la linea due.\n\n\n", + "Aquí está la línea tres.\n" + "Hier ist Zeile ", + "vier.\n\nEstá aqui a linha cinco.\nHere is li", + "ne six.\n" + ] + end + end end end |