Skip to content

Commit cc6bc8f

Browse files
committed
[Test] Add FrogcjnTest (event + associated value).
1 parent 910a642 commit cc6bc8f

File tree

2 files changed

+149
-0
lines changed

2 files changed

+149
-0
lines changed

SwiftState.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
/* Begin PBXBuildFile section */
1010
1F198C5C19972320001C3700 /* QiitaTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F198C5B19972320001C3700 /* QiitaTests.swift */; };
1111
1F24C72C19D068B900C2FDC7 /* SwiftState.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4872D5AC19B4211900F326B5 /* SwiftState.framework */; };
12+
1F27771E1BE68D1D00C57CC9 /* FrogcjnTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F27771C1BE68C7F00C57CC9 /* FrogcjnTest.swift */; };
13+
1F27771F1BE68D1E00C57CC9 /* FrogcjnTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F27771C1BE68C7F00C57CC9 /* FrogcjnTest.swift */; };
1214
1FA620061996601000460108 /* SwiftState.h in Headers */ = {isa = PBXBuildFile; fileRef = 1FA620051996601000460108 /* SwiftState.h */; settings = {ATTRIBUTES = (Public, ); }; };
1315
1FA620201996606300460108 /* StateEventType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FA620191996606200460108 /* StateEventType.swift */; };
1416
1FA620211996606300460108 /* StateMachine.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FA6201A1996606300460108 /* StateMachine.swift */; };
@@ -71,6 +73,7 @@
7173

7274
/* Begin PBXFileReference section */
7375
1F198C5B19972320001C3700 /* QiitaTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = QiitaTests.swift; sourceTree = "<group>"; };
76+
1F27771C1BE68C7F00C57CC9 /* FrogcjnTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FrogcjnTest.swift; sourceTree = "<group>"; };
7477
1FA620001996601000460108 /* SwiftState.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SwiftState.framework; sourceTree = BUILT_PRODUCTS_DIR; };
7578
1FA620041996601000460108 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
7679
1FA620051996601000460108 /* SwiftState.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SwiftState.h; sourceTree = "<group>"; };
@@ -189,6 +192,7 @@
189192
1FD01B6219EC2B6700DA1C91 /* HierarchicalStateMachineTests.swift */,
190193
1F198C5B19972320001C3700 /* QiitaTests.swift */,
191194
C662B6F41B861CC400479524 /* RasmusTest.swift */,
195+
1F27771C1BE68C7F00C57CC9 /* FrogcjnTest.swift */,
192196
1FA6202A199660CA00460108 /* StateMachineChainTests.swift */,
193197
1FA6202B199660CA00460108 /* StateMachineEventTests.swift */,
194198
1FA6202C199660CA00460108 /* StateMachineTests.swift */,
@@ -427,6 +431,7 @@
427431
1FA62035199660CA00460108 /* StateMachineTests.swift in Sources */,
428432
1FA62037199660CA00460108 /* StateTransitionChainTests.swift in Sources */,
429433
C662B6F51B861CC400479524 /* RasmusTest.swift in Sources */,
434+
1F27771E1BE68D1D00C57CC9 /* FrogcjnTest.swift in Sources */,
430435
1FA62032199660CA00460108 /* MyState.swift in Sources */,
431436
);
432437
runOnlyForDeploymentPostprocessing = 0;
@@ -448,6 +453,7 @@
448453
4822F0B219D008EB00F5F572 /* StateRouteTests.swift in Sources */,
449454
4822F0B119D008EB00F5F572 /* StateTransitionChainTests.swift in Sources */,
450455
C662B6F61B861CC400479524 /* RasmusTest.swift in Sources */,
456+
1F27771F1BE68D1E00C57CC9 /* FrogcjnTest.swift in Sources */,
451457
4822F0AD19D008EB00F5F572 /* StateMachineTests.swift in Sources */,
452458
);
453459
runOnlyForDeploymentPostprocessing = 0;

