ResultTests.swift 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // ResultTests.swift
  2. //
  3. // Copyright (c) 2014–2015 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
  30. // When
  31. let result = Result.Success("success")
  32. // Then
  33. XCTAssertTrue(result.isSuccess, "result is success should be true for success case")
  34. }
  35. func testThatIsSuccessPropertyReturnsFalseForFailureCase() {
  36. // Given
  37. // When
  38. let result = Result<String>.Failure(NSData(), 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
  45. // When
  46. let result = Result.Success("success")
  47. // Then
  48. XCTAssertFalse(result.isFailure, "result is failure should be false for success case")
  49. }
  50. func testThatIsFailurePropertyReturnsTrueForFailureCase() {
  51. // Given
  52. // When
  53. let result = Result<String>.Failure(NSData(), error)
  54. // Then
  55. XCTAssertTrue(result.isFailure, "result is failure should be true for failure case")
  56. }
  57. // MARK: - Value Tests
  58. func testThatValuePropertyReturnsValueForSuccessCase() {
  59. // Given
  60. // When
  61. let result = Result.Success("success")
  62. // Then
  63. XCTAssertEqual(result.value ?? "", "success", "result value should match expected value")
  64. }
  65. func testThatValuePropertyReturnsNilForFailureCase() {
  66. // Given
  67. // When
  68. let result = Result<String>.Failure(NSData(), error)
  69. // Then
  70. XCTAssertNil(result.value, "result value should be nil for failure case")
  71. }
  72. // MARK: - Data Tests
  73. func testThatDataPropertyReturnsNilForSuccessCase() {
  74. // Given
  75. // When
  76. let result = Result.Success("success")
  77. // Then
  78. XCTAssertNil(result.data, "result data should be nil for success case")
  79. }
  80. func testThatDataPropertyReturnsDataForFailureCase() {
  81. // Given
  82. // When
  83. let resultWithData = Result<String>.Failure(NSData(), error)
  84. let resultWithoutData = Result<String>.Failure(nil, error)
  85. // Then
  86. XCTAssertNotNil(resultWithData.data, "result with data should not be nil for failure case")
  87. XCTAssertNil(resultWithoutData.data, "result without data should be nil for failure case")
  88. }
  89. // MARK: - Error Tests
  90. func testThatErrorPropertyReturnsNilForSuccessCase() {
  91. // Given
  92. // When
  93. let result = Result.Success("success")
  94. // Then
  95. XCTAssertNil(result.error, "result error should be nil for success case")
  96. }
  97. func testThatErrorPropertyReturnsErrorForFailureCase() {
  98. // Given
  99. // When
  100. let result = Result<String>.Failure(nil, error)
  101. // Then
  102. XCTAssertNotNil(result.error, "result error should not be nil for failure case")
  103. }
  104. // MARK: - Description Tests
  105. func testThatDescriptionStringMatchesExpectedValueForSuccessCase() {
  106. // Given
  107. // When
  108. let result = Result.Success("success")
  109. // Then
  110. XCTAssertEqual(result.description, "SUCCESS", "result description should match expected value for success case")
  111. }
  112. func testThatDescriptionStringMatchesExpectedValueForFailureCase() {
  113. // Given
  114. // When
  115. let result = Result<String>.Failure(nil, error)
  116. // Then
  117. XCTAssertEqual(result.description, "FAILURE", "result description should match expected value for failure case")
  118. }
  119. // MARK: - Debug Description Tests
  120. func testThatDebugDescriptionStringMatchesExpectedValueForSuccessCase() {
  121. // Given
  122. // When
  123. let result = Result.Success("success value")
  124. // Then
  125. XCTAssertEqual(
  126. result.debugDescription,
  127. "SUCCESS: success value",
  128. "result debug description should match expected value for success case"
  129. )
  130. }
  131. func testThatDebugDescriptionStringMatchesExpectedValueForFailureCase() {
  132. // Given
  133. let utf8Data = "failure value".dataUsingEncoding(NSUTF8StringEncoding)!
  134. let imageData = NSData(contentsOfURL: URLForResource("rainbow", withExtension: "jpg"))!
  135. // When
  136. let resultWithUTF8Data = Result<String>.Failure(utf8Data, error)
  137. let resultWithImageData = Result<String>.Failure(imageData, error)
  138. let resultWithNoData = Result<String>.Failure(nil, error)
  139. // Then
  140. XCTAssertEqual(
  141. resultWithUTF8Data.debugDescription,
  142. "FAILURE: \(error) failure value",
  143. "result debug description should match expected value for failure case"
  144. )
  145. XCTAssertEqual(
  146. resultWithImageData.debugDescription,
  147. "FAILURE with Error: \(error)",
  148. "result debug description should match expected value for failure case"
  149. )
  150. XCTAssertEqual(
  151. resultWithNoData.debugDescription,
  152. "FAILURE with Error: \(error)",
  153. "result debug description should match expected value for failure case"
  154. )
  155. }
  156. }