Skip to content

Commit 8ade68e

Browse files
committed
[Test] Add more RouteMapping tests.
1 parent f7ce748 commit 8ade68e

File tree

3 files changed

+125
-3
lines changed

3 files changed

+125
-3
lines changed

Tests/MachineTests.swift

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,4 +297,74 @@ class MachineTests: _TestCase
297297

298298
XCTAssertEqual(invokeCount, 0, "Handler should NOT be performed")
299299
}
300+
301+
//--------------------------------------------------
302+
// MARK: - EventRouteMapping
303+
//--------------------------------------------------
304+
305+
func testAddEventRouteMapping()
306+
{
307+
var invokeCount = 0
308+
309+
let machine = Machine<MyState2, MyEvent2>(state: .State0("initial")) { machine in
310+
311+
// add EventRouteMapping
312+
machine.addRouteMapping { event, fromState, userInfo -> MyState2? in
313+
// no route for no-event
314+
guard let event = event else { return nil }
315+
316+
switch (event, fromState) {
317+
case (.Event0("gogogo"), .State0("initial")):
318+
return .State0("Phase 1")
319+
case (.Event0("gogogo"), .State0("Phase 1")):
320+
return .State0("Phase 2")
321+
case (.Event0("finish"), .State0("Phase 2")):
322+
return .State0("end")
323+
default:
324+
return nil
325+
}
326+
}
327+
328+
machine.addHandler(event: .Event0("gogogo")) { context in
329+
invokeCount++
330+
return
331+
}
332+
333+
}
334+
335+
// initial
336+
XCTAssertEqual(machine.state, MyState2.State0("initial"))
337+
338+
// tryEvent (fails)
339+
machine <-! .Event0("go?")
340+
XCTAssertEqual(machine.state, MyState2.State0("initial"), "No change.")
341+
XCTAssertEqual(invokeCount, 0, "Handler should NOT be performed")
342+
343+
// tryEvent
344+
machine <-! .Event0("gogogo")
345+
XCTAssertEqual(machine.state, MyState2.State0("Phase 1"))
346+
XCTAssertEqual(invokeCount, 1)
347+
348+
// tryEvent (fails)
349+
machine <-! .Event0("finish")
350+
XCTAssertEqual(machine.state, MyState2.State0("Phase 1"), "No change.")
351+
XCTAssertEqual(invokeCount, 1, "Handler should NOT be performed")
352+
353+
// tryEvent
354+
machine <-! .Event0("gogogo")
355+
XCTAssertEqual(machine.state, MyState2.State0("Phase 2"))
356+
XCTAssertEqual(invokeCount, 2)
357+
358+
// tryEvent (fails)
359+
machine <-! .Event0("gogogo")
360+
XCTAssertEqual(machine.state, MyState2.State0("Phase 2"), "No change.")
361+
XCTAssertEqual(invokeCount, 2, "Handler should NOT be performed")
362+
363+
// tryEvent
364+
machine <-! .Event0("finish")
365+
XCTAssertEqual(machine.state, MyState2.State0("end"))
366+
XCTAssertEqual(invokeCount, 2, "gogogo-Handler should NOT be performed")
367+
368+
}
369+
300370
}

Tests/RouteMappingTests.swift

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ class RouteMappingTests: _TestCase
8080

8181
let machine = StateMachine<_State, _Event>(state: .Pending) { machine in
8282

83+
// add EventRouteMapping
8384
machine.addRouteMapping { event, fromState, userInfo in
8485
// no routes for no event
8586
guard let event = event else {
@@ -119,7 +120,6 @@ class RouteMappingTests: _TestCase
119120

120121
// LoadAction(1) (same as before)
121122
machine <-! .LoadAction(1)
122-
print(machine.state)
123123
XCTAssertEqual(machine.state, _State.Loading(1))
124124
XCTAssertEqual(count, 1, "`tryEvent()` failed, and `count` should not be incremented.")
125125

@@ -131,4 +131,55 @@ class RouteMappingTests: _TestCase
131131
XCTAssertEqual(machine.state, _State.Pending)
132132
XCTAssertEqual(count, 3)
133133
}
134+
135+
func testStateWithAssociatedValue()
136+
{
137+
var count = 0
138+
139+
let machine = StateMachine<_State, _Event>(state: .Pending) { machine in
140+
141+
// add StateRouteMapping
142+
machine.addRouteMapping { fromState, userInfo in
143+
switch fromState {
144+
case .Pending:
145+
return [.Loading(1)]
146+
case .Loading(let actionId):
147+
return [.Loading(actionId+10), .Loading(actionId+100)]
148+
}
149+
}
150+
151+
// increment `count` when any events i.e. `.CancelAction` and `.LoadAction(x)` succeed.
152+
machine.addHandler(.Any => .Any) { event, transition, order, userInfo in
153+
count++
154+
}
155+
156+
}
157+
158+
// initial
159+
XCTAssertEqual(machine.state, _State.Pending)
160+
XCTAssertEqual(count, 0)
161+
162+
// .Loading(999) (fails)
163+
machine <- .Loading(999)
164+
XCTAssertEqual(machine.state, _State.Pending)
165+
XCTAssertEqual(count, 0, "`tryState()` failed, and `count` should not be incremented.")
166+
167+
// .Loading(1)
168+
machine <- .Loading(1)
169+
XCTAssertEqual(machine.state, _State.Loading(1))
170+
XCTAssertEqual(count, 1)
171+
172+
// .Loading(999) (fails)
173+
machine <- .Loading(999)
174+
XCTAssertEqual(machine.state, _State.Loading(1))
175+
XCTAssertEqual(count, 1, "`tryState()` failed, and `count` should not be incremented.")
176+
177+
machine <- .Loading(11)
178+
XCTAssertEqual(machine.state, _State.Loading(11))
179+
XCTAssertEqual(count, 2)
180+
181+
machine <- .Loading(111)
182+
XCTAssertEqual(machine.state, _State.Loading(111))
183+
XCTAssertEqual(count, 3)
184+
}
134185
}

Tests/StateMachineTests.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -544,10 +544,11 @@ class StateMachineTests: _TestCase
544544
}
545545

546546
//--------------------------------------------------
547-
// MARK: - StateRouteMapping
547+
// MARK: - Event/StateRouteMapping
548548
//--------------------------------------------------
549549

550-
func testStateRouteMapping()
550+
/// Test `Event/StateRouteMapping`s.
551+
func testRouteMapping()
551552
{
552553
var routeMappingDisposable: Disposable?
553554

0 commit comments

Comments
 (0)