diff options
Diffstat (limited to 'spec/ruby/core/io/shared/new.rb')
| -rw-r--r-- | spec/ruby/core/io/shared/new.rb | 119 |
1 files changed, 65 insertions, 54 deletions
diff --git a/spec/ruby/core/io/shared/new.rb b/spec/ruby/core/io/shared/new.rb index f2a0970a40..6f318ddee5 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 @@ -20,7 +22,7 @@ describe :io_new, shared: true do it "creates an IO instance from an Integer argument" do @io = IO.send(@method, @fd, "w") - @io.should be_an_instance_of(IO) + @io.should.instance_of?(IO) end it "creates an IO instance when STDOUT is closed" do @@ -30,7 +32,7 @@ describe :io_new, shared: true do begin @io = IO.send(@method, @fd, "w") - @io.should be_an_instance_of(IO) + @io.should.instance_of?(IO) ensure STDOUT = stdout rm_r stdout_file @@ -47,7 +49,7 @@ describe :io_new, shared: true do begin @io = IO.send(@method, @fd, "w") - @io.should be_an_instance_of(IO) + @io.should.instance_of?(IO) ensure STDERR = stderr rm_r stderr_file @@ -59,7 +61,16 @@ describe :io_new, shared: true 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) + @io.should.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(ArgumentError, "wrong number of arguments (given 3, expected 1..2)") end it "accepts a :mode option" do @@ -197,21 +208,30 @@ describe :io_new, shared: true do @io.internal_encoding.to_s.should == 'IBM866' end - ruby_version_is ''...'3.0' do - it "accepts nil options" do - @io = suppress_keyword_warning do - IO.send(@method, @fd, 'w', nil) - end - @io.write("foo").should == 3 - end + 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 - ruby_version_is '3.0' do - it "raises ArgumentError for nil options" do - -> { - IO.send(@method, @fd, 'w', nil) - }.should raise_error(ArgumentError) - 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(ArgumentError) end it "coerces mode with #to_str" do @@ -294,109 +314,100 @@ describe :io_new_errors, shared: true do end it "raises an Errno::EBADF if the file descriptor is not valid" do - -> { IO.send(@method, -1, "w") }.should raise_error(Errno::EBADF) + -> { IO.send(@method, -1, "w") }.should.raise(Errno::EBADF) end it "raises an IOError if passed a closed stream" do - -> { IO.send(@method, IOSpecs.closed_io.fileno, 'w') }.should raise_error(IOError) + -> { IO.send(@method, IOSpecs.closed_io.fileno, 'w') }.should.raise(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 - -> { IO.send(@method, @fd, "r") }.should raise_error(Errno::EINVAL) + -> { IO.send(@method, @fd, "r") }.should.raise(Errno::EINVAL) end end it "raises ArgumentError if passed an empty mode string" do - -> { IO.send(@method, @fd, "") }.should raise_error(ArgumentError) + -> { IO.send(@method, @fd, "") }.should.raise(ArgumentError) end it "raises an error if passed modes two ways" do -> { IO.send(@method, @fd, "w", mode: "w") - }.should raise_error(ArgumentError) + }.should.raise(ArgumentError) end it "raises an error if passed encodings two ways" do -> { @io = IO.send(@method, @fd, 'w:ISO-8859-1', encoding: 'ISO-8859-1') - }.should raise_error(ArgumentError) + }.should.raise(ArgumentError) -> { @io = IO.send(@method, @fd, 'w:ISO-8859-1', external_encoding: 'ISO-8859-1') - }.should raise_error(ArgumentError) + }.should.raise(ArgumentError) + -> { + @io = IO.send(@method, @fd, 'w:ISO-8859-1', internal_encoding: 'ISO-8859-1') + }.should.raise(ArgumentError) -> { @io = IO.send(@method, @fd, 'w:ISO-8859-1:UTF-8', internal_encoding: 'ISO-8859-1') - }.should raise_error(ArgumentError) + }.should.raise(ArgumentError) end it "raises an error if passed matching binary/text mode two ways" do -> { @io = IO.send(@method, @fd, "wb", binmode: true) - }.should raise_error(ArgumentError) + }.should.raise(ArgumentError) -> { @io = IO.send(@method, @fd, "wt", textmode: true) - }.should raise_error(ArgumentError) + }.should.raise(ArgumentError) -> { @io = IO.send(@method, @fd, "wb", textmode: false) - }.should raise_error(ArgumentError) + }.should.raise(ArgumentError) -> { @io = IO.send(@method, @fd, "wt", binmode: false) - }.should raise_error(ArgumentError) + }.should.raise(ArgumentError) end it "raises an error if passed conflicting binary/text mode two ways" do -> { @io = IO.send(@method, @fd, "wb", binmode: false) - }.should raise_error(ArgumentError) + }.should.raise(ArgumentError) -> { @io = IO.send(@method, @fd, "wt", textmode: false) - }.should raise_error(ArgumentError) + }.should.raise(ArgumentError) -> { @io = IO.send(@method, @fd, "wb", textmode: true) - }.should raise_error(ArgumentError) + }.should.raise(ArgumentError) -> { @io = IO.send(@method, @fd, "wt", binmode: true) - }.should raise_error(ArgumentError) + }.should.raise(ArgumentError) end it "raises an error when trying to set both binmode and textmode" do -> { @io = IO.send(@method, @fd, "w", textmode: true, binmode: true) - }.should raise_error(ArgumentError) + }.should.raise(ArgumentError) -> { @io = IO.send(@method, @fd, File::Constants::WRONLY, textmode: true, binmode: true) - }.should raise_error(ArgumentError) + }.should.raise(ArgumentError) end it "raises ArgumentError if not passed a hash or nil for options" do -> { @io = IO.send(@method, @fd, 'w', false) - }.should raise_error(ArgumentError) + }.should.raise(ArgumentError) -> { @io = IO.send(@method, @fd, false, false) - }.should raise_error(ArgumentError) + }.should.raise(ArgumentError) -> { @io = IO.send(@method, @fd, nil, false) - }.should raise_error(ArgumentError) - end - - ruby_version_is ''...'3.0' do - it "raises TypeError if passed a hash for mode and nil for options" do - -> { - suppress_keyword_warning do - @io = IO.send(@method, @fd, {mode: 'w'}, nil) - end - }.should raise_error(TypeError) - end + }.should.raise(ArgumentError) end - ruby_version_is '3.0' do - 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(ArgumentError) - end + it "raises ArgumentError if passed a hash for mode and nil for options" do + -> { + @io = IO.send(@method, @fd, {mode: 'w'}, nil) + }.should.raise(ArgumentError) end end |
