|
| 1 | +import XCTest |
| 2 | +@testable import ACInteractor |
| 3 | + |
| 4 | +class MutableInteractorErrorTests: XCTestCase { |
| 5 | + |
| 6 | + var testError: MutableInteractorError? |
| 7 | + var testErrorDict = [String: Any]() |
| 8 | + |
| 9 | + override func setUp() { |
| 10 | + super.setUp() |
| 11 | + // Put setup code here. This method is called before the invocation of each test method in the class. |
| 12 | + |
| 13 | + testError = MutableInteractorError(message: "") |
| 14 | + |
| 15 | + testErrorDict["key1"] = "value1" |
| 16 | + testErrorDict["key2"] = 2 |
| 17 | + } |
| 18 | + |
| 19 | + // MARK: - Init |
| 20 | + |
| 21 | + func testInit_withMessage_setsMessage() { |
| 22 | + // Act |
| 23 | + let error = MutableInteractorError(message: "testMessage") |
| 24 | + |
| 25 | + // Assert |
| 26 | + XCTAssertEqual(error.message, "testMessage") |
| 27 | + } |
| 28 | + |
| 29 | + func testInit_withCode_setsCode() { |
| 30 | + // Act |
| 31 | + let error = MutableInteractorError(message: "", code: 42) |
| 32 | + |
| 33 | + // Assert |
| 34 | + XCTAssertEqual(error.code, 42) |
| 35 | + } |
| 36 | + |
| 37 | + func testInit_withDict_setsDict() { |
| 38 | + // Act |
| 39 | + let error = MutableInteractorError(message: "", code: 0, dict: testErrorDict) |
| 40 | + |
| 41 | + // Assert |
| 42 | + XCTAssertEqual(error.dict["key1"] as? String, "value1") |
| 43 | + XCTAssertEqual(error.dict["key2"] as? Int, 2) |
| 44 | + } |
| 45 | + |
| 46 | + // MARK: - Init with Error |
| 47 | + |
| 48 | + func testInit_withNsError_copiesValuesFromNsError() { |
| 49 | + // Arrange |
| 50 | + let userInfo: [String: Any] = [NSLocalizedDescriptionKey: "localizedTestDescription", "key2": 2] |
| 51 | + let nsError = NSError(domain: "nserror.domain", code: 13, userInfo: userInfo) |
| 52 | + |
| 53 | + // Act |
| 54 | + let error = MutableInteractorError(error: nsError) |
| 55 | + |
| 56 | + // Assert |
| 57 | + XCTAssertEqual(error.message, nsError.message) |
| 58 | + XCTAssertEqual(error.localizedDescription, nsError.localizedDescription) |
| 59 | + |
| 60 | + XCTAssertEqual(error.code, nsError.code) |
| 61 | + XCTAssertEqual(error.domain, nsError.domain) |
| 62 | + |
| 63 | + XCTAssertEqual(error.userInfo[NSLocalizedDescriptionKey] as? String, "localizedTestDescription") |
| 64 | + XCTAssertEqual(error.userInfo["key2"] as? Int, 2) |
| 65 | + } |
| 66 | + |
| 67 | + func testInit_withInteractorError_copiesValuesFromInteractorError() { |
| 68 | + // Arrange |
| 69 | + let interactorError = InteractorError(message: "testMessage", code: 42, dict: testErrorDict) |
| 70 | + |
| 71 | + // Act |
| 72 | + let error = MutableInteractorError(error: interactorError) |
| 73 | + |
| 74 | + // Assert |
| 75 | + XCTAssertEqual(error.message, interactorError.message) |
| 76 | + XCTAssertEqual(error.localizedDescription, interactorError.localizedDescription) |
| 77 | + |
| 78 | + XCTAssertEqual(error.code, interactorError.code) |
| 79 | + XCTAssertEqual(error.domain, interactorError.domain) |
| 80 | + |
| 81 | + XCTAssertEqual(error.dict["key1"] as? String, "value1") |
| 82 | + XCTAssertEqual(error.dict["key2"] as? Int, 2) |
| 83 | + } |
| 84 | + |
| 85 | + // MARK: - Setters |
| 86 | + |
| 87 | + func testSetMessage_setsMessage() { |
| 88 | + // Act |
| 89 | + testError?.message = "newMessage" |
| 90 | + |
| 91 | + // Assert |
| 92 | + XCTAssertEqual(testError?.message, "newMessage") |
| 93 | + } |
| 94 | + |
| 95 | + func testSetCode_setCode() { |
| 96 | + // Act |
| 97 | + testError?.code = 42 |
| 98 | + |
| 99 | + // Assert |
| 100 | + XCTAssertEqual(testError?.code, 42) |
| 101 | + } |
| 102 | + |
| 103 | + func testSetDict_setsDict() { |
| 104 | + // Act |
| 105 | + testError?.dict = testErrorDict |
| 106 | + |
| 107 | + // Assert |
| 108 | + XCTAssertEqual(testError?.dict["key1"] as? String, "value1") |
| 109 | + XCTAssertEqual(testError?.dict["key2"] as? Int, 2) |
| 110 | + } |
| 111 | + |
| 112 | + func testSetDictValue_addsDictValue() { |
| 113 | + // Arrange |
| 114 | + testError?.dict = testErrorDict |
| 115 | + |
| 116 | + // Act |
| 117 | + testError?.dict["newKey"] = "newValue" |
| 118 | + |
| 119 | + // Assert |
| 120 | + XCTAssertEqual(testError?.dict["newKey"] as? String, "newValue") |
| 121 | + XCTAssertEqual(testError?.dict.count, testErrorDict.count + 1) |
| 122 | + } |
| 123 | + |
| 124 | +} |
0 commit comments