|
| 1 | +// |
| 2 | +// FutureTests.swift |
| 3 | +// HarmonyTests |
| 4 | +// |
| 5 | +// Created by Joan Martin on 16/6/22. |
| 6 | +// |
| 7 | + |
| 8 | +import XCTest |
| 9 | +import Harmony |
| 10 | +import HarmonyTesting |
| 11 | +import Nimble |
| 12 | + |
| 13 | +class FutureTests: XCTestCase { |
| 14 | + |
| 15 | + override func setUpWithError() throws { |
| 16 | + // Put setup code here. This method is called before the invocation of each test method in the class. |
| 17 | + } |
| 18 | + |
| 19 | + override func tearDownWithError() throws { |
| 20 | + // Put teardown code here. This method is called after the invocation of each test method in the class. |
| 21 | + } |
| 22 | + |
| 23 | + // Success Errors |
| 24 | + func test_future_value_set_on_init() throws { |
| 25 | + // Given |
| 26 | + let anyValue = Int.random(in: Int.min...Int.max) |
| 27 | + |
| 28 | + // When |
| 29 | + let future = Future(anyValue) |
| 30 | + |
| 31 | + // Then |
| 32 | + expect(try future.result.get()) == anyValue |
| 33 | + } |
| 34 | + |
| 35 | + func test_future_value_set_on_set_value() throws { |
| 36 | + // Given |
| 37 | + let anyValue = Int.random(in: Int.min...Int.max) |
| 38 | + let future = Future<Int>() |
| 39 | + |
| 40 | + // When |
| 41 | + future.set(anyValue) |
| 42 | + |
| 43 | + // Then |
| 44 | + expect(try future.result.get()) == anyValue |
| 45 | + } |
| 46 | + |
| 47 | + func test_future_value_set_on_set_value_or_error() throws { |
| 48 | + // Given |
| 49 | + let anyValue = Int.random(in: Int.min...Int.max) |
| 50 | + let future = Future<Int>() |
| 51 | + |
| 52 | + // When |
| 53 | + future.set(value: anyValue, error: nil) |
| 54 | + |
| 55 | + // Then |
| 56 | + expect(try future.result.get()) == anyValue |
| 57 | + } |
| 58 | + |
| 59 | + func test_future_error_set_on_init() throws { |
| 60 | + // Given |
| 61 | + let anyError = CoreError.Unknown() |
| 62 | + |
| 63 | + // When |
| 64 | + let future = Future<Int>(anyError) |
| 65 | + |
| 66 | + // Then |
| 67 | + expect(try future.result.get()).to(throwError(anyError)) |
| 68 | + } |
| 69 | + |
| 70 | + func test_future_error_set_on_set_value() throws { |
| 71 | + // Given |
| 72 | + let anyError = CoreError.Unknown() |
| 73 | + let future = Future<Int>() |
| 74 | + |
| 75 | + // When |
| 76 | + future.set(anyError) |
| 77 | + |
| 78 | + // Then |
| 79 | + expect(try future.result.get()).to(throwError(anyError)) |
| 80 | + } |
| 81 | + |
| 82 | + func test_future_error_set_on_set_value_or_error() throws { |
| 83 | + // Given |
| 84 | + let anyError = CoreError.Unknown() |
| 85 | + |
| 86 | + // When |
| 87 | + let future = Future<Int>() |
| 88 | + future.set(value: nil, error: anyError) |
| 89 | + |
| 90 | + // Then |
| 91 | + expect(try future.result.get()).to(throwError(anyError)) |
| 92 | + } |
| 93 | + |
| 94 | + func test_future_states_on_value_first_then_after() throws { |
| 95 | + // Given |
| 96 | + let anyValue = Int.random(in: Int.min...Int.max) |
| 97 | + let future = Future<Int>() |
| 98 | + |
| 99 | + // When |
| 100 | + expect(future.state) == Future.State.blank |
| 101 | + future.set(value: anyValue, error: nil) |
| 102 | + expect(future.state) == Future.State.waitingThen |
| 103 | + |
| 104 | + // Then |
| 105 | + let expectation = expectation(description: "") |
| 106 | + future.then { val in |
| 107 | + expect(val) == anyValue |
| 108 | + expectation.fulfill() |
| 109 | + } |
| 110 | + waitForExpectations(timeout: 1, handler: nil) |
| 111 | + expect(future.state) == Future.State.sent |
| 112 | + } |
| 113 | + |
| 114 | + func test_future_states_on_then_first_value_after() throws { |
| 115 | + // Given |
| 116 | + let anyValue = Int.random(in: Int.min...Int.max) |
| 117 | + let future = Future<Int>() |
| 118 | + |
| 119 | + // When |
| 120 | + expect(future.state) == Future.State.blank |
| 121 | + future.set(value: anyValue, error: nil) |
| 122 | + expect(future.state) == Future.State.waitingThen |
| 123 | + |
| 124 | + // Then |
| 125 | + let expectation = expectation(description: "") |
| 126 | + future.then { val in |
| 127 | + expect(val) == anyValue |
| 128 | + expectation.fulfill() |
| 129 | + } |
| 130 | + waitForExpectations(timeout: 1, handler: nil) |
| 131 | + expect(future.state) == Future.State.sent |
| 132 | + } |
| 133 | + |
| 134 | + func test_future_states_result_first_value_after() throws { |
| 135 | + // Given |
| 136 | + let anyValue = Int.random(in: Int.min...Int.max) |
| 137 | + let queue = DispatchQueue(label: "") |
| 138 | + let future = Future<Int>() |
| 139 | + let expectation = expectation(description: "") |
| 140 | + |
| 141 | + // When |
| 142 | + expect(future.state) == Future.State.blank |
| 143 | + queue.async { |
| 144 | + expect(future.state) == Future.State.blank |
| 145 | + expect(try? future.result.get()) == anyValue |
| 146 | + expect(future.state) == Future.State.sent |
| 147 | + expectation.fulfill() |
| 148 | + } |
| 149 | + |
| 150 | + // Then |
| 151 | + DispatchQueue.main.asyncAfter(deadline: .now() + 0.25) { |
| 152 | + // TODO: This next line fails the test. Not sure if it should. |
| 153 | + // expect(future.state) == Future.State.waitingContent |
| 154 | + future.set(anyValue) |
| 155 | + } |
| 156 | + waitForExpectations(timeout: 10, handler: nil) |
| 157 | + } |
| 158 | + |
| 159 | + func test_future_states_value_first_result_after() throws { |
| 160 | + // Given |
| 161 | + let anyValue = Int.random(in: Int.min...Int.max) |
| 162 | + let future = Future<Int>() |
| 163 | + |
| 164 | + // When |
| 165 | + expect(future.state) == Future.State.blank |
| 166 | + future.set(anyValue) |
| 167 | + |
| 168 | + // Then |
| 169 | + expect(future.state) == Future.State.waitingThen |
| 170 | + expect(try? future.result.get()) == anyValue |
| 171 | + expect(future.state) == Future.State.sent |
| 172 | + } |
| 173 | +} |
0 commit comments