|
6 | 6 | // See http://swift.org/LICENSE.txt for license information |
7 | 7 | // See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
8 | 8 | // |
9 | | -// |
10 | | -// XCTestCase.ClosureType.swift |
11 | | -// Extension on XCTestCase which encapsulates ClosureType |
12 | | -// |
13 | | - |
14 | | -/// XCTestCase.TeardownBlocksState |
15 | | -/// A class which encapsulates teardown blocks which are registered via the `addTearDownBlock(_block:)` method. |
16 | | -/// Supports async and sync throwing methods |
| 9 | +// XCTestCase.TeardownBlocksState |
| 10 | +// A class which encapsulates teardown blocks which are registered via the `addTearDownBlock(_block:)` method. |
| 11 | +// Supports async and sync throwing methods |
17 | 12 |
|
18 | 13 | extension XCTestCase { |
19 | 14 | final class TeardownBlocksState { |
20 | | - |
| 15 | + |
21 | 16 | private var wasFinalized = false |
22 | 17 | private var blocks: [() throws -> Void] = [] |
23 | | - |
| 18 | + |
24 | 19 | @available(macOS 12, *) |
25 | | - func append(_ block: @Sendable @escaping () async throws -> Void) { |
| 20 | + |
| 21 | + // We don't want to overload append(_:) below because of how Swift will implicitly promote sync closures to async closures, |
| 22 | + // which can unexpectedly change their semantics in difficult to track down ways. |
| 23 | + // |
| 24 | + // Because of this, we chose the unusual decision to forgo overloading (which is a super sweet language feature <3) to prevent this issue from surprising any contributors to corelibs-xctest |
| 25 | + func appendAsync(_ block: @Sendable @escaping () async throws -> Void) { |
26 | 26 | self.append { |
27 | 27 | try awaitUsingExpectation { try await block() } |
28 | 28 | } |
29 | 29 | } |
30 | | - |
| 30 | + |
31 | 31 | func append(_ block: @escaping () throws -> Void) { |
32 | 32 | XCTWaiter.subsystemQueue.sync { |
33 | 33 | precondition(wasFinalized == false, "API violation -- attempting to add a teardown block after teardown blocks have been dequeued") |
34 | | - blocks.append(block) } |
| 34 | + blocks.append(block) |
| 35 | + } |
35 | 36 | } |
36 | 37 |
|
37 | 38 | func finalize() -> [() throws -> Void] { |
|
0 commit comments