Skip to content

Commit fe1c332

Browse files
committed
Added test suite
1 parent 824f562 commit fe1c332

File tree

2 files changed

+190
-56
lines changed

2 files changed

+190
-56
lines changed
Lines changed: 176 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Foundation
22
@testable import MiniPromises
3+
@testable import MiniTasks
34
import Nimble
45
import RxBlocking
56
import RxSwift
@@ -25,47 +26,73 @@ private func equalAction<A: Action & Equatable>(_ by: A) -> Predicate<A> {
2526
final class PrimitiveSequenceTypeTests: XCTestCase {
2627
fileprivate enum Error: Swift.Error { case dummy }
2728

28-
class TestCompletableAction: CompletableAction, Equatable {
29-
typealias Payload = Int
29+
// MARK: - Promises test actions.
3030

31-
let counter: Promise<Payload>
31+
struct PromisesTestCompletableAction: MiniPromises.CompletableAction, Equatable {
32+
typealias Payload = Int
3233

33-
required init(promise: Promise<Payload>) {
34-
counter = promise
35-
}
34+
let promise: Promise<Payload>
3635

37-
static func == (lhs: TestCompletableAction, rhs: TestCompletableAction) -> Bool {
38-
lhs.counter == rhs.counter
36+
static func == (lhs: PromisesTestCompletableAction, rhs: PromisesTestCompletableAction) -> Bool {
37+
lhs.promise == rhs.promise
3938
}
4039
}
4140

42-
class TestKeyedCompletableAction: KeyedCompletableAction, Equatable {
41+
struct PromisesTestKeyedCompletableAction: MiniPromises.KeyedCompletableAction, Equatable {
4342
typealias Payload = Int
4443
typealias Key = String
4544

46-
let counterMap: [Key: Promise<Payload>]
45+
let promise: [Key: Promise<Payload>]
4746

48-
required init(promise: [Key: Promise<Payload>]) {
49-
counterMap = promise
47+
static func == (lhs: PromisesTestKeyedCompletableAction, rhs: PromisesTestKeyedCompletableAction) -> Bool {
48+
lhs.promise == rhs.promise
5049
}
50+
}
51+
52+
struct PromisesTestEmptyAction: MiniPromises.EmptyAction, Equatable {
53+
let promise: Promise<Void>
5154

52-
static func == (lhs: TestKeyedCompletableAction, rhs: TestKeyedCompletableAction) -> Bool {
53-
lhs.counterMap == rhs.counterMap
55+
static func == (_: PromisesTestEmptyAction, _: PromisesTestEmptyAction) -> Bool {
56+
true
5457
}
5558
}
5659

57-
class TestEmptyAction: EmptyAction, Equatable {
58-
let promise: Promise<Void>
60+
// MARK: - Tasks test actions.
61+
62+
struct TasksTestCompletableAction: MiniTasks.CompletableAction, Equatable {
63+
typealias Payload = Int
64+
65+
let task: AnyTask
66+
let payload: Payload?
5967

60-
required init(promise: Promise<Void>) {
61-
self.promise = promise
68+
static func == (lhs: TasksTestCompletableAction, rhs: TasksTestCompletableAction) -> Bool {
69+
lhs.payload == rhs.payload && lhs.task.status == rhs.task.status
6270
}
71+
}
6372

64-
static func == (_: TestEmptyAction, _: TestEmptyAction) -> Bool {
65-
true
73+
struct TasksTestKeyedCompletableAction: MiniTasks.KeyedCompletableAction, Equatable {
74+
typealias Payload = Int
75+
typealias Key = String
76+
77+
let task: AnyTask
78+
let payload: Int?
79+
let key: Key
80+
81+
static func == (lhs: TasksTestKeyedCompletableAction, rhs: TasksTestKeyedCompletableAction) -> Bool {
82+
lhs.key == rhs.key && lhs.payload == rhs.payload && lhs.task.status == rhs.task.status
6683
}
6784
}
6885

86+
struct TasksTestEmptyAction: MiniTasks.EmptyAction, Equatable {
87+
let task: AnyTask
88+
89+
static func == (lhs: TasksTestEmptyAction, rhs: TasksTestEmptyAction) -> Bool {
90+
lhs.task.status == rhs.task.status
91+
}
92+
}
93+
94+
// MARK: - Common
95+
6996
var dispatcher: Dispatcher!
7097
var disposeBag: DisposeBag!
7198
var testMiddleware: TestMiddleware!
@@ -79,99 +106,199 @@ final class PrimitiveSequenceTypeTests: XCTestCase {
79106
dispatcher.add(middleware: testMiddleware)
80107
}
81108

82-
func test_completable_action_dispatch() {
109+
// MARK: - Promises tests
110+
111+
func test_promises_completable_action_dispatch() {
112+
Single
113+
.just(1)
114+
.dispatch(action: PromisesTestCompletableAction.self, on: dispatcher)
115+
.disposed(by: disposeBag)
116+
117+
expect(
118+
self.testMiddleware.actions(of: PromisesTestCompletableAction.self).count
119+
).toEventually(be(1))
120+
}
121+
122+
func test_promises_completable_action_dispatch_error() {
123+
Single
124+
.error(Error.dummy)
125+
.dispatch(action: PromisesTestCompletableAction.self, on: dispatcher)
126+
.disposed(by: disposeBag)
127+
128+
expect(
129+
self.testMiddleware.actions(of: PromisesTestCompletableAction.self).count
130+
).toEventually(be(1))
131+
132+
expect(
133+
self.testMiddleware.action(of: PromisesTestCompletableAction.self) {
134+
$0.promise == .error(Error.dummy)
135+
}
136+
).toEventually(beTrue())
137+
}
138+
139+
func test_promises_keyed_completable_action_dispatch() {
140+
Single
141+
.just(1)
142+
.dispatch(action: PromisesTestKeyedCompletableAction.self, key: "hello", on: dispatcher)
143+
.disposed(by: disposeBag)
144+
145+
expect(
146+
self.testMiddleware
147+
.action(of: PromisesTestKeyedCompletableAction.self) {
148+
$0.promise == ["hello": .value(1)]
149+
}
150+
).toEventually(beTrue())
151+
}
152+
153+
func test_promises_keyed_completable_action_dispatch_error() {
154+
Single
155+
.error(Error.dummy)
156+
.dispatch(action: PromisesTestKeyedCompletableAction.self, key: "hello", on: dispatcher)
157+
.disposed(by: disposeBag)
158+
159+
expect(
160+
self.testMiddleware
161+
.action(of: PromisesTestKeyedCompletableAction.self) {
162+
$0.promise == ["hello": .error(Error.dummy)]
163+
}
164+
).toEventually(beTrue())
165+
}
166+
167+
func test_promises_completable_action_action() throws {
168+
guard let action =
169+
try Single
170+
.just(1)
171+
.action(PromisesTestCompletableAction.self)
172+
.toBlocking(timeout: 5.0).first() else { fatalError() }
173+
174+
expect(action).to(equalAction(PromisesTestCompletableAction(promise: .value(1))))
175+
}
176+
177+
func test_promises_empty_action_dispatch() {
178+
Completable
179+
.empty()
180+
.dispatch(action: PromisesTestEmptyAction.self, on: dispatcher)
181+
.disposed(by: disposeBag)
182+
183+
expect(
184+
self.testMiddleware.actions(of: PromisesTestEmptyAction.self).count
185+
).toEventually(be(1))
186+
187+
expect(self.testMiddleware.actions(of: PromisesTestEmptyAction.self).first?.promise.isResolved) == true
188+
}
189+
190+
func test_promises_empty_action_dispatch_error() {
191+
Completable
192+
.error(Error.dummy)
193+
.dispatch(action: PromisesTestEmptyAction.self, on: dispatcher)
194+
.disposed(by: disposeBag)
195+
196+
expect(
197+
self.testMiddleware.actions(of: PromisesTestEmptyAction.self).count
198+
).toEventually(be(1))
199+
200+
expect(self.testMiddleware.actions(of: PromisesTestEmptyAction.self).first?.promise.isResolved) == true
201+
202+
expect(self.testMiddleware.actions(of: PromisesTestEmptyAction.self).first?.promise.isCompleted) == true
203+
204+
expect(self.testMiddleware.actions(of: PromisesTestEmptyAction.self).first?.promise.isRejected) == true
205+
}
206+
207+
// MARK: - Tasks tests
208+
209+
func test_tasks_completable_action_dispatch() {
83210
Single
84211
.just(1)
85-
.dispatch(action: TestCompletableAction.self, on: dispatcher)
212+
.dispatch(action: TasksTestCompletableAction.self, on: dispatcher)
86213
.disposed(by: disposeBag)
87214

88215
expect(
89-
self.testMiddleware.actions(of: TestCompletableAction.self).count
216+
self.testMiddleware.actions(of: TasksTestCompletableAction.self).count
90217
).toEventually(be(1))
91218
}
92219

93-
func test_completable_action_dispatch_error() {
220+
func test_tasks_completable_action_dispatch_error() {
94221
Single
95222
.error(Error.dummy)
96-
.dispatch(action: TestCompletableAction.self, on: dispatcher)
223+
.dispatch(action: TasksTestCompletableAction.self, on: dispatcher)
97224
.disposed(by: disposeBag)
98225

99226
expect(
100-
self.testMiddleware.actions(of: TestCompletableAction.self).count
227+
self.testMiddleware.actions(of: TasksTestCompletableAction.self).count
101228
).toEventually(be(1))
102229

103230
expect(
104-
self.testMiddleware.action(of: TestCompletableAction.self) {
105-
$0.counter == .error(Error.dummy)
231+
self.testMiddleware.action(of: TasksTestCompletableAction.self) {
232+
$0.task.error as? Error == Error.dummy
106233
}
107234
).toEventually(beTrue())
108235
}
109236

110-
func test_keyed_completable_action_dispatch() {
237+
func test_tasks_keyed_completable_action_dispatch() {
111238
Single
112239
.just(1)
113-
.dispatch(action: TestKeyedCompletableAction.self, key: "hello", on: dispatcher)
240+
.dispatch(action: TasksTestKeyedCompletableAction.self, key: "hello", on: dispatcher)
114241
.disposed(by: disposeBag)
115242

116243
expect(
117244
self.testMiddleware
118-
.action(of: TestKeyedCompletableAction.self) {
119-
$0.counterMap == ["hello": .value(1)]
245+
.action(of: TasksTestKeyedCompletableAction.self) {
246+
$0.payload == 1 && $0.key == "hello" && $0.task.status == .success
120247
}
121248
).toEventually(beTrue())
122249
}
123250

124-
func test_keyed_completable_action_dispatch_error() {
251+
func test_tasks_keyed_completable_action_dispatch_error() {
125252
Single
126253
.error(Error.dummy)
127-
.dispatch(action: TestKeyedCompletableAction.self, key: "hello", on: dispatcher)
254+
.dispatch(action: TasksTestKeyedCompletableAction.self, key: "hello", on: dispatcher)
128255
.disposed(by: disposeBag)
129256

130257
expect(
131258
self.testMiddleware
132-
.action(of: TestKeyedCompletableAction.self) {
133-
$0.counterMap == ["hello": .error(Error.dummy)]
259+
.action(of: TasksTestKeyedCompletableAction.self) {
260+
$0.payload == nil && $0.key == "hello" && $0.task.status == .error
134261
}
135262
).toEventually(beTrue())
136263
}
137264

138-
func test_completable_action_action() throws {
265+
func test_tasks_completable_action_action() throws {
139266
guard let action =
140267
try Single
141268
.just(1)
142-
.action(TestCompletableAction.self)
269+
.action(TasksTestCompletableAction.self)
143270
.toBlocking(timeout: 5.0).first() else { fatalError() }
144271

145-
expect(action).to(equalAction(TestCompletableAction(promise: .value(1))))
272+
expect(action).to(equalAction(TasksTestCompletableAction(task: .success(), payload: 1)))
146273
}
147274

148-
func test_empty_action_dispatch() {
275+
func test_tasks_empty_action_dispatch() {
149276
Completable
150277
.empty()
151-
.dispatch(action: TestEmptyAction.self, on: dispatcher)
278+
.dispatch(action: TasksTestEmptyAction.self, on: dispatcher)
152279
.disposed(by: disposeBag)
153280

154281
expect(
155-
self.testMiddleware.actions(of: TestEmptyAction.self).count
282+
self.testMiddleware.actions(of: TasksTestEmptyAction.self).count
156283
).toEventually(be(1))
157284

158-
expect(self.testMiddleware.actions(of: TestEmptyAction.self).first?.promise.isResolved) == true
285+
expect(self.testMiddleware.actions(of: TasksTestEmptyAction.self).first?.task.isCompleted) == true
159286
}
160287

161-
func test_empty_action_dispatch_error() {
288+
func test_tasks_empty_action_dispatch_error() {
162289
Completable
163290
.error(Error.dummy)
164-
.dispatch(action: TestEmptyAction.self, on: dispatcher)
291+
.dispatch(action: TasksTestEmptyAction.self, on: dispatcher)
165292
.disposed(by: disposeBag)
166293

167294
expect(
168-
self.testMiddleware.actions(of: TestEmptyAction.self).count
295+
self.testMiddleware.actions(of: TasksTestEmptyAction.self).count
169296
).toEventually(be(1))
170297

171-
expect(self.testMiddleware.actions(of: TestEmptyAction.self).first?.promise.isResolved) == true
298+
expect(self.testMiddleware.actions(of: TasksTestEmptyAction.self).first?.task.isRunning) == false
172299

173-
expect(self.testMiddleware.actions(of: TestEmptyAction.self).first?.promise.isCompleted) == true
300+
expect(self.testMiddleware.actions(of: TasksTestEmptyAction.self).first?.task.isCompleted) == true
174301

175-
expect(self.testMiddleware.actions(of: TestEmptyAction.self).first?.promise.isRejected) == true
302+
expect(self.testMiddleware.actions(of: TasksTestEmptyAction.self).first?.task.isFailure) == true
176303
}
177304
}

Tests/MiniSwiftTests/XCTestManifests.swift

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,20 @@
7171
// `swift test --generate-linuxmain`
7272
// to regenerate.
7373
static let __allTests__PrimitiveSequenceTypeTests = [
74-
("test_completable_action_action", test_completable_action_action),
75-
("test_completable_action_dispatch", test_completable_action_dispatch),
76-
("test_completable_action_dispatch_error", test_completable_action_dispatch_error),
77-
("test_empty_action_dispatch", test_empty_action_dispatch),
78-
("test_empty_action_dispatch_error", test_empty_action_dispatch_error),
79-
("test_keyed_completable_action_dispatch", test_keyed_completable_action_dispatch),
80-
("test_keyed_completable_action_dispatch_error", test_keyed_completable_action_dispatch_error),
74+
("test_promises_completable_action_action", test_promises_completable_action_action),
75+
("test_promises_completable_action_dispatch", test_promises_completable_action_dispatch),
76+
("test_promises_completable_action_dispatch_error", test_promises_completable_action_dispatch_error),
77+
("test_promises_empty_action_dispatch", test_promises_empty_action_dispatch),
78+
("test_promises_empty_action_dispatch_error", test_promises_empty_action_dispatch_error),
79+
("test_promises_keyed_completable_action_dispatch", test_promises_keyed_completable_action_dispatch),
80+
("test_promises_keyed_completable_action_dispatch_error", test_promises_keyed_completable_action_dispatch_error),
81+
("test_tasks_completable_action_action", test_tasks_completable_action_action),
82+
("test_tasks_completable_action_dispatch", test_tasks_completable_action_dispatch),
83+
("test_tasks_completable_action_dispatch_error", test_tasks_completable_action_dispatch_error),
84+
("test_tasks_empty_action_dispatch", test_tasks_empty_action_dispatch),
85+
("test_tasks_empty_action_dispatch_error", test_tasks_empty_action_dispatch_error),
86+
("test_tasks_keyed_completable_action_dispatch", test_tasks_keyed_completable_action_dispatch),
87+
("test_tasks_keyed_completable_action_dispatch_error", test_tasks_keyed_completable_action_dispatch_error),
8188
]
8289
}
8390

0 commit comments

Comments
 (0)