|
11 | 11 | private import _Testing_ExperimentalInfrastructure
|
12 | 12 |
|
13 | 13 | extension Event {
|
14 |
| - /// The implementation of ``fallbackEventHandler``. |
| 14 | + /// Attempt to handle an event encoded as JSON as if it had been generated in |
| 15 | + /// the current testing context. |
15 | 16 | ///
|
16 | 17 | /// - Parameters:
|
17 |
| - /// - abi: The ABI version to use for decoding `recordJSON`. |
18 | 18 | /// - recordJSON: The JSON encoding of an event record.
|
| 19 | + /// - abi: The ABI version to use for decoding `recordJSON`. |
19 | 20 | ///
|
20 | 21 | /// - Throws: Any error that prevented handling the encoded record.
|
21 |
| - private static func _fallbackEventHandler<V>(_ abi: V.Type, _ recordJSON: UnsafeRawBufferPointer) throws where V: ABI.Version { |
22 |
| - let record = try JSON.decode(ABI.Record<ABI.CurrentVersion>.self, from: recordJSON) |
| 22 | + /// |
| 23 | + /// - Important: This function only handles a subset of event kinds. |
| 24 | + static func handle<V>(_ recordJSON: UnsafeRawBufferPointer, encodedWith abi: V.Type) throws where V: ABI.Version { |
| 25 | + let record = try JSON.decode(ABI.Record<V>.self, from: recordJSON) |
23 | 26 | guard case let .event(event) = record.kind else {
|
24 | 27 | return
|
25 | 28 | }
|
26 |
| - switch event.kind { |
27 |
| - case .issueRecorded: |
28 |
| - Issue(event)?.record() |
29 |
| - case .valueAttached: |
30 |
| - if let attachment = event.attachment { |
31 |
| - Attachment.record(attachment, sourceLocation: attachment._sourceLocation ?? .__here()) |
32 |
| - } |
33 |
| - default: |
34 |
| - // Not handled here. |
35 |
| - break |
| 29 | + |
| 30 | + lazy var comments: [Comment] = event._comments?.map(Comment.init(rawValue:)) ?? [] |
| 31 | + lazy var sourceContext = SourceContext( |
| 32 | + backtrace: nil, // A backtrace from the child process will have the wrong address space. |
| 33 | + sourceLocation: event._sourceLocation |
| 34 | + ) |
| 35 | + lazy var skipInfo = SkipInfo(comment: comments.first, sourceContext: sourceContext) |
| 36 | + if let issue = Issue(event) { |
| 37 | + issue.record() |
| 38 | + } else if let attachment = event.attachment { |
| 39 | + Attachment.record(attachment, sourceLocation: event._sourceLocation!) |
| 40 | + } else if case .testCancelled = event.kind { |
| 41 | + _ = try? Test.cancel(with: skipInfo) |
| 42 | + } else if case .testCaseCancelled = event.kind { |
| 43 | + _ = try? Test.Case.cancel(with: skipInfo) |
36 | 44 | }
|
37 | 45 | }
|
38 | 46 |
|
| 47 | + /// The implementation of ``fallbackEventHandler``. |
| 48 | + /// |
| 49 | + /// - Parameters: |
| 50 | + /// - abi: The ABI version to use for decoding `recordJSON`. |
| 51 | + /// - recordJSON: The JSON encoding of an event record. |
| 52 | + /// |
| 53 | + /// - Throws: Any error that prevented handling the encoded record. |
| 54 | + private static func _fallbackEventHandler<V>(_ abi: V.Type, _ recordJSON: UnsafeRawBufferPointer) throws where V: ABI.Version { |
| 55 | + try handle(recordJSON, encodedWith: abi) |
| 56 | + } |
| 57 | + |
39 | 58 | /// The fallback event handler to set when Swift Testing is the active testing
|
40 | 59 | /// library.
|
41 | 60 | private static let _fallbackEventHandler: FallbackEventHandler = { recordJSONSchemaVersionNumber, recordJSONBaseAddress, recordJSONByteCount, _ in
|
|
0 commit comments