|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the AsyncHTTPClient open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2021 Apple Inc. and the AsyncHTTPClient project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of AsyncHTTPClient project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | +/* |
| 15 | + * Copyright 2021, gRPC Authors All rights reserved. |
| 16 | + * |
| 17 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 18 | + * you may not use this file except in compliance with the License. |
| 19 | + * You may obtain a copy of the License at |
| 20 | + * |
| 21 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 22 | + * |
| 23 | + * Unless required by applicable law or agreed to in writing, software |
| 24 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 25 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 26 | + * See the License for the specific language governing permissions and |
| 27 | + * limitations under the License. |
| 28 | + */ |
| 29 | +#if compiler(>=5.5) |
| 30 | +import XCTest |
| 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 @Sendable () 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() |
| 59 | + } |
| 60 | + self.wait(for: [expectation], timeout: timeout) |
| 61 | + } |
| 62 | +} |
| 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 | + } |
| 77 | +} |
| 78 | + |
| 79 | +#endif // compiler(>=5.5) |
0 commit comments