ResultTests.swift 14 KB

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