EventLoopFuture+Assertions.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright 2019, 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 Foundation
  17. import NIOCore
  18. import XCTest
  19. extension EventLoopFuture where Value: Equatable {
  20. /// Registers a callback which asserts the value promised by this future is equal to
  21. /// the expected value. Causes a test failure if the future returns an error.
  22. ///
  23. /// - Parameters:
  24. /// - expected: The expected value.
  25. /// - expectation: A test expectation to fulfill once the future has completed.
  26. func assertEqual(
  27. _ expected: Value,
  28. fulfill expectation: XCTestExpectation,
  29. file: StaticString = #filePath,
  30. line: UInt = #line
  31. ) {
  32. self.whenComplete { result in
  33. defer {
  34. expectation.fulfill()
  35. }
  36. switch result {
  37. case let .success(actual):
  38. // swiftformat:disable:next redundantParens
  39. XCTAssertEqual(expected, actual, file: (file), line: line)
  40. case let .failure(error):
  41. // swiftformat:disable:next redundantParens
  42. XCTFail("Expecteded '\(expected)' but received error: \(error)", file: (file), line: line)
  43. }
  44. }
  45. }
  46. }
  47. extension EventLoopFuture {
  48. /// Registers a callback which asserts that this future is fulfilled with an error. Causes a test
  49. /// failure if the future is not fulfilled with an error.
  50. ///
  51. /// Callers can additionally verify the error by providing an error handler.
  52. ///
  53. /// - Parameters:
  54. /// - expectation: A test expectation to fulfill once the future has completed.
  55. /// - handler: A block to run additional verification on the error. Defaults to no-op.
  56. func assertError(
  57. fulfill expectation: XCTestExpectation,
  58. file: StaticString = #filePath,
  59. line: UInt = #line,
  60. handler: @escaping (Error) -> Void = { _ in }
  61. ) {
  62. self.whenComplete { result in
  63. defer {
  64. expectation.fulfill()
  65. }
  66. switch result {
  67. case .success:
  68. // swiftformat:disable:next redundantParens
  69. XCTFail("Unexpectedly received \(Value.self), expected an error", file: (file), line: line)
  70. case let .failure(error):
  71. handler(error)
  72. }
  73. }
  74. }
  75. /// Registers a callback which fulfills an expectation when the future succeeds.
  76. ///
  77. /// - Parameter expectation: The expectation to fulfill.
  78. func assertSuccess(
  79. fulfill expectation: XCTestExpectation,
  80. file: StaticString = #filePath,
  81. line: UInt = #line
  82. ) {
  83. self.whenSuccess { _ in
  84. expectation.fulfill()
  85. }
  86. }
  87. }
  88. extension EventLoopFuture {
  89. // TODO: Replace with `always` once https://github.com/apple/swift-nio/pull/981 is released.
  90. func peekError(callback: @escaping (Error) -> Void) -> EventLoopFuture<Value> {
  91. self.whenFailure(callback)
  92. return self
  93. }
  94. // TODO: Replace with `always` once https://github.com/apple/swift-nio/pull/981 is released.
  95. func peek(callback: @escaping (Value) -> Void) -> EventLoopFuture<Value> {
  96. self.whenSuccess(callback)
  97. return self
  98. }
  99. }