diff options
Diffstat (limited to 'spec/ruby/core/io/shared/readlines.rb')
-rw-r--r-- | spec/ruby/core/io/shared/readlines.rb | 140 |
1 files changed, 95 insertions, 45 deletions
diff --git a/spec/ruby/core/io/shared/readlines.rb b/spec/ruby/core/io/shared/readlines.rb index 8e4a73bb98..6c1fa11a59 100644 --- a/spec/ruby/core/io/shared/readlines.rb +++ b/spec/ruby/core/io/shared/readlines.rb @@ -73,6 +73,23 @@ describe :io_readlines_options_19, shared: true do result = IO.send(@method, @name, 10, &@object) (result ? result : ScratchPad.recorded).should == IOSpecs.lines_limit end + + it "ignores the object as a limit if it is negative" do + result = IO.send(@method, @name, -2, &@object) + (result ? result : ScratchPad.recorded).should == IOSpecs.lines + end + + it "does not accept Integers that don't fit in a C off_t" do + -> { IO.send(@method, @name, 2**128, &@object) }.should raise_error(RangeError) + 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 @@ -82,36 +99,38 @@ describe :io_readlines_options_19, shared: true do end it "accepts non-ASCII data as separator" do - result = IO.send(@method, @name, "\303\250".force_encoding("utf-8"), &@object) + result = IO.send(@method, @name, "\303\250".dup.force_encoding("utf-8"), &@object) (result ? result : ScratchPad.recorded).should == IOSpecs.lines_arbitrary_separator 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 + it "raises TypeError exception" do + -> { + IO.send(@method, @name, { chomp: true }, &@object) + }.should raise_error(TypeError) 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) @@ -124,32 +143,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 @@ -159,24 +164,57 @@ 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 - it "uses the second object as an options Hash" do + describe "when the second object is an options Hash" do + it "raises TypeError exception" do + -> { + IO.send(@method, @name, "", { chomp: true }, &@object) + }.should raise_error(TypeError) + 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 + + 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) @@ -198,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 |