AFResultTests.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //
  2. // AFResultTests.swift
  3. //
  4. // Copyright (c) 2014-2018 Alamofire Software Foundation (http://alamofire.org/)
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. //
  24. @testable import Alamofire
  25. import Foundation
  26. import XCTest
  27. class AFResultTestCase: BaseTestCase {
  28. let error = AFError.responseValidationFailed(reason: .unacceptableStatusCode(code: 404))
  29. // MARK: - Value Tests
  30. func testThatValuePropertyReturnsValueForSuccessCase() {
  31. // Given, When
  32. let result = AFResult<String>.success("success")
  33. // Then
  34. XCTAssertEqual(result.value ?? "", "success", "result value should match expected value")
  35. }
  36. func testThatValuePropertyReturnsNilForFailureCase() {
  37. // Given, When
  38. let result = AFResult<String>.failure(error)
  39. // Then
  40. XCTAssertNil(result.value, "result value should be nil for failure case")
  41. }
  42. // MARK: - Error Tests
  43. func testThatErrorPropertyReturnsNilForSuccessCase() {
  44. // Given, When
  45. let result = AFResult<String>.success("success")
  46. // Then
  47. XCTAssertNil(result.error, "result error should be nil for success case")
  48. }
  49. func testThatErrorPropertyReturnsErrorForFailureCase() {
  50. // Given, When
  51. let result = AFResult<String>.failure(error)
  52. // Then
  53. XCTAssertNotNil(result.error, "result error should not be nil for failure case")
  54. }
  55. // MARK: - Initializer Tests
  56. func testThatInitializerFromThrowingClosureStoresResultAsASuccess() {
  57. // Given
  58. let value = "success value"
  59. // When
  60. let result1 = AFResult(catching: { value })
  61. let result2 = AFResult { value }
  62. // Then
  63. for result in [result1, result2] {
  64. XCTAssertTrue(result.isSuccess)
  65. XCTAssertEqual(result.value, value)
  66. }
  67. }
  68. func testThatInitializerFromThrowingClosureCatchesErrorAsAFailure() {
  69. // Given
  70. struct ResultError: Error {}
  71. // When
  72. let result1 = AFResult(catching: { throw ResultError() })
  73. let result2 = AFResult { throw ResultError() }
  74. // Then
  75. for result in [result1, result2] {
  76. XCTAssertTrue(result.isFailure)
  77. XCTAssertTrue(result.error! is ResultError)
  78. }
  79. }
  80. // MARK: - FlatMap Tests
  81. func testThatFlatMapTransformsSuccessValue() {
  82. // Given
  83. let result = AFResult<String>.success("success value")
  84. // When
  85. let mappedResult = result.map { $0.count }
  86. // Then
  87. XCTAssertEqual(mappedResult.value, 13)
  88. }
  89. func testThatFlatMapCatchesTransformationError() {
  90. // Given
  91. struct TransformError: Error {}
  92. let result = AFResult<String>.success("success value")
  93. // When
  94. let mappedResult = result.flatMap { _ in throw TransformError() }
  95. // Then
  96. if let error = mappedResult.error {
  97. XCTAssertTrue(error is TransformError)
  98. } else {
  99. XCTFail("flatMap should catch the transformation error")
  100. }
  101. }
  102. func testThatFlatMapPreservesFailureError() {
  103. // Given
  104. struct ResultError: Error {}
  105. struct TransformError: Error {}
  106. let result = AFResult<String>.failure(ResultError())
  107. // When
  108. let mappedResult = result.flatMap { _ in throw TransformError() }
  109. // Then
  110. if let error = mappedResult.error {
  111. XCTAssertTrue(error is ResultError)
  112. } else {
  113. XCTFail("flatMap should preserve the failure error")
  114. }
  115. }
  116. // MARK: - FlatMapError Tests
  117. func testFlatMapErrorTransformsErrorValue() {
  118. // Given
  119. struct ResultError: Error {}
  120. struct OtherError: Error { let error: Error }
  121. let result: AFResult<String> = .failure(ResultError())
  122. // When
  123. let mappedResult = result.flatMapError { OtherError(error: $0) }
  124. // Then
  125. if let error = mappedResult.error {
  126. XCTAssertTrue(error is OtherError)
  127. } else {
  128. XCTFail("flatMapError should transform error value")
  129. }
  130. }
  131. func testFlatMapErrorCapturesThrownError() {
  132. // Given
  133. struct ResultError: Error {}
  134. struct OtherError: Error {
  135. let error: Error
  136. init(error: Error) throws { throw ThrownError() }
  137. }
  138. struct ThrownError: Error {}
  139. let result: AFResult<String> = .failure(ResultError())
  140. // When
  141. let mappedResult = result.flatMapError { try OtherError(error: $0) }
  142. // Then
  143. if let error = mappedResult.error {
  144. XCTAssertTrue(error is ThrownError)
  145. } else {
  146. XCTFail("flatMapError should capture thrown error value")
  147. }
  148. }
  149. }