@@ -143,15 +143,15 @@ public struct AnyAttachable: AttachableContainer, Copyable, Sendable {
143
143
attachableValue. estimatedAttachmentByteCount
144
144
}
145
145
146
- public func withUnsafeBufferPointer < R> ( for attachment: borrowing Attachment < Self > , _ body: ( UnsafeRawBufferPointer ) throws -> R ) throws -> R {
146
+ public func withUnsafeBytes < R> ( for attachment: borrowing Attachment < Self > , _ body: ( UnsafeRawBufferPointer ) throws -> R ) throws -> R {
147
147
func open< T> ( _ attachableValue: T , for attachment: borrowing Attachment < Self > ) throws -> R where T: Attachable & Sendable & Copyable {
148
148
let temporaryAttachment = Attachment < T > (
149
149
_attachableValue: attachableValue,
150
150
fileSystemPath: attachment. fileSystemPath,
151
151
_preferredName: attachment. _preferredName,
152
152
sourceLocation: attachment. sourceLocation
153
153
)
154
- return try temporaryAttachment. withUnsafeBufferPointer ( body)
154
+ return try temporaryAttachment. withUnsafeBytes ( body)
155
155
}
156
156
return try open ( attachableValue, for: attachment)
157
157
}
@@ -220,25 +220,61 @@ extension Attachment where AttachableValue: AttachableContainer & ~Copyable {
220
220
221
221
#if !SWT_NO_LAZY_ATTACHMENTS
222
222
extension Attachment where AttachableValue: Sendable & Copyable {
223
- /// Attach this instance to the current test.
223
+ /// Attach an attachment to the current test.
224
224
///
225
225
/// - Parameters:
226
+ /// - attachment: The attachment to attach.
226
227
/// - sourceLocation: The source location of the call to this function.
227
228
///
229
+ /// When attaching a value of a type that does not conform to both
230
+ /// [`Sendable`](https://developer.apple.com/documentation/swift/sendable) and
231
+ /// [`Copyable`](https://developer.apple.com/documentation/swift/copyable),
232
+ /// the testing library encodes it as data immediately. If the value cannot be
233
+ /// encoded and an error is thrown, that error is recorded as an issue in the
234
+ /// current test and the attachment is not written to the test report or to
235
+ /// disk.
236
+ ///
228
237
/// An attachment can only be attached once.
229
238
@_documentation ( visibility: private)
230
- public consuming func attach ( sourceLocation: SourceLocation = #_sourceLocation) {
231
- var attachmentCopy = Attachment < AnyAttachable > ( self )
239
+ public static func record ( _ attachment : consuming Self , sourceLocation: SourceLocation = #_sourceLocation) {
240
+ var attachmentCopy = Attachment < AnyAttachable > ( attachment )
232
241
attachmentCopy. sourceLocation = sourceLocation
233
242
Event . post ( . valueAttached( attachmentCopy) )
234
243
}
244
+
245
+ /// Attach a value to the current test.
246
+ ///
247
+ /// - Parameters:
248
+ /// - attachableValue: The value to attach.
249
+ /// - preferredName: The preferred name of the attachment when writing it to
250
+ /// a test report or to disk. If `nil`, the testing library attempts to
251
+ /// derive a reasonable filename for the attached value.
252
+ /// - sourceLocation: The source location of the call to this function.
253
+ ///
254
+ /// When attaching a value of a type that does not conform to both
255
+ /// [`Sendable`](https://developer.apple.com/documentation/swift/sendable) and
256
+ /// [`Copyable`](https://developer.apple.com/documentation/swift/copyable),
257
+ /// the testing library encodes it as data immediately. If the value cannot be
258
+ /// encoded and an error is thrown, that error is recorded as an issue in the
259
+ /// current test and the attachment is not written to the test report or to
260
+ /// disk.
261
+ ///
262
+ /// This function creates a new instance of ``Attachment`` and immediately
263
+ /// attaches it to the current test.
264
+ ///
265
+ /// An attachment can only be attached once.
266
+ @_documentation ( visibility: private)
267
+ public static func record( _ attachableValue: consuming AttachableValue , named preferredName: String ? = nil , sourceLocation: SourceLocation = #_sourceLocation) {
268
+ record ( Self ( attachableValue, named: preferredName) , sourceLocation: sourceLocation)
269
+ }
235
270
}
236
271
#endif
237
272
238
273
extension Attachment where AttachableValue: ~ Copyable {
239
- /// Attach this instance to the current test.
274
+ /// Attach an attachment to the current test.
240
275
///
241
276
/// - Parameters:
277
+ /// - attachment: The attachment to attach.
242
278
/// - sourceLocation: The source location of the call to this function.
243
279
///
244
280
/// When attaching a value of a type that does not conform to both
@@ -250,14 +286,14 @@ extension Attachment where AttachableValue: ~Copyable {
250
286
/// disk.
251
287
///
252
288
/// An attachment can only be attached once.
253
- public consuming func attach ( sourceLocation: SourceLocation = #_sourceLocation) {
289
+ public static func record ( _ attachment : consuming Self , sourceLocation: SourceLocation = #_sourceLocation) {
254
290
do {
255
- let attachmentCopy = try withUnsafeBufferPointer { buffer in
291
+ let attachmentCopy = try attachment . withUnsafeBytes { buffer in
256
292
let attachableContainer = AnyAttachable ( attachableValue: Array ( buffer) )
257
293
return Attachment < AnyAttachable > (
258
294
_attachableValue: attachableContainer,
259
- fileSystemPath: fileSystemPath,
260
- _preferredName: preferredName, // invokes preferredName(for:basedOn:)
295
+ fileSystemPath: attachment . fileSystemPath,
296
+ _preferredName: attachment . preferredName, // invokes preferredName(for:basedOn:)
261
297
sourceLocation: sourceLocation
262
298
)
263
299
}
@@ -267,6 +303,31 @@ extension Attachment where AttachableValue: ~Copyable {
267
303
Issue ( kind: . valueAttachmentFailed( error) , comments: [ ] , sourceContext: sourceContext) . record ( )
268
304
}
269
305
}
306
+
307
+ /// Attach a value to the current test.
308
+ ///
309
+ /// - Parameters:
310
+ /// - attachableValue: The value to attach.
311
+ /// - preferredName: The preferred name of the attachment when writing it to
312
+ /// a test report or to disk. If `nil`, the testing library attempts to
313
+ /// derive a reasonable filename for the attached value.
314
+ /// - sourceLocation: The source location of the call to this function.
315
+ ///
316
+ /// When attaching a value of a type that does not conform to both
317
+ /// [`Sendable`](https://developer.apple.com/documentation/swift/sendable) and
318
+ /// [`Copyable`](https://developer.apple.com/documentation/swift/copyable),
319
+ /// the testing library encodes it as data immediately. If the value cannot be
320
+ /// encoded and an error is thrown, that error is recorded as an issue in the
321
+ /// current test and the attachment is not written to the test report or to
322
+ /// disk.
323
+ ///
324
+ /// This function creates a new instance of ``Attachment`` and immediately
325
+ /// attaches it to the current test.
326
+ ///
327
+ /// An attachment can only be attached once.
328
+ public static func record( _ attachableValue: consuming AttachableValue , named preferredName: String ? = nil , sourceLocation: SourceLocation = #_sourceLocation) {
329
+ record ( Self ( attachableValue, named: preferredName) , sourceLocation: sourceLocation)
330
+ }
270
331
}
271
332
272
333
// MARK: - Getting the serialized form of an attachable value (generically)
@@ -286,10 +347,10 @@ extension Attachment where AttachableValue: ~Copyable {
286
347
///
287
348
/// The testing library uses this function when writing an attachment to a
288
349
/// test report or to a file on disk. This function calls the
289
- /// ``Attachable/withUnsafeBufferPointer (for:_:)`` function on this
290
- /// attachment's ``attachableValue-2tnj5`` property.
291
- @inlinable public borrowing func withUnsafeBufferPointer < R> ( _ body: ( UnsafeRawBufferPointer ) throws -> R ) throws -> R {
292
- try attachableValue. withUnsafeBufferPointer ( for: self , body)
350
+ /// ``Attachable/withUnsafeBytes (for:_:)`` function on this attachment's
351
+ /// ``attachableValue-2tnj5`` property.
352
+ @inlinable public borrowing func withUnsafeBytes < R> ( _ body: ( UnsafeRawBufferPointer ) throws -> R ) throws -> R {
353
+ try attachableValue. withUnsafeBytes ( for: self , body)
293
354
}
294
355
}
295
356
@@ -391,7 +452,7 @@ extension Attachment where AttachableValue: ~Copyable {
391
452
392
453
// There should be no code path that leads to this call where the attachable
393
454
// value is nil.
394
- try withUnsafeBufferPointer { buffer in
455
+ try withUnsafeBytes { buffer in
395
456
try file!. write ( buffer)
396
457
}
397
458
0 commit comments