ResultTests.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. //
  2. // ResultTests.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 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 false 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. XCTAssertNil(result.error, "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. XCTAssertNotNil(result.error, "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 })
  121. let result2 = Result { value }
  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() })
  133. let result2 = Result { throw ResultError() }
  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
  143. let result = Result<String>.success("success value")
  144. // When
  145. let unwrappedValue = try? result.unwrap()
  146. // Then
  147. XCTAssertEqual(unwrappedValue, "success value")
  148. }
  149. func testThatUnwrapThrowsFailureError() {
  150. // Given
  151. struct ResultError: Error {}
  152. // When
  153. let result = Result<String>.failure(ResultError())
  154. // Then
  155. do {
  156. _ = try result.unwrap()
  157. XCTFail("result unwrapping should throw the failure error")
  158. } catch {
  159. XCTAssertTrue(error is ResultError)
  160. }
  161. }
  162. // MARK: - Map Tests
  163. func testThatMapTransformsSuccessValue() {
  164. // Given
  165. let result = Result<String>.success("success value")
  166. // When
  167. let mappedResult = result.map { $0.count }
  168. // Then
  169. XCTAssertEqual(mappedResult.value, 13)
  170. }
  171. func testThatMapPreservesFailureError() {
  172. // Given
  173. struct ResultError: Error {}
  174. let result = Result<String>.failure(ResultError())
  175. // When
  176. let mappedResult = result.map { $0.count }
  177. // Then
  178. if let error = mappedResult.error {
  179. XCTAssertTrue(error is ResultError)
  180. } else {
  181. XCTFail("map should preserve the failure error")
  182. }
  183. }
  184. // MARK: - FlatMap Tests
  185. func testThatFlatMapTransformsSuccessValue() {
  186. // Given
  187. let result = Result<String>.success("success value")
  188. // When
  189. let mappedResult = result.map { $0.count }
  190. // Then
  191. XCTAssertEqual(mappedResult.value, 13)
  192. }
  193. func testThatFlatMapCatchesTransformationError() {
  194. // Given
  195. struct TransformError: Error {}
  196. let result = Result<String>.success("success value")
  197. // When
  198. let mappedResult = result.flatMap { _ in throw TransformError() }
  199. // Then
  200. if let error = mappedResult.error {
  201. XCTAssertTrue(error is TransformError)
  202. } else {
  203. XCTFail("flatMap should catch the transformation error")
  204. }
  205. }
  206. func testThatFlatMapPreservesFailureError() {
  207. // Given
  208. struct ResultError: Error {}
  209. struct TransformError: Error {}
  210. let result = Result<String>.failure(ResultError())
  211. // When
  212. let mappedResult = result.flatMap { _ in throw TransformError() }
  213. // Then
  214. if let error = mappedResult.error {
  215. XCTAssertTrue(error is ResultError)
  216. } else {
  217. XCTFail("flatMap should preserve the failure error")
  218. }
  219. }
  220. // MARK: - Error Mapping Tests
  221. func testMapErrorTransformsErrorValue() {
  222. // Given
  223. struct ResultError: Error {}
  224. struct OtherError: Error { let error: Error }
  225. let result: Result<String> = .failure(ResultError())
  226. // When
  227. let mappedResult = result.mapError { OtherError(error: $0) }
  228. // Then
  229. if let error = mappedResult.error {
  230. XCTAssertTrue(error is OtherError)
  231. } else {
  232. XCTFail("mapError should transform error value")
  233. }
  234. }
  235. func testMapErrorPreservesSuccessError() {
  236. // Given
  237. struct ResultError: Error {}
  238. struct OtherError: Error { let error: Error }
  239. let result: Result<String> = .success("success")
  240. // When
  241. let mappedResult = result.mapError { OtherError(error: $0) }
  242. // Then
  243. XCTAssertEqual(mappedResult.value, "success")
  244. }
  245. func testFlatMapErrorTransformsErrorValue() {
  246. // Given
  247. struct ResultError: Error {}
  248. struct OtherError: Error { let error: Error }
  249. let result: Result<String> = .failure(ResultError())
  250. // When
  251. let mappedResult = result.flatMapError { OtherError(error: $0) }
  252. // Then
  253. if let error = mappedResult.error {
  254. XCTAssertTrue(error is OtherError)
  255. } else {
  256. XCTFail("mapError should transform error value")
  257. }
  258. }
  259. func testFlatMapErrorCapturesThrownError() {
  260. // Given
  261. struct ResultError: Error {}
  262. struct OtherError: Error {
  263. let error: Error
  264. init(error: Error) throws { throw ThrownError() }
  265. }
  266. struct ThrownError: Error {}
  267. let result: Result<String> = .failure(ResultError())
  268. // When
  269. let mappedResult = result.flatMapError { try OtherError(error: $0) }
  270. // Then
  271. if let error = mappedResult.error {
  272. XCTAssertTrue(error is ThrownError)
  273. } else {
  274. XCTFail("mapError should capture thrown error value")
  275. }
  276. }
  277. // MARK: - With Value or Error Tests
  278. func testWithValueExecutesWhenSuccess() {
  279. // Given
  280. let result: Result<String> = .success("success")
  281. var string = "failure"
  282. // When
  283. result.withValue { string = $0 }
  284. // Then
  285. XCTAssertEqual(string, "success")
  286. }
  287. func testWithValueDoesNotExecutesWhenFailure() {
  288. // Given
  289. struct ResultError: Error {}
  290. let result: Result<String> = .failure(ResultError())
  291. var string = "failure"
  292. // When
  293. result.withValue { string = $0 }
  294. // Then
  295. XCTAssertEqual(string, "failure")
  296. }
  297. func testWithErrorExecutesWhenFailure() {
  298. // Given
  299. struct ResultError: Error {}
  300. let result: Result<String> = .failure(ResultError())
  301. var string = "success"
  302. // When
  303. result.withError { string = "\(type(of: $0))" }
  304. // Then
  305. XCTAssertEqual(string, "ResultError")
  306. }
  307. func testWithErrorDoesNotExecuteWhenSuccess() {
  308. // Given
  309. let result: Result<String> = .success("success")
  310. var string = "success"
  311. // When
  312. result.withError { string = "\(type(of: $0))" }
  313. // Then
  314. XCTAssertEqual(string, "success")
  315. }
  316. // MARK: - If Success or Failure Tests
  317. func testIfSuccessExecutesWhenSuccess() {
  318. // Given
  319. let result: Result<String> = .success("success")
  320. var string = "failure"
  321. // When
  322. result.ifSuccess { string = "success" }
  323. // Then
  324. XCTAssertEqual(string, "success")
  325. }
  326. func testIfSuccessDoesNotExecutesWhenFailure() {
  327. // Given
  328. struct ResultError: Error {}
  329. let result: Result<String> = .failure(ResultError())
  330. var string = "failure"
  331. // When
  332. result.ifSuccess { string = "success" }
  333. // Then
  334. XCTAssertEqual(string, "failure")
  335. }
  336. func testIfFailureExecutesWhenFailure() {
  337. // Given
  338. struct ResultError: Error {}
  339. let result: Result<String> = .failure(ResultError())
  340. var string = "success"
  341. // When
  342. result.ifFailure { string = "failure" }
  343. // Then
  344. XCTAssertEqual(string, "failure")
  345. }
  346. func testIfFailureDoesNotExecuteWhenSuccess() {
  347. // Given
  348. let result: Result<String> = .success("success")
  349. var string = "success"
  350. // When
  351. result.ifFailure { string = "failure" }
  352. // Then
  353. XCTAssertEqual(string, "success")
  354. }
  355. // MARK: - Functional Chaining Tests
  356. func testFunctionalMethodsCanBeChained() {
  357. // Given
  358. struct ResultError: Error {}
  359. let result: Result<String> = .success("first")
  360. var string = "first"
  361. var success = false
  362. // When
  363. let endResult = result
  364. .map { _ in "second" }
  365. .flatMap { _ in "third" }
  366. .withValue { if $0 == "third" { string = "fourth" } }
  367. .ifSuccess { success = true }
  368. // Then
  369. XCTAssertEqual(endResult.value, "third")
  370. XCTAssertEqual(string, "fourth")
  371. XCTAssertTrue(success)
  372. }
  373. }