|
| 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 | +@_spi(Experimental) |
| 12 | +extension Test { |
| 13 | + /// A protocol describing a type that can be attached to a test report or |
| 14 | + /// written to disk when a test is run. |
| 15 | + /// |
| 16 | + /// To attach an attachable value to a test report or test run output, use it |
| 17 | + /// to initialize a new instance of ``Test/Attachment``, then call |
| 18 | + /// ``Test/Attachment/attach()``. An attachment can only be attached once. |
| 19 | + /// |
| 20 | + /// Generally speaking, you should not need to make new types conform to this |
| 21 | + /// protocol. |
| 22 | + // TODO: write more about this protocol, how it works, and list conforming |
| 23 | + // types (including discussion of the Foundation cross-import overlay.) |
| 24 | + public protocol Attachable: ~Copyable { |
| 25 | + /// Call a function and pass a buffer representing this instance to it. |
| 26 | + /// |
| 27 | + /// - Parameters: |
| 28 | + /// - attachment: The attachment that is requesting a buffer (that is, the |
| 29 | + /// attachment containing this instance.) |
| 30 | + /// - body: A function to call. A temporary buffer containing a data |
| 31 | + /// representation of this instance is passed to it. |
| 32 | + /// |
| 33 | + /// - Returns: Whatever is returned by `body`. |
| 34 | + /// |
| 35 | + /// - Throws: Whatever is thrown by `body`, or any error that prevented the |
| 36 | + /// creation of the buffer. |
| 37 | + /// |
| 38 | + /// The testing library uses this function when writing an attachment to a |
| 39 | + /// test report or to a file on disk. The format of the buffer is |
| 40 | + /// implementation-defined, but should be "idiomatic" for this type: for |
| 41 | + /// example, if this type represents an image, it would be appropriate for |
| 42 | + /// the buffer to contain an image in PNG format, JPEG format, etc., but it |
| 43 | + /// would not be idiomatic for the buffer to contain a textual description |
| 44 | + /// of the image. |
| 45 | + borrowing func withUnsafeBufferPointer<R>(for attachment: borrowing Test.Attachment, _ body: (UnsafeRawBufferPointer) throws -> R) throws -> R |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +// MARK: - Default implementations |
| 50 | + |
| 51 | +// Implement the protocol requirements for byte arrays and buffers so that |
| 52 | +// developers can attach raw data when needed. |
| 53 | +@_spi(Experimental) |
| 54 | +extension [UInt8]: Test.Attachable { |
| 55 | + public func withUnsafeBufferPointer<R>(for attachment: borrowing Test.Attachment, _ body: (UnsafeRawBufferPointer) throws -> R) throws -> R { |
| 56 | + try withUnsafeBytes(body) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +@_spi(Experimental) |
| 61 | +extension UnsafeBufferPointer<UInt8>: Test.Attachable { |
| 62 | + public func withUnsafeBufferPointer<R>(for attachment: borrowing Test.Attachment, _ body: (UnsafeRawBufferPointer) throws -> R) throws -> R { |
| 63 | + try body(.init(self)) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +@_spi(Experimental) |
| 68 | +extension UnsafeRawBufferPointer: Test.Attachable { |
| 69 | + public func withUnsafeBufferPointer<R>(for attachment: borrowing Test.Attachment, _ body: (UnsafeRawBufferPointer) throws -> R) throws -> R { |
| 70 | + try body(self) |
| 71 | + } |
| 72 | +} |
0 commit comments