SwiftStateTests/FrogcjnTest.swift

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
//
2+
// FrogcjnTest.swift
3+
// SwiftState
4+
//
5+
// Created by Yasuhiro Inami on 2015-11-02.
6+
// Copyright © 2015 Yasuhiro Inami. All rights reserved.
7+
//
8+
9+
import SwiftState
10+
import XCTest
11+
12+
// https://github.com/ReactKit/SwiftState/issues/34
13+
14+
private enum _State: StateType, Hashable
15+
{
16+
case Pending
17+
case Loading(Int)
18+
case AnyState
19+
20+
init(nilLiteral: ())
21+
{
22+
self = AnyState
23+
}
24+
25+
var hashValue: Int
26+
{
27+
switch self {
28+
case .Pending:
29+
return "Pending".hashValue
30+
case let .Loading(x):
31+
return "Loading\(x)".hashValue
32+
case .AnyState:
33+
return "AnyState".hashValue
34+
}
35+
}
36+
}
37+
38+
private func ==(lhs: _State, rhs: _State) -> Bool
39+
{
40+
switch (lhs, rhs) {
41+
case (.Pending, .Pending):
42+
return true
43+
case let (.Loading(x1), .Loading(x2)):
44+
return x1 == x2
45+
case (.AnyState, .AnyState):
46+
return true
47+
default:
48+
return false
49+
}
50+
}
51+
52+
private enum _Event: StateEventType, Hashable
53+
{
54+
case CancelAction
55+
case LoadAction(Int)
56+
case AnyEvent
57+
58+
init(nilLiteral: ())
59+
{
60+
self = AnyEvent
61+
}
62+
63+
var hashValue: Int
64+
{
65+
switch self {
66+
case .CancelAction:
67+
return "CancelAction".hashValue
68+
case let .LoadAction(x):
69+
return "LoadAction\(x)".hashValue
70+
case .AnyEvent:
71+
return "AnyEvent".hashValue
72+
}
73+
}
74+
}
75+
76+
private func ==(lhs: _Event, rhs: _Event) -> Bool
77+
{
78+
switch (lhs, rhs) {
79+
case (.CancelAction, .CancelAction):
80+
return true
81+
case let (.LoadAction(x1), .LoadAction(x2)):
82+
return x1 == x2
83+
case (.AnyEvent, .AnyEvent):
84+
return true
85+
default:
86+
return false
87+
}
88+
}
89+
90+
class FrogcjnTest: _TestCase
91+
{
92+
func testEventWithAssociatedValue()
93+
{
94+
var count = 0
95+
96+
let machine = StateMachine<_State, _Event>(state: .Pending) { machine in
97+
98+
machine.addRouteEvent(.CancelAction, transitions: [ .AnyState => .Pending ], condition: { $0.fromState != .Pending })
99+
100+
//
101+
// If you have **finite** number of `LoadActionId`s (let's say 1 to 100),
102+
// you can `addRouteEvent()` in finite number of times.
103+
// (In this case, `LoadActionId` should have enum type instead)
104+
//
105+
for actionId in 1...100 {
106+
machine.addRouteEvent(.LoadAction(actionId), transitions: [ .AnyState => .Loading(actionId) ], condition: { $0.fromState != .Loading(actionId) })
107+
}
108+
109+
// increment `count` when any events i.e. `.CancelAction` and `.LoadAction(x)` succeed.
110+
machine.addEventHandler(.AnyEvent) { event, transition, order, userInfo in
111+
count++
112+
}
113+
}
114+
115+
// initial
116+
XCTAssertTrue(machine.state == .Pending)
117+
XCTAssertEqual(count, 0)
118+
119+
// CancelAction (to .Pending state, same as before)
120+
machine <-! .CancelAction
121+
XCTAssertTrue(machine.state == .Pending)
122+
XCTAssertEqual(count, 0, "`tryEvent()` failed, and `count` should not be incremented.")
123+
124+
// LoadAction(1) (to .Loading(1) state)
125+
machine <-! .LoadAction(1)
126+
XCTAssertTrue(machine.state == .Loading(1))
127+
XCTAssertEqual(count, 1)
128+
129+
// LoadAction(1) (same as before)
130+
machine <-! .LoadAction(1)
131+
print(machine.state)
132+
XCTAssertTrue(machine.state == .Loading(1))
133+
XCTAssertEqual(count, 1, "`tryEvent()` failed, and `count` should not be incremented.")
134+
135+
machine <-! .LoadAction(2)
136+
XCTAssertTrue(machine.state == .Loading(2))
137+
XCTAssertEqual(count, 2)
138+
139+
machine <-! .CancelAction
140+
XCTAssertTrue(machine.state == .Pending)
141+
XCTAssertEqual(count, 3)
142+
}
143+
}

0 commit comments

Comments
 (0)