XCTest+Utilities.swift 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright 2024, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import GRPCCore
  17. import XCTest
  18. func XCTAssertThrowsError<T, E: Error>(
  19. ofType: E.Type,
  20. file: StaticString = #filePath,
  21. line: UInt = #line,
  22. _ expression: @autoclosure () throws -> T,
  23. _ errorHandler: (E) -> Void
  24. ) {
  25. XCTAssertThrowsError(try expression(), file: file, line: line) { error in
  26. guard let error = error as? E else {
  27. return XCTFail("Error had unexpected type '\(type(of: error))'", file: file, line: line)
  28. }
  29. errorHandler(error)
  30. }
  31. }
  32. func XCTAssertDescription(
  33. _ subject: some CustomStringConvertible,
  34. _ expected: String,
  35. file: StaticString = #filePath,
  36. line: UInt = #line
  37. ) {
  38. XCTAssertEqual(String(describing: subject), expected, file: file, line: line)
  39. }
  40. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  41. func XCTUnwrapAsync<T>(_ expression: () async throws -> T?) async throws -> T {
  42. let value = try await expression()
  43. return try XCTUnwrap(value)
  44. }
  45. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  46. func XCTAssertThrowsErrorAsync<T, E: Error>(
  47. ofType: E.Type = E.self,
  48. _ expression: () async throws -> T,
  49. errorHandler: (E) -> Void
  50. ) async {
  51. do {
  52. _ = try await expression()
  53. XCTFail("Expression didn't throw")
  54. } catch let error as E {
  55. errorHandler(error)
  56. } catch {
  57. XCTFail("Error had unexpected type '\(type(of: error))'")
  58. }
  59. }
  60. func XCTAssert<T>(_ value: Any, as type: T.Type, _ verify: (T) throws -> Void) rethrows {
  61. if let value = value as? T {
  62. try verify(value)
  63. } else {
  64. XCTFail("\(value) couldn't be cast to \(T.self)")
  65. }
  66. }
  67. @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
  68. func XCTPoll(
  69. every interval: Duration,
  70. timeLimit: Duration = .seconds(5),
  71. until predicate: () async throws -> Bool
  72. ) async throws {
  73. let becameTrue = try await Task.poll(every: interval, timeLimit: timeLimit, until: predicate)
  74. XCTAssertTrue(becameTrue, "Predicate didn't return true within \(timeLimit)")
  75. }