Files
swift-nio/Tests/NIOCoreTests/NIOAnyDebugTest.swift
T
Mirza UčanbarlićandFranz Busch 06ff6e8c84 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 <f.busch@apple.com>
2024-09-09 10:05:51 +00:00

73 lines
2.5 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2021 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import NIOCore
import XCTest
class NIOAnyDebugTest: XCTestCase {
func testCustomStringConvertible() throws {
XCTAssertEqual(wrappedInNIOAnyBlock("string").description, "String: string")
XCTAssertEqual(wrappedInNIOAnyBlock(123).description, "Int: 123")
let bb = ByteBuffer(string: "byte buffer string")
XCTAssertEqual(
wrappedInNIOAnyBlock(bb).description,
"ByteBuffer: [627974652062756666657220737472696e67](18 bytes)"
)
let fileHandle = NIOFileHandle(descriptor: 1)
defer {
XCTAssertNoThrow(_ = try fileHandle.takeDescriptorOwnership())
}
let fileRegion = FileRegion(fileHandle: fileHandle, readerIndex: 1, endIndex: 5)
XCTAssertEqual(
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).description,
"""
AddressedEnvelope<ByteBuffer>: \
AddressedEnvelope { \
remoteAddress: \(socketAddress), \
data: \(envelopeByteBuffer) }
"""
)
}
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)
}
}