Skip to content

Commit

Permalink
Uniform #to_s and #inspect implementations.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Oct 12, 2024
1 parent 05771ea commit 35649c7
Show file tree
Hide file tree
Showing 15 changed files with 122 additions and 7 deletions.
6 changes: 4 additions & 2 deletions fixtures/with_temporary_directory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
module WithTemporaryDirectory
attr :temporary_directory

def around
def around(&block)
Dir.mktmpdir do |temporary_directory|
@temporary_directory = temporary_directory
yield
super(&block)
ensure
@temporary_directory = nil
end
end
end
11 changes: 11 additions & 0 deletions lib/io/endpoint/address_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ def initialize(address, **options)
end

def to_s
case @address.afamily
when Socket::AF_INET
"inet:#{@address.inspect_sockaddr}"
when Socket::AF_INET6
"inet6:#{@address.inspect_sockaddr}"
else
"address:#{@address.inspect_sockaddr}"
end
end

def inspect
"\#<#{self.class} address=#{@address.inspect}>"
end

Expand Down
4 changes: 4 additions & 0 deletions lib/io/endpoint/bound_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ def close
end

def to_s
"bound:#{@endpoint}"
end

def inspect
"\#<#{self.class} #{@sockets.size} bound sockets for #{@endpoint}>"
end

Expand Down
8 changes: 8 additions & 0 deletions lib/io/endpoint/composite_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ def initialize(endpoints, **options)
@endpoints = endpoints
end

def to_s
"composite:#{@endpoints.join(",")}"
end

def inspect
"\#<#{self.class} endpoints=#{@endpoints}>"
end

def with(**options)
self.class.new(endpoints.map{|endpoint| endpoint.with(**options)}, **@options.merge(options))
end
Expand Down
4 changes: 4 additions & 0 deletions lib/io/endpoint/connected_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ def close
end

def to_s
"connected:#{@endpoint}"
end

def inspect
"\#<#{self.class} #{@socket} connected for #{@endpoint}>"
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/io/endpoint/host_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ def initialize(specification, **options)
end

def to_s
"host:#{@specification[0]}:#{@specification[1]}"
end

def inspect
nodename, service, family, socktype, protocol, flags = @specification

"\#<#{self.class} name=#{nodename.inspect} service=#{service.inspect} family=#{family.inspect} type=#{socktype.inspect} protocol=#{protocol.inspect} flags=#{flags.inspect}>"
Expand Down
4 changes: 4 additions & 0 deletions lib/io/endpoint/socket_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ def initialize(socket, **options)
end

def to_s
"socket:#{@socket}"
end

def inspect
"\#<#{self.class} #{@socket.inspect}>"
end

Expand Down
6 changes: 5 additions & 1 deletion lib/io/endpoint/ssl_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ def initialize(endpoint, **options)
end

def to_s
"\#<#{self.class} #{@endpoint}>"
"ssl:#{@endpoint}"
end

def inspect
"\#<#{self.class} endpoint=#{@endpoint.inspect}>"
end

def address
Expand Down
6 changes: 5 additions & 1 deletion lib/io/endpoint/unix_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ def initialize(path, type = Socket::SOCK_STREAM, **options)
end

def to_s
"\#<#{self.class} #{@path.inspect}>"
"unix:#{@path}"
end

def inspect
"\#<#{self.class} path=#{@path.inspect}>"
end

attr :path
Expand Down
8 changes: 7 additions & 1 deletion test/io/endpoint/address_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@

with "#to_s" do
it "can generate a string representation" do
expect(endpoint.to_s).to be =~ /#<IO::Endpoint::AddressEndpoint address=/
expect(endpoint.to_s).to be =~ /inet(6):/
end
end

with "#inspect" do
it "can generate a string representation" do
expect(endpoint.inspect).to be =~ /#<IO::Endpoint::AddressEndpoint address=/
end
end
end
20 changes: 20 additions & 0 deletions test/io/endpoint/bound_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,24 @@
connected_endpoint&.close
end
end

with "a bound instance" do
let(:endpoint) {subject.bound(internal_endpoint)}

after do
@endpoint&.close
end

with "#to_s" do
it "can generate a string representation" do
expect(endpoint.to_s).to be =~ /bound:/
end
end

with "#inspect" do
it "can generate a string representation" do
expect(endpoint.inspect).to be =~ /#<IO::Endpoint::BoundEndpoint 1 bound sockets/
end
end
end
end
12 changes: 12 additions & 0 deletions test/io/endpoint/composite_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,16 @@
end
end
end

with "#to_s" do
it "can generate a string representation" do
expect(endpoint.to_s).to be =~ /composite:/
end
end

with "#inspect" do
it "can generate a string representation" do
expect(endpoint.inspect).to be =~ /#<IO::Endpoint::CompositeEndpoint endpoints=/
end
end
end
4 changes: 2 additions & 2 deletions test/io/endpoint/host_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@
bound&.close
end

with "#to_s" do
with "#inspect" do
it "can generate a string representation" do
expect(endpoint.to_s).to be == "#<IO::Endpoint::HostEndpoint name=\"localhost\" service=0 family=nil type=1 protocol=nil flags=nil>"
expect(endpoint.inspect).to be == "#<IO::Endpoint::HostEndpoint name=\"localhost\" service=0 family=nil type=1 protocol=nil flags=nil>"
end
end
end
Expand Down
16 changes: 16 additions & 0 deletions test/io/endpoint/ssl_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,20 @@ def client_endpoint(address)
bound&.close
end
end

with "a simple SSL endpoint" do
let(:endpoint) {subject.new(IO::Endpoint.tcp("localhost", 0))}

with "#to_s" do
it "can generate a string representation" do
expect(endpoint.to_s).to be =~ /ssl:/
end
end

with "#inspect" do
it "can generate a string representation" do
expect(endpoint.inspect).to be =~ /#<IO::Endpoint::SSLEndpoint endpoint=/
end
end
end
end
16 changes: 16 additions & 0 deletions test/io/endpoint/unix_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@

expect(endpoint).to be(:bound?)

# Wait for the server to start accepting connections:
# I noticed on slow CI, that the connect would fail because the server has not called `#accept` yet, even if it's bound and listening!
Thread.pass until thread.status == "sleep"

endpoint.connect do |socket|
expect(socket).to be_a(Socket)

Expand All @@ -53,6 +57,18 @@
thread&.kill
sockets&.each(&:close)
end

with "#to_s" do
it "can generate a string representation" do
expect(endpoint.to_s).to be =~ /unix:.*test\.ipc/
end
end

with "#inspect" do
it "can generate a string representation" do
expect(endpoint.inspect).to be =~ /#<IO::Endpoint::UNIXEndpoint path=.*test\.ipc/
end
end
end

describe IO::Endpoint do
Expand Down

0 comments on commit 35649c7

Please sign in to comment.