|
| 1 | +// |
| 2 | +// This source file is part of the Swift.org open source project |
| 3 | +// |
| 4 | +// Copyright (c) 2024 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(Foundation) |
| 12 | +@_spi(Experimental) public import Testing |
| 13 | +private import Foundation |
| 14 | + |
| 15 | +/// A common implementation of ``withUnsafeBufferPointer(for:_:)`` that is |
| 16 | +/// used when a type conforms to `Encodable`, whether or not it also conforms |
| 17 | +/// to `NSSecureCoding`. |
| 18 | +/// |
| 19 | +/// - Parameters: |
| 20 | +/// - attachableValue: The value to encode. |
| 21 | +/// - attachment: The attachment that is requesting a buffer (that is, the |
| 22 | +/// attachment containing this instance.) |
| 23 | +/// - body: A function to call. A temporary buffer containing a data |
| 24 | +/// representation of this instance is passed to it. |
| 25 | +/// |
| 26 | +/// - Returns: Whatever is returned by `body`. |
| 27 | +/// |
| 28 | +/// - Throws: Whatever is thrown by `body`, or any error that prevented the |
| 29 | +/// creation of the buffer. |
| 30 | +func withUnsafeBufferPointer<E, R>(encoding attachableValue: borrowing E, for attachment: borrowing Attachment<E>, _ body: (UnsafeRawBufferPointer) throws -> R) throws -> R where E: Attachable & Encodable { |
| 31 | + let format = try EncodingFormat(for: attachment) |
| 32 | + |
| 33 | + let data: Data |
| 34 | + switch format { |
| 35 | + case let .propertyListFormat(propertyListFormat): |
| 36 | + let plistEncoder = PropertyListEncoder() |
| 37 | + plistEncoder.outputFormat = propertyListFormat |
| 38 | + data = try plistEncoder.encode(attachableValue) |
| 39 | + case .default: |
| 40 | + // The default format is JSON. |
| 41 | + fallthrough |
| 42 | + case .json: |
| 43 | + // We cannot use our own JSON encoding wrapper here because that would |
| 44 | + // require it be exported with (at least) package visibility which would |
| 45 | + // create a visible external dependency on Foundation in the main testing |
| 46 | + // library target. |
| 47 | + data = try JSONEncoder().encode(attachableValue) |
| 48 | + } |
| 49 | + |
| 50 | + return try data.withUnsafeBytes(body) |
| 51 | +} |
| 52 | + |
| 53 | +// Implement the protocol requirements generically for any encodable value by |
| 54 | +// encoding to JSON. This lets developers provide trivial conformance to the |
| 55 | +// protocol for types that already support Codable. |
| 56 | +@_spi(Experimental) |
| 57 | +extension Attachable where Self: Encodable { |
| 58 | + /// Encode this value into a buffer using either [`PropertyListEncoder`](https://developer.apple.com/documentation/foundation/propertylistencoder) |
| 59 | + /// or [`JSONEncoder`](https://developer.apple.com/documentation/foundation/jsonencoder), |
| 60 | + /// then call a function and pass that buffer to it. |
| 61 | + /// |
| 62 | + /// - Parameters: |
| 63 | + /// - attachment: The attachment that is requesting a buffer (that is, the |
| 64 | + /// attachment containing this instance.) |
| 65 | + /// - body: A function to call. A temporary buffer containing a data |
| 66 | + /// representation of this instance is passed to it. |
| 67 | + /// |
| 68 | + /// - Returns: Whatever is returned by `body`. |
| 69 | + /// |
| 70 | + /// - Throws: Whatever is thrown by `body`, or any error that prevented the |
| 71 | + /// creation of the buffer. |
| 72 | + /// |
| 73 | + /// The testing library uses this function when writing an attachment to a |
| 74 | + /// test report or to a file on disk. The encoding used depends on the path |
| 75 | + /// extension specified by the value of `attachment`'s ``Testing/Attachment/preferredName`` |
| 76 | + /// property: |
| 77 | + /// |
| 78 | + /// | Extension | Encoding Used | Encoder Used | |
| 79 | + /// |-|-|-| |
| 80 | + /// | `".xml"` | XML property list | [`PropertyListEncoder`](https://developer.apple.com/documentation/foundation/propertylistencoder) | |
| 81 | + /// | `".plist"` | Binary property list | [`PropertyListEncoder`](https://developer.apple.com/documentation/foundation/propertylistencoder) | |
| 82 | + /// | None, `".json"` | JSON | [`JSONEncoder`](https://developer.apple.com/documentation/foundation/jsonencoder) | |
| 83 | + /// |
| 84 | + /// OpenStep-style property lists are not supported. If a value conforms to |
| 85 | + /// _both_ [`Encodable`](https://developer.apple.com/documentation/swift/encodable) |
| 86 | + /// _and_ [`NSSecureCoding`](https://developer.apple.com/documentation/foundation/nssecurecoding), |
| 87 | + /// the default implementation of this function uses the value's conformance |
| 88 | + /// to `Encodable`. |
| 89 | + /// |
| 90 | + /// - Note: On Apple platforms, if the attachment's preferred name includes |
| 91 | + /// some other path extension, that path extension must represent a type |
| 92 | + /// that conforms to [`UTType.propertyList`](https://developer.apple.com/documentation/uniformtypeidentifiers/uttype-swift.struct/propertylist) |
| 93 | + /// or to [`UTType.json`](https://developer.apple.com/documentation/uniformtypeidentifiers/uttype-swift.struct/json). |
| 94 | + public func withUnsafeBufferPointer<R>(for attachment: borrowing Attachment<Self>, _ body: (UnsafeRawBufferPointer) throws -> R) throws -> R { |
| 95 | + try _Testing_Foundation.withUnsafeBufferPointer(encoding: self, for: attachment, body) |
| 96 | + } |
| 97 | +} |
| 98 | +#endif |
0 commit comments