Browse Source

[Issue #703] Updated Result Failure to store an ErrorType instead of NSError.

Christian Noon 10 years ago
parent
commit
e489c5fece

+ 2 - 2
Source/Result.swift

@@ -32,7 +32,7 @@ import Foundation
 */
 public enum Result<Value> {
     case Success(Value)
-    case Failure(NSData?, NSError)
+    case Failure(NSData?, ErrorType)
 
     /// Returns `true` if the result is a success, `false` otherwise.
     public var isSuccess: Bool {
@@ -70,7 +70,7 @@ public enum Result<Value> {
     }
 
     /// Returns the associated error value if the result is a failure, `nil` otherwise.
-    public var error: NSError? {
+    public var error: ErrorType? {
         switch self {
         case .Success:
             return nil

+ 1 - 1
Tests/DownloadTests.swift

@@ -360,7 +360,7 @@ class DownloadResumeDataTestCase: BaseTestCase {
 
         XCTAssertTrue(result.isFailure, "result should be a failure")
         XCTAssertNotNil(result.data, "data should not be nil")
-        XCTAssertNotNil(result.error, "error should not be nil")
+        XCTAssertTrue(result.error != nil, "error should not be nil")
 
         XCTAssertNotNil(download.resumeData, "resume data should not be nil")
     }

+ 27 - 27
Tests/ResponseSerializationTests.swift

@@ -40,7 +40,7 @@ class ResponseSerializationTestCase: BaseTestCase {
         XCTAssertTrue(result.isSuccess, "result is success should be true")
         XCTAssertNotNil(result.value, "result value should not be nil")
         XCTAssertNil(result.data, "result data should be nil")
-        XCTAssertNil(result.error, "result error should be nil")
+        XCTAssertTrue(result.error == nil, "result error should be nil")
     }
 
     func testThatDataResponseSerializerFailsWhenDataIsNil() {
@@ -54,9 +54,9 @@ class ResponseSerializationTestCase: BaseTestCase {
         XCTAssertTrue(result.isFailure, "result is failure should be true")
         XCTAssertNil(result.value, "result value should be nil")
         XCTAssertNil(result.data, "result data should be nil")
-        XCTAssertNotNil(result.error, "result error should not be nil")
+        XCTAssertTrue(result.error != nil, "result error should not be nil")
 
-        if let error = result.error {
+        if let error = result.error as? NSError {
             XCTAssertEqual(error.domain, Error.Domain, "error domain should match expected value")
             XCTAssertEqual(error.code, Error.Code.DataSerializationFailed.rawValue, "error code should match expected value")
         } else {
@@ -77,9 +77,9 @@ class ResponseSerializationTestCase: BaseTestCase {
         XCTAssertTrue(result.isFailure, "result is failure should be true")
         XCTAssertNil(result.value, "result value should be nil")
         XCTAssertNil(result.data, "result data should be nil")
-        XCTAssertNotNil(result.error, "result error should not be nil")
+        XCTAssertTrue(result.error != nil, "result error should not be nil")
 
-        if let error = result.error {
+        if let error = result.error as? NSError {
             XCTAssertEqual(error.domain, Error.Domain, "error domain should match expected value")
             XCTAssertEqual(error.code, Error.Code.StringSerializationFailed.rawValue, "error code should match expected value")
         } else {
@@ -98,7 +98,7 @@ class ResponseSerializationTestCase: BaseTestCase {
         XCTAssertTrue(result.isSuccess, "result is success should be true")
         XCTAssertNotNil(result.value, "result value should not be nil")
         XCTAssertNil(result.data, "result data should be nil")
-        XCTAssertNil(result.error, "result error should be nil")
+        XCTAssertTrue(result.error == nil, "result error should be nil")
     }
 
     func testThatStringResponseSerializerSucceedsWithUTF8DataAndNoProvidedEncoding() {
@@ -112,7 +112,7 @@ class ResponseSerializationTestCase: BaseTestCase {
         XCTAssertTrue(result.isSuccess, "result is success should be true")
         XCTAssertNotNil(result.value, "result value should not be nil")
         XCTAssertNil(result.data, "result data should be nil")
-        XCTAssertNil(result.error, "result error should be nil")
+        XCTAssertTrue(result.error == nil, "result error should be nil")
     }
 
     func testThatStringResponseSerializerSucceedsWithUTF8DataAndUTF8ProvidedEncoding() {
@@ -126,7 +126,7 @@ class ResponseSerializationTestCase: BaseTestCase {
         XCTAssertTrue(result.isSuccess, "result is success should be true")
         XCTAssertNotNil(result.value, "result value should not be nil")
         XCTAssertNil(result.data, "result data should be nil")
-        XCTAssertNil(result.error, "result error should be nil")
+        XCTAssertTrue(result.error == nil, "result error should be nil")
     }
 
     func testThatStringResponseSerializerSucceedsWithUTF8DataUsingResponseTextEncodingName() {
@@ -146,7 +146,7 @@ class ResponseSerializationTestCase: BaseTestCase {
         XCTAssertTrue(result.isSuccess, "result is success should be true")
         XCTAssertNotNil(result.value, "result value should not be nil")
         XCTAssertNil(result.data, "result data should be nil")
-        XCTAssertNil(result.error, "result error should be nil")
+        XCTAssertTrue(result.error == nil, "result error should be nil")
     }
 
     func testThatStringResponseSerializerFailsWithUTF32DataAndUTF8ProvidedEncoding() {
@@ -161,9 +161,9 @@ class ResponseSerializationTestCase: BaseTestCase {
         XCTAssertTrue(result.isFailure, "result is failure should be true")
         XCTAssertNil(result.value, "result value should be nil")
         XCTAssertNotNil(result.data, "result data should not be nil")
-        XCTAssertNotNil(result.error, "result error should not be nil")
+        XCTAssertTrue(result.error != nil, "result error should not be nil")
 
-        if let error = result.error {
+        if let error = result.error as? NSError {
             XCTAssertEqual(error.domain, Error.Domain, "error domain should match expected value")
             XCTAssertEqual(error.code, Error.Code.StringSerializationFailed.rawValue, "error code should match expected value")
         } else {
@@ -189,9 +189,9 @@ class ResponseSerializationTestCase: BaseTestCase {
         XCTAssertTrue(result.isFailure, "result is failure should be true")
         XCTAssertNil(result.value, "result value should be nil")
         XCTAssertNotNil(result.data, "result data should not be nil")
-        XCTAssertNotNil(result.error, "result error should not be nil")
+        XCTAssertTrue(result.error != nil, "result error should not be nil")
 
-        if let error = result.error {
+        if let error = result.error as? NSError {
             XCTAssertEqual(error.domain, Error.Domain, "error domain should match expected value")
             XCTAssertEqual(error.code, Error.Code.StringSerializationFailed.rawValue, "error code should match expected value")
         } else {
@@ -212,9 +212,9 @@ class ResponseSerializationTestCase: BaseTestCase {
         XCTAssertTrue(result.isFailure, "result is failure should be true")
         XCTAssertNil(result.value, "result value should be nil")
         XCTAssertNil(result.data, "result data should be nil")
-        XCTAssertNotNil(result.error, "result error should not be nil")
+        XCTAssertTrue(result.error != nil, "result error should not be nil")
 
-        if let error = result.error {
+        if let error = result.error as? NSError {
             XCTAssertEqual(error.domain, Error.Domain, "error domain should match expected value")
             XCTAssertEqual(error.code, Error.Code.JSONSerializationFailed.rawValue, "error code should match expected value")
         } else {
@@ -233,9 +233,9 @@ class ResponseSerializationTestCase: BaseTestCase {
         XCTAssertTrue(result.isFailure, "result is failure should be true")
         XCTAssertNil(result.value, "result value should be nil")
         XCTAssertNotNil(result.data, "result data should not be nil")
-        XCTAssertNotNil(result.error, "result error should not be nil")
+        XCTAssertTrue(result.error != nil, "result error should not be nil")
 
-        if let error = result.error {
+        if let error = result.error as? NSError {
             XCTAssertEqual(error.domain, NSCocoaErrorDomain, "error domain should match expected value")
             XCTAssertEqual(error.code, 3840, "error code should match expected value")
         } else {
@@ -255,7 +255,7 @@ class ResponseSerializationTestCase: BaseTestCase {
         XCTAssertTrue(result.isSuccess, "result is success should be true")
         XCTAssertNotNil(result.value, "result value should not be nil")
         XCTAssertNil(result.data, "result data should be nil")
-        XCTAssertNil(result.error, "result error should be nil")
+        XCTAssertTrue(result.error == nil, "result error should be nil")
     }
 
     func testThatJSONResponseSerializerFailsWhenDataIsInvalidJSON() {
@@ -270,9 +270,9 @@ class ResponseSerializationTestCase: BaseTestCase {
         XCTAssertTrue(result.isFailure, "result is failure should be true")
         XCTAssertNil(result.value, "result value should be nil")
         XCTAssertNotNil(result.data, "result data should not be nil")
-        XCTAssertNotNil(result.error, "result error should not be nil")
+        XCTAssertTrue(result.error != nil, "result error should not be nil")
 
-        if let error = result.error {
+        if let error = result.error as? NSError {
             XCTAssertEqual(error.domain, NSCocoaErrorDomain, "error domain should match expected value")
             XCTAssertEqual(error.code, 3840, "error code should match expected value")
         } else {
@@ -293,9 +293,9 @@ class ResponseSerializationTestCase: BaseTestCase {
         XCTAssertTrue(result.isFailure, "result is failure should be true")
         XCTAssertNil(result.value, "result value should be nil")
         XCTAssertNil(result.data, "result data should be nil")
-        XCTAssertNotNil(result.error, "result error should not be nil")
+        XCTAssertTrue(result.error != nil, "result error should not be nil")
 
-        if let error = result.error {
+        if let error = result.error as? NSError {
             XCTAssertEqual(error.domain, Error.Domain, "error domain should match expected value")
             XCTAssertEqual(error.code, Error.Code.PropertyListSerializationFailed.rawValue, "error code should match expected value")
         } else {
@@ -314,9 +314,9 @@ class ResponseSerializationTestCase: BaseTestCase {
         XCTAssertTrue(result.isFailure, "result is failure should be true")
         XCTAssertNil(result.value, "result value should be nil")
         XCTAssertNotNil(result.data, "result data should not be nil")
-        XCTAssertNotNil(result.error, "result error should not be nil")
+        XCTAssertTrue(result.error != nil, "result error should not be nil")
 
-        if let error = result.error {
+        if let error = result.error as? NSError {
             XCTAssertEqual(error.domain, NSCocoaErrorDomain, "error domain should match expected value")
             XCTAssertEqual(error.code, 3840, "error code should match expected value")
         } else {
@@ -336,7 +336,7 @@ class ResponseSerializationTestCase: BaseTestCase {
         XCTAssertTrue(result.isSuccess, "result is success should be true")
         XCTAssertNotNil(result.value, "result value should not be nil")
         XCTAssertNil(result.data, "result data should be nil")
-        XCTAssertNil(result.error, "result error should be nil")
+        XCTAssertTrue(result.error == nil, "result error should be nil")
     }
 
     func testThatPropertyListResponseSerializerFailsWhenDataIsInvalidPropertyListData() {
@@ -351,9 +351,9 @@ class ResponseSerializationTestCase: BaseTestCase {
         XCTAssertTrue(result.isFailure, "result is failure should be true")
         XCTAssertNil(result.value, "result value should be nil")
         XCTAssertNotNil(result.data, "result data should not be nil")
-        XCTAssertNotNil(result.error, "result error should not be nil")
+        XCTAssertTrue(result.error != nil, "result error should not be nil")
 
-        if let error = result.error {
+        if let error = result.error as? NSError {
             XCTAssertEqual(error.domain, NSCocoaErrorDomain, "error domain should match expected value")
             XCTAssertEqual(error.code, 3840, "error code should match expected value")
         } else {

+ 6 - 6
Tests/ResponseTests.swift

@@ -52,7 +52,7 @@ class ResponseDataTestCase: BaseTestCase {
         XCTAssertTrue(result.isSuccess, "result should be success")
         XCTAssertNotNil(result.value, "result value should not be nil")
         XCTAssertNil(result.data, "result data should be nil")
-        XCTAssertNil(result.error, "result error should be nil")
+        XCTAssertTrue(result.error == nil, "result error should be nil")
     }
 
     func testThatResponseDataReturnsFailureResultWithOptionalDataAndError() {
@@ -82,7 +82,7 @@ class ResponseDataTestCase: BaseTestCase {
         XCTAssertTrue(result.isFailure, "result should be a failure")
         XCTAssertNil(result.value, "result value should not be nil")
         XCTAssertNotNil(result.data, "result data should be nil")
-        XCTAssertNotNil(result.error, "result error should be nil")
+        XCTAssertTrue(result.error != nil, "result error should not be nil")
     }
 }
 
@@ -116,7 +116,7 @@ class ResponseStringTestCase: BaseTestCase {
         XCTAssertTrue(result.isSuccess, "result should be success")
         XCTAssertNotNil(result.value, "result value should not be nil")
         XCTAssertNil(result.data, "result data should be nil")
-        XCTAssertNil(result.error, "result error should be nil")
+        XCTAssertTrue(result.error == nil, "result error should be nil")
     }
 
     func testThatResponseStringReturnsFailureResultWithOptionalDataAndError() {
@@ -146,7 +146,7 @@ class ResponseStringTestCase: BaseTestCase {
         XCTAssertTrue(result.isFailure, "result should be a failure")
         XCTAssertNil(result.value, "result value should not be nil")
         XCTAssertNotNil(result.data, "result data should be nil")
-        XCTAssertNotNil(result.error, "result error should be nil")
+        XCTAssertTrue(result.error != nil, "result error should not be nil")
     }
 }
 
@@ -180,7 +180,7 @@ class ResponseJSONTestCase: BaseTestCase {
         XCTAssertTrue(result.isSuccess, "result should be success")
         XCTAssertNotNil(result.value, "result value should not be nil")
         XCTAssertNil(result.data, "result data should be nil")
-        XCTAssertNil(result.error, "result error should be nil")
+        XCTAssertTrue(result.error == nil, "result error should be nil")
     }
 
     func testThatResponseStringReturnsFailureResultWithOptionalDataAndError() {
@@ -210,7 +210,7 @@ class ResponseJSONTestCase: BaseTestCase {
         XCTAssertTrue(result.isFailure, "result should be a failure")
         XCTAssertNil(result.value, "result value should not be nil")
         XCTAssertNotNil(result.data, "result data should be nil")
-        XCTAssertNotNil(result.error, "result error should be nil")
+        XCTAssertTrue(result.error != nil, "result error should not be nil")
     }
 
     func testThatResponseJSONReturnsSuccessResultForGETRequest() {

+ 2 - 2
Tests/ResultTests.swift

@@ -117,7 +117,7 @@ class ResultTestCase: BaseTestCase {
         let result = Result.Success("success")
 
         // Then
-        XCTAssertNil(result.error, "result error should be nil for success case")
+        XCTAssertTrue(result.error == nil, "result error should be nil for success case")
     }
 
     func testThatErrorPropertyReturnsErrorForFailureCase() {
@@ -126,7 +126,7 @@ class ResultTestCase: BaseTestCase {
         let result = Result<String>.Failure(nil, error)
 
         // Then
-        XCTAssertNotNil(result.error, "result error should not be nil for failure case")
+        XCTAssertTrue(result.error != nil, "result error should not be nil for failure case")
     }
 
     // MARK: - Description Tests