Skip to content

Commit 7ae3647

Browse files
authored
Make Attachment conform to CustomStringConvertible. (#849)
This PR adds `CustomStringConvertible` conformance to `Attachment`. Note that if the attachable value is a move-only type, the conformance is not available in the stdlib, but we still provide a `description` property. ### Checklist: - [x] Code and documentation should follow the style of the [Style Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md). - [x] If public symbols are renamed or modified, DocC references should be updated.
1 parent 0fd04d1 commit 7ae3647

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

Sources/Testing/Attachments/Attachment.swift

+15
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,21 @@ public struct AnyAttachable: AttachableContainer, Copyable, Sendable {
148148
}
149149
}
150150

151+
// MARK: - Describing an attachment
152+
153+
extension Attachment where AttachableValue: ~Copyable {
154+
public var description: String {
155+
let typeInfo = TypeInfo(describing: AttachableValue.self)
156+
return #""\#(preferredName)": instance of '\#(typeInfo.unqualifiedName)'"#
157+
}
158+
}
159+
160+
extension Attachment: CustomStringConvertible {
161+
public var description: String {
162+
#""\#(preferredName)": \#(String(describingForTest: attachableValue))"#
163+
}
164+
}
165+
151166
// MARK: - Getting an attachable value from an attachment
152167

153168
@_spi(Experimental)

Tests/TestingTests/AttachmentTests.swift

+14
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,20 @@ struct AttachmentTests {
3030
attachment.attach()
3131
}
3232

33+
@Test func description() {
34+
let attachableValue = MySendableAttachable(string: "<!doctype html>")
35+
let attachment = Attachment(attachableValue, named: "AttachmentTests.saveValue.html")
36+
#expect(String(describing: attachment).contains(#""\#(attachment.preferredName)""#))
37+
#expect(attachment.description.contains("MySendableAttachable("))
38+
}
39+
40+
@Test func moveOnlyDescription() {
41+
let attachableValue = MyAttachable(string: "<!doctype html>")
42+
let attachment = Attachment(attachableValue, named: "AttachmentTests.saveValue.html")
43+
#expect(attachment.description.contains(#""\#(attachment.preferredName)""#))
44+
#expect(attachment.description.contains("'MyAttachable'"))
45+
}
46+
3347
#if !SWT_NO_FILE_IO
3448
func compare(_ attachableValue: borrowing MySendableAttachable, toContentsOfFileAtPath filePath: String) throws {
3549
let file = try FileHandle(forReadingAtPath: filePath)

0 commit comments

Comments
 (0)