Skip to content

Commit

Permalink
Add tests for warnings when passing block to creation of new IO object
Browse files Browse the repository at this point in the history
Update the spec for StringIO.new to match the style of the other ones,
and include the check to see the block is not used.
  • Loading branch information
herwinw authored and eregon committed Sep 30, 2023
1 parent 3e71d89 commit e7c3c53
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 2 deletions.
6 changes: 6 additions & 0 deletions core/file/new_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,12 @@
}.should raise_error(Errno::EEXIST, /File exists/)
end

it "does not use the given block and warns to use File::open" do
-> {
@fh = File.new(@file) { raise }
}.should complain(/warning: File::new\(\) does not take block; use File::open\(\) instead/)
end

it "raises a TypeError if the first parameter can't be coerced to a string" do
-> { File.new(true) }.should raise_error(TypeError)
-> { File.new(false) }.should raise_error(TypeError)
Expand Down
6 changes: 6 additions & 0 deletions core/io/new_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@

describe "IO.new" do
it_behaves_like :io_new, :new

it "does not use the given block and warns to use IO::open" do
-> {
@io = IO.send(@method, @fd) { raise }
}.should complain(/warning: IO::new\(\) does not take block; use IO::open\(\) instead/)
end
end

describe "IO.new" do
Expand Down
6 changes: 6 additions & 0 deletions library/socket/tcpserver/new_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@
addr[1].should be_kind_of(Integer)
end

it "does not use the given block and warns to use TCPServer::open" do
-> {
@server = TCPServer.new(0) { raise }
}.should complain(/warning: TCPServer::new\(\) does not take block; use TCPServer::open\(\) instead/)
end

it "raises Errno::EADDRNOTAVAIL when the address is unknown" do
-> { TCPServer.new("1.2.3.4", 0) }.should raise_error(Errno::EADDRNOTAVAIL)
end
Expand Down
21 changes: 21 additions & 0 deletions library/socket/tcpsocket/initialize_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,27 @@

describe 'TCPSocket#initialize' do
it_behaves_like :tcpsocket_new, :new

describe "with a running server" do
before :each do
@server = SocketSpecs::SpecTCPServer.new
@hostname = @server.hostname
end

after :each do
if @socket
@socket.write "QUIT"
@socket.close
end
@server.shutdown
end

it "does not use the given block and warns to use TCPSocket::open" do
-> {
@socket = TCPSocket.new(@hostname, @server.port, nil) { raise }
}.should complain(/warning: TCPSocket::new\(\) does not take block; use TCPSocket::open\(\) instead/)
end
end
end

describe 'TCPSocket#initialize' do
Expand Down
6 changes: 6 additions & 0 deletions library/socket/udpsocket/new_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
@socket.should be_an_instance_of(UDPSocket)
end

it "does not use the given block and warns to use UDPSocket::open" do
-> {
@socket = UDPSocket.new { raise }
}.should complain(/warning: UDPSocket::new\(\) does not take block; use UDPSocket::open\(\) instead/)
end

it 'raises Errno::EAFNOSUPPORT or Errno::EPROTONOSUPPORT if unsupported family passed' do
-> { UDPSocket.new(-1) }.should raise_error(SystemCallError) { |e|
[Errno::EAFNOSUPPORT, Errno::EPROTONOSUPPORT].should include(e.class)
Expand Down
6 changes: 6 additions & 0 deletions library/socket/unixserver/new_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,11 @@
with_feature :unix_socket do
describe "UNIXServer.new" do
it_behaves_like :unixserver_new, :new

it "does not use the given block and warns to use UNIXServer::open" do
-> {
@server = UNIXServer.new(@path) { raise }
}.should complain(/warning: UNIXServer::new\(\) does not take block; use UNIXServer::open\(\) instead/)
end
end
end
6 changes: 6 additions & 0 deletions library/socket/unixsocket/new_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,11 @@
with_feature :unix_socket do
describe "UNIXSocket.new" do
it_behaves_like :unixsocket_new, :new

it "does not use the given block and warns to use UNIXSocket::open" do
-> {
@client = UNIXSocket.new(@path) { raise }
}.should complain(/warning: UNIXSocket::new\(\) does not take block; use UNIXSocket::open\(\) instead/)
end
end
end
6 changes: 4 additions & 2 deletions library/stringio/new_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
require 'stringio'

describe "StringIO.new" do
it "warns when called with a block" do
-> { eval("StringIO.new {}") }.should complain(/StringIO::new\(\) does not take block; use StringIO::open\(\) instead/)
it "does not use the given block and warns to use StringIO::open" do
-> {
StringIO.new { raise }
}.should complain(/warning: StringIO::new\(\) does not take block; use StringIO::open\(\) instead/)
end
end

0 comments on commit e7c3c53

Please sign in to comment.