ResultTests.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // ResultTests.swift
  2. //
  3. // Copyright (c) 2014–2016 Alamofire Software Foundation (http://alamofire.org/)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. @testable import Alamofire
  23. import Foundation
  24. import XCTest
  25. class ResultTestCase: BaseTestCase {
  26. let error = Error.errorWithCode(.StatusCodeValidationFailed, failureReason: "Status code validation failed")
  27. // MARK: - Is Success Tests
  28. func testThatIsSuccessPropertyReturnsTrueForSuccessCase() {
  29. // Given, When
  30. let result = Result<String, NSError>.Success("success")
  31. // Then
  32. XCTAssertTrue(result.isSuccess, "result is success should be true for success case")
  33. }
  34. func testThatIsSuccessPropertyReturnsFalseForFailureCase() {
  35. // Given, When
  36. let result = Result<String, NSError>.Failure(error)
  37. // Then
  38. XCTAssertFalse(result.isSuccess, "result is success should be true for failure case")
  39. }
  40. // MARK: - Is Failure Tests
  41. func testThatIsFailurePropertyReturnsFalseForSuccessCase() {
  42. // Given, When
  43. let result = Result<String, NSError>.Success("success")
  44. // Then
  45. XCTAssertFalse(result.isFailure, "result is failure should be false for success case")
  46. }
  47. func testThatIsFailurePropertyReturnsTrueForFailureCase() {
  48. // Given, When
  49. let result = Result<String, NSError>.Failure(error)
  50. // Then
  51. XCTAssertTrue(result.isFailure, "result is failure should be true for failure case")
  52. }
  53. // MARK: - Value Tests
  54. func testThatValuePropertyReturnsValueForSuccessCase() {
  55. // Given, When
  56. let result = Result<String, NSError>.Success("success")
  57. // Then
  58. XCTAssertEqual(result.value ?? "", "success", "result value should match expected value")
  59. }
  60. func testThatValuePropertyReturnsNilForFailureCase() {
  61. // Given, When
  62. let result = Result<String, NSError>.Failure(error)
  63. // Then
  64. XCTAssertNil(result.value, "result value should be nil for failure case")
  65. }
  66. // MARK: - Error Tests
  67. func testThatErrorPropertyReturnsNilForSuccessCase() {
  68. // Given, When
  69. let result = Result<String, NSError>.Success("success")
  70. // Then
  71. XCTAssertTrue(result.error == nil, "result error should be nil for success case")
  72. }
  73. func testThatErrorPropertyReturnsErrorForFailureCase() {
  74. // Given, When
  75. let result = Result<String, NSError>.Failure(error)
  76. // Then
  77. XCTAssertTrue(result.error != nil, "result error should not be nil for failure case")
  78. }
  79. // MARK: - Description Tests
  80. func testThatDescriptionStringMatchesExpectedValueForSuccessCase() {
  81. // Given, When
  82. let result = Result<String, NSError>.Success("success")
  83. // Then
  84. XCTAssertEqual(result.description, "SUCCESS", "result description should match expected value for success case")
  85. }
  86. func testThatDescriptionStringMatchesExpectedValueForFailureCase() {
  87. // Given, When
  88. let result = Result<String, NSError>.Failure(error)
  89. // Then
  90. XCTAssertEqual(result.description, "FAILURE", "result description should match expected value for failure case")
  91. }
  92. // MARK: - Debug Description Tests
  93. func testThatDebugDescriptionStringMatchesExpectedValueForSuccessCase() {
  94. // Given, When
  95. let result = Result<String, NSError>.Success("success value")
  96. // Then
  97. XCTAssertEqual(
  98. result.debugDescription,
  99. "SUCCESS: success value",
  100. "result debug description should match expected value for success case"
  101. )
  102. }
  103. func testThatDebugDescriptionStringMatchesExpectedValueForFailureCase() {
  104. // Given, When
  105. let result = Result<String, NSError>.Failure(error)
  106. // Then
  107. XCTAssertEqual(
  108. result.debugDescription,
  109. "FAILURE: \(error)",
  110. "result debug description should match expected value for failure case"
  111. )
  112. }
  113. }