Skip to content

Commit 06ff6e8

Browse files
Improving description and adding debugDescription to NIOAny (#2866)
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]>
1 parent 393eb06 commit 06ff6e8

File tree

2 files changed

+37
-30
lines changed

2 files changed

+37
-30
lines changed

Sources/NIOCore/NIOAny.swift

+7-1
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,12 @@ extension NIOAny: Sendable {}
272272

273273
extension NIOAny: CustomStringConvertible {
274274
public var description: String {
275-
"NIOAny { \(self.asAny()) }"
275+
"\(type(of: self.asAny())): \(self.asAny())"
276+
}
277+
}
278+
279+
extension NIOAny: CustomDebugStringConvertible {
280+
public var debugDescription: String {
281+
"(\(self.description))"
276282
}
277283
}

Tests/NIOCoreTests/NIOAnyDebugTest.swift

+30-29
Original file line numberDiff line numberDiff line change
@@ -18,54 +18,55 @@ import XCTest
1818
class NIOAnyDebugTest: XCTestCase {
1919

2020
func testCustomStringConvertible() throws {
21-
XCTAssertEqual(wrappedInNIOAnyBlock("string"), wrappedInNIOAnyBlock("string"))
22-
XCTAssertEqual(wrappedInNIOAnyBlock(123), wrappedInNIOAnyBlock("123"))
21+
XCTAssertEqual(wrappedInNIOAnyBlock("string").description, "String: string")
22+
XCTAssertEqual(wrappedInNIOAnyBlock(123).description, "Int: 123")
2323

2424
let bb = ByteBuffer(string: "byte buffer string")
25-
XCTAssertTrue(
26-
wrappedInNIOAnyBlock(bb).contains(
27-
"NIOAny { [627974652062756666657220737472696e67](18 bytes) }"
28-
)
25+
XCTAssertEqual(
26+
wrappedInNIOAnyBlock(bb).description,
27+
"ByteBuffer: [627974652062756666657220737472696e67](18 bytes)"
2928
)
30-
XCTAssertTrue(wrappedInNIOAnyBlock(bb).hasSuffix(" }"))
3129

3230
let fileHandle = NIOFileHandle(descriptor: 1)
3331
defer {
3432
XCTAssertNoThrow(_ = try fileHandle.takeDescriptorOwnership())
3533
}
3634
let fileRegion = FileRegion(fileHandle: fileHandle, readerIndex: 1, endIndex: 5)
3735
XCTAssertEqual(
38-
wrappedInNIOAnyBlock(fileRegion),
39-
wrappedInNIOAnyBlock(
40-
"""
41-
FileRegion { \
42-
handle: \
43-
FileHandle \
44-
{ descriptor: 1 \
45-
}, \
46-
readerIndex: \(fileRegion.readerIndex), \
47-
endIndex: \(fileRegion.endIndex) }
48-
"""
49-
)
36+
wrappedInNIOAnyBlock(fileRegion).description,
37+
"""
38+
FileRegion: \
39+
FileRegion { \
40+
handle: \
41+
FileHandle \
42+
{ descriptor: 1 \
43+
}, \
44+
readerIndex: \(fileRegion.readerIndex), \
45+
endIndex: \(fileRegion.endIndex) }
46+
"""
5047
)
5148

5249
let socketAddress = try SocketAddress(unixDomainSocketPath: "socketAdress")
5350
let envelopeByteBuffer = ByteBuffer(string: "envelope buffer")
5451
let envelope = AddressedEnvelope<ByteBuffer>(remoteAddress: socketAddress, data: envelopeByteBuffer)
5552
XCTAssertEqual(
56-
wrappedInNIOAnyBlock("\(envelope)"),
57-
wrappedInNIOAnyBlock(
58-
"""
59-
AddressedEnvelope { \
60-
remoteAddress: \(socketAddress), \
61-
data: \(envelopeByteBuffer) }
62-
"""
63-
)
53+
wrappedInNIOAnyBlock(envelope).description,
54+
"""
55+
AddressedEnvelope<ByteBuffer>: \
56+
AddressedEnvelope { \
57+
remoteAddress: \(socketAddress), \
58+
data: \(envelopeByteBuffer) }
59+
"""
6460
)
6561
}
6662

67-
private func wrappedInNIOAnyBlock(_ item: Any) -> String {
68-
"NIOAny { \(item) }"
63+
func testCustomDebugStringConvertible() {
64+
XCTAssertEqual(wrappedInNIOAnyBlock("string").debugDescription, "(String: string)")
65+
let any = wrappedInNIOAnyBlock("test")
66+
XCTAssertEqual(any.debugDescription, "(\(any.description))")
6967
}
7068

69+
private func wrappedInNIOAnyBlock(_ item: Any) -> NIOAny {
70+
NIOAny(item)
71+
}
7172
}

0 commit comments

Comments
 (0)