diff options
Diffstat (limited to 'spec/ruby/core/io/shared')
| -rw-r--r-- | spec/ruby/core/io/shared/binwrite.rb | 27 | ||||
| -rw-r--r-- | spec/ruby/core/io/shared/chars.rb | 4 | ||||
| -rw-r--r-- | spec/ruby/core/io/shared/codepoints.rb | 4 | ||||
| -rw-r--r-- | spec/ruby/core/io/shared/each.rb | 84 | ||||
| -rw-r--r-- | spec/ruby/core/io/shared/gets_ascii.rb | 2 | ||||
| -rw-r--r-- | spec/ruby/core/io/shared/new.rb | 193 | ||||
| -rw-r--r-- | spec/ruby/core/io/shared/pos.rb | 16 | ||||
| -rw-r--r-- | spec/ruby/core/io/shared/readlines.rb | 164 | ||||
| -rw-r--r-- | spec/ruby/core/io/shared/tty.rb | 5 | ||||
| -rw-r--r-- | spec/ruby/core/io/shared/write.rb | 71 |
10 files changed, 398 insertions, 172 deletions
diff --git a/spec/ruby/core/io/shared/binwrite.rb b/spec/ruby/core/io/shared/binwrite.rb index 1a88442a3b..e51093329b 100644 --- a/spec/ruby/core/io/shared/binwrite.rb +++ b/spec/ruby/core/io/shared/binwrite.rb @@ -21,12 +21,20 @@ describe :io_binwrite, shared: true do IO.send(@method, @filename, "abcde").should == 5 end + it "accepts options as a keyword argument" do + IO.send(@method, @filename, "hi", 0, flags: File::CREAT).should == 2 + + -> { + IO.send(@method, @filename, "hi", 0, {flags: File::CREAT}) + }.should raise_error(ArgumentError, "wrong number of arguments (given 4, expected 2..3)") + end + it "creates a file if missing" do fn = @filename + "xxx" begin - File.exist?(fn).should be_false + File.should_not.exist?(fn) IO.send(@method, fn, "test") - File.exist?(fn).should be_true + File.should.exist?(fn) ensure rm_r fn end @@ -35,9 +43,9 @@ describe :io_binwrite, shared: true do it "creates file if missing even if offset given" do fn = @filename + "xxx" begin - File.exist?(fn).should be_false + File.should_not.exist?(fn) IO.send(@method, fn, "test", 0) - File.exist?(fn).should be_true + File.should.exist?(fn) ensure rm_r fn end @@ -56,7 +64,7 @@ describe :io_binwrite, shared: true do end it "doesn't truncate and writes at the given offset after passing empty opts" do - IO.send(@method, @filename, "hello world!", 1, {}) + IO.send(@method, @filename, "hello world!", 1, **{}) File.read(@filename).should == "0hello world!34567890123456789" end @@ -67,12 +75,17 @@ describe :io_binwrite, shared: true do File.read(@filename).should == "\0\0foo" end + it "accepts a :flags option without :mode one" do + IO.send(@method, @filename, "hello, world!", flags: File::CREAT) + File.read(@filename).should == "hello, world!" + end + it "raises an error if readonly mode is specified" do - lambda { IO.send(@method, @filename, "abcde", mode: "r") }.should raise_error(IOError) + -> { IO.send(@method, @filename, "abcde", mode: "r") }.should raise_error(IOError) end it "truncates if empty :opts provided and offset skipped" do - IO.send(@method, @filename, "hello, world!", {}) + IO.send(@method, @filename, "hello, world!", **{}) File.read(@filename).should == "hello, world!" end end diff --git a/spec/ruby/core/io/shared/chars.rb b/spec/ruby/core/io/shared/chars.rb index 7f2edd2b6d..266566f221 100644 --- a/spec/ruby/core/io/shared/chars.rb +++ b/spec/ruby/core/io/shared/chars.rb @@ -46,11 +46,11 @@ describe :io_chars, shared: true do end it "raises an IOError when an enumerator created on a closed stream is accessed" do - lambda { IOSpecs.closed_io.send(@method).first }.should raise_error(IOError) + -> { IOSpecs.closed_io.send(@method).first }.should raise_error(IOError) end it "raises IOError on closed stream" do - lambda { IOSpecs.closed_io.send(@method) {} }.should raise_error(IOError) + -> { IOSpecs.closed_io.send(@method) {} }.should raise_error(IOError) end end diff --git a/spec/ruby/core/io/shared/codepoints.rb b/spec/ruby/core/io/shared/codepoints.rb index a5062e7f79..6872846c1a 100644 --- a/spec/ruby/core/io/shared/codepoints.rb +++ b/spec/ruby/core/io/shared/codepoints.rb @@ -39,7 +39,7 @@ describe :io_codepoints, shared: true do it "raises an error if reading invalid sequence" do @io.pos = 60 # inside of a multibyte sequence - lambda { @enum.first }.should raise_error(ArgumentError) + -> { @enum.first }.should raise_error(ArgumentError) end it "does not change $_" do @@ -49,6 +49,6 @@ describe :io_codepoints, shared: true do end it "raises an IOError when self is not readable" do - lambda { IOSpecs.closed_io.send(@method).to_a }.should raise_error(IOError) + -> { IOSpecs.closed_io.send(@method).to_a }.should raise_error(IOError) end end diff --git a/spec/ruby/core/io/shared/each.rb b/spec/ruby/core/io/shared/each.rb index da562e03b1..0747f31b8a 100644 --- a/spec/ruby/core/io/shared/each.rb +++ b/spec/ruby/core/io/shared/each.rb @@ -33,12 +33,8 @@ describe :io_each, shared: true do $_.should == "test" end - it "returns self" do - @io.send(@method) { |l| l }.should equal(@io) - end - it "raises an IOError when self is not readable" do - lambda { IOSpecs.closed_io.send(@method) {} }.should raise_error(IOError) + -> { IOSpecs.closed_io.send(@method) {} }.should raise_error(IOError) end it "makes line count accessible via lineno" do @@ -74,9 +70,13 @@ describe :io_each, shared: true do describe "when limit is 0" do it "raises an ArgumentError" do # must pass block so Enumerator is evaluated and raises - lambda { @io.send(@method, 0){} }.should raise_error(ArgumentError) + -> { @io.send(@method, 0){} }.should raise_error(ArgumentError) end end + + it "does not accept Integers that don't fit in a C off_t" do + -> { @io.send(@method, 2**128){} }.should raise_error(RangeError) + end end describe "when passed a String containing one space as a separator" do @@ -113,6 +113,13 @@ describe :io_each, shared: true do @io.send(@method, "") { |s| ScratchPad << s } ScratchPad.recorded.should == IOSpecs.paragraphs end + + it "discards leading newlines" do + @io.readline + @io.readline + @io.send(@method, "") { |s| ScratchPad << s } + ScratchPad.recorded.should == IOSpecs.paragraphs[1..-1] + end end describe "with both separator and limit" do @@ -152,6 +159,13 @@ describe :io_each, shared: true do @io.send(@method, "", 1024) { |s| ScratchPad << s } ScratchPad.recorded.should == IOSpecs.paragraphs end + + it "discards leading newlines" do + @io.readline + @io.readline + @io.send(@method, "", 1024) { |s| ScratchPad << s } + ScratchPad.recorded.should == IOSpecs.paragraphs[1..-1] + end end end end @@ -161,6 +175,60 @@ describe :io_each, shared: true do @io.send(@method, chomp: true) { |s| ScratchPad << s } ScratchPad.recorded.should == IOSpecs.lines_without_newline_characters end + + 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 + + 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" do + @io.pos = 100 + @io.send(@method, nil, chomp: true) { |s| ScratchPad << s } + ScratchPad.recorded.should == ["qui a linha cinco.\nHere is line six.\n"] + 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 + + describe "when passed too many arguments" do + it "raises ArgumentError" do + -> { + @io.send(@method, "", 1, "excess argument", chomp: true) {} + }.should raise_error(ArgumentError) + end end end @@ -168,12 +236,12 @@ describe :io_each_default_separator, shared: true do before :each do @io = IOSpecs.io_fixture "lines.txt" ScratchPad.record [] - @sep, $/ = $/, " " + suppress_warning {@sep, $/ = $/, " "} end after :each do @io.close if @io - $/ = @sep + suppress_warning {$/ = @sep} end it "uses $/ as the default line separator" do diff --git a/spec/ruby/core/io/shared/gets_ascii.rb b/spec/ruby/core/io/shared/gets_ascii.rb index 2a8fe3c9a5..2bd5470d99 100644 --- a/spec/ruby/core/io/shared/gets_ascii.rb +++ b/spec/ruby/core/io/shared/gets_ascii.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary describe :io_gets_ascii, shared: true do describe "with ASCII separator" do before :each do diff --git a/spec/ruby/core/io/shared/new.rb b/spec/ruby/core/io/shared/new.rb index f6069a4cdc..e84133493c 100644 --- a/spec/ruby/core/io/shared/new.rb +++ b/spec/ruby/core/io/shared/new.rb @@ -1,5 +1,7 @@ require_relative '../fixtures/classes' +# NOTE: should be synchronized with library/stringio/initialize_spec.rb + # This group of specs may ONLY contain specs that do successfully create # an IO instance from the file descriptor returned by #new_fd helper. describe :io_new, shared: true do @@ -18,50 +20,59 @@ describe :io_new, shared: true do rm_r @name end - it "creates an IO instance from a Fixnum argument" do + it "creates an IO instance from an Integer argument" do @io = IO.send(@method, @fd, "w") @io.should be_an_instance_of(IO) end it "creates an IO instance when STDOUT is closed" do - verbose, $VERBOSE = $VERBOSE, nil - stdout = STDOUT - stdout_file = tmp("stdout.txt") - - begin - @io = IO.send(@method, @fd, "w") - @io.should be_an_instance_of(IO) - ensure - STDOUT = stdout - $VERBOSE = verbose - rm_r stdout_file + suppress_warning do + stdout = STDOUT + stdout_file = tmp("stdout.txt") + + begin + @io = IO.send(@method, @fd, "w") + @io.should be_an_instance_of(IO) + ensure + STDOUT = stdout + rm_r stdout_file + end end end it "creates an IO instance when STDERR is closed" do - verbose, $VERBOSE = $VERBOSE, nil - stderr = STDERR - stderr_file = tmp("stderr.txt") - STDERR = new_io stderr_file - STDERR.close - - begin - @io = IO.send(@method, @fd, "w") - @io.should be_an_instance_of(IO) - ensure - STDERR = stderr - $VERBOSE = verbose - rm_r stderr_file + suppress_warning do + stderr = STDERR + stderr_file = tmp("stderr.txt") + STDERR = new_io stderr_file + STDERR.close + + begin + @io = IO.send(@method, @fd, "w") + @io.should be_an_instance_of(IO) + ensure + STDERR = stderr + rm_r stderr_file + end end end - it "calls #to_int on an object to convert to a Fixnum" do + it "calls #to_int on an object to convert to an Integer" do obj = mock("file descriptor") obj.should_receive(:to_int).and_return(@fd) @io = IO.send(@method, obj, "w") @io.should be_an_instance_of(IO) end + it "accepts options as keyword arguments" do + @io = IO.send(@method, @fd, "w", flags: File::CREAT) + @io.write("foo").should == 3 + + -> { + IO.send(@method, @fd, "w", {flags: File::CREAT}) + }.should raise_error(ArgumentError, "wrong number of arguments (given 3, expected 1..2)") + end + it "accepts a :mode option" do @io = IO.send(@method, @fd, mode: "w") @io.write("foo").should == 3 @@ -89,81 +100,81 @@ describe :io_new, shared: true do end it "uses the external encoding specified via the :external_encoding option" do - @io = IO.send(@method, @fd, 'w', {external_encoding: 'utf-8'}) + @io = IO.send(@method, @fd, 'w', external_encoding: 'utf-8') @io.external_encoding.to_s.should == 'UTF-8' end it "uses the internal encoding specified via the :internal_encoding option" do - @io = IO.send(@method, @fd, 'w', {internal_encoding: 'ibm866'}) + @io = IO.send(@method, @fd, 'w', internal_encoding: 'ibm866') @io.internal_encoding.to_s.should == 'IBM866' end it "uses the colon-separated encodings specified via the :encoding option" do - @io = IO.send(@method, @fd, 'w', {encoding: 'utf-8:ISO-8859-1'}) + @io = IO.send(@method, @fd, 'w', encoding: 'utf-8:ISO-8859-1') @io.external_encoding.to_s.should == 'UTF-8' @io.internal_encoding.to_s.should == 'ISO-8859-1' end it "uses the :encoding option as the external encoding when only one is given" do - @io = IO.send(@method, @fd, 'w', {encoding: 'ISO-8859-1'}) + @io = IO.send(@method, @fd, 'w', encoding: 'ISO-8859-1') @io.external_encoding.to_s.should == 'ISO-8859-1' end it "uses the :encoding options as the external encoding when it's an Encoding object" do - @io = IO.send(@method, @fd, 'w', {encoding: Encoding::ISO_8859_1}) + @io = IO.send(@method, @fd, 'w', encoding: Encoding::ISO_8859_1) @io.external_encoding.should == Encoding::ISO_8859_1 end it "ignores the :encoding option when the :external_encoding option is present" do - lambda { - @io = IO.send(@method, @fd, 'w', {external_encoding: 'utf-8', encoding: 'iso-8859-1:iso-8859-1'}) + -> { + @io = IO.send(@method, @fd, 'w', external_encoding: 'utf-8', encoding: 'iso-8859-1:iso-8859-1') }.should complain(/Ignoring encoding parameter/) @io.external_encoding.to_s.should == 'UTF-8' end it "ignores the :encoding option when the :internal_encoding option is present" do - lambda { - @io = IO.send(@method, @fd, 'w', {internal_encoding: 'ibm866', encoding: 'iso-8859-1:iso-8859-1'}) + -> { + @io = IO.send(@method, @fd, 'w', internal_encoding: 'ibm866', encoding: 'iso-8859-1:iso-8859-1') }.should complain(/Ignoring encoding parameter/) @io.internal_encoding.to_s.should == 'IBM866' end it "uses the encoding specified via the :mode option hash" do - @io = IO.send(@method, @fd, {mode: 'w:utf-8:ISO-8859-1'}) + @io = IO.send(@method, @fd, mode: 'w:utf-8:ISO-8859-1') @io.external_encoding.to_s.should == 'UTF-8' @io.internal_encoding.to_s.should == 'ISO-8859-1' end it "ignores the :internal_encoding option when the same as the external encoding" do - @io = IO.send(@method, @fd, 'w', {external_encoding: 'utf-8', internal_encoding: 'utf-8'}) + @io = IO.send(@method, @fd, 'w', external_encoding: 'utf-8', internal_encoding: 'utf-8') @io.external_encoding.to_s.should == 'UTF-8' @io.internal_encoding.to_s.should == '' end it "sets internal encoding to nil when passed '-'" do - @io = IO.send(@method, @fd, 'w', {external_encoding: 'utf-8', internal_encoding: '-'}) + @io = IO.send(@method, @fd, 'w', external_encoding: 'utf-8', internal_encoding: '-') @io.external_encoding.to_s.should == 'UTF-8' @io.internal_encoding.to_s.should == '' end it "sets binmode from mode string" do @io = IO.send(@method, @fd, 'wb') - @io.binmode?.should == true + @io.should.binmode? end it "does not set binmode without being asked" do @io = IO.send(@method, @fd, 'w') - @io.binmode?.should == false + @io.should_not.binmode? end it "sets binmode from :binmode option" do - @io = IO.send(@method, @fd, 'w', {binmode: true}) - @io.binmode?.should == true + @io = IO.send(@method, @fd, 'w', binmode: true) + @io.should.binmode? end it "does not set binmode from false :binmode" do - @io = IO.send(@method, @fd, 'w', {binmode: false}) - @io.binmode?.should == false + @io = IO.send(@method, @fd, 'w', binmode: false) + @io.should_not.binmode? end it "sets external encoding to binary with binmode in mode string" do @@ -173,7 +184,7 @@ describe :io_new, shared: true do # #5917 it "sets external encoding to binary with :binmode option" do - @io = IO.send(@method, @fd, 'w', {binmode: true}) + @io = IO.send(@method, @fd, 'w', binmode: true) @io.external_encoding.should == Encoding::BINARY end @@ -197,9 +208,30 @@ describe :io_new, shared: true do @io.internal_encoding.to_s.should == 'IBM866' end - it "accepts nil options" do - @io = IO.send(@method, @fd, 'w', nil) - @io.write("foo").should == 3 + it "does not use binary encoding when mode encoding is specified along with binmode: true option" do + @io = IO.send(@method, @fd, 'w:iso-8859-1', binmode: true) + @io.external_encoding.to_s.should == 'ISO-8859-1' + end + + it "does not use textmode argument when mode encoding is specified" do + @io = IO.send(@method, @fd, 'w:ascii-8bit', textmode: true) + @io.external_encoding.to_s.should == 'ASCII-8BIT' + end + + it "does not use binmode argument when external encoding is specified via the :external_encoding option" do + @io = IO.send(@method, @fd, 'w', binmode: true, external_encoding: 'iso-8859-1') + @io.external_encoding.to_s.should == 'ISO-8859-1' + end + + it "does not use textmode argument when external encoding is specified via the :external_encoding option" do + @io = IO.send(@method, @fd, 'w', textmode: true, external_encoding: 'ascii-8bit') + @io.external_encoding.to_s.should == 'ASCII-8BIT' + end + + it "raises ArgumentError for nil options" do + -> { + IO.send(@method, @fd, 'w', nil) + }.should raise_error(ArgumentError) end it "coerces mode with #to_str" do @@ -247,24 +279,24 @@ describe :io_new, shared: true do it "coerces options as third argument with #to_hash" do options = mock("options") options.should_receive(:to_hash).and_return({}) - @io = IO.send(@method, @fd, 'w', options) + @io = IO.send(@method, @fd, 'w', **options) end it "coerces options as second argument with #to_hash" do options = mock("options") options.should_receive(:to_hash).and_return({}) - @io = IO.send(@method, @fd, options) + @io = IO.send(@method, @fd, **options) end it "accepts an :autoclose option" do @io = IO.send(@method, @fd, 'w', autoclose: false) - @io.autoclose?.should == false + @io.should_not.autoclose? @io.autoclose = true end it "accepts any truthy option :autoclose" do @io = IO.send(@method, @fd, 'w', autoclose: 42) - @io.autoclose?.should == true + @io.should.autoclose? end end @@ -282,97 +314,100 @@ describe :io_new_errors, shared: true do end it "raises an Errno::EBADF if the file descriptor is not valid" do - lambda { IO.send(@method, -1, "w") }.should raise_error(Errno::EBADF) + -> { IO.send(@method, -1, "w") }.should raise_error(Errno::EBADF) end it "raises an IOError if passed a closed stream" do - lambda { IO.send(@method, IOSpecs.closed_io.fileno, 'w') }.should raise_error(IOError) + -> { IO.send(@method, IOSpecs.closed_io.fileno, 'w') }.should raise_error(IOError) end platform_is_not :windows do it "raises an Errno::EINVAL if the new mode is not compatible with the descriptor's current mode" do - lambda { IO.send(@method, @fd, "r") }.should raise_error(Errno::EINVAL) + -> { IO.send(@method, @fd, "r") }.should raise_error(Errno::EINVAL) end end it "raises ArgumentError if passed an empty mode string" do - lambda { IO.send(@method, @fd, "") }.should raise_error(ArgumentError) + -> { IO.send(@method, @fd, "") }.should raise_error(ArgumentError) end it "raises an error if passed modes two ways" do - lambda { + -> { IO.send(@method, @fd, "w", mode: "w") }.should raise_error(ArgumentError) end it "raises an error if passed encodings two ways" do - lambda { - @io = IO.send(@method, @fd, 'w:ISO-8859-1', {encoding: 'ISO-8859-1'}) + -> { + @io = IO.send(@method, @fd, 'w:ISO-8859-1', encoding: 'ISO-8859-1') }.should raise_error(ArgumentError) - lambda { - @io = IO.send(@method, @fd, 'w:ISO-8859-1', {external_encoding: 'ISO-8859-1'}) + -> { + @io = IO.send(@method, @fd, 'w:ISO-8859-1', external_encoding: 'ISO-8859-1') }.should raise_error(ArgumentError) - lambda { - @io = IO.send(@method, @fd, 'w:ISO-8859-1:UTF-8', {internal_encoding: 'ISO-8859-1'}) + -> { + @io = IO.send(@method, @fd, 'w:ISO-8859-1', internal_encoding: 'ISO-8859-1') + }.should raise_error(ArgumentError) + -> { + @io = IO.send(@method, @fd, 'w:ISO-8859-1:UTF-8', internal_encoding: 'ISO-8859-1') }.should raise_error(ArgumentError) end it "raises an error if passed matching binary/text mode two ways" do - lambda { + -> { @io = IO.send(@method, @fd, "wb", binmode: true) }.should raise_error(ArgumentError) - lambda { + -> { @io = IO.send(@method, @fd, "wt", textmode: true) }.should raise_error(ArgumentError) - lambda { + -> { @io = IO.send(@method, @fd, "wb", textmode: false) }.should raise_error(ArgumentError) - lambda { + -> { @io = IO.send(@method, @fd, "wt", binmode: false) }.should raise_error(ArgumentError) end it "raises an error if passed conflicting binary/text mode two ways" do - lambda { + -> { @io = IO.send(@method, @fd, "wb", binmode: false) }.should raise_error(ArgumentError) - lambda { + -> { @io = IO.send(@method, @fd, "wt", textmode: false) }.should raise_error(ArgumentError) - lambda { + -> { @io = IO.send(@method, @fd, "wb", textmode: true) }.should raise_error(ArgumentError) - lambda { + -> { @io = IO.send(@method, @fd, "wt", binmode: true) }.should raise_error(ArgumentError) end it "raises an error when trying to set both binmode and textmode" do - lambda { + -> { @io = IO.send(@method, @fd, "w", textmode: true, binmode: true) }.should raise_error(ArgumentError) - lambda { + -> { @io = IO.send(@method, @fd, File::Constants::WRONLY, textmode: true, binmode: true) }.should raise_error(ArgumentError) end it "raises ArgumentError if not passed a hash or nil for options" do - lambda { + -> { @io = IO.send(@method, @fd, 'w', false) }.should raise_error(ArgumentError) - lambda { + -> { @io = IO.send(@method, @fd, false, false) }.should raise_error(ArgumentError) - lambda { + -> { @io = IO.send(@method, @fd, nil, false) }.should raise_error(ArgumentError) end - it "raises TypeError if passed a hash for mode and nil for options" do - lambda { + it "raises ArgumentError if passed a hash for mode and nil for options" do + -> { @io = IO.send(@method, @fd, {mode: 'w'}, nil) - }.should raise_error(TypeError) + }.should raise_error(ArgumentError) end end diff --git a/spec/ruby/core/io/shared/pos.rb b/spec/ruby/core/io/shared/pos.rb index fef7ab2bf7..3fdd3eb2b3 100644 --- a/spec/ruby/core/io/shared/pos.rb +++ b/spec/ruby/core/io/shared/pos.rb @@ -19,7 +19,7 @@ describe :io_pos, shared: true do end it "raises IOError on closed stream" do - lambda { IOSpecs.closed_io.send(@method) }.should raise_error(IOError) + -> { IOSpecs.closed_io.send(@method) }.should raise_error(IOError) end it "resets #eof?" do @@ -27,7 +27,7 @@ describe :io_pos, shared: true do io.read 1 io.read 1 io.send(@method) - io.eof?.should == false + io.should_not.eof? end end end @@ -60,13 +60,19 @@ describe :io_set_pos, shared: true do end end - it "does not accept Bignums that don't fit in a C long" do + it "raises TypeError when cannot convert implicitly argument to Integer" do File.open @fname do |io| - lambda { io.send @method, 2**128 }.should raise_error(RangeError) + -> { io.send @method, Object.new }.should raise_error(TypeError, "no implicit conversion of Object into Integer") + end + end + + it "does not accept Integers that don't fit in a C off_t" do + File.open @fname do |io| + -> { io.send @method, 2**128 }.should raise_error(RangeError) end end it "raises IOError on closed stream" do - lambda { IOSpecs.closed_io.send @method, 0 }.should raise_error(IOError) + -> { IOSpecs.closed_io.send @method, 0 }.should raise_error(IOError) end end diff --git a/spec/ruby/core/io/shared/readlines.rb b/spec/ruby/core/io/shared/readlines.rb index 08d41e0a4c..6c1fa11a59 100644 --- a/spec/ruby/core/io/shared/readlines.rb +++ b/spec/ruby/core/io/shared/readlines.rb @@ -1,11 +1,11 @@ describe :io_readlines, shared: true do it "raises TypeError if the first parameter is nil" do - lambda { IO.send(@method, nil, &@object) }.should raise_error(TypeError) + -> { IO.send(@method, nil, &@object) }.should raise_error(TypeError) end it "raises an Errno::ENOENT if the file does not exist" do name = tmp("nonexistent.txt") - lambda { IO.send(@method, name, &@object) }.should raise_error(Errno::ENOENT) + -> { IO.send(@method, name, &@object) }.should raise_error(Errno::ENOENT) end it "yields a single string with entire content when the separator is nil" do @@ -54,25 +54,42 @@ describe :io_readlines_options_19, shared: true do (result ? result : ScratchPad.recorded).should == IOSpecs.lines_space_separator end - describe "when the object is a Fixnum" do + describe "when the object is an Integer" do before :each do @sep = $/ end after :each do - $/ = @sep + suppress_warning {$/ = @sep} end it "defaults to $/ as the separator" do - $/ = " " + suppress_warning {$/ = " "} result = IO.send(@method, @name, 10, &@object) (result ? result : ScratchPad.recorded).should == IOSpecs.lines_space_separator_limit end - it "uses the object as a limit if it is a Fixnum" do + it "uses the object as a limit if it is an Integer" 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,38 +99,40 @@ 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 a Fixnum" do - it "uses the second object as an options Hash" do - lambda 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" }) - lambda 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 a Fixnum" do + it "uses the second object as a limit if it is an Integer" do result = IO.send(@method, @name, " ", 10, &@object) (result ? result : ScratchPad.recorded).should == IOSpecs.lines_space_separator_limit end @@ -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 - lambda 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" }) - lambda do - IO.send(@method, @filename, " ", options, &@object) - end.should raise_error(IOError) - end end - describe "when the first object is not a String or Fixnum" do + 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 a Fixnum" do - result = IO.send(@method, @name, " ", 10, mode: "r", &@object) + it "uses the second object as a limit if it is an Integer" do + 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 - lambda 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" }) - lambda do - IO.send(@method, @filename, " ", options, &@object) + -> do + 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" }) - lambda do - IO.send(@method, @filename, " ", 10, options, &@object) + it "uses the keyword arguments as options" do + -> do + 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 diff --git a/spec/ruby/core/io/shared/tty.rb b/spec/ruby/core/io/shared/tty.rb index 947b887f81..89ac08ec86 100644 --- a/spec/ruby/core/io/shared/tty.rb +++ b/spec/ruby/core/io/shared/tty.rb @@ -7,8 +7,7 @@ describe :io_tty, shared: true do # check to enabled tty File.open('/dev/tty') {} rescue Errno::ENXIO - # workaround for not configured environment like OS X - 1.should == 1 + skip "workaround for not configured environment like OS X" else File.open('/dev/tty') { |f| f.send(@method) }.should == true end @@ -20,6 +19,6 @@ describe :io_tty, shared: true do end it "raises IOError on closed stream" do - lambda { IOSpecs.closed_io.send @method }.should raise_error(IOError) + -> { IOSpecs.closed_io.send @method }.should raise_error(IOError) end end diff --git a/spec/ruby/core/io/shared/write.rb b/spec/ruby/core/io/shared/write.rb index aa6b3eedeb..964064746a 100644 --- a/spec/ruby/core/io/shared/write.rb +++ b/spec/ruby/core/io/shared/write.rb @@ -23,7 +23,7 @@ describe :io_write, shared: true do end it "checks if the file is writable if writing more than zero bytes" do - lambda { @readonly_file.send(@method, "abcde") }.should raise_error(IOError) + -> { @readonly_file.send(@method, "abcde") }.should raise_error(IOError) end it "returns the number of bytes written" do @@ -50,7 +50,7 @@ describe :io_write, shared: true do it "does not warn if called after IO#read" do @file.read(5) - lambda { @file.send(@method, "fghij") }.should_not complain + -> { @file.send(@method, "fghij") }.should_not complain end it "writes to the current position after IO#read" do @@ -66,7 +66,7 @@ describe :io_write, shared: true do end it "raises IOError on closed stream" do - lambda { IOSpecs.closed_io.send(@method, "hello") }.should raise_error(IOError) + -> { IOSpecs.closed_io.send(@method, "hello") }.should raise_error(IOError) end describe "on a pipe" do @@ -85,11 +85,11 @@ describe :io_write, shared: true do @r.read.should == "foo" end - # [ruby-core:90895] MJIT worker may leave fd open in a forked child. - # For instance, MJIT creates a worker before @r.close with fork(), @r.close happens, - # and the MJIT worker keeps the pipe open until the worker execve(). - # TODO: consider acquiring GVL from MJIT worker. - guard_not -> { defined?(RubyVM::MJIT) && RubyVM::MJIT.enabled? } do + # [ruby-core:90895] RJIT worker may leave fd open in a forked child. + # For instance, RJIT creates a worker before @r.close with fork(), @r.close happens, + # and the RJIT worker keeps the pipe open until the worker execve(). + # TODO: consider acquiring GVL from RJIT worker. + guard_not -> { defined?(RubyVM::RJIT) && RubyVM::RJIT.enabled? } do it "raises Errno::EPIPE if the read end is closed and does not die from SIGPIPE" do @r.close -> { @w.send(@method, "foo") }.should raise_error(Errno::EPIPE, /Broken pipe/) @@ -97,3 +97,58 @@ describe :io_write, shared: true do end end end + +describe :io_write_transcode, shared: true do + before :each do + @transcode_filename = tmp("io_write_transcode") + end + + after :each do + rm_r @transcode_filename + end + + it "transcodes the given string when the external encoding is set and neither is BINARY" do + utf8_str = "hello" + + File.open(@transcode_filename, "w", external_encoding: Encoding::UTF_16BE) do |file| + file.external_encoding.should == Encoding::UTF_16BE + file.send(@method, utf8_str) + end + + result = File.binread(@transcode_filename) + expected = [0, 104, 0, 101, 0, 108, 0, 108, 0, 111] # UTF-16BE bytes for "hello" + + result.bytes.should == expected + end + + it "transcodes the given string when the external encoding is set and the string encoding is BINARY" do + str = "été".b + + File.open(@transcode_filename, "w", external_encoding: Encoding::UTF_16BE) do |file| + file.external_encoding.should == Encoding::UTF_16BE + -> { file.send(@method, str) }.should raise_error(Encoding::UndefinedConversionError) + end + end +end + +describe :io_write_no_transcode, shared: true do + before :each do + @transcode_filename = tmp("io_write_no_transcode") + end + + after :each do + rm_r @transcode_filename + end + + it "does not transcode the given string even when the external encoding is set" do + utf8_str = "hello" + + File.open(@transcode_filename, "w", external_encoding: Encoding::UTF_16BE) do |file| + file.external_encoding.should == Encoding::UTF_16BE + file.send(@method, utf8_str) + end + + result = File.binread(@transcode_filename) + result.bytes.should == utf8_str.bytes + end +end |
