/
FrogcjnTest.swift
180 lines (150 loc) · 5.58 KB
/
FrogcjnTest.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
//
// FrogcjnTest.swift
// SwiftState
//
// Created by Yasuhiro Inami on 2015-11-02.
// Copyright © 2015 Yasuhiro Inami. All rights reserved.
//
import SwiftState
import XCTest
// https://github.com/ReactKit/SwiftState/issues/34
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 FrogcjnTest: _TestCase
{
func testEventWithAssociatedValue()
{
var count = 0
let machine = Machine<_State, _Event>(state: .Pending) { machine in
machine.addRouteEvent(.CancelAction, transitions: [ .Any => .Pending ], condition: { $0.fromState != .Pending })
//
// If you have **finite** number of `LoadActionId`s (let's say 1 to 100),
// you can `addRouteEvent()` in finite number of times.
// (In this case, `LoadActionId` should have enum type instead)
//
for actionId in 1...100 {
machine.addRouteEvent(.LoadAction(actionId), transitions: [ .Any => .Loading(actionId) ], condition: { $0.fromState != .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)
}
func testEventWithAssociatedValue2()
{
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)
}
}