Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions Sources/Testing/Attachments/Attachment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,19 @@ public struct Attachment<AttachableValue> where AttachableValue: Attachable & ~C
extension Attachment: Sendable where AttachableValue: Sendable {}
extension Attachment.Storage: Sendable where AttachableValue: Sendable {}

#if !hasFeature(Embedded)
/// A protocol that describes an attachment with a copyable value.
///
/// We can use this protocol to make runtime decisions about attachments based
/// on whether or not their attachable values are copyable.
private protocol _AttachmentWithCopyableValue {
associatedtype AttachableValue: Attachable & Copyable
var attachableValue: AttachableValue { get }
}

extension Attachment: _AttachmentWithCopyableValue where AttachableValue: Copyable {}
#endif

// MARK: - Initializing an attachment

extension Attachment where AttachableValue: ~Copyable {
Expand Down Expand Up @@ -180,21 +193,19 @@ public struct AnyAttachable: AttachableWrapper, Sendable, Copyable {

// MARK: - Describing an attachment

extension Attachment where AttachableValue: ~Copyable {
@_documentation(visibility: private)
public var description: String {
let typeInfo = TypeInfo(describing: AttachableValue.self)
return #""\#(preferredName)": instance of '\#(typeInfo.unqualifiedName)'"#
}
}

extension Attachment: CustomStringConvertible {
extension Attachment: CustomStringConvertible where AttachableValue: ~Copyable {
/// @Metadata {
/// @Available(Swift, introduced: 6.2)
/// @Available(Xcode, introduced: 26.0)
/// }
public var description: String {
#""\#(preferredName)": \#(String(describingForTest: attachableValue))"#
#if !hasFeature(Embedded)
if let selfCopy = self as? any _AttachmentWithCopyableValue {
return #""\#(preferredName)": \#(String(describingForTest: selfCopy.attachableValue))"#
}
#endif
let typeInfo = TypeInfo(describing: AttachableValue.self)
return #""\#(preferredName)": instance of '\#(typeInfo.unqualifiedName)'"#
}
}

Expand Down
6 changes: 3 additions & 3 deletions Tests/TestingTests/AttachmentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ struct AttachmentTests {
let attachableValue = MySendableAttachable(string: "<!doctype html>")
let attachment = Attachment(attachableValue, named: "AttachmentTests.saveValue.html")
#expect(String(describing: attachment).contains(#""\#(attachment.preferredName)""#))
#expect(attachment.description.contains("MySendableAttachable("))
#expect(String(describing: attachment).contains("MySendableAttachable("))
}

#if compiler(>=6.3) || !os(Windows) // WORKAROUND: swift-#84184
@Test func moveOnlyDescription() {
let attachableValue = MyAttachable(string: "<!doctype html>")
let attachment = Attachment(attachableValue, named: "AttachmentTests.saveValue.html")
#expect(attachment.description.contains(#""\#(attachment.preferredName)""#))
#expect(attachment.description.contains("'MyAttachable'"))
#expect(String(describing: attachment).contains(#""\#(attachment.preferredName)""#))
#expect(String(describing: attachment).contains("'MyAttachable'"))
}
#endif

Expand Down
Loading