/
RouteMappingTests.swift
134 lines (114 loc) · 3.7 KB
/
RouteMappingTests.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
//
// RouteMappingTests.swift
// SwiftState
//
// Created by Yasuhiro Inami on 2015-11-02.
// Copyright © 2015 Yasuhiro Inami. All rights reserved.
//
import SwiftState
import XCTest
//
// RouteMapping for StateType & EventType using associated values
//
// https://github.com/ReactKit/SwiftState/issues/34
// https://github.com/ReactKit/SwiftState/pull/36
//
private enum _State: StateType, Hashable
{
case Pending
case Loading(Int)
var hashValue: Int
{
switch self {
case .Pending:
return "Pending".hashValue
case let .Loading(x):
return "Loading\(x)".hashValue
}
}
}
private func ==(lhs: _State, rhs: _State) -> Bool
{
switch (lhs, rhs) {
case (.Pending, .Pending):
return true
case let (.Loading(x1), .Loading(x2)):
return x1 == x2
default:
return false
}
}
private enum _Event: EventType, Hashable
{
case CancelAction
case LoadAction(Int)
var hashValue: Int
{
switch self {
case .CancelAction:
return "CancelAction".hashValue
case let .LoadAction(x):
return "LoadAction\(x)".hashValue
}
}
}
private func ==(lhs: _Event, rhs: _Event) -> Bool
{
switch (lhs, rhs) {
case (.CancelAction, .CancelAction):
return true
case let (.LoadAction(x1), .LoadAction(x2)):
return x1 == x2
default:
return false
}
}
class RouteMappingTests: _TestCase
{
func testEventWithAssociatedValue()
{
var count = 0
let machine = Machine<_State, _Event>(state: .Pending) { machine in
machine.addRouteMapping { event, fromState, userInfo in
// no routes for no event
guard let event = event else {
return nil
}
switch event {
case .CancelAction:
// can transit to `.Pending` if current state is not the same
return fromState == .Pending ? nil : .Pending
case .LoadAction(let actionId):
// can transit to `.Loading(actionId)` if current state is not the same
return fromState == .Loading(actionId) ? nil : .Loading(actionId)
}
}
// increment `count` when any events i.e. `.CancelAction` and `.LoadAction(x)` succeed.
machine.addEventHandler(.Any) { event, transition, order, userInfo in
count++
}
}
// initial
XCTAssertTrue(machine.state == .Pending)
XCTAssertEqual(count, 0)
// CancelAction (to .Pending state, same as before)
machine <-! .CancelAction
XCTAssertTrue(machine.state == .Pending)
XCTAssertEqual(count, 0, "`tryEvent()` failed, and `count` should not be incremented.")
// LoadAction(1) (to .Loading(1) state)
machine <-! .LoadAction(1)
XCTAssertTrue(machine.state == .Loading(1))
XCTAssertEqual(count, 1)
// LoadAction(1) (same as before)
machine <-! .LoadAction(1)
print(machine.state)
XCTAssertTrue(machine.state == .Loading(1))
XCTAssertEqual(count, 1, "`tryEvent()` failed, and `count` should not be incremented.")
machine <-! .LoadAction(2)
XCTAssertTrue(machine.state == .Loading(2))
XCTAssertEqual(count, 2)
machine <-! .CancelAction
XCTAssertTrue(machine.state == .Pending)
XCTAssertEqual(count, 3)
}
}