-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathAttachable.swift
More file actions
283 lines (259 loc) · 9.83 KB
/
Attachable.swift
File metadata and controls
283 lines (259 loc) · 9.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors
//
private import _TestingInternals
/// A protocol describing a type whose instances can be recorded and saved as
/// part of a test run.
///
/// To attach an attachable value to a test, pass it to ``Attachment/record(_:named:sourceLocation:)``.
/// To further configure an attachable value before you attach it, use it to
/// initialize an instance of ``Attachment`` and set its properties before
/// passing it to ``Attachment/record(_:sourceLocation:)``.
///
/// The testing library provides default conformances to this protocol for a
/// variety of standard library types. Most user-defined types do not need to
/// conform to this protocol.
///
/// A type should conform to this protocol if it can be represented as a
/// sequence of bytes that would be diagnostically useful if a test fails. If a
/// type cannot conform directly to this protocol (such as a non-final class or
/// a type declared in a third-party module), you can create a wrapper type that
/// conforms to ``AttachableWrapper`` to act as a proxy.
///
/// @Metadata {
/// @Available(Swift, introduced: 6.2)
/// @Available(Xcode, introduced: 26.0)
/// }
public protocol Attachable: ~Copyable {
/// An estimate of the number of bytes of memory needed to store this value as
/// an attachment.
///
/// The testing library uses this property to determine if an attachment
/// should be held in memory or should be immediately saved. Larger
/// attachments are more likely to be saved immediately, but the algorithm the
/// testing library uses is an implementation detail and is subject to change.
///
/// The value of this property is approximately equal to the number of bytes
/// that will actually be needed, or `nil` if the value cannot be computed
/// efficiently. The default implementation of this property returns `nil`.
///
/// - Complexity: O(1) unless `Self` conforms to `Collection`, in which case
/// up to O(_n_) where _n_ is the length of the collection.
///
/// @Metadata {
/// @Available(Swift, introduced: 6.2)
/// @Available(Xcode, introduced: 26.0)
/// }
var estimatedAttachmentByteCount: Int? { get }
/// Call a function and pass a buffer representing this instance to it.
///
/// - Parameters:
/// - attachment: The attachment that is requesting a buffer (that is, the
/// attachment containing this instance.)
/// - body: A function to call. A temporary buffer containing a data
/// representation of this instance is passed to it.
///
/// - Returns: Whatever is returned by `body`.
///
/// - Throws: Whatever is thrown by `body`, or any error that prevented the
/// creation of the buffer.
///
/// The testing library uses this function when saving an attachment. The
/// format of the buffer is implementation-defined, but should be "idiomatic"
/// for this type: for example, if this type represents an image, it would be
/// appropriate for the buffer to contain an image in PNG format, JPEG format,
/// etc., but it would not be idiomatic for the buffer to contain a textual
/// description of the image.
///
/// @Metadata {
/// @Available(Swift, introduced: 6.2)
/// @Available(Xcode, introduced: 26.0)
/// }
borrowing func withUnsafeBytes<R>(for attachment: borrowing Attachment<Self>, _ body: (UnsafeRawBufferPointer) throws -> R) throws -> R
@_lifetime(borrow self)
borrowing func bytes(for attachment: borrowing Attachment<Self>) throws -> RawSpan
/// Generate a preferred name for the given attachment.
///
/// - Parameters:
/// - attachment: The attachment that needs to be named.
/// - suggestedName: A suggested name to use as the basis of the preferred
/// name. This string was provided by the developer when they initialized
/// `attachment`.
///
/// - Returns: The preferred name for `attachment`.
///
/// The testing library uses this function to determine the best name to use
/// when saving `attachment`. The default implementation of this function
/// returns `suggestedName` without any changes.
///
/// @Metadata {
/// @Available(Swift, introduced: 6.2)
/// @Available(Xcode, introduced: 26.0)
/// }
borrowing func preferredName(for attachment: borrowing Attachment<Self>, basedOn suggestedName: String) -> String
}
// MARK: -
/// An error type that is thrown when the testing library cannot satisfy a
/// request for an attachment's bytes.
struct BytesUnavailableError: Error {}
extension BytesUnavailableError: CustomStringConvertible {
var description: String {
"The attachment's content could not be deserialized."
}
}
// MARK: - Default implementations
/// @Metadata {
/// @Available(Swift, introduced: 6.2)
/// @Available(Xcode, introduced: 26.0)
/// }
extension Attachable where Self: ~Copyable {
/// @Metadata {
/// @Available(Swift, introduced: 6.2)
/// @Available(Xcode, introduced: 26.0)
/// }
public var estimatedAttachmentByteCount: Int? {
nil
}
/// @Metadata {
/// @Available(Swift, introduced: 6.2)
/// @Available(Xcode, introduced: 26.0)
/// }
public borrowing func preferredName(for attachment: borrowing Attachment<Self>, basedOn suggestedName: String) -> String {
suggestedName
}
@_lifetime(borrow self)
public borrowing func bytes(for attachment: borrowing Attachment<Self>) throws -> RawSpan {
throw BytesUnavailableError()
}
}
/// @Metadata {
/// @Available(Swift, introduced: 6.2)
/// @Available(Xcode, introduced: 26.0)
/// }
extension Attachable where Self: Collection, Element == UInt8 {
/// @Metadata {
/// @Available(Swift, introduced: 6.2)
/// @Available(Xcode, introduced: 26.0)
/// }
public var estimatedAttachmentByteCount: Int? {
count
}
// We do not provide an implementation of withUnsafeBytes(for:_:) here because
// there is no way in the standard library to statically detect if a
// collection can provide contiguous storage (_HasContiguousBytes is not API.)
// If withContiguousStorageIfAvailable(_:) fails, we don't want to make a
// (potentially expensive!) copy of the collection.
}
/// @Metadata {
/// @Available(Swift, introduced: 6.2)
/// @Available(Xcode, introduced: 26.0)
/// }
extension Attachable where Self: StringProtocol {
/// @Metadata {
/// @Available(Swift, introduced: 6.2)
/// @Available(Xcode, introduced: 26.0)
/// }
public var estimatedAttachmentByteCount: Int? {
// NOTE: utf8.count may be O(n) for foreign strings.
// SEE: https://github.com/swiftlang/swift/blob/main/stdlib/public/core/StringUTF8View.swift
utf8.count
}
/// @Metadata {
/// @Available(Swift, introduced: 6.2)
/// @Available(Xcode, introduced: 26.0)
/// }
public borrowing func preferredName(for attachment: borrowing Attachment<Self>, basedOn suggestedName: String) -> String {
if suggestedName.contains(".") {
return suggestedName
}
return "\(suggestedName).txt"
}
}
// MARK: - Default conformances
// Implement the protocol requirements for byte arrays and buffers so that
// developers can attach raw data when needed.
/// @Metadata {
/// @Available(Swift, introduced: 6.2)
/// @Available(Xcode, introduced: 26.0)
/// }
extension Array<UInt8>: Attachable {
/// @Metadata {
/// @Available(Swift, introduced: 6.2)
/// @Available(Xcode, introduced: 26.0)
/// }
public func withUnsafeBytes<R>(for attachment: borrowing Attachment<Self>, _ body: (UnsafeRawBufferPointer) throws -> R) throws -> R {
try withUnsafeBytes(body)
}
}
/// @Metadata {
/// @Available(Swift, introduced: 6.2)
/// @Available(Xcode, introduced: 26.0)
/// }
extension ContiguousArray<UInt8>: Attachable {
/// @Metadata {
/// @Available(Swift, introduced: 6.2)
/// @Available(Xcode, introduced: 26.0)
/// }
public func withUnsafeBytes<R>(for attachment: borrowing Attachment<Self>, _ body: (UnsafeRawBufferPointer) throws -> R) throws -> R {
try withUnsafeBytes(body)
}
@_lifetime(borrow self)
public borrowing func bytes(for attachment: borrowing Attachment<Self>) throws -> RawSpan {
span.bytes
}
}
/// @Metadata {
/// @Available(Swift, introduced: 6.2)
/// @Available(Xcode, introduced: 26.0)
/// }
extension ArraySlice<UInt8>: Attachable {
/// @Metadata {
/// @Available(Swift, introduced: 6.2)
/// @Available(Xcode, introduced: 26.0)
/// }
public func withUnsafeBytes<R>(for attachment: borrowing Attachment<Self>, _ body: (UnsafeRawBufferPointer) throws -> R) throws -> R {
try withUnsafeBytes(body)
}
@_lifetime(borrow self)
public borrowing func bytes(for attachment: borrowing Attachment<Self>) throws -> RawSpan {
span.bytes
}
}
/// @Metadata {
/// @Available(Swift, introduced: 6.2)
/// @Available(Xcode, introduced: 26.0)
/// }
extension String: Attachable {
/// @Metadata {
/// @Available(Swift, introduced: 6.2)
/// @Available(Xcode, introduced: 26.0)
/// }
public func withUnsafeBytes<R>(for attachment: borrowing Attachment<Self>, _ body: (UnsafeRawBufferPointer) throws -> R) throws -> R {
var selfCopy = self
return try selfCopy.withUTF8 { utf8 in
try body(UnsafeRawBufferPointer(utf8))
}
}
}
/// @Metadata {
/// @Available(Swift, introduced: 6.2)
/// @Available(Xcode, introduced: 26.0)
/// }
extension Substring: Attachable {
/// @Metadata {
/// @Available(Swift, introduced: 6.2)
/// @Available(Xcode, introduced: 26.0)
/// }
public func withUnsafeBytes<R>(for attachment: borrowing Attachment<Self>, _ body: (UnsafeRawBufferPointer) throws -> R) throws -> R {
var selfCopy = self
return try selfCopy.withUTF8 { utf8 in
try body(UnsafeRawBufferPointer(utf8))
}
}
}