-
Notifications
You must be signed in to change notification settings - Fork 93
/
HierarchicalMachineTests.swift
285 lines (230 loc) · 11.1 KB
/
HierarchicalMachineTests.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
//
// HierarchicalMachineTests.swift
// SwiftState
//
// Created by Yasuhiro Inami on 2015-11-29.
// Copyright © 2015 Yasuhiro Inami. All rights reserved.
//
import SwiftState
import XCTest
private enum _MainState: StateType
{
case MainState0
case SubMachine1(_SubState)
case SubMachine2(_SubState)
var hashValue: Int
{
switch self {
case .MainState0:
return "MainState0".hashValue
case let .SubMachine1(state):
return "SubMachine1-\(state)".hashValue
case let .SubMachine2(state):
return "SubMachine2-\(state)".hashValue
}
}
}
private enum _SubState: StateType
{
case SubState0, SubState1, SubState2
}
private func ==(lhs: _MainState, rhs: _MainState) -> Bool
{
switch (lhs, rhs) {
case (.MainState0, .MainState0):
return true
case let (.SubMachine1(state1), .SubMachine1(state2)):
return state1 == state2
case let (.SubMachine2(state1), .SubMachine2(state2)):
return state1 == state2
default:
return false
}
}
class HierarchicalMachineTests: _TestCase
{
///
/// Hierarchical state machine.
///
/// - mainMachine
/// - MainState0 (initial)
/// - subMachine1
/// - SubState0
/// - SubState1
/// - subMachine2
/// - SubState0
/// - SubState1
///
/// - Warning:
/// This is a naive implementation and easily lose consistency when `subMachine.state` is changed directly, e.g. `subMachine1 <- .SubState1`.
///
private var mainMachine: StateMachine<_MainState, NoEvent>?
private var subMachine1: StateMachine<_SubState, NoEvent>?
private var subMachine2: StateMachine<_SubState, NoEvent>?
override func setUp()
{
super.setUp()
let subMachine1 = StateMachine<_SubState, NoEvent>(state: .SubState0) { subMachine1 in
// add Sub1-0 => Sub1-1
subMachine1.addRoute(.SubState0 => .SubState1)
subMachine1.addHandler(.Any => .Any) { print("[Sub1] \($0.fromState) => \($0.toState)") }
subMachine1.addErrorHandler { print("[ERROR][Sub1] \($0.fromState) => \($0.toState)") }
}
let subMachine2 = StateMachine<_SubState, NoEvent>(state: .SubState0) { subMachine2 in
// add Sub2-0 => Sub2-1
subMachine2.addRoute(.SubState0 => .SubState1)
subMachine2.addHandler(.Any => .Any) { print("[Sub2] \($0.fromState) => \($0.toState)") }
subMachine2.addErrorHandler { print("[ERROR][Sub2] \($0.fromState) => \($0.toState)") }
}
let mainMachine = StateMachine<_MainState, NoEvent>(state: .MainState0) { mainMachine in
// add routes & handle for same-subMachine internal transitions
mainMachine.addRoute(.Any => .Any, condition: { _, fromState, toState, userInfo in
switch (fromState, toState) {
case let (.SubMachine1(state1), .SubMachine1(state2)):
return subMachine1.hasRoute(fromState: state1, toState: state2)
case let (.SubMachine2(state1), .SubMachine2(state2)):
return subMachine2.hasRoute(fromState: state1, toState: state2)
default:
return false
}
}, handler: { _, fromState, toState, userInfo in
switch (fromState, toState) {
case let (.SubMachine1, .SubMachine1(state2)):
subMachine1 <- state2
case let (.SubMachine2, .SubMachine2(state2)):
subMachine2 <- state2
default:
break
}
})
// add routes for mainMachine-state transitions (submachine switching)
mainMachine.addRouteMapping { event, fromState, userInfo -> _MainState? in
// NOTE: use external submachine's states only for evaluating `toState`, but not for `fromState`
switch fromState {
// "Main0" => "Sub1-0"
case .MainState0 where subMachine1.state == .SubState0:
return .SubMachine1(.SubState0)
// "Sub1-1" => "Sub2-0"
case let .SubMachine1(state) where state == .SubState1 && subMachine2.state == .SubState0:
return .SubMachine2(.SubState0)
// "Sub2-1" => "Main0"
case let .SubMachine2(state) where state == .SubState1:
return .MainState0
default:
return nil
}
}
mainMachine.addHandler(.Any => .Any) { print("[Main] \($0.fromState) => \($0.toState)") }
mainMachine.addErrorHandler { print("[ERROR][Main] \($0.fromState) => \($0.toState)") }
}
self.mainMachine = mainMachine
self.subMachine1 = subMachine1
self.subMachine2 = subMachine2
}
/// `mainMachine.hasRoute()` test to check submachine's internal routes
func testHasRoute_submachine_internal()
{
let mainMachine = self.mainMachine!
let subMachine1 = self.subMachine1!
let subMachine2 = self.subMachine2!
// initial
XCTAssertTrue(mainMachine.state == .MainState0)
XCTAssertTrue(subMachine1.state == .SubState0)
XCTAssertTrue(subMachine2.state == .SubState0)
// subMachine1 internal routes
XCTAssertFalse(mainMachine.hasRoute(.SubMachine1(.SubState0) => .SubMachine1(.SubState0)))
XCTAssertTrue(mainMachine.hasRoute(.SubMachine1(.SubState0) => .SubMachine1(.SubState1)))
XCTAssertFalse(mainMachine.hasRoute(.SubMachine1(.SubState1) => .SubMachine1(.SubState0)))
XCTAssertFalse(mainMachine.hasRoute(.SubMachine1(.SubState1) => .SubMachine1(.SubState1)))
// subMachine2 internal routes
XCTAssertFalse(mainMachine.hasRoute(.SubMachine2(.SubState0) => .SubMachine2(.SubState0)))
XCTAssertTrue(mainMachine.hasRoute(.SubMachine2(.SubState0) => .SubMachine2(.SubState1)))
XCTAssertFalse(mainMachine.hasRoute(.SubMachine2(.SubState1) => .SubMachine2(.SubState0)))
XCTAssertFalse(mainMachine.hasRoute(.SubMachine2(.SubState1) => .SubMachine2(.SubState1)))
}
/// `mainMachine.hasRoute()` test to check switchable submachines
func testHasRoute_submachine_switching()
{
let mainMachine = self.mainMachine!
let subMachine1 = self.subMachine1!
let subMachine2 = self.subMachine2!
// NOTE: mainMachine can check switchable submachines
// (external routes between submachines = SubState1, SubState2, or nil)
// initial
XCTAssertTrue(mainMachine.state == .MainState0)
XCTAssertTrue(subMachine1.state == .SubState0)
XCTAssertTrue(subMachine2.state == .SubState0)
// from Main0
XCTAssertTrue(mainMachine.hasRoute(.MainState0 => .SubMachine1(.SubState0)))
XCTAssertFalse(mainMachine.hasRoute(.MainState0 => .SubMachine1(.SubState1)))
XCTAssertFalse(mainMachine.hasRoute(.MainState0 => .SubMachine2(.SubState0)))
XCTAssertFalse(mainMachine.hasRoute(.MainState0 => .SubMachine2(.SubState1)))
// from Sub1-0
XCTAssertFalse(mainMachine.hasRoute(.SubMachine1(.SubState0) => .SubMachine2(.SubState0)))
XCTAssertFalse(mainMachine.hasRoute(.SubMachine1(.SubState0) => .SubMachine2(.SubState1)))
XCTAssertFalse(mainMachine.hasRoute(.SubMachine1(.SubState0) => .MainState0))
// from Sub1-1
XCTAssertTrue(mainMachine.hasRoute(.SubMachine1(.SubState1) => .SubMachine2(.SubState0)))
XCTAssertFalse(mainMachine.hasRoute(.SubMachine1(.SubState1) => .SubMachine2(.SubState1)))
XCTAssertFalse(mainMachine.hasRoute(.SubMachine1(.SubState1) => .MainState0))
// from Sub2-0
XCTAssertFalse(mainMachine.hasRoute(.SubMachine2(.SubState0) => .SubMachine1(.SubState0)))
XCTAssertFalse(mainMachine.hasRoute(.SubMachine2(.SubState0) => .SubMachine1(.SubState1)))
XCTAssertFalse(mainMachine.hasRoute(.SubMachine2(.SubState0) => .MainState0))
// from Sub2-1
XCTAssertFalse(mainMachine.hasRoute(.SubMachine2(.SubState1) => .SubMachine1(.SubState0)))
XCTAssertFalse(mainMachine.hasRoute(.SubMachine2(.SubState1) => .SubMachine1(.SubState1)))
XCTAssertTrue(mainMachine.hasRoute(.SubMachine2(.SubState1) => .MainState0))
}
func testTryState()
{
let mainMachine = self.mainMachine!
// initial
XCTAssertTrue(mainMachine.state == .MainState0)
// Main0 => Sub1-0
mainMachine <- .SubMachine1(.SubState0)
XCTAssertTrue(mainMachine.state == .SubMachine1(.SubState0))
// Sub1-0 => Sub1-1 (Sub1 internal transition)
mainMachine <- .SubMachine1(.SubState1)
XCTAssertTrue(mainMachine.state == .SubMachine1(.SubState1))
// Sub1-1 => Sub1-2 (Sub1 internal transition, but fails)
mainMachine <- .SubMachine1(.SubState2)
XCTAssertTrue(mainMachine.state == .SubMachine1(.SubState1), "No change.")
// Sub1-1 => Sub2-2 (fails)
mainMachine <- .SubMachine2(.SubState2)
XCTAssertTrue(mainMachine.state == .SubMachine1(.SubState1), "No change.")
// Sub1-1 => Sub2-0
mainMachine <- .SubMachine2(.SubState0)
XCTAssertTrue(mainMachine.state == .SubMachine2(.SubState0))
// Sub2-0 => Main0 (fails)
mainMachine <- .MainState0
XCTAssertTrue(mainMachine.state == .SubMachine2(.SubState0), "No change.")
// Sub2-0 => Sub2-1
mainMachine <- .SubMachine2(.SubState1)
XCTAssertTrue(mainMachine.state == .SubMachine2(.SubState1))
// Sub2-1 => Main
mainMachine <- .MainState0
XCTAssertTrue(mainMachine.state == .MainState0)
}
func testAddHandler()
{
let mainMachine = self.mainMachine!
var didPass = false
// NOTE: this handler is added to mainMachine and doesn't make submachines dirty
mainMachine.addHandler(.MainState0 => .SubMachine1(.SubState0)) { context in
print("[Main] 1-1 => 1-2 (specific)")
didPass = true
}
// initial
XCTAssertTrue(mainMachine.state == .MainState0)
XCTAssertFalse(didPass)
// Main0 => Sub1-1 (fails)
mainMachine <- .SubMachine1(.SubState1)
XCTAssertTrue(mainMachine.state == .MainState0, "No change.")
XCTAssertFalse(didPass)
// Main0 => Sub1-0
mainMachine <- .SubMachine1(.SubState0)
XCTAssertTrue(mainMachine.state == .SubMachine1(.SubState0))
XCTAssertTrue(didPass)
}
}