|
14 | 14 | * limitations under the License.
|
15 | 15 | */
|
16 | 16 | #if compiler(>=5.5)
|
17 |
| -import XCTest |
| 17 | + import XCTest |
18 | 18 |
|
19 |
| -extension XCTestCase { |
20 |
| - @available(macOS 12, iOS 15, tvOS 15, watchOS 8, *) |
21 |
| - /// Cross-platform XCTest support for async-await tests. |
22 |
| - /// |
23 |
| - /// Currently the Linux implementation of XCTest doesn't have async-await support. |
24 |
| - /// Until it does, we make use of this shim which uses a detached `Task` along with |
25 |
| - /// `XCTest.wait(for:timeout:)` to wrap the operation. |
26 |
| - /// |
27 |
| - /// - NOTE: Support for Linux is tracked by https://bugs.swift.org/browse/SR-14403. |
28 |
| - /// - NOTE: Implementation currently in progress: https://github.com/apple/swift-corelibs-xctest/pull/326 |
29 |
| - func XCTAsyncTest( |
30 |
| - expectationDescription: String = "Async operation", |
31 |
| - timeout: TimeInterval = 30, |
32 |
| - file: StaticString = #filePath, |
33 |
| - line: UInt = #line, |
34 |
| - function: StaticString = #function, |
35 |
| - operation: @escaping () async throws -> Void |
36 |
| - ) { |
37 |
| - let expectation = self.expectation(description: expectationDescription) |
38 |
| - Task { |
39 |
| - do { |
40 |
| - try await operation() |
41 |
| - } catch { |
42 |
| - XCTFail("Error thrown while executing \(function): \(error)", file: file, line: line) |
43 |
| - Thread.callStackSymbols.forEach { print($0) } |
44 |
| - } |
45 |
| - expectation.fulfill() |
| 19 | + extension XCTestCase { |
| 20 | + @available(macOS 12, iOS 15, tvOS 15, watchOS 8, *) |
| 21 | + /// Cross-platform XCTest support for async-await tests. |
| 22 | + /// |
| 23 | + /// Currently the Linux implementation of XCTest doesn't have async-await support. |
| 24 | + /// Until it does, we make use of this shim which uses a detached `Task` along with |
| 25 | + /// `XCTest.wait(for:timeout:)` to wrap the operation. |
| 26 | + /// |
| 27 | + /// - NOTE: Support for Linux is tracked by https://bugs.swift.org/browse/SR-14403. |
| 28 | + /// - NOTE: Implementation currently in progress: https://github.com/apple/swift-corelibs-xctest/pull/326 |
| 29 | + func XCTAsyncTest( |
| 30 | + expectationDescription: String = "Async operation", |
| 31 | + timeout: TimeInterval = 30, |
| 32 | + file: StaticString = #filePath, |
| 33 | + line: UInt = #line, |
| 34 | + function: StaticString = #function, |
| 35 | + operation: @escaping () async throws -> Void |
| 36 | + ) { |
| 37 | + let expectation = self.expectation(description: expectationDescription) |
| 38 | + Task { |
| 39 | + do { |
| 40 | + try await operation() |
| 41 | + } catch { |
| 42 | + XCTFail("Error thrown while executing \(function): \(error)", file: file, line: line) |
| 43 | + Thread.callStackSymbols.forEach { print($0) } |
| 44 | + } |
| 45 | + expectation.fulfill() |
| 46 | + } |
| 47 | + self.wait(for: [expectation], timeout: timeout) |
| 48 | + } |
46 | 49 | }
|
47 |
| - self.wait(for: [expectation], timeout: timeout) |
48 |
| - } |
49 |
| -} |
50 | 50 |
|
51 |
| -@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *) |
52 |
| -internal func XCTAssertThrowsError<T>( |
53 |
| - _ expression: @autoclosure () async throws -> T, |
54 |
| - verify: (Error) -> Void = { _ in }, |
55 |
| - file: StaticString = #file, |
56 |
| - line: UInt = #line |
57 |
| -) async { |
58 |
| - do { |
59 |
| - _ = try await expression() |
60 |
| - XCTFail("Expression did not throw error", file: file, line: line) |
61 |
| - } catch { |
62 |
| - verify(error) |
63 |
| - } |
64 |
| -} |
| 51 | + @available(macOS 12, iOS 15, tvOS 15, watchOS 8, *) |
| 52 | + internal func XCTAssertThrowsError<T>( |
| 53 | + _ expression: @autoclosure () async throws -> T, |
| 54 | + verify: (Error) -> Void = { _ in }, |
| 55 | + file: StaticString = #file, |
| 56 | + line: UInt = #line |
| 57 | + ) async { |
| 58 | + do { |
| 59 | + _ = try await expression() |
| 60 | + XCTFail("Expression did not throw error", file: file, line: line) |
| 61 | + } catch { |
| 62 | + verify(error) |
| 63 | + } |
| 64 | + } |
65 | 65 |
|
66 | 66 | #endif // compiler(>=5.5)
|
0 commit comments