1
1
import Foundation
2
2
@testable import MiniPromises
3
+ @testable import MiniTasks
3
4
import Nimble
4
5
import RxBlocking
5
6
import RxSwift
@@ -25,47 +26,73 @@ private func equalAction<A: Action & Equatable>(_ by: A) -> Predicate<A> {
25
26
final class PrimitiveSequenceTypeTests : XCTestCase {
26
27
fileprivate enum Error : Swift . Error { case dummy }
27
28
28
- class TestCompletableAction : CompletableAction , Equatable {
29
- typealias Payload = Int
29
+ // MARK: - Promises test actions.
30
30
31
- let counter : Promise < Payload >
31
+ struct PromisesTestCompletableAction : MiniPromises . CompletableAction , Equatable {
32
+ typealias Payload = Int
32
33
33
- required init ( promise: Promise < Payload > ) {
34
- counter = promise
35
- }
34
+ let promise : Promise < Payload >
36
35
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
39
38
}
40
39
}
41
40
42
- class TestKeyedCompletableAction : KeyedCompletableAction , Equatable {
41
+ struct PromisesTestKeyedCompletableAction : MiniPromises . KeyedCompletableAction , Equatable {
43
42
typealias Payload = Int
44
43
typealias Key = String
45
44
46
- let counterMap : [ Key : Promise < Payload > ]
45
+ let promise : [ Key : Promise < Payload > ]
47
46
48
- required init ( promise : [ Key : Promise < Payload > ] ) {
49
- counterMap = promise
47
+ static func == ( lhs : PromisesTestKeyedCompletableAction , rhs : PromisesTestKeyedCompletableAction ) -> Bool {
48
+ lhs . promise == rhs . promise
50
49
}
50
+ }
51
+
52
+ struct PromisesTestEmptyAction : MiniPromises . EmptyAction , Equatable {
53
+ let promise : Promise < Void >
51
54
52
- static func == ( lhs : TestKeyedCompletableAction , rhs : TestKeyedCompletableAction ) -> Bool {
53
- lhs . counterMap == rhs . counterMap
55
+ static func == ( _ : PromisesTestEmptyAction , _ : PromisesTestEmptyAction ) -> Bool {
56
+ true
54
57
}
55
58
}
56
59
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 ?
59
67
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
62
70
}
71
+ }
63
72
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
66
83
}
67
84
}
68
85
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
+
69
96
var dispatcher : Dispatcher !
70
97
var disposeBag : DisposeBag !
71
98
var testMiddleware : TestMiddleware !
@@ -79,99 +106,199 @@ final class PrimitiveSequenceTypeTests: XCTestCase {
79
106
dispatcher. add ( middleware: testMiddleware)
80
107
}
81
108
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( ) {
83
210
Single
84
211
. just ( 1 )
85
- . dispatch ( action: TestCompletableAction . self, on: dispatcher)
212
+ . dispatch ( action: TasksTestCompletableAction . self, on: dispatcher)
86
213
. disposed ( by: disposeBag)
87
214
88
215
expect (
89
- self . testMiddleware. actions ( of: TestCompletableAction . self) . count
216
+ self . testMiddleware. actions ( of: TasksTestCompletableAction . self) . count
90
217
) . toEventually ( be ( 1 ) )
91
218
}
92
219
93
- func test_completable_action_dispatch_error ( ) {
220
+ func test_tasks_completable_action_dispatch_error ( ) {
94
221
Single
95
222
. error ( Error . dummy)
96
- . dispatch ( action: TestCompletableAction . self, on: dispatcher)
223
+ . dispatch ( action: TasksTestCompletableAction . self, on: dispatcher)
97
224
. disposed ( by: disposeBag)
98
225
99
226
expect (
100
- self . testMiddleware. actions ( of: TestCompletableAction . self) . count
227
+ self . testMiddleware. actions ( of: TasksTestCompletableAction . self) . count
101
228
) . toEventually ( be ( 1 ) )
102
229
103
230
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
106
233
}
107
234
) . toEventually ( beTrue ( ) )
108
235
}
109
236
110
- func test_keyed_completable_action_dispatch ( ) {
237
+ func test_tasks_keyed_completable_action_dispatch ( ) {
111
238
Single
112
239
. just ( 1 )
113
- . dispatch ( action: TestKeyedCompletableAction . self, key: " hello " , on: dispatcher)
240
+ . dispatch ( action: TasksTestKeyedCompletableAction . self, key: " hello " , on: dispatcher)
114
241
. disposed ( by: disposeBag)
115
242
116
243
expect (
117
244
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
120
247
}
121
248
) . toEventually ( beTrue ( ) )
122
249
}
123
250
124
- func test_keyed_completable_action_dispatch_error ( ) {
251
+ func test_tasks_keyed_completable_action_dispatch_error ( ) {
125
252
Single
126
253
. error ( Error . dummy)
127
- . dispatch ( action: TestKeyedCompletableAction . self, key: " hello " , on: dispatcher)
254
+ . dispatch ( action: TasksTestKeyedCompletableAction . self, key: " hello " , on: dispatcher)
128
255
. disposed ( by: disposeBag)
129
256
130
257
expect (
131
258
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
134
261
}
135
262
) . toEventually ( beTrue ( ) )
136
263
}
137
264
138
- func test_completable_action_action ( ) throws {
265
+ func test_tasks_completable_action_action ( ) throws {
139
266
guard let action =
140
267
try Single
141
268
. just ( 1 )
142
- . action ( TestCompletableAction . self)
269
+ . action ( TasksTestCompletableAction . self)
143
270
. toBlocking ( timeout: 5.0 ) . first ( ) else { fatalError ( ) }
144
271
145
- expect ( action) . to ( equalAction ( TestCompletableAction ( promise : . value ( 1 ) ) ) )
272
+ expect ( action) . to ( equalAction ( TasksTestCompletableAction ( task : . success ( ) , payload : 1 ) ) )
146
273
}
147
274
148
- func test_empty_action_dispatch ( ) {
275
+ func test_tasks_empty_action_dispatch ( ) {
149
276
Completable
150
277
. empty ( )
151
- . dispatch ( action: TestEmptyAction . self, on: dispatcher)
278
+ . dispatch ( action: TasksTestEmptyAction . self, on: dispatcher)
152
279
. disposed ( by: disposeBag)
153
280
154
281
expect (
155
- self . testMiddleware. actions ( of: TestEmptyAction . self) . count
282
+ self . testMiddleware. actions ( of: TasksTestEmptyAction . self) . count
156
283
) . toEventually ( be ( 1 ) )
157
284
158
- expect ( self . testMiddleware. actions ( of: TestEmptyAction . self) . first? . promise . isResolved ) == true
285
+ expect ( self . testMiddleware. actions ( of: TasksTestEmptyAction . self) . first? . task . isCompleted ) == true
159
286
}
160
287
161
- func test_empty_action_dispatch_error ( ) {
288
+ func test_tasks_empty_action_dispatch_error ( ) {
162
289
Completable
163
290
. error ( Error . dummy)
164
- . dispatch ( action: TestEmptyAction . self, on: dispatcher)
291
+ . dispatch ( action: TasksTestEmptyAction . self, on: dispatcher)
165
292
. disposed ( by: disposeBag)
166
293
167
294
expect (
168
- self . testMiddleware. actions ( of: TestEmptyAction . self) . count
295
+ self . testMiddleware. actions ( of: TasksTestEmptyAction . self) . count
169
296
) . toEventually ( be ( 1 ) )
170
297
171
- expect ( self . testMiddleware. actions ( of: TestEmptyAction . self) . first? . promise . isResolved ) == true
298
+ expect ( self . testMiddleware. actions ( of: TasksTestEmptyAction . self) . first? . task . isRunning ) == false
172
299
173
- expect ( self . testMiddleware. actions ( of: TestEmptyAction . self) . first? . promise . isCompleted) == true
300
+ expect ( self . testMiddleware. actions ( of: TasksTestEmptyAction . self) . first? . task . isCompleted) == true
174
301
175
- expect ( self . testMiddleware. actions ( of: TestEmptyAction . self) . first? . promise . isRejected ) == true
302
+ expect ( self . testMiddleware. actions ( of: TasksTestEmptyAction . self) . first? . task . isFailure ) == true
176
303
}
177
304
}
0 commit comments