From e7c3c5396cbe0b12ce9f66f04287e64fb83fc097 Mon Sep 17 00:00:00 2001 From: Herwin Date: Thu, 28 Sep 2023 18:21:27 +0200 Subject: [PATCH] Add tests for warnings when passing block to creation of new IO object 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. --- core/file/new_spec.rb | 6 ++++++ core/io/new_spec.rb | 6 ++++++ library/socket/tcpserver/new_spec.rb | 6 ++++++ library/socket/tcpsocket/initialize_spec.rb | 21 +++++++++++++++++++++ library/socket/udpsocket/new_spec.rb | 6 ++++++ library/socket/unixserver/new_spec.rb | 6 ++++++ library/socket/unixsocket/new_spec.rb | 6 ++++++ library/stringio/new_spec.rb | 6 ++++-- 8 files changed, 61 insertions(+), 2 deletions(-) diff --git a/core/file/new_spec.rb b/core/file/new_spec.rb index 715ac1aaf3..3e2641aed3 100644 --- a/core/file/new_spec.rb +++ b/core/file/new_spec.rb @@ -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) diff --git a/core/io/new_spec.rb b/core/io/new_spec.rb index 9d14ec18ad..979ac0efcb 100644 --- a/core/io/new_spec.rb +++ b/core/io/new_spec.rb @@ -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 diff --git a/library/socket/tcpserver/new_spec.rb b/library/socket/tcpserver/new_spec.rb index 8d9696c9d8..dd1ba676bd 100644 --- a/library/socket/tcpserver/new_spec.rb +++ b/library/socket/tcpserver/new_spec.rb @@ -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 diff --git a/library/socket/tcpsocket/initialize_spec.rb b/library/socket/tcpsocket/initialize_spec.rb index 065c8f4190..3bec06c697 100644 --- a/library/socket/tcpsocket/initialize_spec.rb +++ b/library/socket/tcpsocket/initialize_spec.rb @@ -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 diff --git a/library/socket/udpsocket/new_spec.rb b/library/socket/udpsocket/new_spec.rb index 6cc0cadbcb..79bfcb624d 100644 --- a/library/socket/udpsocket/new_spec.rb +++ b/library/socket/udpsocket/new_spec.rb @@ -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) diff --git a/library/socket/unixserver/new_spec.rb b/library/socket/unixserver/new_spec.rb index 5a8ca8fcdc..a160e3ce5c 100644 --- a/library/socket/unixserver/new_spec.rb +++ b/library/socket/unixserver/new_spec.rb @@ -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 diff --git a/library/socket/unixsocket/new_spec.rb b/library/socket/unixsocket/new_spec.rb index df6a2eefcc..6d8ea6dcfe 100644 --- a/library/socket/unixsocket/new_spec.rb +++ b/library/socket/unixsocket/new_spec.rb @@ -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 diff --git a/library/stringio/new_spec.rb b/library/stringio/new_spec.rb index 328455134e..ec4b32aa11 100644 --- a/library/stringio/new_spec.rb +++ b/library/stringio/new_spec.rb @@ -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