-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathStateMachine_Turnstile_Tests.swift
260 lines (206 loc) · 8.59 KB
/
StateMachine_Turnstile_Tests.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
//
// Created by Christopher Fuller on 12/21/19.
// Copyright © 2019 Tinder. All rights reserved.
//
import Nimble
@testable import StateMachine
import XCTest
final class StateMachine_Turnstile_Tests: XCTestCase, StateMachineBuilder {
enum Constant {
static let farePrice: Int = 50
}
indirect enum State: Equatable {
case locked(credit: Int), unlocked, broken(oldState: State)
}
enum Event: Equatable {
case insertCoin(Int), admitPerson, machineDidFail, machineRepairDidComplete
}
enum SideEffect {
case soundAlarm, closeDoors, openDoors, orderRepair
}
typealias TurnstileStateMachine = StateMachine<State, Event, SideEffect>
typealias ValidTransition = TurnstileStateMachine.Transition.Valid
static func turnstileStateMachine(withInitialState _state: State, logger: Logger) -> TurnstileStateMachine {
TurnstileStateMachine {
initialState(_state)
state(.locked) {
on(.insertCoin) { locked, insertCoin in
let newCredit: Int = try locked.credit() + insertCoin.value()
if newCredit >= Constant.farePrice {
return transition(to: .unlocked, emit: .openDoors)
} else {
return transition(to: .locked(credit: newCredit))
}
}
on(.admitPerson) {
dontTransition(emit: .soundAlarm)
}
on(.machineDidFail) {
transition(to: .broken(oldState: $0), emit: .orderRepair)
}
}
state(.unlocked) {
on(.admitPerson) {
transition(to: .locked(credit: 0), emit: .closeDoors)
}
}
state(.broken) {
on(.machineRepairDidComplete) { broken in
transition(to: try broken.oldState())
}
}
}
}
var logger: Logger!
override func setUp() {
super.setUp()
logger = .init()
}
override func tearDown() {
logger = nil
super.tearDown()
}
func givenState(is state: State) -> TurnstileStateMachine {
let stateMachine: TurnstileStateMachine = Self.turnstileStateMachine(withInitialState: state, logger: logger)
expect(stateMachine.state).to(equal(state))
return stateMachine
}
func test_givenStateIsLocked_whenInsertCoin_andCreditLessThanFarePrice_shouldTransitionToLockedState() throws {
// Given
let stateMachine = givenState(is: .locked(credit: 0))
// When
let transition = try stateMachine.transition(.insertCoin(10))
// Then
expect(stateMachine.state).to(equal(.locked(credit: 10)))
expect(transition).to(equal(ValidTransition(fromState: .locked(credit: 0),
event: .insertCoin(10),
toState: .locked(credit: 10),
sideEffect: nil)))
}
func test_givenStateIsLocked_whenInsertCoin_andCreditEqualsFarePrice_shouldTransitionToUnlockedStateAndOpenDoors() throws {
// Given
let stateMachine = givenState(is: .locked(credit: 35))
// When
let transition = try stateMachine.transition(.insertCoin(15))
// Then
expect(stateMachine.state).to(equal(.unlocked))
expect(transition).to(equal(ValidTransition(fromState: .locked(credit: 35),
event: .insertCoin(15),
toState: .unlocked,
sideEffect: .openDoors)))
}
func test_givenStateIsLocked_whenInsertCoin_andCreditMoreThanFarePrice_shouldTransitionToUnlockedStateAndOpenDoors() throws {
// Given
let stateMachine = givenState(is: .locked(credit: 35))
// When
let transition = try stateMachine.transition(.insertCoin(20))
// Then
expect(stateMachine.state).to(equal(.unlocked))
expect(transition).to(equal(ValidTransition(fromState: .locked(credit: 35),
event: .insertCoin(20),
toState: .unlocked,
sideEffect: .openDoors)))
}
func test_givenStateIsLocked_whenAdmitPerson_shouldTransitionToLockedStateAndSoundAlarm() throws {
// Given
let stateMachine = givenState(is: .locked(credit: 35))
// When
let transition = try stateMachine.transition(.admitPerson)
// Then
expect(stateMachine.state).to(equal(.locked(credit: 35)))
expect(transition).to(equal(ValidTransition(fromState: .locked(credit: 35),
event: .admitPerson,
toState: .locked(credit: 35),
sideEffect: .soundAlarm)))
}
func test_givenStateIsLocked_whenMachineDidFail_shouldTransitionToBrokenStateAndOrderRepair() throws {
// Given
let stateMachine = givenState(is: .locked(credit: 15))
// When
let transition = try stateMachine.transition(.machineDidFail)
// Then
expect(stateMachine.state).to(equal(.broken(oldState: .locked(credit: 15))))
expect(transition).to(equal(ValidTransition(fromState: .locked(credit: 15),
event: .machineDidFail,
toState: .broken(oldState: .locked(credit: 15)),
sideEffect: .orderRepair)))
}
func test_givenStateIsUnlocked_whenAdmitPerson_shouldTransitionToLockedStateAndCloseDoors() throws {
// Given
let stateMachine = givenState(is: .unlocked)
// When
let transition = try stateMachine.transition(.admitPerson)
// Then
expect(stateMachine.state).to(equal(.locked(credit: 0)))
expect(transition).to(equal(ValidTransition(fromState: .unlocked,
event: .admitPerson,
toState: .locked(credit: 0),
sideEffect: .closeDoors)))
}
func test_givenStateIsBroken_whenMachineRepairDidComplete_shouldTransitionToLockedState() throws {
// Given
let stateMachine = givenState(is: .broken(oldState: .locked(credit: 15)))
// When
let transition = try stateMachine.transition(.machineRepairDidComplete)
// Then
expect(stateMachine.state).to(equal(.locked(credit: 15)))
expect(transition).to(equal(ValidTransition(fromState: .broken(oldState: .locked(credit: 15)),
event: .machineRepairDidComplete,
toState: .locked(credit: 15),
sideEffect: nil)))
}
}
extension StateMachine_Turnstile_Tests.State: StateMachineHashable {
enum HashableIdentifier {
case locked, unlocked, broken
}
var hashableIdentifier: HashableIdentifier {
switch self {
case .locked:
return .locked
case .unlocked:
return .unlocked
case .broken:
return .broken
}
}
var associatedValue: Any {
switch self {
case let .locked(credit):
return credit
case .unlocked:
return ()
case let .broken(oldState):
return oldState
}
}
}
extension StateMachine_Turnstile_Tests.Event: StateMachineHashable {
enum HashableIdentifier {
case insertCoin, admitPerson, machineDidFail, machineRepairDidComplete
}
var hashableIdentifier: HashableIdentifier {
switch self {
case .insertCoin:
return .insertCoin
case .admitPerson:
return .admitPerson
case .machineDidFail:
return .machineDidFail
case .machineRepairDidComplete:
return .machineRepairDidComplete
}
}
var associatedValue: Any {
switch self {
case let .insertCoin(value):
return value
case .admitPerson:
return ()
case .machineDidFail:
return ()
case .machineRepairDidComplete:
return ()
}
}
}