ResultTests.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 = NSError(code: .statusCodeValidationFailed, failureReason: "Status code validation failed")
  29. // MARK: - Is Success Tests
  30. func testThatIsSuccessPropertyReturnsTrueForSuccessCase() {
  31. // Given, When
  32. let result = Result<String, NSError>.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, NSError>.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, NSError>.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, NSError>.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, NSError>.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, NSError>.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, NSError>.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, NSError>.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, NSError>.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, NSError>.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, NSError>.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, NSError>.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. }