|
| 1 | +// |
| 2 | +// |
| 3 | +// Created by Tapash Majumder on 10/26/18. |
| 4 | +// Copyright © 2018 Iterable. All rights reserved. |
| 5 | +// |
| 6 | + |
| 7 | +import XCTest |
| 8 | + |
| 9 | +@testable import IterableSDK |
| 10 | + |
| 11 | +class PromiseTests: XCTestCase { |
| 12 | + struct MyError : Error, CustomStringConvertible { |
| 13 | + let message: String |
| 14 | + |
| 15 | + var description: String { |
| 16 | + return message |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + func testMap() { |
| 21 | + let expectation1 = expectation(description: "test map") |
| 22 | + let expectation2 = expectation(description: "test map, inverted") |
| 23 | + expectation2.isInverted = true |
| 24 | + |
| 25 | + let f1 = createSucessfulFuture(withValue: "zeeString") |
| 26 | + let f2 = f1.map {$0.count} |
| 27 | + |
| 28 | + f2.onSuccess = { (value) in |
| 29 | + XCTAssertEqual(value, "zeeString".count) |
| 30 | + expectation1.fulfill() |
| 31 | + } |
| 32 | + f2.onFailure = {_ in |
| 33 | + expectation2.fulfill() |
| 34 | + } |
| 35 | + |
| 36 | + wait(for: [expectation1], timeout: testExpectationTimeout) |
| 37 | + wait(for: [expectation2], timeout: testExpectationTimeoutForInverted) |
| 38 | + } |
| 39 | + |
| 40 | + func testMapFailure() { |
| 41 | + let expectation1 = expectation(description: "test map failure, inverted") |
| 42 | + expectation1.isInverted = true |
| 43 | + let expectation2 = expectation(description: "test map failure") |
| 44 | + |
| 45 | + let f1: Future<String, MyError> = createFailureFuture(withError: MyError(message: "zeeErrorMessage")) |
| 46 | + let f2 = f1.map {$0.count} |
| 47 | + |
| 48 | + f2.onSuccess = { (value) in |
| 49 | + expectation1.fulfill() |
| 50 | + } |
| 51 | + f2.onFailure = {error in |
| 52 | + XCTAssertEqual(error.message, "zeeErrorMessage") |
| 53 | + expectation2.fulfill() |
| 54 | + } |
| 55 | + |
| 56 | + wait(for: [expectation1], timeout: testExpectationTimeoutForInverted) |
| 57 | + wait(for: [expectation2], timeout: testExpectationTimeout) |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | + func testFlatMap() { |
| 62 | + let expectation1 = expectation(description: "test flatMap") |
| 63 | + let expectation2 = expectation(description: "test flatMap, inverted") |
| 64 | + expectation2.isInverted = true |
| 65 | + |
| 66 | + let f1 = createSucessfulFuture(withValue: "zeeString") |
| 67 | + |
| 68 | + let f2 = f1.flatMap { (firstValue) in |
| 69 | + return self.createSucessfulFuture(withValue: firstValue + firstValue) |
| 70 | + } |
| 71 | + |
| 72 | + f2.onSuccess = { (secondValue) in |
| 73 | + XCTAssertEqual(secondValue, "zeeStringzeeString") |
| 74 | + expectation1.fulfill() |
| 75 | + } |
| 76 | + f2.onFailure = {_ in |
| 77 | + expectation2.fulfill() |
| 78 | + } |
| 79 | + |
| 80 | + wait(for: [expectation1], timeout: testExpectationTimeout) |
| 81 | + wait(for: [expectation2], timeout: testExpectationTimeoutForInverted) |
| 82 | + } |
| 83 | + |
| 84 | + // The first future fails |
| 85 | + func testFlatMapFailure1() { |
| 86 | + let expectation1 = expectation(description: "test flatMap failure, inverted") |
| 87 | + expectation1.isInverted = true |
| 88 | + let expectation2 = expectation(description: "test flatMap failure") |
| 89 | + |
| 90 | + let f1: Future<String, MyError> = createFailureFuture(withError: MyError(message: "zeeErrorMessage")) |
| 91 | + |
| 92 | + let f2 = f1.flatMap { (firstValue) -> Future<String, MyError> in |
| 93 | + return self.createSucessfulFuture(withValue: "zeeString") |
| 94 | + } |
| 95 | + |
| 96 | + f2.onSuccess = { (secondValue) in |
| 97 | + expectation1.fulfill() |
| 98 | + } |
| 99 | + f2.onFailure = {(error) in |
| 100 | + XCTAssertEqual(error.message, "zeeErrorMessage") |
| 101 | + expectation2.fulfill() |
| 102 | + } |
| 103 | + |
| 104 | + wait(for: [expectation1], timeout: testExpectationTimeoutForInverted) |
| 105 | + wait(for: [expectation2], timeout: testExpectationTimeout) |
| 106 | + } |
| 107 | + |
| 108 | + // The second future fails |
| 109 | + func testFlatMapFailure2() { |
| 110 | + let expectation1 = expectation(description: "test flatMap success, inverted") |
| 111 | + expectation1.isInverted = true |
| 112 | + let expectation2 = expectation(description: "test flatMap failure") |
| 113 | + |
| 114 | + let f1 = createSucessfulFuture(withValue: "zeeString") |
| 115 | + |
| 116 | + let f2 = f1.flatMap { (firstValue) -> Future<String, MyError> in |
| 117 | + return self.createFailureFuture(withError: MyError(message: "zeeErrorMessage")) |
| 118 | + } |
| 119 | + |
| 120 | + f2.onSuccess = { (secondValue) in |
| 121 | + expectation1.fulfill() |
| 122 | + } |
| 123 | + f2.onFailure = {(error) in |
| 124 | + XCTAssertEqual(error.message, "zeeErrorMessage") |
| 125 | + expectation2.fulfill() |
| 126 | + } |
| 127 | + |
| 128 | + wait(for: [expectation1], timeout: testExpectationTimeoutForInverted) |
| 129 | + wait(for: [expectation2], timeout: testExpectationTimeout) |
| 130 | + } |
| 131 | + |
| 132 | + func testFutureInitWithSuccess() { |
| 133 | + let expectation1 = expectation(description: "test future init with success") |
| 134 | + let expectation2 = expectation(description: "test future init with success, inverted") |
| 135 | + expectation2.isInverted = true |
| 136 | + |
| 137 | + let f1: Future<String, MyError> = Promise<String, MyError>(value: "zeeValue") |
| 138 | + |
| 139 | + f1.onSuccess = { (value) in |
| 140 | + XCTAssertEqual(value, "zeeValue") |
| 141 | + expectation1.fulfill() |
| 142 | + } |
| 143 | + f1.onFailure = {_ in |
| 144 | + expectation2.fulfill() |
| 145 | + } |
| 146 | + |
| 147 | + wait(for: [expectation1], timeout: testExpectationTimeout) |
| 148 | + wait(for: [expectation2], timeout: testExpectationTimeoutForInverted) |
| 149 | + } |
| 150 | + |
| 151 | + func testFutureInitWithFailure() { |
| 152 | + let expectation1 = expectation(description: "test future init with failure, inverted") |
| 153 | + expectation1.isInverted = true |
| 154 | + let expectation2 = expectation(description: "test future init with failure") |
| 155 | + |
| 156 | + let f1: Future<String, MyError> = Promise<String, MyError>(error: MyError(message: "zeeErrorMessage")) |
| 157 | + |
| 158 | + f1.onSuccess = { (value) in |
| 159 | + expectation1.fulfill() |
| 160 | + } |
| 161 | + f1.onFailure = { error in |
| 162 | + XCTAssertEqual(error.message, "zeeErrorMessage") |
| 163 | + expectation2.fulfill() |
| 164 | + } |
| 165 | + |
| 166 | + wait(for: [expectation1], timeout: testExpectationTimeoutForInverted) |
| 167 | + wait(for: [expectation2], timeout: testExpectationTimeout) |
| 168 | + } |
| 169 | + |
| 170 | + |
| 171 | + private func createSucessfulFuture<T>(withValue value: T) -> Future<T, MyError> { |
| 172 | + let future = Promise<T, MyError>() |
| 173 | + |
| 174 | + DispatchQueue.main.async { |
| 175 | + future.resolve(with: value) |
| 176 | + } |
| 177 | + |
| 178 | + return future |
| 179 | + } |
| 180 | + |
| 181 | + private func createFailureFuture<T>(withError error: MyError) -> Future<T, MyError> { |
| 182 | + let future = Promise<T, MyError>() |
| 183 | + |
| 184 | + DispatchQueue.main.async { |
| 185 | + future.reject(with: error) |
| 186 | + } |
| 187 | + |
| 188 | + return future |
| 189 | + } |
| 190 | +} |
| 191 | + |
0 commit comments