Skip to content

Commit

Permalink
Improving description and adding debugDescription to NIOAny (#2866
Browse files Browse the repository at this point in the history
)

Improving `description` and adding `debugDescription` to `NIOAny`

### Motivation
As per:
#2864 (comment) we
would like that the `description` of `NIOAny` prints the type of the
underlaying value that it wraps.

### Modification: 
Changing `description` and adding a conformance to
`CustomDebugStringConvertible`.

### Result:
A nicer `description` and `debugDescription` for `NIOAny`.

Co-authored-by: Franz Busch <[email protected]>
  • Loading branch information
supersonicbyte and FranzBusch authored Sep 9, 2024
1 parent 393eb06 commit 06ff6e8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 30 deletions.
8 changes: 7 additions & 1 deletion Sources/NIOCore/NIOAny.swift
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,12 @@ extension NIOAny: Sendable {}

extension NIOAny: CustomStringConvertible {
public var description: String {
"NIOAny { \(self.asAny()) }"
"\(type(of: self.asAny())): \(self.asAny())"
}
}

extension NIOAny: CustomDebugStringConvertible {
public var debugDescription: String {
"(\(self.description))"
}
}
59 changes: 30 additions & 29 deletions Tests/NIOCoreTests/NIOAnyDebugTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,54 +18,55 @@ import XCTest
class NIOAnyDebugTest: XCTestCase {

func testCustomStringConvertible() throws {
XCTAssertEqual(wrappedInNIOAnyBlock("string"), wrappedInNIOAnyBlock("string"))
XCTAssertEqual(wrappedInNIOAnyBlock(123), wrappedInNIOAnyBlock("123"))
XCTAssertEqual(wrappedInNIOAnyBlock("string").description, "String: string")
XCTAssertEqual(wrappedInNIOAnyBlock(123).description, "Int: 123")

let bb = ByteBuffer(string: "byte buffer string")
XCTAssertTrue(
wrappedInNIOAnyBlock(bb).contains(
"NIOAny { [627974652062756666657220737472696e67](18 bytes) }"
)
XCTAssertEqual(
wrappedInNIOAnyBlock(bb).description,
"ByteBuffer: [627974652062756666657220737472696e67](18 bytes)"
)
XCTAssertTrue(wrappedInNIOAnyBlock(bb).hasSuffix(" }"))

let fileHandle = NIOFileHandle(descriptor: 1)
defer {
XCTAssertNoThrow(_ = try fileHandle.takeDescriptorOwnership())
}
let fileRegion = FileRegion(fileHandle: fileHandle, readerIndex: 1, endIndex: 5)
XCTAssertEqual(
wrappedInNIOAnyBlock(fileRegion),
wrappedInNIOAnyBlock(
"""
FileRegion { \
handle: \
FileHandle \
{ descriptor: 1 \
}, \
readerIndex: \(fileRegion.readerIndex), \
endIndex: \(fileRegion.endIndex) }
"""
)
wrappedInNIOAnyBlock(fileRegion).description,
"""
FileRegion: \
FileRegion { \
handle: \
FileHandle \
{ descriptor: 1 \
}, \
readerIndex: \(fileRegion.readerIndex), \
endIndex: \(fileRegion.endIndex) }
"""
)

let socketAddress = try SocketAddress(unixDomainSocketPath: "socketAdress")
let envelopeByteBuffer = ByteBuffer(string: "envelope buffer")
let envelope = AddressedEnvelope<ByteBuffer>(remoteAddress: socketAddress, data: envelopeByteBuffer)
XCTAssertEqual(
wrappedInNIOAnyBlock("\(envelope)"),
wrappedInNIOAnyBlock(
"""
AddressedEnvelope { \
remoteAddress: \(socketAddress), \
data: \(envelopeByteBuffer) }
"""
)
wrappedInNIOAnyBlock(envelope).description,
"""
AddressedEnvelope<ByteBuffer>: \
AddressedEnvelope { \
remoteAddress: \(socketAddress), \
data: \(envelopeByteBuffer) }
"""
)
}

private func wrappedInNIOAnyBlock(_ item: Any) -> String {
"NIOAny { \(item) }"
func testCustomDebugStringConvertible() {
XCTAssertEqual(wrappedInNIOAnyBlock("string").debugDescription, "(String: string)")
let any = wrappedInNIOAnyBlock("test")
XCTAssertEqual(any.debugDescription, "(\(any.description))")
}

private func wrappedInNIOAnyBlock(_ item: Any) -> NIOAny {
NIOAny(item)
}
}

0 comments on commit 06ff6e8

Please sign in to comment.