-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathWorkflowNodeTests.swift
369 lines (293 loc) · 10.7 KB
/
WorkflowNodeTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
* Copyright 2020 Square Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import XCTest
@testable import Workflow
final class WorkflowNodeTests: XCTestCase {
func test_rendersSimpleWorkflow() {
let node = WorkflowNode(workflow: SimpleWorkflow(string: "Foo"))
XCTAssertEqual(node.render(), "ooF")
}
func test_rendersNestedWorkflows() {
let node = WorkflowNode(
workflow: CompositeWorkflow(
a: SimpleWorkflow(string: "Hello"),
b: SimpleWorkflow(string: "World")
))
XCTAssertEqual(node.render().aRendering, "olleH")
XCTAssertEqual(node.render().bRendering, "dlroW")
}
func test_childWorkflowsEmitOutputEvents() {
typealias WorkflowType = CompositeWorkflow<EventEmittingWorkflow, SimpleWorkflow>
let workflow = CompositeWorkflow(
a: EventEmittingWorkflow(string: "Hello"),
b: SimpleWorkflow(string: "World")
)
let node = WorkflowNode(workflow: workflow)
let rendering = node.render()
node.enableEvents()
var outputs: [WorkflowType.Output] = []
let expectation = XCTestExpectation(description: "Node output")
node.onOutput = { value in
if let output = value.outputEvent {
outputs.append(output)
expectation.fulfill()
}
}
rendering.aRendering.someoneTappedTheButton()
wait(for: [expectation], timeout: 1.0)
XCTAssertEqual(outputs, [WorkflowType.Output.childADidSomething(.helloWorld)])
}
func test_childWorkflowsEmitStateChangeEvents() {
typealias WorkflowType = CompositeWorkflow<StateTransitioningWorkflow, SimpleWorkflow>
let workflow = CompositeWorkflow(
a: StateTransitioningWorkflow(),
b: SimpleWorkflow(string: "World")
)
let node = WorkflowNode(workflow: workflow)
let expectation = XCTestExpectation(description: "State Change")
var stateChangeCount = 0
node.onOutput = { _ in
stateChangeCount += 1
if stateChangeCount == 3 {
expectation.fulfill()
}
}
var aRendering = node.render().aRendering
node.enableEvents()
aRendering.toggle()
aRendering = node.render().aRendering
node.enableEvents()
aRendering.toggle()
aRendering = node.render().aRendering
node.enableEvents()
aRendering.toggle()
wait(for: [expectation], timeout: 1.0)
XCTAssertEqual(stateChangeCount, 3)
}
func test_debugUpdateInfo() {
typealias WorkflowType = CompositeWorkflow<EventEmittingWorkflow, SimpleWorkflow>
let workflow = CompositeWorkflow(
a: EventEmittingWorkflow(string: "Hello"),
b: SimpleWorkflow(string: "World")
)
let node = WorkflowNode(workflow: workflow)
let rendering = node.render()
node.enableEvents()
var emittedDebugInfo: [WorkflowUpdateDebugInfo] = []
let expectation = XCTestExpectation(description: "Output")
node.onOutput = { value in
emittedDebugInfo.append(value.debugInfo)
expectation.fulfill()
}
rendering.aRendering.someoneTappedTheButton()
wait(for: [expectation], timeout: 1.0)
XCTAssertEqual(emittedDebugInfo.count, 1)
let debugInfo = emittedDebugInfo[0]
XCTAssert(debugInfo.workflowType == "\(WorkflowType.self)")
/// Test the shape of the emitted debug info
switch debugInfo.kind {
case .childDidUpdate:
XCTFail()
case .didUpdate(let source):
switch source {
case .external, .worker, .sideEffect:
XCTFail()
case .subtree(let childInfo):
XCTAssert(childInfo.workflowType == "\(EventEmittingWorkflow.self)")
switch childInfo.kind {
case .childDidUpdate:
XCTFail()
case .didUpdate(let source):
switch source {
case .external:
break
case .subtree(_), .worker, .sideEffect:
XCTFail()
}
}
}
}
}
func test_debugTreeSnapshots() {
typealias WorkflowType = CompositeWorkflow<EventEmittingWorkflow, SimpleWorkflow>
let workflow = CompositeWorkflow(
a: EventEmittingWorkflow(string: "Hello"),
b: SimpleWorkflow(string: "World")
)
let node = WorkflowNode(workflow: workflow)
_ = node.render() // the debug snapshow always reflects the tree after the latest render pass
let snapshot = node.makeDebugSnapshot()
let expectedSnapshot = WorkflowHierarchyDebugSnapshot(
workflowType: "\(WorkflowType.self)",
stateDescription: "\(WorkflowType.State())",
children: [
WorkflowHierarchyDebugSnapshot.Child(
key: "a",
snapshot: WorkflowHierarchyDebugSnapshot(
workflowType: "\(EventEmittingWorkflow.self)",
stateDescription: "\(EventEmittingWorkflow.State())"
)
),
WorkflowHierarchyDebugSnapshot.Child(
key: "b",
snapshot: WorkflowHierarchyDebugSnapshot(
workflowType: "\(SimpleWorkflow.self)",
stateDescription: "\(SimpleWorkflow.State())"
)
),
]
)
XCTAssertEqual(snapshot, expectedSnapshot)
}
}
/// Renders two child state machines of types `A` and `B`.
private struct CompositeWorkflow<A, B>: Workflow where
A: Workflow,
B: Workflow {
var a: A
var b: B
}
extension CompositeWorkflow {
struct State {}
struct Rendering {
var aRendering: A.Rendering
var bRendering: B.Rendering
}
enum Output {
case childADidSomething(A.Output)
case childBDidSomething(B.Output)
}
enum Event: WorkflowAction {
case a(A.Output)
case b(B.Output)
typealias WorkflowType = CompositeWorkflow<A, B>
func apply(toState state: inout CompositeWorkflow<A, B>.State) -> CompositeWorkflow<A, B>.Output? {
switch self {
case .a(let childOutput):
return .childADidSomething(childOutput)
case .b(let childOutput):
return .childBDidSomething(childOutput)
}
}
}
func makeInitialState() -> CompositeWorkflow<A, B>.State {
return State()
}
func render(state: State, context: RenderContext<CompositeWorkflow<A, B>>) -> Rendering {
return Rendering(
aRendering: a
.mapOutput { Event.a($0) }
.rendered(in: context, key: "a"),
bRendering: b
.mapOutput { Event.b($0) }
.rendered(in: context, key: "b")
)
}
}
extension CompositeWorkflow.Rendering: Equatable where A.Rendering: Equatable, B.Rendering: Equatable {
fileprivate static func == (lhs: CompositeWorkflow.Rendering, rhs: CompositeWorkflow.Rendering) -> Bool {
return lhs.aRendering == rhs.aRendering
&& lhs.bRendering == rhs.bRendering
}
}
extension CompositeWorkflow.Output: Equatable where A.Output: Equatable, B.Output: Equatable {
fileprivate static func == (lhs: CompositeWorkflow.Output, rhs: CompositeWorkflow.Output) -> Bool {
switch (lhs, rhs) {
case (.childADidSomething(let l), .childADidSomething(let r)):
return l == r
case (.childBDidSomething(let l), .childBDidSomething(let r)):
return l == r
default:
return false
}
}
}
/// Has no state or output, simply renders a reversed string
private struct SimpleWorkflow: Workflow {
var string: String
struct State {}
func makeInitialState() -> State {
return State()
}
func render(state: State, context: RenderContext<SimpleWorkflow>) -> String {
return String(string.reversed())
}
}
/// Renders to a model that contains a callback, which in turn sends an output event.
private struct EventEmittingWorkflow: Workflow {
var string: String
}
extension EventEmittingWorkflow {
struct State {}
struct Rendering {
var someoneTappedTheButton: () -> Void
}
func makeInitialState() -> State {
return State()
}
enum Event: Equatable, WorkflowAction {
case tapped
typealias WorkflowType = EventEmittingWorkflow
func apply(toState state: inout EventEmittingWorkflow.State) -> EventEmittingWorkflow.Output? {
switch self {
case .tapped:
return .helloWorld
}
}
}
enum Output: Equatable {
case helloWorld
}
func render(state: State, context: RenderContext<EventEmittingWorkflow>) -> Rendering {
let sink = context.makeSink(of: Event.self)
return Rendering(someoneTappedTheButton: { sink.send(.tapped) })
}
}
/// Renders to a model that contains a callback, which in turn sends an output event.
private struct StateTransitioningWorkflow: Workflow {
typealias State = Bool
typealias Output = Never
struct Rendering {
var toggle: () -> Void
var currentValue: Bool
}
func makeInitialState() -> Bool {
return false
}
func render(state: State, context: RenderContext<StateTransitioningWorkflow>) -> Rendering {
let sink = context.makeSink(of: Event.self)
return Rendering(
toggle: { sink.send(.toggle) },
currentValue: state
)
}
enum Event: WorkflowAction {
case toggle
typealias WorkflowType = StateTransitioningWorkflow
func apply(toState state: inout Bool) -> Never? {
switch self {
case .toggle:
state.toggle()
}
return nil
}
}
}
#if compiler(>=5.0)
// Never gains Equatable and Hashable conformance in Swift 5
#else
extension Never: Equatable {}
#endif