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