@@ -200,7 +200,9 @@ Third-party test content should set the `kind` field to a unique value only used
200
200
by that tool, or used by that tool in collaboration with other compatible tools.
201
201
At runtime, Swift Testing ignores test content records with unrecognized ` kind `
202
202
values. To reserve a new unique ` kind ` value, open a [ GitHub issue] ( https://github.com/swiftlang/swift-testing/issues/new/choose )
203
- against Swift Testing.
203
+ against Swift Testing. The value you reserve does not need to be representable
204
+ as a [ FourCC] ( https://en.wikipedia.org/wiki/FourCC ) value, but it can be helpful
205
+ for debugging purposes.
204
206
205
207
The layout of third-party test content records must be compatible with that of
206
208
` TestContentRecord ` as specified above. Third-party tools are ultimately
@@ -213,3 +215,78 @@ TODO: elaborate further, give examples
213
215
TODO: standardize a mechanism for third parties to produce `Test` instances
214
216
since we don't have a public initializer for the `Test` type.
215
217
-->
218
+
219
+ ## Discovering previously-emitted test content
220
+
221
+ <!--
222
+ TODO: add more detail here about how to set up a package
223
+ -->
224
+
225
+ To add test content discovery support to your package, add a dependency on the
226
+ ` _TestDiscovery ` module in the ` swift-testing ` package (not the copy of Swift
227
+ Testing included with the Swift toolchain or Xcode), then import the module with
228
+ SPI enabled:
229
+
230
+ ``` swift
231
+ @_spi (Experimental) @_spi (ForToolsIntegrationOnly) import _TestDiscovery
232
+ ```
233
+
234
+ > [ !IMPORTANT]
235
+ > Don't add a dependency on the ` swift-testing ` package's ` Testing ` module. If
236
+ > you add a dependency on this module, it will cause you to build and link Swift
237
+ > Testing every time you build your package. You only need the ` _TestDiscovery `
238
+ > module in order to discover your own test content types.
239
+
240
+ After importing ` _TestDiscovery ` , find the type in your module that should be
241
+ discoverable at runtime and add conformance to the ` DiscoverableAsTestContent `
242
+ protocol:
243
+
244
+ ``` swift
245
+ extension FoodTruckDiagnostic : DiscoverableAsTestContent {
246
+ static var testContentKind: UInt32 { /* Your `kind` value here. */ }
247
+ }
248
+ ```
249
+
250
+ This type does not need to be publicly visible. However, if the values produced
251
+ by your accessor functions are members of a public type, you may be able to
252
+ simplify your code by using the same type.
253
+
254
+ If you have defined a custom ` context ` type other than ` UInt ` , you can specify
255
+ it here by setting the associated ` TestContentContext ` type. If you have defined
256
+ a custom ` hint ` type for your accessor functions, you can set
257
+ ` TestContentAccessorHint ` :
258
+
259
+ ``` swift
260
+ extension FoodTruckDiagnostic : DiscoverableAsTestContent {
261
+ static var testContentKind: UInt32 { /* Your `kind` value here. */ }
262
+
263
+ typealias TestContentContext = UnsafePointer <FoodTruck.Name>
264
+ typealias TestContentAccessorHint = String
265
+ }
266
+ ```
267
+
268
+ If you customize ` TestContentContext ` , be aware that the type you specify must
269
+ have the same stride and alignment as ` UInt ` .
270
+
271
+ When you are done configuring your type's protocol conformance, you can then
272
+ enumerate all test content records matching it as instances of
273
+ ` TestContentRecord ` .
274
+
275
+ You can use the ` context ` property to access the ` context ` field of the record
276
+ (as emitted into the test content section). The testing library will
277
+ automatically cast the value of the field to an instance of ` TestContentContext `
278
+ for you.
279
+
280
+ If you find a record you wish to resolve to an instance of your conforming type,
281
+ call its ` load() ` function. ` load() ` calls the record's accessor function and,
282
+ if you have set a hint type, lets you pass an optional instance of that type:
283
+
284
+ ``` swift
285
+ for diagnosticRecord in FoodTruckDiagnostic.allTestContentRecords () {
286
+ if diagnosticRecord.context.pointee == .briansBranMuffins {
287
+ if let diagnostic = diagnosticRecord.load (withHint : " ..." ) {
288
+ diagnostic.run ()
289
+ }
290
+ }
291
+ }
292
+ ```
0 commit comments