From f2611bac420f7e1477c387f91d0e4dca306adb41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mirza=20U=C4=8Danbarli=C4=87?= <56406159+supersonicbyte@users.noreply.github.com> Date: Mon, 9 Sep 2024 12:05:51 +0200 Subject: [PATCH] Improving `description` and adding `debugDescription` to `NIOAny` (#2866) Improving `description` and adding `debugDescription` to `NIOAny` ### Motivation As per: https://github.com/apple/swift-nio/pull/2864#discussion_r1742192300 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 --- Sources/NIOCore/NIOAny.swift | 8 +++- Tests/NIOCoreTests/NIOAnyDebugTest.swift | 59 ++++++++++++------------ 2 files changed, 37 insertions(+), 30 deletions(-) diff --git a/Sources/NIOCore/NIOAny.swift b/Sources/NIOCore/NIOAny.swift index 1f687b134b..3837a8159c 100644 --- a/Sources/NIOCore/NIOAny.swift +++ b/Sources/NIOCore/NIOAny.swift @@ -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))" } } diff --git a/Tests/NIOCoreTests/NIOAnyDebugTest.swift b/Tests/NIOCoreTests/NIOAnyDebugTest.swift index cdf637affb..8d9b6f4aa3 100644 --- a/Tests/NIOCoreTests/NIOAnyDebugTest.swift +++ b/Tests/NIOCoreTests/NIOAnyDebugTest.swift @@ -18,16 +18,14 @@ 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 { @@ -35,37 +33,40 @@ class NIOAnyDebugTest: XCTestCase { } 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(remoteAddress: socketAddress, data: envelopeByteBuffer) XCTAssertEqual( - wrappedInNIOAnyBlock("\(envelope)"), - wrappedInNIOAnyBlock( - """ - AddressedEnvelope { \ - remoteAddress: \(socketAddress), \ - data: \(envelopeByteBuffer) } - """ - ) + wrappedInNIOAnyBlock(envelope).description, + """ + AddressedEnvelope: \ + 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) + } }