Skip to content

Commit e7c3c53

Browse files
herwinweregon
authored andcommitted
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.
1 parent 3e71d89 commit e7c3c53

File tree

8 files changed

+61
-2
lines changed

8 files changed

+61
-2
lines changed

core/file/new_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,12 @@
188188
}.should raise_error(Errno::EEXIST, /File exists/)
189189
end
190190

191+
it "does not use the given block and warns to use File::open" do
192+
-> {
193+
@fh = File.new(@file) { raise }
194+
}.should complain(/warning: File::new\(\) does not take block; use File::open\(\) instead/)
195+
end
196+
191197
it "raises a TypeError if the first parameter can't be coerced to a string" do
192198
-> { File.new(true) }.should raise_error(TypeError)
193199
-> { File.new(false) }.should raise_error(TypeError)

core/io/new_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55

66
describe "IO.new" do
77
it_behaves_like :io_new, :new
8+
9+
it "does not use the given block and warns to use IO::open" do
10+
-> {
11+
@io = IO.send(@method, @fd) { raise }
12+
}.should complain(/warning: IO::new\(\) does not take block; use IO::open\(\) instead/)
13+
end
814
end
915

1016
describe "IO.new" do

library/socket/tcpserver/new_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@
9797
addr[1].should be_kind_of(Integer)
9898
end
9999

100+
it "does not use the given block and warns to use TCPServer::open" do
101+
-> {
102+
@server = TCPServer.new(0) { raise }
103+
}.should complain(/warning: TCPServer::new\(\) does not take block; use TCPServer::open\(\) instead/)
104+
end
105+
100106
it "raises Errno::EADDRNOTAVAIL when the address is unknown" do
101107
-> { TCPServer.new("1.2.3.4", 0) }.should raise_error(Errno::EADDRNOTAVAIL)
102108
end

library/socket/tcpsocket/initialize_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,27 @@
44

55
describe 'TCPSocket#initialize' do
66
it_behaves_like :tcpsocket_new, :new
7+
8+
describe "with a running server" do
9+
before :each do
10+
@server = SocketSpecs::SpecTCPServer.new
11+
@hostname = @server.hostname
12+
end
13+
14+
after :each do
15+
if @socket
16+
@socket.write "QUIT"
17+
@socket.close
18+
end
19+
@server.shutdown
20+
end
21+
22+
it "does not use the given block and warns to use TCPSocket::open" do
23+
-> {
24+
@socket = TCPSocket.new(@hostname, @server.port, nil) { raise }
25+
}.should complain(/warning: TCPSocket::new\(\) does not take block; use TCPSocket::open\(\) instead/)
26+
end
27+
end
728
end
829

930
describe 'TCPSocket#initialize' do

library/socket/udpsocket/new_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@
2626
@socket.should be_an_instance_of(UDPSocket)
2727
end
2828

29+
it "does not use the given block and warns to use UDPSocket::open" do
30+
-> {
31+
@socket = UDPSocket.new { raise }
32+
}.should complain(/warning: UDPSocket::new\(\) does not take block; use UDPSocket::open\(\) instead/)
33+
end
34+
2935
it 'raises Errno::EAFNOSUPPORT or Errno::EPROTONOSUPPORT if unsupported family passed' do
3036
-> { UDPSocket.new(-1) }.should raise_error(SystemCallError) { |e|
3137
[Errno::EAFNOSUPPORT, Errno::EPROTONOSUPPORT].should include(e.class)

library/socket/unixserver/new_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,11 @@
44
with_feature :unix_socket do
55
describe "UNIXServer.new" do
66
it_behaves_like :unixserver_new, :new
7+
8+
it "does not use the given block and warns to use UNIXServer::open" do
9+
-> {
10+
@server = UNIXServer.new(@path) { raise }
11+
}.should complain(/warning: UNIXServer::new\(\) does not take block; use UNIXServer::open\(\) instead/)
12+
end
713
end
814
end

library/socket/unixsocket/new_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,11 @@
44
with_feature :unix_socket do
55
describe "UNIXSocket.new" do
66
it_behaves_like :unixsocket_new, :new
7+
8+
it "does not use the given block and warns to use UNIXSocket::open" do
9+
-> {
10+
@client = UNIXSocket.new(@path) { raise }
11+
}.should complain(/warning: UNIXSocket::new\(\) does not take block; use UNIXSocket::open\(\) instead/)
12+
end
713
end
814
end

library/stringio/new_spec.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
require 'stringio'
33

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

0 commit comments

Comments
 (0)