ResultTests.swift 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. //
  2. // ResultTests.swift
  3. //
  4. // Copyright (c) 2014-2016 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 ResultTestCase: BaseTestCase {
  28. let error = AFError.responseValidationFailed(reason: .unacceptableStatusCode(code: 404))
  29. // MARK: - Is Success Tests
  30. func testThatIsSuccessPropertyReturnsTrueForSuccessCase() {
  31. // Given, When
  32. let result = Result<String>.success("success")
  33. // Then
  34. XCTAssertTrue(result.isSuccess, "result is success should be true for success case")
  35. }
  36. func testThatIsSuccessPropertyReturnsFalseForFailureCase() {
  37. // Given, When
  38. let result = Result<String>.failure(error)
  39. // Then
  40. XCTAssertFalse(result.isSuccess, "result is success should be true for failure case")
  41. }
  42. // MARK: - Is Failure Tests
  43. func testThatIsFailurePropertyReturnsFalseForSuccessCase() {
  44. // Given, When
  45. let result = Result<String>.success("success")
  46. // Then
  47. XCTAssertFalse(result.isFailure, "result is failure should be false for success case")
  48. }
  49. func testThatIsFailurePropertyReturnsTrueForFailureCase() {
  50. // Given, When
  51. let result = Result<String>.failure(error)
  52. // Then
  53. XCTAssertTrue(result.isFailure, "result is failure should be true for failure case")
  54. }
  55. // MARK: - Value Tests
  56. func testThatValuePropertyReturnsValueForSuccessCase() {
  57. // Given, When
  58. let result = Result<String>.success("success")
  59. // Then
  60. XCTAssertEqual(result.value ?? "", "success", "result value should match expected value")
  61. }
  62. func testThatValuePropertyReturnsNilForFailureCase() {
  63. // Given, When
  64. let result = Result<String>.failure(error)
  65. // Then
  66. XCTAssertNil(result.value, "result value should be nil for failure case")
  67. }
  68. // MARK: - Error Tests
  69. func testThatErrorPropertyReturnsNilForSuccessCase() {
  70. // Given, When
  71. let result = Result<String>.success("success")
  72. // Then
  73. XCTAssertTrue(result.error == nil, "result error should be nil for success case")
  74. }
  75. func testThatErrorPropertyReturnsErrorForFailureCase() {
  76. // Given, When
  77. let result = Result<String>.failure(error)
  78. // Then
  79. XCTAssertTrue(result.error != nil, "result error should not be nil for failure case")
  80. }
  81. // MARK: - Description Tests
  82. func testThatDescriptionStringMatchesExpectedValueForSuccessCase() {
  83. // Given, When
  84. let result = Result<String>.success("success")
  85. // Then
  86. XCTAssertEqual(result.description, "SUCCESS", "result description should match expected value for success case")
  87. }
  88. func testThatDescriptionStringMatchesExpectedValueForFailureCase() {
  89. // Given, When
  90. let result = Result<String>.failure(error)
  91. // Then
  92. XCTAssertEqual(result.description, "FAILURE", "result description should match expected value for failure case")
  93. }
  94. // MARK: - Debug Description Tests
  95. func testThatDebugDescriptionStringMatchesExpectedValueForSuccessCase() {
  96. // Given, When
  97. let result = Result<String>.success("success value")
  98. // Then
  99. XCTAssertEqual(
  100. result.debugDescription,
  101. "SUCCESS: success value",
  102. "result debug description should match expected value for success case"
  103. )
  104. }
  105. func testThatDebugDescriptionStringMatchesExpectedValueForFailureCase() {
  106. // Given, When
  107. let result = Result<String>.failure(error)
  108. // Then
  109. XCTAssertEqual(
  110. result.debugDescription,
  111. "FAILURE: \(error)",
  112. "result debug description should match expected value for failure case"
  113. )
  114. }
  115. // MARK: - Initializer Tests
  116. func testThatInitializerFromThrowingClosureStoresResultAsASuccess() {
  117. // Given
  118. let value = "success value"
  119. // When
  120. let result1 = Result(value: { value }) // syntax 1
  121. let result2 = Result { value } // syntax 2
  122. // Then
  123. for result in [result1, result2] {
  124. XCTAssertTrue(result.isSuccess)
  125. XCTAssertEqual(result.value, value)
  126. }
  127. }
  128. func testThatInitializerFromThrowingClosureCatchesErrorAsAFailure() {
  129. // Given
  130. struct ResultError: Error {}
  131. // When
  132. let result1 = Result(value: { throw ResultError() }) // syntax 1
  133. let result2 = Result { throw ResultError() } // syntax 2
  134. // Then
  135. for result in [result1, result2] {
  136. XCTAssertTrue(result.isFailure)
  137. XCTAssertTrue(result.error! is ResultError)
  138. }
  139. }
  140. // MARK: - Unwrap Tests
  141. func testThatUnwrapReturnsSuccessValue() {
  142. // Given, When
  143. let result = Result<String>.success("success value")
  144. // Then
  145. XCTAssertEqual(try result.unwrap(), "success value")
  146. }
  147. func testThatUnwrapThrowsFailureError() {
  148. // Given
  149. struct ResultError: Error {}
  150. // When
  151. let result = Result<String>.failure(ResultError())
  152. // Then
  153. do {
  154. _ = try result.unwrap()
  155. XCTFail("result unwrapping should throw the failure error")
  156. } catch {
  157. XCTAssertTrue(error is ResultError)
  158. }
  159. }
  160. // MARK: - Map Tests
  161. func testThatMapTransformsSuccessValue() {
  162. // Given
  163. let result = Result<String>.success("success value")
  164. // When
  165. let mappedResult = result.map { $0.characters.count }
  166. // Then
  167. XCTAssertEqual(mappedResult.value, 13)
  168. }
  169. func testThatMapPreservesFailureError() {
  170. // Given
  171. struct ResultError: Error {}
  172. let result = Result<String>.failure(ResultError())
  173. // When
  174. let mappedResult = result.map { $0.characters.count }
  175. // Then
  176. if let error = mappedResult.error {
  177. XCTAssertTrue(error is ResultError)
  178. } else {
  179. XCTFail("map should preserve the failure error")
  180. }
  181. }
  182. // MARK: - FlatMap Tests
  183. func testThatFlatMapTransformsSuccessValue() {
  184. // Given
  185. let result = Result<String>.success("success value")
  186. // When
  187. let mappedResult = result.flatMap { $0.characters.count }
  188. // Then
  189. XCTAssertEqual(mappedResult.value, 13)
  190. }
  191. func testThatFlatMapCatchesTransformationError() {
  192. // Given, When
  193. struct TransformError: Error {}
  194. let result = Result<String>.success("success value")
  195. // When
  196. let mappedResult = result.flatMap { _ in throw TransformError() }
  197. // Then
  198. if let error = mappedResult.error {
  199. XCTAssertTrue(error is TransformError)
  200. } else {
  201. XCTFail("flatMap should catch the transformation error")
  202. }
  203. }
  204. func testThatFlatMapPreservesFailureError() {
  205. // Given, When
  206. struct ResultError: Error {}
  207. struct TransformError: Error {}
  208. let result = Result<String>.failure(ResultError())
  209. // When
  210. let mappedResult = result.flatMap { _ in throw TransformError() }
  211. // Then
  212. if let error = mappedResult.error {
  213. XCTAssertTrue(error is ResultError)
  214. } else {
  215. XCTFail("flatMap should preserve the failure error")
  216. }
  217. }
  218. }