@@ -21,22 +21,29 @@ extension Test {
21
21
/// instance of ``Test/Attachment`` with that value and, optionally, a
22
22
/// preferred filename to use when writing to disk.
23
23
public struct Attachment : Sendable {
24
+ #if !SWT_NO_LAZY_ATTACHMENTS
25
+ /// Storage for ``attachableValue``.
26
+ private var _attachableValue : any Attachable & Sendable /* & Copyable rdar://137614425 */
27
+
24
28
/// The value of this attachment.
25
29
///
26
30
/// The type of this property's value may not match the type of the value
27
31
/// originally used to create this attachment.
28
- public var attachableValue : any Attachable & Sendable /* & Copyable rdar://137614425 */
32
+ public var attachableValue : any Attachable & Sendable /* & Copyable rdar://137614425 */ {
33
+ _attachableValue
34
+ }
35
+ #else
36
+ /// Storage for ``attachableValue``.
37
+ private var _attachableValue : _AttachableProxy
29
38
30
- /// The source location where the attachment was initialized .
39
+ /// The value of this attachment.
31
40
///
32
- /// The value of this property is used when recording issues associated with
33
- /// the attachment.
34
- public var sourceLocation : SourceLocation
35
-
36
- /// The default preferred name to use if the developer does not supply one.
37
- package static var defaultPreferredName : String {
38
- " untitled "
41
+ /// The type of this property's value may not match the type of the value
42
+ /// originally used to create this attachment.
43
+ public var attachableValue : some Test . Attachable & Sendable & Copyable {
44
+ _attachableValue
39
45
}
46
+ #endif
40
47
41
48
/// The path to which the this attachment was written, if any.
42
49
///
@@ -51,26 +58,9 @@ extension Test {
51
58
@_spi ( ForToolsIntegrationOnly)
52
59
public var fileSystemPath : String ?
53
60
54
- /// Initialize an instance of this type that encloses the given attachable
55
- /// value.
56
- ///
57
- /// - Parameters:
58
- /// - attachableValue: The value that will be attached to the output of
59
- /// the test run.
60
- /// - preferredName: The preferred name of the attachment when writing it
61
- /// to a test report or to disk. If `nil`, the testing library attempts
62
- /// to derive a reasonable filename for the attached value.
63
- /// - sourceLocation: The source location of the call to this initializer.
64
- /// This value is used when recording issues associated with the
65
- /// attachment.
66
- public init (
67
- _ attachableValue: some Attachable & Sendable & Copyable ,
68
- named preferredName: String ? = nil ,
69
- sourceLocation: SourceLocation = #_sourceLocation
70
- ) {
71
- self . attachableValue = attachableValue
72
- self . preferredName = preferredName ?? Self . defaultPreferredName
73
- self . sourceLocation = sourceLocation
61
+ /// The default preferred name to use if the developer does not supply one.
62
+ package static var defaultPreferredName : String {
63
+ " untitled "
74
64
}
75
65
76
66
/// A filename to use when writing this attachment to a test report or to a
@@ -81,12 +71,41 @@ extension Test {
81
71
/// value of this property has not been explicitly set, the testing library
82
72
/// will attempt to generate its own value.
83
73
public var preferredName : String
74
+
75
+ /// The source location where the attachment was initialized.
76
+ ///
77
+ /// The value of this property is used when recording issues associated with
78
+ /// the attachment.
79
+ public var sourceLocation : SourceLocation
84
80
}
85
81
}
86
82
87
83
// MARK: -
88
84
89
85
extension Test . Attachment {
86
+ #if !SWT_NO_LAZY_ATTACHMENTS
87
+ /// Initialize an instance of this type that encloses the given attachable
88
+ /// value.
89
+ ///
90
+ /// - Parameters:
91
+ /// - attachableValue: The value that will be attached to the output of
92
+ /// the test run.
93
+ /// - preferredName: The preferred name of the attachment when writing it
94
+ /// to a test report or to disk. If `nil`, the testing library attempts
95
+ /// to derive a reasonable filename for the attached value.
96
+ /// - sourceLocation: The source location of the call to this initializer.
97
+ /// This value is used when recording issues associated with the
98
+ /// attachment.
99
+ public init (
100
+ _ attachableValue: some Test . Attachable & Sendable & Copyable ,
101
+ named preferredName: String ? = nil ,
102
+ sourceLocation: SourceLocation = #_sourceLocation
103
+ ) {
104
+ let preferredName = preferredName ?? Self . defaultPreferredName
105
+ self . init ( _attachableValue: attachableValue, preferredName: preferredName, sourceLocation: sourceLocation)
106
+ }
107
+ #endif
108
+
90
109
/// Attach this instance to the current test.
91
110
///
92
111
/// An attachment can only be attached once.
@@ -129,19 +148,22 @@ extension Test.Attachment {
129
148
/// value cannot be encoded and an error is thrown, that error is recorded as
130
149
/// an issue in the current test and the resulting instance of
131
150
/// ``Test/Attachment`` is empty.
151
+ #if !SWT_NO_LAZY_ATTACHMENTS
132
152
@_disfavoredOverload
153
+ #endif
133
154
public init (
134
155
_ attachableValue: borrowing some Test . Attachable & ~ Copyable,
135
156
named preferredName: String ? = nil ,
136
157
sourceLocation: SourceLocation = #_sourceLocation
137
158
) {
159
+ let preferredName = preferredName ?? Self . defaultPreferredName
138
160
var proxyAttachable = _AttachableProxy ( )
139
161
proxyAttachable. estimatedAttachmentByteCount = attachableValue. estimatedAttachmentByteCount
140
162
141
163
// BUG: the borrow checker thinks that withErrorRecording() is consuming
142
164
// attachableValue, so get around it with an additional do/catch clause.
143
165
do {
144
- let proxyAttachment = Self ( proxyAttachable, named : preferredName, sourceLocation: sourceLocation)
166
+ let proxyAttachment = Self ( _attachableValue : proxyAttachable, preferredName : preferredName, sourceLocation: sourceLocation)
145
167
proxyAttachable. encodedValue = try attachableValue. withUnsafeBufferPointer ( for: proxyAttachment) { buffer in
146
168
[ UInt8] ( buffer)
147
169
}
@@ -155,7 +177,7 @@ extension Test.Attachment {
155
177
}
156
178
}
157
179
158
- self . init ( proxyAttachable, named : preferredName, sourceLocation: sourceLocation)
180
+ self . init ( _attachableValue : proxyAttachable, preferredName : preferredName, sourceLocation: sourceLocation)
159
181
}
160
182
}
161
183
0 commit comments