|
| 1 | +// |
| 2 | +// This source file is part of the Swift.org open source project |
| 3 | +// |
| 4 | +// Copyright (c) 2026 Apple Inc. and the Swift project authors |
| 5 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 6 | +// |
| 7 | +// See https://swift.org/LICENSE.txt for license information |
| 8 | +// See https://swift.org/CONTRIBUTORS.txt for Swift project authors |
| 9 | +// |
| 10 | + |
| 11 | +#if canImport(CoreTransferable) |
| 12 | +public import Testing |
| 13 | +public import CoreTransferable |
| 14 | + |
| 15 | +public import UniformTypeIdentifiers |
| 16 | + |
| 17 | +/// @Metadata { |
| 18 | +/// @Available(Swift, introduced: 6.4) |
| 19 | +/// } |
| 20 | +@available(_transferableAPI, *) |
| 21 | +extension Attachment { |
| 22 | + /// Initialize an instance of this type that encloses the given transferable |
| 23 | + /// value. |
| 24 | + /// |
| 25 | + /// - Parameters: |
| 26 | + /// - transferableValue: The value that will be attached to the output of |
| 27 | + /// the test run. |
| 28 | + /// - contentType: The content type with which to export `transferableValue`. |
| 29 | + /// If this argument is `nil`, the testing library calls [`exportedContentTypes(_:)`](https://developer.apple.com/documentation/coretransferable/transferable/exportedcontenttypes(_:)) |
| 30 | + /// on `transferableValue` and uses the first type the function returns |
| 31 | + /// that conforms to [`UTType.data`](https://developer.apple.com/documentation/uniformtypeidentifiers/uttype-swift.struct/data). |
| 32 | + /// - preferredName: The preferred name of the attachment to use when saving |
| 33 | + /// it. If `nil`, the testing library attempts to generate a reasonable |
| 34 | + /// filename for the attached value. |
| 35 | + /// - sourceLocation: The source location of the call to this initializer. |
| 36 | + /// This value is used when recording issues associated with the |
| 37 | + /// attachment. |
| 38 | + /// |
| 39 | + /// - Throws: Any error that occurs while exporting `transferableValue`. |
| 40 | + /// |
| 41 | + /// Use this initializer to create an instance of ``Attachment`` from a value |
| 42 | + /// that conforms to the [`Transferable`](https://developer.apple.com/documentation/coretransferable/transferable) |
| 43 | + /// protocol. |
| 44 | + /// |
| 45 | + /// ```swift |
| 46 | + /// let menu = FoodTruck.menu |
| 47 | + /// let attachment = try await Attachment(exporting: menu, as: .pdf) |
| 48 | + /// Attachment.record(attachment) |
| 49 | + /// ``` |
| 50 | + /// |
| 51 | + /// When you call this initializer and pass it a transferable value, it |
| 52 | + /// calls [`exported(as:)`](https://developer.apple.com/documentation/coretransferable/transferable/exported(as:)) |
| 53 | + /// on that value. This operation may take some time, so this initializer |
| 54 | + /// suspends the calling task until it is complete. |
| 55 | + /// |
| 56 | + /// @Metadata { |
| 57 | + /// @Available(Swift, introduced: 6.4) |
| 58 | + /// } |
| 59 | + public init<T>( |
| 60 | + exporting transferableValue: T, |
| 61 | + as contentType: UTType? = nil, |
| 62 | + named preferredName: String? = nil, |
| 63 | + sourceLocation: SourceLocation = #_sourceLocation |
| 64 | + ) async throws where T: Transferable, AttachableValue == _AttachableTransferableWrapper<T> { |
| 65 | + let transferableWrapper = try await _AttachableTransferableWrapper(exporting: transferableValue, as: contentType) |
| 66 | + self.init(transferableWrapper, named: preferredName, sourceLocation: sourceLocation) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +// MARK: - |
| 71 | + |
| 72 | +/// A type describing errors that can occur when attaching a transferable value. |
| 73 | +enum TransferableAttachmentError: Error { |
| 74 | + /// The developer did not pass a content type and the value did not list any |
| 75 | + /// that conform to `UTType.data`. |
| 76 | + case suitableContentTypeNotFound |
| 77 | +} |
| 78 | + |
| 79 | +extension TransferableAttachmentError: CustomStringConvertible { |
| 80 | + var description: String { |
| 81 | + switch self { |
| 82 | + case .suitableContentTypeNotFound: |
| 83 | + "The value does not list any exported content types that conform to 'UTType.data'." |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | +#endif |
0 commit comments