|
| 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