From 35649c7b594cf8467fbd453424aca34cb278f0c3 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Sat, 12 Oct 2024 23:28:08 +1300 Subject: [PATCH] Uniform `#to_s` and `#inspect` implementations. --- fixtures/with_temporary_directory.rb | 6 ++++-- lib/io/endpoint/address_endpoint.rb | 11 +++++++++++ lib/io/endpoint/bound_endpoint.rb | 4 ++++ lib/io/endpoint/composite_endpoint.rb | 8 ++++++++ lib/io/endpoint/connected_endpoint.rb | 4 ++++ lib/io/endpoint/host_endpoint.rb | 4 ++++ lib/io/endpoint/socket_endpoint.rb | 4 ++++ lib/io/endpoint/ssl_endpoint.rb | 6 +++++- lib/io/endpoint/unix_endpoint.rb | 6 +++++- test/io/endpoint/address_endpoint.rb | 8 +++++++- test/io/endpoint/bound_endpoint.rb | 20 ++++++++++++++++++++ test/io/endpoint/composite_endpoint.rb | 12 ++++++++++++ test/io/endpoint/host_endpoint.rb | 4 ++-- test/io/endpoint/ssl_endpoint.rb | 16 ++++++++++++++++ test/io/endpoint/unix_endpoint.rb | 16 ++++++++++++++++ 15 files changed, 122 insertions(+), 7 deletions(-) diff --git a/fixtures/with_temporary_directory.rb b/fixtures/with_temporary_directory.rb index 842fe9d..ec2313d 100644 --- a/fixtures/with_temporary_directory.rb +++ b/fixtures/with_temporary_directory.rb @@ -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 diff --git a/lib/io/endpoint/address_endpoint.rb b/lib/io/endpoint/address_endpoint.rb index a3478bc..a5d071a 100644 --- a/lib/io/endpoint/address_endpoint.rb +++ b/lib/io/endpoint/address_endpoint.rb @@ -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 diff --git a/lib/io/endpoint/bound_endpoint.rb b/lib/io/endpoint/bound_endpoint.rb index e6f1a0d..1ffa19c 100644 --- a/lib/io/endpoint/bound_endpoint.rb +++ b/lib/io/endpoint/bound_endpoint.rb @@ -55,6 +55,10 @@ def close end def to_s + "bound:#{@endpoint}" + end + + def inspect "\#<#{self.class} #{@sockets.size} bound sockets for #{@endpoint}>" end diff --git a/lib/io/endpoint/composite_endpoint.rb b/lib/io/endpoint/composite_endpoint.rb index 1365775..2ec9554 100644 --- a/lib/io/endpoint/composite_endpoint.rb +++ b/lib/io/endpoint/composite_endpoint.rb @@ -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 diff --git a/lib/io/endpoint/connected_endpoint.rb b/lib/io/endpoint/connected_endpoint.rb index ad42d80..4bb1cb7 100644 --- a/lib/io/endpoint/connected_endpoint.rb +++ b/lib/io/endpoint/connected_endpoint.rb @@ -57,6 +57,10 @@ def close end def to_s + "connected:#{@endpoint}" + end + + def inspect "\#<#{self.class} #{@socket} connected for #{@endpoint}>" end end diff --git a/lib/io/endpoint/host_endpoint.rb b/lib/io/endpoint/host_endpoint.rb index 063350a..1a93068 100644 --- a/lib/io/endpoint/host_endpoint.rb +++ b/lib/io/endpoint/host_endpoint.rb @@ -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}>" diff --git a/lib/io/endpoint/socket_endpoint.rb b/lib/io/endpoint/socket_endpoint.rb index 87c5170..7c0318c 100644 --- a/lib/io/endpoint/socket_endpoint.rb +++ b/lib/io/endpoint/socket_endpoint.rb @@ -15,6 +15,10 @@ def initialize(socket, **options) end def to_s + "socket:#{@socket}" + end + + def inspect "\#<#{self.class} #{@socket.inspect}>" end diff --git a/lib/io/endpoint/ssl_endpoint.rb b/lib/io/endpoint/ssl_endpoint.rb index 8e213a5..a984c69 100644 --- a/lib/io/endpoint/ssl_endpoint.rb +++ b/lib/io/endpoint/ssl_endpoint.rb @@ -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 diff --git a/lib/io/endpoint/unix_endpoint.rb b/lib/io/endpoint/unix_endpoint.rb index ece1245..ddc2549 100644 --- a/lib/io/endpoint/unix_endpoint.rb +++ b/lib/io/endpoint/unix_endpoint.rb @@ -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 diff --git a/test/io/endpoint/address_endpoint.rb b/test/io/endpoint/address_endpoint.rb index 342c20e..a73485f 100644 --- a/test/io/endpoint/address_endpoint.rb +++ b/test/io/endpoint/address_endpoint.rb @@ -43,7 +43,13 @@ with "#to_s" do it "can generate a string representation" do - expect(endpoint.to_s).to be =~ /#" + expect(endpoint.inspect).to be == "#" end end end diff --git a/test/io/endpoint/ssl_endpoint.rb b/test/io/endpoint/ssl_endpoint.rb index dfb60b0..28320cc 100644 --- a/test/io/endpoint/ssl_endpoint.rb +++ b/test/io/endpoint/ssl_endpoint.rb @@ -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 =~ /#