From 8d2b4cadd3f57d0a376f500dd3d35459b8c13116 Mon Sep 17 00:00:00 2001 From: Yasuhiro Inami Date: Tue, 2 Aug 2016 10:36:03 +0900 Subject: [PATCH 01/27] Migrate to Swift 3.0 --- Sources/EventType.swift | 26 +- Sources/Machine.swift | 138 +++--- Sources/Route.swift | 10 +- Sources/StateMachine.swift | 96 ++-- Sources/StateType.swift | 26 +- Sources/Transition.swift | 12 +- Sources/TransitionChain.swift | 8 +- Sources/_Random.swift | 2 +- SwiftState.xcodeproj/project.pbxproj | 16 +- .../xcschemes/SwiftState.xcscheme | 2 +- Tests/BasicTests.swift | 92 ++-- Tests/EventTests.swift | 16 +- Tests/HierarchicalMachineTests.swift | 164 +++---- Tests/MachineTests.swift | 310 ++++++------ Tests/MiscTests.swift | 112 ++--- Tests/MyEvent.swift | 8 +- Tests/MyState.swift | 8 +- Tests/QiitaTests.swift | 30 +- Tests/RouteChainTests.swift | 134 ++--- Tests/RouteMappingTests.swift | 94 ++-- Tests/RouteTests.swift | 18 +- Tests/StateMachineEventTests.swift | 272 +++++------ Tests/StateMachineTests.swift | 458 +++++++++--------- Tests/StateTests.swift | 16 +- Tests/TransitionChainTests.swift | 72 +-- Tests/TransitionTests.swift | 46 +- 26 files changed, 1116 insertions(+), 1070 deletions(-) diff --git a/Sources/EventType.swift b/Sources/EventType.swift index 3af6a43..2601960 100644 --- a/Sources/EventType.swift +++ b/Sources/EventType.swift @@ -13,8 +13,8 @@ public protocol EventType: Hashable {} /// `EventType` wrapper for handling `.Any` event. public enum Event { - case Some(E) - case Any + case some(E) + case any } extension Event: Hashable @@ -22,8 +22,8 @@ extension Event: Hashable public var hashValue: Int { switch self { - case .Some(let x): return x.hashValue - case .Any: return _hashValueForAny + case .some(let x): return x.hashValue + case .any: return _hashValueForAny } } } @@ -33,17 +33,17 @@ extension Event: RawRepresentable public init(rawValue: E?) { if let rawValue = rawValue { - self = .Some(rawValue) + self = .some(rawValue) } else { - self = .Any + self = .any } } public var rawValue: E? { switch self { - case .Some(let x): return x + case .some(let x): return x default: return nil } } @@ -52,9 +52,9 @@ extension Event: RawRepresentable public func == (lhs: Event, rhs: Event) -> Bool { switch (lhs, rhs) { - case let (.Some(x1), .Some(x2)) where x1 == x2: + case let (.some(x1), .some(x2)) where x1 == x2: return true - case (.Any, .Any): + case (.any, .any): return true default: return false @@ -64,9 +64,9 @@ public func == (lhs: Event, rhs: Event) -> Bool public func == (lhs: Event, rhs: E) -> Bool { switch lhs { - case .Some(let x): + case .some(let x): return x == rhs - case .Any: + case .any: return false } } @@ -74,9 +74,9 @@ public func == (lhs: Event, rhs: E) -> Bool public func == (lhs: E, rhs: Event) -> Bool { switch rhs { - case .Some(let x): + case .some(let x): return x == lhs - case .Any: + case .any: return false } } diff --git a/Sources/Machine.swift b/Sources/Machine.swift index 1eebc4c..f8917d8 100644 --- a/Sources/Machine.swift +++ b/Sources/Machine.swift @@ -21,10 +21,10 @@ public class Machine /// Closure for validating transition. /// If condition returns `false`, transition will fail and associated handlers will not be invoked. - public typealias Condition = Context -> Bool + public typealias Condition = (Context) -> Bool /// Transition callback invoked when state has been changed successfully. - public typealias Handler = Context -> () + public typealias Handler = (Context) -> () /// Closure-based route, mainly for `tryEvent()` (and also works for subclass's `tryState()`). /// - Returns: Preferred `toState`. @@ -46,14 +46,14 @@ public class Machine // MARK: - Init //-------------------------------------------------- - public init(state: S, initClosure: (Machine -> ())? = nil) + public init(state: S, initClosure: ((Machine) -> ())? = nil) { self._state = state initClosure?(self) } - public func configure(closure: Machine -> ()) + public func configure(_ closure: (Machine) -> ()) { closure(self) } @@ -68,10 +68,10 @@ public class Machine //-------------------------------------------------- /// Check for added routes & routeMappings. - public func hasRoute(event event: E, transition: Transition, userInfo: Any? = nil) -> Bool + public func hasRoute(event: E, transition: Transition, userInfo: Any? = nil) -> Bool { guard let fromState = transition.fromState.rawValue, - toState = transition.toState.rawValue else + let toState = transition.toState.rawValue else { assertionFailure("State = `.Any` is not supported for `hasRoute()` (always returns `false`)") return false @@ -81,18 +81,18 @@ public class Machine } /// Check for added routes & routeMappings. - public func hasRoute(event event: E, fromState: S, toState: S, userInfo: Any? = nil) -> Bool + public func hasRoute(event: E, fromState: S, toState: S, userInfo: Any? = nil) -> Bool { return self._hasRoute(event: event, fromState: fromState, toState: toState, userInfo: userInfo) } - internal func _hasRoute(event event: E?, fromState: S, toState: S, userInfo: Any? = nil) -> Bool + internal func _hasRoute(event: E?, fromState: S, toState: S, userInfo: Any? = nil) -> Bool { if self._hasRouteInDict(event: event, fromState: fromState, toState: toState, userInfo: userInfo) { return true } - if self._hasRouteMappingInDict(event: event, fromState: fromState, toState: .Some(toState), userInfo: userInfo) != nil { + if self._hasRouteMappingInDict(event: event, fromState: fromState, toState: .some(toState), userInfo: userInfo) != nil { return true } @@ -100,7 +100,7 @@ public class Machine } /// Check for `_routes`. - private func _hasRouteInDict(event event: E?, fromState: S, toState: S, userInfo: Any? = nil) -> Bool + private func _hasRouteInDict(event: E?, fromState: S, toState: S, userInfo: Any? = nil) -> Bool { let validTransitions = _validTransitions(fromState: fromState, toState: toState) @@ -110,7 +110,7 @@ public class Machine if let event = event { for (ev, routeDict) in self._routes { - if ev.rawValue == event || ev == .Any { + if ev.rawValue == event || ev == .any { routeDicts += [routeDict] } } @@ -139,11 +139,11 @@ public class Machine } /// Check for `_routeMappings`. - private func _hasRouteMappingInDict(event event: E?, fromState: S, toState: S?, userInfo: Any? = nil) -> S? + private func _hasRouteMappingInDict(event: E?, fromState: S, toState: S?, userInfo: Any? = nil) -> S? { for mapping in self._routeMappings.values { - if let preferredToState = mapping(event: event, fromState: fromState, userInfo: userInfo) - where preferredToState == toState || toState == nil + if let preferredToState = mapping(event: event, fromState: fromState, userInfo: userInfo), + preferredToState == toState || toState == nil { return preferredToState } @@ -157,12 +157,12 @@ public class Machine //-------------------------------------------------- /// - Returns: Preferred-`toState`. - public func canTryEvent(event: E, userInfo: Any? = nil) -> S? + public func canTryEvent(_ event: E, userInfo: Any? = nil) -> S? { // check for `_routes` - for case let routeDict? in [self._routes[.Some(event)], self._routes[.Any]] { + for case let routeDict? in [self._routes[.some(event)], self._routes[.any]] { for (transition, keyConditionDict) in routeDict { - if transition.fromState == .Some(self.state) || transition.fromState == .Any { + if transition.fromState == .some(self.state) || transition.fromState == .any { for (_, condition) in keyConditionDict { // if toState is `.Any`, always treat as identity transition let toState = transition.toState.rawValue ?? self.state @@ -183,7 +183,8 @@ public class Machine return nil } - public func tryEvent(event: E, userInfo: Any? = nil) -> Bool + @discardableResult + public func tryEvent(_ event: E, userInfo: Any? = nil) -> Bool { let fromState = self.state @@ -212,14 +213,14 @@ public class Machine } } - private func _validHandlerInfos(event event: E, fromState: S, toState: S) -> [_HandlerInfo] + private func _validHandlerInfos(event: E, fromState: S, toState: S) -> [_HandlerInfo] { - let validHandlerInfos = [ self._handlers[.Some(event)], self._handlers[.Any] ] + let validHandlerInfos = [ self._handlers[.some(event)], self._handlers[.any] ] .filter { $0 != nil } .map { $0! } .flatten() - return validHandlerInfos.sort { info1, info2 in + return validHandlerInfos.sorted { info1, info2 in return info1.order < info2.order } } @@ -230,23 +231,27 @@ public class Machine // MARK: addRoutes(event:) - public func addRoutes(event event: E, transitions: [Transition], condition: Machine.Condition? = nil) -> Disposable + @discardableResult + public func addRoutes(event: E, transitions: [Transition], condition: Machine.Condition? = nil) -> Disposable { - return self.addRoutes(event: .Some(event), transitions: transitions, condition: condition) + return self.addRoutes(event: .some(event), transitions: transitions, condition: condition) } - public func addRoutes(event event: Event, transitions: [Transition], condition: Machine.Condition? = nil) -> Disposable + @discardableResult + public func addRoutes(event: Event, transitions: [Transition], condition: Machine.Condition? = nil) -> Disposable { let routes = transitions.map { Route(transition: $0, condition: condition) } return self.addRoutes(event: event, routes: routes) } - public func addRoutes(event event: E, routes: [Route]) -> Disposable + @discardableResult + public func addRoutes(event: E, routes: [Route]) -> Disposable { - return self.addRoutes(event: .Some(event), routes: routes) + return self.addRoutes(event: .some(event), routes: routes) } - public func addRoutes(event event: Event, routes: [Route]) -> Disposable + @discardableResult + public func addRoutes(event: Event, routes: [Route]) -> Disposable { // NOTE: uses `map` with side-effects let disposables = routes.map { self._addRoute(event: event, route: $0) } @@ -256,7 +261,7 @@ public class Machine } } - internal func _addRoute(event event: Event = .Any, route: Route) -> Disposable + internal func _addRoute(event: Event = .any, route: Route) -> Disposable { let transition = route.transition let condition = route.condition @@ -287,23 +292,27 @@ public class Machine // MARK: addRoutes(event:) + conditional handler - public func addRoutes(event event: E, transitions: [Transition], condition: Condition? = nil, handler: Handler) -> Disposable + @discardableResult + public func addRoutes(event: E, transitions: [Transition], condition: Condition? = nil, handler: Handler) -> Disposable { - return self.addRoutes(event: .Some(event), transitions: transitions, condition: condition, handler: handler) + return self.addRoutes(event: .some(event), transitions: transitions, condition: condition, handler: handler) } - public func addRoutes(event event: Event, transitions: [Transition], condition: Condition? = nil, handler: Handler) -> Disposable + @discardableResult + public func addRoutes(event: Event, transitions: [Transition], condition: Condition? = nil, handler: Handler) -> Disposable { let routes = transitions.map { Route(transition: $0, condition: condition) } return self.addRoutes(event: event, routes: routes, handler: handler) } - public func addRoutes(event event: E, routes: [Route], handler: Handler) -> Disposable + @discardableResult + public func addRoutes(event: E, routes: [Route], handler: Handler) -> Disposable { - return self.addRoutes(event: .Some(event), routes: routes, handler: handler) + return self.addRoutes(event: .some(event), routes: routes, handler: handler) } - public func addRoutes(event event: Event, routes: [Route], handler: Handler) -> Disposable + @discardableResult + public func addRoutes(event: Event, routes: [Route], handler: Handler) -> Disposable { let routeDisposable = self.addRoutes(event: event, routes: routes) let handlerDisposable = self.addHandler(event: event, handler: handler) @@ -316,7 +325,8 @@ public class Machine // MARK: removeRoute - private func _removeRoute(_routeID: _RouteID) -> Bool + @discardableResult + private func _removeRoute(_ _routeID: _RouteID) -> Bool { guard let event = _routeID.event else { return false } @@ -356,7 +366,8 @@ public class Machine // MARK: addRouteMapping - public func addRouteMapping(routeMapping: RouteMapping) -> Disposable + @discardableResult + public func addRouteMapping(_ routeMapping: RouteMapping) -> Disposable { let key = _createUniqueString() @@ -371,14 +382,15 @@ public class Machine // MARK: addRouteMapping + conditional handler - public func addRouteMapping(routeMapping: RouteMapping, order: HandlerOrder = _defaultOrder, handler: Machine.Handler) -> Disposable + @discardableResult + public func addRouteMapping(_ routeMapping: RouteMapping, order: HandlerOrder = _defaultOrder, handler: Machine.Handler) -> Disposable { let routeDisposable = self.addRouteMapping(routeMapping) - let handlerDisposable = self._addHandler(event: .Any, order: order) { context in + let handlerDisposable = self._addHandler(event: .any, order: order) { context in - guard let preferredToState = routeMapping(event: context.event, fromState: context.fromState, userInfo: context.userInfo) - where preferredToState == context.toState else + guard let preferredToState = routeMapping(event: context.event, fromState: context.fromState, userInfo: context.userInfo), + preferredToState == context.toState else { return } @@ -394,7 +406,8 @@ public class Machine // MARK: removeRouteMapping - private func _removeRouteMapping(routeMappingID: _RouteMappingID) -> Bool + @discardableResult + private func _removeRouteMapping(_ routeMappingID: _RouteMappingID) -> Bool { if self._routeMappings[routeMappingID.key] != nil { self._routeMappings[routeMappingID.key] = nil @@ -411,12 +424,14 @@ public class Machine // MARK: addHandler(event:) - public func addHandler(event event: E, order: HandlerOrder = _defaultOrder, handler: Machine.Handler) -> Disposable + @discardableResult + public func addHandler(event: E, order: HandlerOrder = _defaultOrder, handler: Machine.Handler) -> Disposable { - return self.addHandler(event: .Some(event), order: order, handler: handler) + return self.addHandler(event: .some(event), order: order, handler: handler) } - public func addHandler(event event: Event, order: HandlerOrder = _defaultOrder, handler: Machine.Handler) -> Disposable + @discardableResult + public func addHandler(event: Event, order: HandlerOrder = _defaultOrder, handler: Machine.Handler) -> Disposable { return self._addHandler(event: event, order: order) { context in // skip if not event-based transition @@ -424,13 +439,14 @@ public class Machine return } - if triggeredEvent == event.rawValue || event == .Any { + if triggeredEvent == event.rawValue || event == .any { handler(context) } } } - private func _addHandler(event event: Event, order: HandlerOrder = _defaultOrder, handler: Handler) -> Disposable + @discardableResult + private func _addHandler(event: Event, order: HandlerOrder = _defaultOrder, handler: Handler) -> Disposable { if self._handlers[event] == nil { self._handlers[event] = [] @@ -444,7 +460,7 @@ public class Machine self._handlers[event] = handlerInfos - let handlerID = _HandlerID(event: event, transition: .Any => .Any, key: key) // NOTE: use non-`nil` transition + let handlerID = _HandlerID(event: event, transition: .any => .any, key: key) // NOTE: use non-`nil` transition return ActionDisposable { [weak self] in self?._removeHandler(handlerID) @@ -453,7 +469,8 @@ public class Machine // MARK: addErrorHandler - public func addErrorHandler(order order: HandlerOrder = _defaultOrder, handler: Handler) -> Disposable + @discardableResult + public func addErrorHandler(order: HandlerOrder = _defaultOrder, handler: Handler) -> Disposable { let key = _createUniqueString() @@ -469,7 +486,8 @@ public class Machine // MARK: removeHandler - private func _removeHandler(handlerID: _HandlerID) -> Bool + @discardableResult + private func _removeHandler(_ handlerID: _HandlerID) -> Bool { if let event = handlerID.event { if let handlerInfos_ = self._handlers[event] { @@ -502,12 +520,14 @@ public class Machine infix operator <-! { associativity left } +@discardableResult public func <-! (machine: Machine, event: E) -> Machine { machine.tryEvent(event) return machine } +@discardableResult public func <-! (machine: Machine, tuple: (E, Any?)) -> Machine { machine.tryEvent(tuple.0, userInfo: tuple.1) @@ -537,40 +557,40 @@ internal func _createUniqueString() -> String return uniqueString } -internal func _validTransitions(fromState fromState: S, toState: S) -> [Transition] +internal func _validTransitions(fromState: S, toState: S) -> [Transition] { return [ fromState => toState, - fromState => .Any, - .Any => toState, - .Any => .Any + fromState => .any, + .any => toState, + .any => .any ] } -internal func _canPassCondition(condition: Machine.Condition?, forEvent event: E?, fromState: S, toState: S, userInfo: Any?) -> Bool +internal func _canPassCondition(_ condition: Machine.Condition?, forEvent event: E?, fromState: S, toState: S, userInfo: Any?) -> Bool { return condition?((event, fromState, toState, userInfo)) ?? true } -internal func _insertHandlerIntoArray(inout handlerInfos: [_HandlerInfo], newHandlerInfo: _HandlerInfo) +internal func _insertHandlerIntoArray(_ handlerInfos: inout [_HandlerInfo], newHandlerInfo: _HandlerInfo) { var index = handlerInfos.count - for i in Array(0..(inout handlerInfos: [_HandlerInfo], removingHandlerID: _HandlerID) -> Bool +internal func _removeHandlerFromArray(_ handlerInfos: inout [_HandlerInfo], removingHandlerID: _HandlerID) -> Bool { for i in 0.. public func => (leftStates: [S], right: State) -> Route { // NOTE: don't reuse ".Any => .Any + condition" for efficiency - return Route(transition: .Any => right, condition: { context -> Bool in + return Route(transition: .any => right, condition: { context -> Bool in return leftStates.contains(context.fromState) }) } public func => (leftStates: [S], right: S) -> Route { - return leftStates => .Some(right) + return leftStates => .some(right) } /// e.g. .State0 => [.State1, .State], allowing [0 => 1, 0 => 2] public func => (left: State, rightStates: [S]) -> Route { - return Route(transition: left => .Any, condition: { context -> Bool in + return Route(transition: left => .any, condition: { context -> Bool in return rightStates.contains(context.toState) }) } public func => (left: S, rightStates: [S]) -> Route { - return .Some(left) => rightStates + return .some(left) => rightStates } /// e.g. [.State0, .State1] => [.State, .State3], allowing [0 => 2, 0 => 3, 1 => 2, 1 => 3] public func => (leftStates: [S], rightStates: [S]) -> Route { - return Route(transition: .Any => .Any, condition: { context -> Bool in + return Route(transition: .any => .any, condition: { context -> Bool in return leftStates.contains(context.fromState) && rightStates.contains(context.toState) }) } diff --git a/Sources/StateMachine.swift b/Sources/StateMachine.swift index 847054b..1b22e17 100644 --- a/Sources/StateMachine.swift +++ b/Sources/StateMachine.swift @@ -28,7 +28,7 @@ public final class StateMachine: Machine // MARK: - Init //-------------------------------------------------- - public override init(state: S, initClosure: (StateMachine -> ())? = nil) + public override init(state: S, initClosure: ((StateMachine) -> ())? = nil) { super.init(state: state, initClosure: { machine in initClosure?(machine as! StateMachine) // swiftlint:disable:this force_cast @@ -36,7 +36,7 @@ public final class StateMachine: Machine }) } - public override func configure(closure: StateMachine -> ()) + public override func configure(_ closure: (StateMachine) -> ()) { closure(self) } @@ -47,10 +47,10 @@ public final class StateMachine: Machine /// Check for added routes & routeMappings. /// - Note: This method also checks for event-based-routes. - public func hasRoute(transition: Transition, userInfo: Any? = nil) -> Bool + public func hasRoute(_ transition: Transition, userInfo: Any? = nil) -> Bool { guard let fromState = transition.fromState.rawValue, - toState = transition.toState.rawValue else + let toState = transition.toState.rawValue else { assertionFailure("State = `.Any` is not supported for `hasRoute()` (always returns `false`)") return false @@ -61,7 +61,7 @@ public final class StateMachine: Machine /// Check for added routes & routeMappings. /// - Note: This method also checks for event-based-routes. - public func hasRoute(fromState fromState: S, toState: S, userInfo: Any? = nil) -> Bool + public func hasRoute(fromState: S, toState: S, userInfo: Any? = nil) -> Bool { if self._hasRouteInDict(fromState: fromState, toState: toState, userInfo: userInfo) { return true @@ -76,7 +76,7 @@ public final class StateMachine: Machine } /// Check for `_routes`. - private func _hasRouteInDict(fromState fromState: S, toState: S, userInfo: Any? = nil) -> Bool + private func _hasRouteInDict(fromState: S, toState: S, userInfo: Any? = nil) -> Bool { let validTransitions = _validTransitions(fromState: fromState, toState: toState) @@ -96,7 +96,7 @@ public final class StateMachine: Machine } /// Check for `_routeMappings`. - private func _hasRouteMappingInDict(fromState fromState: S, toState: S, userInfo: Any? = nil) -> S? + private func _hasRouteMappingInDict(fromState: S, toState: S, userInfo: Any? = nil) -> S? { for mapping in self._routeMappings.values { if let preferredToStates = mapping(fromState: fromState, userInfo: userInfo) { @@ -112,13 +112,14 @@ public final class StateMachine: Machine //-------------------------------------------------- /// - Note: This method also checks for event-based-routes. - public func canTryState(toState: S, userInfo: Any? = nil) -> Bool + public func canTryState(_ toState: S, userInfo: Any? = nil) -> Bool { return self.hasRoute(fromState: self.state, toState: toState, userInfo: userInfo) } /// - Note: This method also tries state-change for event-based-routes. - public func tryState(toState: S, userInfo: Any? = nil) -> Bool + @discardableResult + public func tryState(_ toState: S, userInfo: Any? = nil) -> Bool { let fromState = self.state @@ -154,7 +155,7 @@ public final class StateMachine: Machine return false } - private func _validHandlerInfos(fromState fromState: S, toState: S) -> [_HandlerInfo] + private func _validHandlerInfos(fromState: S, toState: S) -> [_HandlerInfo] { var validHandlerInfos: [_HandlerInfo] = [] @@ -168,7 +169,7 @@ public final class StateMachine: Machine } } - validHandlerInfos.sortInPlace { info1, info2 in + validHandlerInfos.sort { info1, info2 in return info1.order < info2.order } @@ -181,13 +182,15 @@ public final class StateMachine: Machine // MARK: addRoute (no-event) - public func addRoute(transition: Transition, condition: Condition? = nil) -> Disposable + @discardableResult + public func addRoute(_ transition: Transition, condition: Condition? = nil) -> Disposable { let route = Route(transition: transition, condition: condition) return self.addRoute(route) } - public func addRoute(route: Route) -> Disposable + @discardableResult + public func addRoute(_ route: Route) -> Disposable { let transition = route.transition let condition = route.condition @@ -202,7 +205,7 @@ public final class StateMachine: Machine keyConditionDict[key] = condition self._routes[transition] = keyConditionDict - let _routeID = _RouteID(event: Optional>.None, transition: transition, key: key) + let _routeID = _RouteID(event: Optional>.none, transition: transition, key: key) return ActionDisposable { [weak self] in self?._removeRoute(_routeID) @@ -211,13 +214,15 @@ public final class StateMachine: Machine // MARK: addRoute (no-event) + conditional handler - public func addRoute(transition: Transition, condition: Condition? = nil, handler: Handler) -> Disposable + @discardableResult + public func addRoute(_ transition: Transition, condition: Condition? = nil, handler: Handler) -> Disposable { let route = Route(transition: transition, condition: condition) return self.addRoute(route, handler: handler) } - public func addRoute(route: Route, handler: Handler) -> Disposable + @discardableResult + public func addRoute(_ route: Route, handler: Handler) -> Disposable { let transition = route.transition let condition = route.condition @@ -238,7 +243,8 @@ public final class StateMachine: Machine // MARK: removeRoute - private func _removeRoute(_routeID: _RouteID) -> Bool + @discardableResult + private func _removeRoute(_ _routeID: _RouteID) -> Bool { guard _routeID.event == nil else { return false @@ -251,7 +257,7 @@ public final class StateMachine: Machine } var keyConditionDict = keyConditionDict_ - let removed = keyConditionDict.removeValueForKey(_routeID.key) != nil + let removed = keyConditionDict.removeValue(forKey: _routeID.key) != nil if keyConditionDict.isEmpty == false { self._routes[transition] = keyConditionDict @@ -271,7 +277,8 @@ public final class StateMachine: Machine /// Add `handler` that is called when `tryState()` succeeds for target `transition`. /// - Note: `handler` will not be invoked for `tryEvent()`. - public func addHandler(transition: Transition, order: HandlerOrder = _defaultOrder, handler: Handler) -> Disposable + @discardableResult + public func addHandler(_ transition: Transition, order: HandlerOrder = _defaultOrder, handler: Handler) -> Disposable { if self._handlers[transition] == nil { self._handlers[transition] = [] @@ -294,7 +301,8 @@ public final class StateMachine: Machine // MARK: removeHandler - private func _removeHandler(handlerID: _HandlerID) -> Bool + @discardableResult + private func _removeHandler(_ handlerID: _HandlerID) -> Bool { if let transition = handlerID.transition { if let handlerInfos_ = self._handlers[transition] { @@ -313,12 +321,13 @@ public final class StateMachine: Machine // MARK: addAnyHandler (event-based & state-based) /// Add `handler` that is called when either `tryEvent()` or `tryState()` succeeds for target `transition`. - public func addAnyHandler(transition: Transition, order: HandlerOrder = _defaultOrder, handler: Handler) -> Disposable + @discardableResult + public func addAnyHandler(_ transition: Transition, order: HandlerOrder = _defaultOrder, handler: Handler) -> Disposable { let disposable1 = self.addHandler(transition, order: order, handler: handler) - let disposable2 = self.addHandler(event: .Any, order: order) { context in - if (transition.fromState == .Any || transition.fromState == context.fromState) && - (transition.toState == .Any || transition.toState == context.toState) + let disposable2 = self.addHandler(event: .any, order: order) { context in + if (transition.fromState == .any || transition.fromState == context.fromState) && + (transition.toState == .any || transition.toState == context.toState) { handler(context) } @@ -341,13 +350,15 @@ public final class StateMachine: Machine // MARK: addRouteChain + conditional handler - public func addRouteChain(chain: TransitionChain, condition: Condition? = nil, handler: Handler) -> Disposable + @discardableResult + public func addRouteChain(_ chain: TransitionChain, condition: Condition? = nil, handler: Handler) -> Disposable { let routeChain = RouteChain(transitionChain: chain, condition: condition) return self.addRouteChain(routeChain, handler: handler) } - public func addRouteChain(chain: RouteChain, handler: Handler) -> Disposable + @discardableResult + public func addRouteChain(_ chain: RouteChain, handler: Handler) -> Disposable { let routeDisposables = chain.routes.map { self.addRoute($0) } let handlerDisposable = self.addChainHandler(chain, handler: handler) @@ -360,29 +371,34 @@ public final class StateMachine: Machine // MARK: addChainHandler - public func addChainHandler(chain: TransitionChain, order: HandlerOrder = _defaultOrder, handler: Handler) -> Disposable + @discardableResult + public func addChainHandler(_ chain: TransitionChain, order: HandlerOrder = _defaultOrder, handler: Handler) -> Disposable { return self.addChainHandler(RouteChain(transitionChain: chain), order: order, handler: handler) } - public func addChainHandler(chain: RouteChain, order: HandlerOrder = _defaultOrder, handler: Handler) -> Disposable + @discardableResult + public func addChainHandler(_ chain: RouteChain, order: HandlerOrder = _defaultOrder, handler: Handler) -> Disposable { return self._addChainHandler(chain, order: order, handler: handler, isError: false) } // MARK: addChainErrorHandler - public func addChainErrorHandler(chain: TransitionChain, order: HandlerOrder = _defaultOrder, handler: Handler) -> Disposable + @discardableResult + public func addChainErrorHandler(_ chain: TransitionChain, order: HandlerOrder = _defaultOrder, handler: Handler) -> Disposable { return self.addChainErrorHandler(RouteChain(transitionChain: chain), order: order, handler: handler) } - public func addChainErrorHandler(chain: RouteChain, order: HandlerOrder = _defaultOrder, handler: Handler) -> Disposable + @discardableResult + public func addChainErrorHandler(_ chain: RouteChain, order: HandlerOrder = _defaultOrder, handler: Handler) -> Disposable { return self._addChainHandler(chain, order: order, handler: handler, isError: true) } - private func _addChainHandler(chain: RouteChain, order: HandlerOrder = _defaultOrder, handler: Handler, isError: Bool) -> Disposable + @discardableResult + private func _addChainHandler(_ chain: RouteChain, order: HandlerOrder = _defaultOrder, handler: Handler, isError: Bool) -> Disposable { var handlerDisposables: [Disposable] = [] @@ -423,7 +439,7 @@ public final class StateMachine: Machine } // increment allCount (+ invoke chainErrorHandler) on any routes - handlerDisposable = self.addHandler(.Any => .Any, order: 150) { context in + handlerDisposable = self.addHandler(.any => .any, order: 150) { context in shouldIncrementChainingCount = true @@ -466,7 +482,8 @@ public final class StateMachine: Machine // MARK: addStateRouteMapping - public func addStateRouteMapping(routeMapping: StateRouteMapping) -> Disposable + @discardableResult + public func addStateRouteMapping(_ routeMapping: StateRouteMapping) -> Disposable { let key = _createUniqueString() @@ -481,16 +498,16 @@ public final class StateMachine: Machine // MARK: addStateRouteMapping + conditional handler - public func addStateRouteMapping(routeMapping: StateRouteMapping, handler: Handler) -> Disposable + @discardableResult + public func addStateRouteMapping(_ routeMapping: StateRouteMapping, handler: Handler) -> Disposable { let routeDisposable = self.addStateRouteMapping(routeMapping) - let handlerDisposable = self.addHandler(.Any => .Any) { context in + let handlerDisposable = self.addHandler(.any => .any) { context in guard context.event == nil else { return } - guard let preferredToStates = routeMapping(fromState: context.fromState, userInfo: context.userInfo) - where preferredToStates.contains(context.toState) else + guard let preferredToStates = routeMapping(fromState: context.fromState, userInfo: context.userInfo), preferredToStates.contains(context.toState) else { return } @@ -506,7 +523,8 @@ public final class StateMachine: Machine // MARK: removeStateRouteMapping - private func _removeStateRouteMapping(routeMappingID: _RouteMappingID) -> Bool + @discardableResult + private func _removeStateRouteMapping(_ routeMappingID: _RouteMappingID) -> Bool { if self._routeMappings[routeMappingID.key] != nil { self._routeMappings[routeMappingID.key] = nil @@ -527,12 +545,14 @@ public final class StateMachine: Machine infix operator <- { associativity left } +@discardableResult public func <- (machine: StateMachine, state: S) -> StateMachine { machine.tryState(state) return machine } +@discardableResult public func <- (machine: StateMachine, tuple: (S, Any?)) -> StateMachine { machine.tryState(tuple.0, userInfo: tuple.1) diff --git a/Sources/StateType.swift b/Sources/StateType.swift index 5fb0df9..8a24252 100644 --- a/Sources/StateType.swift +++ b/Sources/StateType.swift @@ -13,8 +13,8 @@ public protocol StateType: Hashable {} /// `StateType` wrapper for handling `.Any` state. public enum State { - case Some(S) - case Any + case some(S) + case any } extension State: Hashable @@ -22,8 +22,8 @@ extension State: Hashable public var hashValue: Int { switch self { - case .Some(let x): return x.hashValue - case .Any: return _hashValueForAny + case .some(let x): return x.hashValue + case .any: return _hashValueForAny } } } @@ -33,17 +33,17 @@ extension State: RawRepresentable public init(rawValue: S?) { if let rawValue = rawValue { - self = .Some(rawValue) + self = .some(rawValue) } else { - self = .Any + self = .any } } public var rawValue: S? { switch self { - case .Some(let x): return x + case .some(let x): return x default: return nil } } @@ -52,9 +52,9 @@ extension State: RawRepresentable public func == (lhs: State, rhs: State) -> Bool { switch (lhs, rhs) { - case let (.Some(x1), .Some(x2)) where x1 == x2: + case let (.some(x1), .some(x2)) where x1 == x2: return true - case (.Any, .Any): + case (.any, .any): return true default: return false @@ -64,9 +64,9 @@ public func == (lhs: State, rhs: State) -> Bool public func == (lhs: State, rhs: S) -> Bool { switch lhs { - case .Some(let x): + case .some(let x): return x == rhs - case .Any: + case .any: return false } } @@ -74,9 +74,9 @@ public func == (lhs: State, rhs: S) -> Bool public func == (lhs: S, rhs: State) -> Bool { switch rhs { - case .Some(let x): + case .some(let x): return x == lhs - case .Any: + case .any: return false } } diff --git a/Sources/Transition.swift b/Sources/Transition.swift index 9e2a644..c84ead1 100644 --- a/Sources/Transition.swift +++ b/Sources/Transition.swift @@ -23,17 +23,17 @@ public struct Transition: Hashable public init(fromState: S, toState: State) { - self.init(fromState: .Some(fromState), toState: toState) + self.init(fromState: .some(fromState), toState: toState) } public init(fromState: State, toState: S) { - self.init(fromState: fromState, toState: .Some(toState)) + self.init(fromState: fromState, toState: .some(toState)) } public init(fromState: S, toState: S) { - self.init(fromState: .Some(fromState), toState: .Some(toState)) + self.init(fromState: .some(fromState), toState: .some(toState)) } public var hashValue: Int @@ -62,17 +62,17 @@ public func => (left: State, right: State) -> Transition public func => (left: State, right: S) -> Transition { - return left => .Some(right) + return left => .some(right) } public func => (left: S, right: State) -> Transition { - return .Some(left) => right + return .some(left) => right } public func => (left: S, right: S) -> Transition { - return .Some(left) => .Some(right) + return .some(left) => .some(right) } //-------------------------------------------------- diff --git a/Sources/TransitionChain.swift b/Sources/TransitionChain.swift index b2172a6..31b957b 100644 --- a/Sources/TransitionChain.swift +++ b/Sources/TransitionChain.swift @@ -45,7 +45,7 @@ public func => (left: Transition, right: State) -> Transitio public func => (left: Transition, right: S) -> TransitionChain { - return left => .Some(right) + return left => .some(right) } public func => (left: TransitionChain, right: State) -> TransitionChain @@ -55,7 +55,7 @@ public func => (left: TransitionChain, right: State) -> Tran public func => (left: TransitionChain, right: S) -> TransitionChain { - return left => .Some(right) + return left => .some(right) } // e.g. .State0 => (.State1 => .State) @@ -66,7 +66,7 @@ public func => (left: State, right: Transition) -> Transitio public func => (left: S, right: Transition) -> TransitionChain { - return .Some(left) => right + return .some(left) => right } public func => (left: State, right: TransitionChain) -> TransitionChain @@ -76,5 +76,5 @@ public func => (left: State, right: TransitionChain) -> Tran public func => (left: S, right: TransitionChain) -> TransitionChain { - return .Some(left) => right + return .some(left) => right } diff --git a/Sources/_Random.swift b/Sources/_Random.swift index cf38838..1d159f7 100644 --- a/Sources/_Random.swift +++ b/Sources/_Random.swift @@ -12,7 +12,7 @@ import Darwin import Glibc #endif -internal func _random(upperBound: Int) -> Int +internal func _random(_ upperBound: Int) -> Int { #if os(OSX) || os(iOS) return Int(arc4random_uniform(UInt32(upperBound))) diff --git a/SwiftState.xcodeproj/project.pbxproj b/SwiftState.xcodeproj/project.pbxproj index 8f67de5..3552690 100644 --- a/SwiftState.xcodeproj/project.pbxproj +++ b/SwiftState.xcodeproj/project.pbxproj @@ -312,7 +312,7 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 0700; - LastUpgradeCheck = 0700; + LastUpgradeCheck = 0800; ORGANIZATIONNAME = "Yasuhiro Inami"; TargetAttributes = { 1FA61FFF1996601000460108 = { @@ -432,8 +432,10 @@ CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; @@ -443,6 +445,7 @@ ENABLE_TESTABILITY = YES; GCC_C_LANGUAGE_STANDARD = gnu99; GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; GCC_OPTIMIZATION_LEVEL = 0; GCC_PREPROCESSOR_DEFINITIONS = ( "DEBUG=1", @@ -477,8 +480,10 @@ CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; @@ -488,6 +493,7 @@ ENABLE_NS_ASSERTIONS = NO; ENABLE_STRICT_OBJC_MSGSEND = YES; GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_NO_COMMON_BLOCKS = YES; GCC_WARN_64_TO_32_BIT_CONVERSION = YES; GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; GCC_WARN_UNDECLARED_SELECTOR = YES; @@ -516,7 +522,7 @@ PRODUCT_BUNDLE_IDENTIFIER = "com.inamiy.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(PROJECT_NAME)"; SKIP_INSTALL = YES; - SWIFT_VERSION = 2.3; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -536,7 +542,7 @@ PRODUCT_BUNDLE_IDENTIFIER = "com.inamiy.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(PROJECT_NAME)"; SKIP_INSTALL = YES; - SWIFT_VERSION = 2.3; + SWIFT_VERSION = 3.0; }; name = Release; }; @@ -556,7 +562,7 @@ INFOPLIST_FILE = Tests/Info.plist; PRODUCT_BUNDLE_IDENTIFIER = "com.inamiy.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 2.3; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -572,7 +578,7 @@ INFOPLIST_FILE = Tests/Info.plist; PRODUCT_BUNDLE_IDENTIFIER = "com.inamiy.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 2.3; + SWIFT_VERSION = 3.0; }; name = Release; }; diff --git a/SwiftState.xcodeproj/xcshareddata/xcschemes/SwiftState.xcscheme b/SwiftState.xcodeproj/xcshareddata/xcschemes/SwiftState.xcscheme index 87e399e..9e845a5 100644 --- a/SwiftState.xcodeproj/xcshareddata/xcschemes/SwiftState.xcscheme +++ b/SwiftState.xcodeproj/xcshareddata/xcschemes/SwiftState.xcscheme @@ -1,6 +1,6 @@ (state: .State0) { machine in + let machine = StateMachine(state: .state0) { machine in - machine.addRoute(.State0 => .State1) - machine.addRoute(.Any => .State2) { context in print("Any => 2, msg=\(context.userInfo)") } - machine.addRoute(.State2 => .Any) { context in print("2 => Any, msg=\(context.userInfo)") } + machine.addRoute(.state0 => .state1) + machine.addRoute(.any => .state2) { context in print("Any => 2, msg=\(context.userInfo)") } + machine.addRoute(.state2 => .any) { context in print("2 => Any, msg=\(context.userInfo)") } // add handler (`context = (event, fromState, toState, userInfo)`) - machine.addHandler(.State0 => .State1) { context in + machine.addHandler(.state0 => .state1) { context in print("0 => 1") } @@ -32,70 +32,70 @@ class BasicTests: _TestCase } // initial - XCTAssertEqual(machine.state, MyState.State0) + XCTAssertEqual(machine.state, MyState.state0) // tryState 0 => 1 => 2 => 1 => 0 - machine <- .State1 - XCTAssertEqual(machine.state, MyState.State1) + machine <- .state1 + XCTAssertEqual(machine.state, MyState.state1) - machine <- (.State2, "Hello") - XCTAssertEqual(machine.state, MyState.State2) + machine <- (.state2, "Hello") + XCTAssertEqual(machine.state, MyState.state2) - machine <- (.State1, "Bye") - XCTAssertEqual(machine.state, MyState.State1) + machine <- (.state1, "Bye") + XCTAssertEqual(machine.state, MyState.state1) - machine <- .State0 // fail: no 1 => 0 - XCTAssertEqual(machine.state, MyState.State1) + machine <- .state0 // fail: no 1 => 0 + XCTAssertEqual(machine.state, MyState.state1) } func testREADME_tryEvent() { - let machine = StateMachine(state: .State0) { machine in + let machine = StateMachine(state: .state0) { machine in // add 0 => 1 => 2 - machine.addRoutes(event: .Event0, transitions: [ - .State0 => .State1, - .State1 => .State2, + machine.addRoutes(event: .event0, transitions: [ + .state0 => .state1, + .state1 => .state2, ]) // add event handler - machine.addHandler(event: .Event0) { context in + machine.addHandler(event: .event0) { context in print(".Event0 triggered!") } } // initial - XCTAssertEqual(machine.state, MyState.State0) + XCTAssertEqual(machine.state, MyState.state0) // tryEvent - machine <-! .Event0 - XCTAssertEqual(machine.state, MyState.State1) + machine <-! .event0 + XCTAssertEqual(machine.state, MyState.state1) // tryEvent - machine <-! .Event0 - XCTAssertEqual(machine.state, MyState.State2) + machine <-! .event0 + XCTAssertEqual(machine.state, MyState.state2) // tryEvent (fails) - machine <-! .Event0 - XCTAssertEqual(machine.state, MyState.State2, "Event0 doesn't have 2 => Any") + machine <-! .event0 + XCTAssertEqual(machine.state, MyState.state2, "Event0 doesn't have 2 => Any") } func testREADME_routeMapping() { - let machine = Machine(state: .Str("initial")) { machine in + let machine = Machine(state: .str("initial")) { machine in machine.addRouteMapping { event, fromState, userInfo -> StrState? in // no route for no-event guard let event = event else { return nil } switch (event, fromState) { - case (.Str("gogogo"), .Str("initial")): - return .Str("Phase 1") - case (.Str("gogogo"), .Str("Phase 1")): - return .Str("Phase 2") - case (.Str("finish"), .Str("Phase 2")): - return .Str("end") + case (.str("gogogo"), .str("initial")): + return .str("Phase 1") + case (.str("gogogo"), .str("Phase 1")): + return .str("Phase 2") + case (.str("finish"), .str("Phase 2")): + return .str("end") default: return nil } @@ -104,30 +104,30 @@ class BasicTests: _TestCase } // initial - XCTAssertEqual(machine.state, StrState.Str("initial")) + XCTAssertEqual(machine.state, StrState.str("initial")) // tryEvent (fails) - machine <-! .Str("go?") - XCTAssertEqual(machine.state, StrState.Str("initial"), "No change.") + machine <-! .str("go?") + XCTAssertEqual(machine.state, StrState.str("initial"), "No change.") // tryEvent - machine <-! .Str("gogogo") - XCTAssertEqual(machine.state, StrState.Str("Phase 1")) + machine <-! .str("gogogo") + XCTAssertEqual(machine.state, StrState.str("Phase 1")) // tryEvent (fails) - machine <-! .Str("finish") - XCTAssertEqual(machine.state, StrState.Str("Phase 1"), "No change.") + machine <-! .str("finish") + XCTAssertEqual(machine.state, StrState.str("Phase 1"), "No change.") // tryEvent - machine <-! .Str("gogogo") - XCTAssertEqual(machine.state, StrState.Str("Phase 2")) + machine <-! .str("gogogo") + XCTAssertEqual(machine.state, StrState.str("Phase 2")) // tryEvent (fails) - machine <-! .Str("gogogo") - XCTAssertEqual(machine.state, StrState.Str("Phase 2"), "No change.") + machine <-! .str("gogogo") + XCTAssertEqual(machine.state, StrState.str("Phase 2"), "No change.") // tryEvent - machine <-! .Str("finish") - XCTAssertEqual(machine.state, StrState.Str("end")) + machine <-! .str("finish") + XCTAssertEqual(machine.state, StrState.str("end")) } } diff --git a/Tests/EventTests.swift b/Tests/EventTests.swift index 0d1e49a..4626005 100644 --- a/Tests/EventTests.swift +++ b/Tests/EventTests.swift @@ -13,27 +13,27 @@ class EventTests: _TestCase { func testInit_event() { - let event = Event(rawValue: .Event0) - XCTAssertTrue(event == .Event0) - XCTAssertTrue(.Event0 == event) + let event = Event(rawValue: .event0) + XCTAssertTrue(event == .event0) + XCTAssertTrue(.event0 == event) } func testInit_nil() { let event = Event(rawValue: nil) - XCTAssertTrue(event == .Any) - XCTAssertTrue(.Any == event) + XCTAssertTrue(event == .any) + XCTAssertTrue(.any == event) } func testRawValue_event() { - let event = Event.Some(.Event0) - XCTAssertTrue(event.rawValue == .Event0) + let event = Event.some(.event0) + XCTAssertTrue(event.rawValue == .event0) } func testRawValue_any() { - let event = Event.Any + let event = Event.any XCTAssertTrue(event.rawValue == nil) } } diff --git a/Tests/HierarchicalMachineTests.swift b/Tests/HierarchicalMachineTests.swift index 7ab399c..de76c17 100644 --- a/Tests/HierarchicalMachineTests.swift +++ b/Tests/HierarchicalMachineTests.swift @@ -11,18 +11,18 @@ import XCTest private enum _MainState: StateType { - case MainState0 - case SubMachine1(_SubState) - case SubMachine2(_SubState) + case mainState0 + case subMachine1(_SubState) + case subMachine2(_SubState) var hashValue: Int { switch self { - case .MainState0: + case .mainState0: return "MainState0".hashValue - case let .SubMachine1(state): + case let .subMachine1(state): return "SubMachine1-\(state)".hashValue - case let .SubMachine2(state): + case let .subMachine2(state): return "SubMachine2-\(state)".hashValue } } @@ -30,17 +30,17 @@ private enum _MainState: StateType private enum _SubState: StateType { - case SubState0, SubState1, SubState2 + case subState0, subState1, subState2 } private func == (lhs: _MainState, rhs: _MainState) -> Bool { switch (lhs, rhs) { - case (.MainState0, .MainState0): + case (.mainState0, .mainState0): return true - case let (.SubMachine1(state1), .SubMachine1(state2)): + case let (.subMachine1(state1), .subMachine1(state2)): return state1 == state2 - case let (.SubMachine2(state1), .SubMachine2(state2)): + case let (.subMachine2(state1), .subMachine2(state2)): return state1 == state2 default: return false @@ -73,31 +73,31 @@ class HierarchicalMachineTests: _TestCase { super.setUp() - let subMachine1 = StateMachine<_SubState, NoEvent>(state: .SubState0) { subMachine1 in + let subMachine1 = StateMachine<_SubState, NoEvent>(state: .subState0) { subMachine1 in // add Sub1-0 => Sub1-1 - subMachine1.addRoute(.SubState0 => .SubState1) + subMachine1.addRoute(.subState0 => .subState1) - subMachine1.addHandler(.Any => .Any) { print("[Sub1] \($0.fromState) => \($0.toState)") } + subMachine1.addHandler(.any => .any) { print("[Sub1] \($0.fromState) => \($0.toState)") } subMachine1.addErrorHandler { print("[ERROR][Sub1] \($0.fromState) => \($0.toState)") } } - let subMachine2 = StateMachine<_SubState, NoEvent>(state: .SubState0) { subMachine2 in + let subMachine2 = StateMachine<_SubState, NoEvent>(state: .subState0) { subMachine2 in // add Sub2-0 => Sub2-1 - subMachine2.addRoute(.SubState0 => .SubState1) + subMachine2.addRoute(.subState0 => .subState1) - subMachine2.addHandler(.Any => .Any) { print("[Sub2] \($0.fromState) => \($0.toState)") } + subMachine2.addHandler(.any => .any) { print("[Sub2] \($0.fromState) => \($0.toState)") } subMachine2.addErrorHandler { print("[ERROR][Sub2] \($0.fromState) => \($0.toState)") } } - let mainMachine = StateMachine<_MainState, NoEvent>(state: .MainState0) { mainMachine in + let mainMachine = StateMachine<_MainState, NoEvent>(state: .mainState0) { mainMachine in // add routes & handle for same-subMachine internal transitions - mainMachine.addRoute(.Any => .Any, condition: { _, fromState, toState, userInfo in + mainMachine.addRoute(.any => .any, condition: { _, fromState, toState, userInfo in switch (fromState, toState) { - case let (.SubMachine1(state1), .SubMachine1(state2)): + case let (.subMachine1(state1), .subMachine1(state2)): return subMachine1.hasRoute(fromState: state1, toState: state2) - case let (.SubMachine2(state1), .SubMachine2(state2)): + case let (.subMachine2(state1), .subMachine2(state2)): return subMachine2.hasRoute(fromState: state1, toState: state2) default: return false @@ -105,9 +105,9 @@ class HierarchicalMachineTests: _TestCase }, handler: { _, fromState, toState, userInfo in switch (fromState, toState) { - case let (.SubMachine1, .SubMachine1(state2)): + case let (.subMachine1, .subMachine1(state2)): subMachine1 <- state2 - case let (.SubMachine2, .SubMachine2(state2)): + case let (.subMachine2, .subMachine2(state2)): subMachine2 <- state2 default: break @@ -120,16 +120,16 @@ class HierarchicalMachineTests: _TestCase // NOTE: use external submachine's states only for evaluating `toState`, but not for `fromState` switch fromState { // "Main0" => "Sub1-0" - case .MainState0 where subMachine1.state == .SubState0: - return .SubMachine1(.SubState0) + case .mainState0 where subMachine1.state == .subState0: + return .subMachine1(.subState0) // "Sub1-1" => "Sub2-0" - case let .SubMachine1(state) where state == .SubState1 && subMachine2.state == .SubState0: - return .SubMachine2(.SubState0) + case let .subMachine1(state) where state == .subState1 && subMachine2.state == .subState0: + return .subMachine2(.subState0) // "Sub2-1" => "Main0" - case let .SubMachine2(state) where state == .SubState1: - return .MainState0 + case let .subMachine2(state) where state == .subState1: + return .mainState0 default: return nil @@ -137,7 +137,7 @@ class HierarchicalMachineTests: _TestCase } - mainMachine.addHandler(.Any => .Any) { print("[Main] \($0.fromState) => \($0.toState)") } + mainMachine.addHandler(.any => .any) { print("[Main] \($0.fromState) => \($0.toState)") } mainMachine.addErrorHandler { print("[ERROR][Main] \($0.fromState) => \($0.toState)") } } @@ -154,21 +154,21 @@ class HierarchicalMachineTests: _TestCase let subMachine2 = self.subMachine2! // initial - XCTAssertTrue(mainMachine.state == .MainState0) - XCTAssertTrue(subMachine1.state == .SubState0) - XCTAssertTrue(subMachine2.state == .SubState0) + XCTAssertTrue(mainMachine.state == .mainState0) + XCTAssertTrue(subMachine1.state == .subState0) + XCTAssertTrue(subMachine2.state == .subState0) // subMachine1 internal routes - XCTAssertFalse(mainMachine.hasRoute(.SubMachine1(.SubState0) => .SubMachine1(.SubState0))) - XCTAssertTrue(mainMachine.hasRoute(.SubMachine1(.SubState0) => .SubMachine1(.SubState1))) - XCTAssertFalse(mainMachine.hasRoute(.SubMachine1(.SubState1) => .SubMachine1(.SubState0))) - XCTAssertFalse(mainMachine.hasRoute(.SubMachine1(.SubState1) => .SubMachine1(.SubState1))) + XCTAssertFalse(mainMachine.hasRoute(.subMachine1(.subState0) => .subMachine1(.subState0))) + XCTAssertTrue(mainMachine.hasRoute(.subMachine1(.subState0) => .subMachine1(.subState1))) + XCTAssertFalse(mainMachine.hasRoute(.subMachine1(.subState1) => .subMachine1(.subState0))) + XCTAssertFalse(mainMachine.hasRoute(.subMachine1(.subState1) => .subMachine1(.subState1))) // subMachine2 internal routes - XCTAssertFalse(mainMachine.hasRoute(.SubMachine2(.SubState0) => .SubMachine2(.SubState0))) - XCTAssertTrue(mainMachine.hasRoute(.SubMachine2(.SubState0) => .SubMachine2(.SubState1))) - XCTAssertFalse(mainMachine.hasRoute(.SubMachine2(.SubState1) => .SubMachine2(.SubState0))) - XCTAssertFalse(mainMachine.hasRoute(.SubMachine2(.SubState1) => .SubMachine2(.SubState1))) + XCTAssertFalse(mainMachine.hasRoute(.subMachine2(.subState0) => .subMachine2(.subState0))) + XCTAssertTrue(mainMachine.hasRoute(.subMachine2(.subState0) => .subMachine2(.subState1))) + XCTAssertFalse(mainMachine.hasRoute(.subMachine2(.subState1) => .subMachine2(.subState0))) + XCTAssertFalse(mainMachine.hasRoute(.subMachine2(.subState1) => .subMachine2(.subState1))) } /// `mainMachine.hasRoute()` test to check switchable submachines @@ -182,35 +182,35 @@ class HierarchicalMachineTests: _TestCase // (external routes between submachines = SubState1, SubState2, or nil) // initial - XCTAssertTrue(mainMachine.state == .MainState0) - XCTAssertTrue(subMachine1.state == .SubState0) - XCTAssertTrue(subMachine2.state == .SubState0) + XCTAssertTrue(mainMachine.state == .mainState0) + XCTAssertTrue(subMachine1.state == .subState0) + XCTAssertTrue(subMachine2.state == .subState0) // from Main0 - XCTAssertTrue(mainMachine.hasRoute(.MainState0 => .SubMachine1(.SubState0))) - XCTAssertFalse(mainMachine.hasRoute(.MainState0 => .SubMachine1(.SubState1))) - XCTAssertFalse(mainMachine.hasRoute(.MainState0 => .SubMachine2(.SubState0))) - XCTAssertFalse(mainMachine.hasRoute(.MainState0 => .SubMachine2(.SubState1))) + XCTAssertTrue(mainMachine.hasRoute(.mainState0 => .subMachine1(.subState0))) + XCTAssertFalse(mainMachine.hasRoute(.mainState0 => .subMachine1(.subState1))) + XCTAssertFalse(mainMachine.hasRoute(.mainState0 => .subMachine2(.subState0))) + XCTAssertFalse(mainMachine.hasRoute(.mainState0 => .subMachine2(.subState1))) // from Sub1-0 - XCTAssertFalse(mainMachine.hasRoute(.SubMachine1(.SubState0) => .SubMachine2(.SubState0))) - XCTAssertFalse(mainMachine.hasRoute(.SubMachine1(.SubState0) => .SubMachine2(.SubState1))) - XCTAssertFalse(mainMachine.hasRoute(.SubMachine1(.SubState0) => .MainState0)) + XCTAssertFalse(mainMachine.hasRoute(.subMachine1(.subState0) => .subMachine2(.subState0))) + XCTAssertFalse(mainMachine.hasRoute(.subMachine1(.subState0) => .subMachine2(.subState1))) + XCTAssertFalse(mainMachine.hasRoute(.subMachine1(.subState0) => .mainState0)) // from Sub1-1 - XCTAssertTrue(mainMachine.hasRoute(.SubMachine1(.SubState1) => .SubMachine2(.SubState0))) - XCTAssertFalse(mainMachine.hasRoute(.SubMachine1(.SubState1) => .SubMachine2(.SubState1))) - XCTAssertFalse(mainMachine.hasRoute(.SubMachine1(.SubState1) => .MainState0)) + XCTAssertTrue(mainMachine.hasRoute(.subMachine1(.subState1) => .subMachine2(.subState0))) + XCTAssertFalse(mainMachine.hasRoute(.subMachine1(.subState1) => .subMachine2(.subState1))) + XCTAssertFalse(mainMachine.hasRoute(.subMachine1(.subState1) => .mainState0)) // from Sub2-0 - XCTAssertFalse(mainMachine.hasRoute(.SubMachine2(.SubState0) => .SubMachine1(.SubState0))) - XCTAssertFalse(mainMachine.hasRoute(.SubMachine2(.SubState0) => .SubMachine1(.SubState1))) - XCTAssertFalse(mainMachine.hasRoute(.SubMachine2(.SubState0) => .MainState0)) + XCTAssertFalse(mainMachine.hasRoute(.subMachine2(.subState0) => .subMachine1(.subState0))) + XCTAssertFalse(mainMachine.hasRoute(.subMachine2(.subState0) => .subMachine1(.subState1))) + XCTAssertFalse(mainMachine.hasRoute(.subMachine2(.subState0) => .mainState0)) // from Sub2-1 - XCTAssertFalse(mainMachine.hasRoute(.SubMachine2(.SubState1) => .SubMachine1(.SubState0))) - XCTAssertFalse(mainMachine.hasRoute(.SubMachine2(.SubState1) => .SubMachine1(.SubState1))) - XCTAssertTrue(mainMachine.hasRoute(.SubMachine2(.SubState1) => .MainState0)) + XCTAssertFalse(mainMachine.hasRoute(.subMachine2(.subState1) => .subMachine1(.subState0))) + XCTAssertFalse(mainMachine.hasRoute(.subMachine2(.subState1) => .subMachine1(.subState1))) + XCTAssertTrue(mainMachine.hasRoute(.subMachine2(.subState1) => .mainState0)) } @@ -219,39 +219,39 @@ class HierarchicalMachineTests: _TestCase let mainMachine = self.mainMachine! // initial - XCTAssertTrue(mainMachine.state == .MainState0) + XCTAssertTrue(mainMachine.state == .mainState0) // Main0 => Sub1-0 - mainMachine <- .SubMachine1(.SubState0) - XCTAssertTrue(mainMachine.state == .SubMachine1(.SubState0)) + mainMachine <- .subMachine1(.subState0) + XCTAssertTrue(mainMachine.state == .subMachine1(.subState0)) // Sub1-0 => Sub1-1 (Sub1 internal transition) - mainMachine <- .SubMachine1(.SubState1) - XCTAssertTrue(mainMachine.state == .SubMachine1(.SubState1)) + mainMachine <- .subMachine1(.subState1) + XCTAssertTrue(mainMachine.state == .subMachine1(.subState1)) // Sub1-1 => Sub1-2 (Sub1 internal transition, but fails) - mainMachine <- .SubMachine1(.SubState2) - XCTAssertTrue(mainMachine.state == .SubMachine1(.SubState1), "No change.") + mainMachine <- .subMachine1(.subState2) + XCTAssertTrue(mainMachine.state == .subMachine1(.subState1), "No change.") // Sub1-1 => Sub2-2 (fails) - mainMachine <- .SubMachine2(.SubState2) - XCTAssertTrue(mainMachine.state == .SubMachine1(.SubState1), "No change.") + mainMachine <- .subMachine2(.subState2) + XCTAssertTrue(mainMachine.state == .subMachine1(.subState1), "No change.") // Sub1-1 => Sub2-0 - mainMachine <- .SubMachine2(.SubState0) - XCTAssertTrue(mainMachine.state == .SubMachine2(.SubState0)) + mainMachine <- .subMachine2(.subState0) + XCTAssertTrue(mainMachine.state == .subMachine2(.subState0)) // Sub2-0 => Main0 (fails) - mainMachine <- .MainState0 - XCTAssertTrue(mainMachine.state == .SubMachine2(.SubState0), "No change.") + mainMachine <- .mainState0 + XCTAssertTrue(mainMachine.state == .subMachine2(.subState0), "No change.") // Sub2-0 => Sub2-1 - mainMachine <- .SubMachine2(.SubState1) - XCTAssertTrue(mainMachine.state == .SubMachine2(.SubState1)) + mainMachine <- .subMachine2(.subState1) + XCTAssertTrue(mainMachine.state == .subMachine2(.subState1)) // Sub2-1 => Main - mainMachine <- .MainState0 - XCTAssertTrue(mainMachine.state == .MainState0) + mainMachine <- .mainState0 + XCTAssertTrue(mainMachine.state == .mainState0) } @@ -262,23 +262,23 @@ class HierarchicalMachineTests: _TestCase var didPass = false // NOTE: this handler is added to mainMachine and doesn't make submachines dirty - mainMachine.addHandler(.MainState0 => .SubMachine1(.SubState0)) { context in + mainMachine.addHandler(.mainState0 => .subMachine1(.subState0)) { context in print("[Main] 1-1 => 1-2 (specific)") didPass = true } // initial - XCTAssertTrue(mainMachine.state == .MainState0) + XCTAssertTrue(mainMachine.state == .mainState0) XCTAssertFalse(didPass) // Main0 => Sub1-1 (fails) - mainMachine <- .SubMachine1(.SubState1) - XCTAssertTrue(mainMachine.state == .MainState0, "No change.") + mainMachine <- .subMachine1(.subState1) + XCTAssertTrue(mainMachine.state == .mainState0, "No change.") XCTAssertFalse(didPass) // Main0 => Sub1-0 - mainMachine <- .SubMachine1(.SubState0) - XCTAssertTrue(mainMachine.state == .SubMachine1(.SubState0)) + mainMachine <- .subMachine1(.subState0) + XCTAssertTrue(mainMachine.state == .subMachine1(.subState0)) XCTAssertTrue(didPass) } diff --git a/Tests/MachineTests.swift b/Tests/MachineTests.swift index 0b567d9..1414e19 100644 --- a/Tests/MachineTests.swift +++ b/Tests/MachineTests.swift @@ -13,13 +13,13 @@ class MachineTests: _TestCase { func testConfigure() { - let machine = Machine(state: .State0) + let machine = Machine(state: .state0) machine.configure { - $0.addRoutes(event: .Event0, transitions: [ .State0 => .State1 ]) + $0.addRoutes(event: .event0, transitions: [ .state0 => .state1 ]) } - XCTAssertTrue(machine.canTryEvent(.Event0) != nil) + XCTAssertTrue(machine.canTryEvent(.event0) != nil) } //-------------------------------------------------- @@ -28,146 +28,146 @@ class MachineTests: _TestCase func testCanTryEvent() { - let machine = Machine(state: .State0) + let machine = Machine(state: .state0) // add 0 => 1 & 1 => 2 // (NOTE: this is not chaining e.g. 0 => 1 => 2) - machine.addRoutes(event: .Event0, transitions: [ - .State0 => .State1, - .State1 => .State2, + machine.addRoutes(event: .event0, transitions: [ + .state0 => .state1, + .state1 => .state2, ]) - XCTAssertTrue(machine.canTryEvent(.Event0) != nil) + XCTAssertTrue(machine.canTryEvent(.event0) != nil) } func testTryEvent() { - let machine = Machine(state: .State0) { machine in + let machine = Machine(state: .state0) { machine in // add 0 => 1 => 2 - machine.addRoutes(event: .Event0, transitions: [ - .State0 => .State1, - .State1 => .State2, + machine.addRoutes(event: .event0, transitions: [ + .state0 => .state1, + .state1 => .state2, ]) } // initial - XCTAssertEqual(machine.state, MyState.State0) + XCTAssertEqual(machine.state, MyState.state0) // tryEvent - machine <-! .Event0 - XCTAssertEqual(machine.state, MyState.State1) + machine <-! .event0 + XCTAssertEqual(machine.state, MyState.state1) // tryEvent - machine <-! .Event0 - XCTAssertEqual(machine.state, MyState.State2) + machine <-! .event0 + XCTAssertEqual(machine.state, MyState.state2) // tryEvent - machine <-! .Event0 - XCTAssertEqual(machine.state, MyState.State2, "Event0 doesn't have 2 => Any") + machine <-! .event0 + XCTAssertEqual(machine.state, MyState.state2, "Event0 doesn't have 2 => Any") } func testTryEvent_userInfo() { var userInfo: Any? = nil - let machine = Machine(state: .State0) { machine in + let machine = Machine(state: .state0) { machine in // add 0 => 1 => 2 - machine.addRoutes(event: .Event0, transitions: [ - .State0 => .State1, - .State1 => .State2, + machine.addRoutes(event: .event0, transitions: [ + .state0 => .state1, + .state1 => .state2, ], handler: { context in userInfo = context.userInfo }) } // initial - XCTAssertEqual(machine.state, MyState.State0) + XCTAssertEqual(machine.state, MyState.state0) XCTAssertNil(userInfo) // tryEvent - machine <-! (.Event0, "gogogo") - XCTAssertEqual(machine.state, MyState.State1) + machine <-! (.event0, "gogogo") + XCTAssertEqual(machine.state, MyState.state1) XCTAssertTrue(userInfo as? String == "gogogo") // tryEvent - machine <-! (.Event0, "done") - XCTAssertEqual(machine.state, MyState.State2) + machine <-! (.event0, "done") + XCTAssertEqual(machine.state, MyState.state2) XCTAssertTrue(userInfo as? String == "done") } func testTryEvent_twice() { - let machine = Machine(state: .State0) { machine in + let machine = Machine(state: .state0) { machine in // add 0 => 1 - machine.addRoutes(event: .Event0, transitions: [ - .State0 => .State1, + machine.addRoutes(event: .event0, transitions: [ + .state0 => .state1, ]) // add 0 => 1 - machine.addRoutes(event: .Event1, transitions: [ - .State1 => .State2, + machine.addRoutes(event: .event1, transitions: [ + .state1 => .state2, ]) } // tryEvent (twice) - machine <-! .Event0 <-! .Event1 - XCTAssertEqual(machine.state, MyState.State2) + machine <-! .event0 <-! .event1 + XCTAssertEqual(machine.state, MyState.state2) } func testTryEvent_string() { - let machine = Machine(state: .State0) + let machine = Machine(state: .state0) // add 0 => 1 => 2 machine.addRoutes(event: "Run", transitions: [ - .State0 => .State1, - .State1 => .State2, + .state0 => .state1, + .state1 => .state2, ]) // tryEvent machine <-! "Run" - XCTAssertEqual(machine.state, MyState.State1) + XCTAssertEqual(machine.state, MyState.state1) // tryEvent machine <-! "Run" - XCTAssertEqual(machine.state, MyState.State2) + XCTAssertEqual(machine.state, MyState.state2) // tryEvent machine <-! "Run" - XCTAssertEqual(machine.state, MyState.State2, "Event=Run doesn't have 2 => Any") + XCTAssertEqual(machine.state, MyState.state2, "Event=Run doesn't have 2 => Any") } // https://github.com/ReactKit/SwiftState/issues/20 func testTryEvent_issue20() { - let machine = Machine(state: MyState.State2) { machine in - machine.addRoutes(event: .Event0, transitions: [.Any => .State0]) + let machine = Machine(state: MyState.state2) { machine in + machine.addRoutes(event: .event0, transitions: [.any => .state0]) } - XCTAssertEqual(machine.state, MyState.State2) + XCTAssertEqual(machine.state, MyState.state2) // tryEvent - machine <-! .Event0 - XCTAssertEqual(machine.state, MyState.State0) + machine <-! .event0 + XCTAssertEqual(machine.state, MyState.state0) } // Fix for transitioning of routes w/ multiple from-states // https://github.com/ReactKit/SwiftState/pull/32 func testTryEvent_issue32() { - let machine = Machine(state: .State0) { machine in - machine.addRoutes(event: .Event0, transitions: [ .State0 => .State1 ]) - machine.addRoutes(event: .Event1, routes: [ [ .State1, .State2 ] => .State3 ]) + let machine = Machine(state: .state0) { machine in + machine.addRoutes(event: .event0, transitions: [ .state0 => .state1 ]) + machine.addRoutes(event: .event1, routes: [ [ .state1, .state2 ] => .state3 ]) } - XCTAssertEqual(machine.state, MyState.State0) + XCTAssertEqual(machine.state, MyState.state0) // tryEvent - machine <-! .Event0 - XCTAssertEqual(machine.state, MyState.State1) + machine <-! .event0 + XCTAssertEqual(machine.state, MyState.state1) // tryEvent - machine <-! .Event1 - XCTAssertEqual(machine.state, MyState.State3) + machine <-! .event1 + XCTAssertEqual(machine.state, MyState.state3) } //-------------------------------------------------- @@ -176,58 +176,58 @@ class MachineTests: _TestCase func testAddRoute_multiple() { - let machine = Machine(state: .State0) { machine in + let machine = Machine(state: .state0) { machine in // add 0 => 1 => 2 - machine.addRoutes(event: .Event0, transitions: [ - .State0 => .State1, - .State1 => .State2, + machine.addRoutes(event: .event0, transitions: [ + .state0 => .state1, + .state1 => .state2, ]) // add 2 => 1 => 0 - machine.addRoutes(event: .Event1, transitions: [ - .State2 => .State1, - .State1 => .State0, + machine.addRoutes(event: .event1, transitions: [ + .state2 => .state1, + .state1 => .state0, ]) } // initial - XCTAssertEqual(machine.state, MyState.State0) + XCTAssertEqual(machine.state, MyState.state0) // tryEvent - machine <-! .Event1 - XCTAssertEqual(machine.state, MyState.State0, "Event1 doesn't have 0 => Any.") + machine <-! .event1 + XCTAssertEqual(machine.state, MyState.state0, "Event1 doesn't have 0 => Any.") // tryEvent - machine <-! .Event0 - XCTAssertEqual(machine.state, MyState.State1) + machine <-! .event0 + XCTAssertEqual(machine.state, MyState.state1) // tryEvent - machine <-! .Event0 - XCTAssertEqual(machine.state, MyState.State2) + machine <-! .event0 + XCTAssertEqual(machine.state, MyState.state2) // tryEvent - machine <-! .Event0 - XCTAssertEqual(machine.state, MyState.State2, "Event0 doesn't have 2 => Any.") + machine <-! .event0 + XCTAssertEqual(machine.state, MyState.state2, "Event0 doesn't have 2 => Any.") // tryEvent - machine <-! .Event1 - XCTAssertEqual(machine.state, MyState.State1) + machine <-! .event1 + XCTAssertEqual(machine.state, MyState.state1) // tryEvent - machine <-! .Event1 - XCTAssertEqual(machine.state, MyState.State0) + machine <-! .event1 + XCTAssertEqual(machine.state, MyState.state0) } func testAddRoute_handler() { var invokeCount = 0 - let machine = Machine(state: .State0) { machine in + let machine = Machine(state: .state0) { machine in // add 0 => 1 => 2 - machine.addRoutes(event: .Event0, transitions: [ - .State0 => .State1, - .State1 => .State2, + machine.addRoutes(event: .event0, transitions: [ + .state0 => .state1, + .state1 => .state2, ], handler: { context in invokeCount += 1 return @@ -235,12 +235,12 @@ class MachineTests: _TestCase } // tryEvent - machine <-! .Event0 - XCTAssertEqual(machine.state, MyState.State1) + machine <-! .event0 + XCTAssertEqual(machine.state, MyState.state1) // tryEvent - machine <-! .Event0 - XCTAssertEqual(machine.state, MyState.State2) + machine <-! .event0 + XCTAssertEqual(machine.state, MyState.state2) XCTAssertEqual(invokeCount, 2) } @@ -249,15 +249,15 @@ class MachineTests: _TestCase { var invokeCount = 0 - let machine = Machine(state: .State0) { machine in + let machine = Machine(state: .state0) { machine in // add 0 => 1 => 2 - let routeDisposable = machine.addRoutes(event: .Event0, transitions: [ - .State0 => .State1, - .State1 => .State2, + let routeDisposable = machine.addRoutes(event: .event0, transitions: [ + .state0 => .state1, + .state1 => .state2, ]) - machine.addHandler(event: .Event0) { context in + machine.addHandler(event: .event0) { context in invokeCount += 1 return } @@ -268,8 +268,8 @@ class MachineTests: _TestCase } // tryEvent - machine <-! .Event0 - XCTAssertEqual(machine.state, MyState.State0, "Route should be removed.") + machine <-! .event0 + XCTAssertEqual(machine.state, MyState.state0, "Route should be removed.") XCTAssertEqual(invokeCount, 0, "Handler should NOT be performed") } @@ -278,12 +278,12 @@ class MachineTests: _TestCase { var invokeCount = 0 - let machine = Machine(state: .State0) { machine in + let machine = Machine(state: .state0) { machine in // add 0 => 1 => 2 - let routeDisposable = machine.addRoutes(event: .Event0, transitions: [ - .State0 => .State1, - .State1 => .State2, + let routeDisposable = machine.addRoutes(event: .event0, transitions: [ + .state0 => .state1, + .state1 => .state2, ], handler: { context in invokeCount += 1 return @@ -295,8 +295,8 @@ class MachineTests: _TestCase } // tryEvent - machine <-! .Event0 - XCTAssertEqual(machine.state, MyState.State0, "Route should be removed.") + machine <-! .event0 + XCTAssertEqual(machine.state, MyState.state0, "Route should be removed.") XCTAssertEqual(invokeCount, 0, "Handler should NOT be performed") } @@ -309,15 +309,15 @@ class MachineTests: _TestCase { var invokeCount = 0 - let machine = Machine(state: .State0) { machine in + let machine = Machine(state: .state0) { machine in // add 0 => 1 => 2 - machine.addRoutes(event: .Event0, transitions: [ - .State0 => .State1, - .State1 => .State2, + machine.addRoutes(event: .event0, transitions: [ + .state0 => .state1, + .state1 => .state2, ]) - machine.addHandler(event: .Event0) { context in + machine.addHandler(event: .event0) { context in invokeCount += 1 return } @@ -325,12 +325,12 @@ class MachineTests: _TestCase } // tryEvent - machine <-! .Event0 - XCTAssertEqual(machine.state, MyState.State1) + machine <-! .event0 + XCTAssertEqual(machine.state, MyState.state1) // tryEvent - machine <-! .Event0 - XCTAssertEqual(machine.state, MyState.State2) + machine <-! .event0 + XCTAssertEqual(machine.state, MyState.state2) XCTAssertEqual(invokeCount, 2) } @@ -339,8 +339,8 @@ class MachineTests: _TestCase { var invokeCount = 0 - let machine = Machine(state: .State0) { machine in - machine.addRoutes(event: .Event0, transitions: [ .State0 => .State1 ]) + let machine = Machine(state: .state0) { machine in + machine.addRoutes(event: .event0, transitions: [ .state0 => .state1 ]) machine.addErrorHandler { event, fromState, toState, userInfo in invokeCount += 1 } @@ -349,7 +349,7 @@ class MachineTests: _TestCase XCTAssertEqual(invokeCount, 0) // tryEvent (fails) - machine <-! .Event1 + machine <-! .event1 XCTAssertEqual(invokeCount, 1, "Error handler should be called.") @@ -359,15 +359,15 @@ class MachineTests: _TestCase { var invokeCount = 0 - let machine = Machine(state: .State0) { machine in + let machine = Machine(state: .state0) { machine in // add 0 => 1 => 2 - machine.addRoutes(event: .Event0, transitions: [ - .State0 => .State1, - .State1 => .State2, + machine.addRoutes(event: .event0, transitions: [ + .state0 => .state1, + .state1 => .state2, ]) - let handlerDisposable = machine.addHandler(event: .Event0) { context in + let handlerDisposable = machine.addHandler(event: .event0) { context in invokeCount += 1 return } @@ -378,12 +378,12 @@ class MachineTests: _TestCase } // tryEvent - machine <-! .Event0 - XCTAssertEqual(machine.state, MyState.State1, "0 => 1 should be succesful") + machine <-! .event0 + XCTAssertEqual(machine.state, MyState.state1, "0 => 1 should be succesful") // tryEvent - machine <-! .Event0 - XCTAssertEqual(machine.state, MyState.State2, "1 => 2 should be succesful") + machine <-! .event0 + XCTAssertEqual(machine.state, MyState.state2, "1 => 2 should be succesful") XCTAssertEqual(invokeCount, 0, "Handler should NOT be performed") } @@ -396,25 +396,25 @@ class MachineTests: _TestCase { var invokeCount = 0 - let machine = Machine(state: .Str("initial")) { machine in + let machine = Machine(state: .str("initial")) { machine in machine.addRouteMapping { event, fromState, userInfo -> StrState? in // no route for no-event guard let event = event else { return nil } switch (event, fromState) { - case (.Str("gogogo"), .Str("initial")): - return .Str("Phase 1") - case (.Str("gogogo"), .Str("Phase 1")): - return .Str("Phase 2") - case (.Str("finish"), .Str("Phase 2")): - return .Str("end") + case (.str("gogogo"), .str("initial")): + return .str("Phase 1") + case (.str("gogogo"), .str("Phase 1")): + return .str("Phase 2") + case (.str("finish"), .str("Phase 2")): + return .str("end") default: return nil } } - machine.addHandler(event: .Str("gogogo")) { context in + machine.addHandler(event: .str("gogogo")) { context in invokeCount += 1 return } @@ -422,36 +422,36 @@ class MachineTests: _TestCase } // initial - XCTAssertEqual(machine.state, StrState.Str("initial")) + XCTAssertEqual(machine.state, StrState.str("initial")) // tryEvent (fails) - machine <-! .Str("go?") - XCTAssertEqual(machine.state, StrState.Str("initial"), "No change.") + machine <-! .str("go?") + XCTAssertEqual(machine.state, StrState.str("initial"), "No change.") XCTAssertEqual(invokeCount, 0, "Handler should NOT be performed") // tryEvent - machine <-! .Str("gogogo") - XCTAssertEqual(machine.state, StrState.Str("Phase 1")) + machine <-! .str("gogogo") + XCTAssertEqual(machine.state, StrState.str("Phase 1")) XCTAssertEqual(invokeCount, 1) // tryEvent (fails) - machine <-! .Str("finish") - XCTAssertEqual(machine.state, StrState.Str("Phase 1"), "No change.") + machine <-! .str("finish") + XCTAssertEqual(machine.state, StrState.str("Phase 1"), "No change.") XCTAssertEqual(invokeCount, 1, "Handler should NOT be performed") // tryEvent - machine <-! .Str("gogogo") - XCTAssertEqual(machine.state, StrState.Str("Phase 2")) + machine <-! .str("gogogo") + XCTAssertEqual(machine.state, StrState.str("Phase 2")) XCTAssertEqual(invokeCount, 2) // tryEvent (fails) - machine <-! .Str("gogogo") - XCTAssertEqual(machine.state, StrState.Str("Phase 2"), "No change.") + machine <-! .str("gogogo") + XCTAssertEqual(machine.state, StrState.str("Phase 2"), "No change.") XCTAssertEqual(invokeCount, 2, "Handler should NOT be performed") // tryEvent - machine <-! .Str("finish") - XCTAssertEqual(machine.state, StrState.Str("end")) + machine <-! .str("finish") + XCTAssertEqual(machine.state, StrState.str("end")) XCTAssertEqual(invokeCount, 2, "gogogo-Handler should NOT be performed") } @@ -462,15 +462,15 @@ class MachineTests: _TestCase var invokeCount2 = 0 var disposables = [Disposable]() - let machine = Machine(state: .Str("initial")) { machine in + let machine = Machine(state: .str("initial")) { machine in let d = machine.addRouteMapping({ event, fromState, userInfo -> StrState? in // no route for no-event guard let event = event else { return nil } switch (event, fromState) { - case (.Str("gogogo"), .Str("initial")): - return .Str("Phase 1") + case (.str("gogogo"), .str("initial")): + return .str("Phase 1") default: return nil } @@ -485,8 +485,8 @@ class MachineTests: _TestCase guard let event = event else { return nil } switch (event, fromState) { - case (.Str("finish"), .Str("Phase 1")): - return .Str("end") + case (.str("finish"), .str("Phase 1")): + return .str("end") default: return nil } @@ -499,41 +499,41 @@ class MachineTests: _TestCase } // initial - XCTAssertEqual(machine.state, StrState.Str("initial")) + XCTAssertEqual(machine.state, StrState.str("initial")) // tryEvent (fails) - machine <-! .Str("go?") - XCTAssertEqual(machine.state, StrState.Str("initial"), "No change.") + machine <-! .str("go?") + XCTAssertEqual(machine.state, StrState.str("initial"), "No change.") XCTAssertEqual(invokeCount1, 0) XCTAssertEqual(invokeCount2, 0) // tryEvent - machine <-! .Str("gogogo") - XCTAssertEqual(machine.state, StrState.Str("Phase 1")) + machine <-! .str("gogogo") + XCTAssertEqual(machine.state, StrState.str("Phase 1")) XCTAssertEqual(invokeCount1, 1) XCTAssertEqual(invokeCount2, 0) // tryEvent (fails) - machine <-! .Str("gogogo") - XCTAssertEqual(machine.state, StrState.Str("Phase 1"), "No change.") + machine <-! .str("gogogo") + XCTAssertEqual(machine.state, StrState.str("Phase 1"), "No change.") XCTAssertEqual(invokeCount1, 1) XCTAssertEqual(invokeCount2, 0) // tryEvent - machine <-! .Str("finish") - XCTAssertEqual(machine.state, StrState.Str("end")) + machine <-! .str("finish") + XCTAssertEqual(machine.state, StrState.str("end")) XCTAssertEqual(invokeCount1, 1) XCTAssertEqual(invokeCount2, 1) // hasRoute (before dispose) - XCTAssertEqual(machine.hasRoute(event: .Str("gogogo"), transition: .Str("initial") => .Str("Phase 1")), true) - XCTAssertEqual(machine.hasRoute(event: .Str("finish"), transition: .Str("Phase 1") => .Str("end")), true) + XCTAssertEqual(machine.hasRoute(event: .str("gogogo"), transition: .str("initial") => .str("Phase 1")), true) + XCTAssertEqual(machine.hasRoute(event: .str("finish"), transition: .str("Phase 1") => .str("end")), true) disposables.forEach { $0.dispose() } // hasRoute (after dispose) - XCTAssertEqual(machine.hasRoute(event: .Str("gogogo"), transition: .Str("initial") => .Str("Phase 1")), false, "Routes & handlers should be disposed.") - XCTAssertEqual(machine.hasRoute(event: .Str("finish"), transition: .Str("Phase 1") => .Str("end")), false, "Routes & handlers should be disposed.") + XCTAssertEqual(machine.hasRoute(event: .str("gogogo"), transition: .str("initial") => .str("Phase 1")), false, "Routes & handlers should be disposed.") + XCTAssertEqual(machine.hasRoute(event: .str("finish"), transition: .str("Phase 1") => .str("end")), false, "Routes & handlers should be disposed.") } diff --git a/Tests/MiscTests.swift b/Tests/MiscTests.swift index 59f946c..71ac2d5 100644 --- a/Tests/MiscTests.swift +++ b/Tests/MiscTests.swift @@ -17,8 +17,8 @@ class MiscTests: _TestCase let machine = StateMachine(state: ".State0") { machine in machine.addRoute(".State0" => ".State1") - machine.addRoute(.Any => ".State2") { context in print("Any => 2, msg=\(context.userInfo)") } - machine.addRoute(".State2" => .Any) { context in print("2 => Any, msg=\(context.userInfo)") } + machine.addRoute(.any => ".State2") { context in print("Any => 2, msg=\(context.userInfo)") } + machine.addRoute(".State2" => .any) { context in print("2 => Any, msg=\(context.userInfo)") } // add handler (handlerContext = (event, transition, order, userInfo)) machine.addHandler(".State0" => ".State1") { context in @@ -51,14 +51,14 @@ class MiscTests: _TestCase // StateType + associated value func testREADME_associatedValue() { - let machine = StateMachine(state: .Str("0")) { machine in + let machine = StateMachine(state: .str("0")) { machine in - machine.addRoute(.Str("0") => .Str("1")) - machine.addRoute(.Any => .Str("2")) { context in print("Any => 2, msg=\(context.userInfo)") } - machine.addRoute(.Str("2") => .Any) { context in print("2 => Any, msg=\(context.userInfo)") } + machine.addRoute(.str("0") => .str("1")) + machine.addRoute(.any => .str("2")) { context in print("Any => 2, msg=\(context.userInfo)") } + machine.addRoute(.str("2") => .any) { context in print("2 => Any, msg=\(context.userInfo)") } // add handler (handlerContext = (event, transition, order, userInfo)) - machine.addHandler(.Str("0") => .Str("1")) { context in + machine.addHandler(.str("0") => .str("1")) { context in print("0 => 1") } @@ -70,71 +70,71 @@ class MiscTests: _TestCase // tryState 0 => 1 => 2 => 1 => 0 - machine <- .Str("1") - XCTAssertEqual(machine.state, StrState.Str("1")) + machine <- .str("1") + XCTAssertEqual(machine.state, StrState.str("1")) - machine <- (.Str("2"), "Hello") - XCTAssertEqual(machine.state, StrState.Str("2")) + machine <- (.str("2"), "Hello") + XCTAssertEqual(machine.state, StrState.str("2")) - machine <- (.Str("1"), "Bye") - XCTAssertEqual(machine.state, StrState.Str("1")) + machine <- (.str("1"), "Bye") + XCTAssertEqual(machine.state, StrState.str("1")) - machine <- .Str("0") // fail: no 1 => 0 - XCTAssertEqual(machine.state, StrState.Str("1")) + machine <- .str("0") // fail: no 1 => 0 + XCTAssertEqual(machine.state, StrState.str("1")) print("machine.state = \(machine.state)") } func testExample() { - let machine = StateMachine(state: .State0) { + let machine = StateMachine(state: .state0) { // add 0 => 1 - $0.addRoute(.State0 => .State1) { context in + $0.addRoute(.state0 => .state1) { context in print("[Transition 0=>1] \(context.fromState) => \(context.toState)") } // add 0 => 1 once more - $0.addRoute(.State0 => .State1) { context in + $0.addRoute(.state0 => .state1) { context in print("[Transition 0=>1b] \(context.fromState) => \(context.toState)") } // add 2 => Any - $0.addRoute(.State2 => .Any) { context in + $0.addRoute(.state2 => .any) { context in print("[Transition exit 2] \(context.fromState) => \(context.toState) (Any)") } // add Any => 2 - $0.addRoute(.Any => .State2) { context in + $0.addRoute(.any => .state2) { context in print("[Transition Entry 2] \(context.fromState) (Any) => \(context.toState)") } // add 1 => 0 (no handler) - $0.addRoute(.State1 => .State0) + $0.addRoute(.state1 => .state0) } // 0 => 1 - XCTAssertTrue(machine.hasRoute(.State0 => .State1)) + XCTAssertTrue(machine.hasRoute(.state0 => .state1)) // 1 => 0 - XCTAssertTrue(machine.hasRoute(.State1 => .State0)) + XCTAssertTrue(machine.hasRoute(.state1 => .state0)) // 2 => Any - XCTAssertTrue(machine.hasRoute(.State2 => .State0)) - XCTAssertTrue(machine.hasRoute(.State2 => .State1)) - XCTAssertTrue(machine.hasRoute(.State2 => .State2)) - XCTAssertTrue(machine.hasRoute(.State2 => .State3)) + XCTAssertTrue(machine.hasRoute(.state2 => .state0)) + XCTAssertTrue(machine.hasRoute(.state2 => .state1)) + XCTAssertTrue(machine.hasRoute(.state2 => .state2)) + XCTAssertTrue(machine.hasRoute(.state2 => .state3)) // Any => 2 - XCTAssertTrue(machine.hasRoute(.State0 => .State2)) - XCTAssertTrue(machine.hasRoute(.State1 => .State2)) - XCTAssertTrue(machine.hasRoute(.State3 => .State2)) + XCTAssertTrue(machine.hasRoute(.state0 => .state2)) + XCTAssertTrue(machine.hasRoute(.state1 => .state2)) + XCTAssertTrue(machine.hasRoute(.state3 => .state2)) // others - XCTAssertFalse(machine.hasRoute(.State0 => .State0)) - XCTAssertFalse(machine.hasRoute(.State0 => .State3)) - XCTAssertFalse(machine.hasRoute(.State1 => .State1)) - XCTAssertFalse(machine.hasRoute(.State1 => .State3)) - XCTAssertFalse(machine.hasRoute(.State3 => .State0)) - XCTAssertFalse(machine.hasRoute(.State3 => .State1)) - XCTAssertFalse(machine.hasRoute(.State3 => .State3)) + XCTAssertFalse(machine.hasRoute(.state0 => .state0)) + XCTAssertFalse(machine.hasRoute(.state0 => .state3)) + XCTAssertFalse(machine.hasRoute(.state1 => .state1)) + XCTAssertFalse(machine.hasRoute(.state1 => .state3)) + XCTAssertFalse(machine.hasRoute(.state3 => .state0)) + XCTAssertFalse(machine.hasRoute(.state3 => .state1)) + XCTAssertFalse(machine.hasRoute(.state3 => .state3)) machine.configure { @@ -144,51 +144,51 @@ class MiscTests: _TestCase } // add entry handlers - $0.addHandler(.Any => .State0) { context in + $0.addHandler(.any => .state0) { context in print("[Entry 0] \(context.fromState) => \(context.toState)") // NOTE: this should not be called } - $0.addHandler(.Any => .State1) { context in + $0.addHandler(.any => .state1) { context in print("[Entry 1] \(context.fromState) => \(context.toState)") } - $0.addHandler(.Any => .State2) { context in + $0.addHandler(.any => .state2) { context in print("[Entry 2] \(context.fromState) => \(context.toState), userInfo = \(context.userInfo)") } - $0.addHandler(.Any => .State2) { context in + $0.addHandler(.any => .state2) { context in print("[Entry 2b] \(context.fromState) => \(context.toState), userInfo = \(context.userInfo)") } // add exit handlers - $0.addHandler(.State0 => .Any) { context in + $0.addHandler(.state0 => .any) { context in print("[Exit 0] \(context.fromState) => \(context.toState)") } - $0.addHandler(.State1 => .Any) { context in + $0.addHandler(.state1 => .any) { context in print("[Exit 1] \(context.fromState) => \(context.toState)") } - $0.addHandler(.State2 => .Any) { context in + $0.addHandler(.state2 => .any) { context in print("[Exit 2] \(context.fromState) => \(context.toState), userInfo = \(context.userInfo)") } - $0.addHandler(.State2 => .Any) { context in + $0.addHandler(.state2 => .any) { context in print("[Exit 2b] \(context.fromState) => \(context.toState), userInfo = \(context.userInfo)") } } - XCTAssertEqual(machine.state, MyState.State0) + XCTAssertEqual(machine.state, MyState.state0) // tryState 0 => 1 => 2 => 1 => 0 => 3 - machine <- .State1 - XCTAssertEqual(machine.state, MyState.State1) + machine <- .state1 + XCTAssertEqual(machine.state, MyState.state1) - machine <- (.State2, "State2 activate") - XCTAssertEqual(machine.state, MyState.State2) + machine <- (.state2, "State2 activate") + XCTAssertEqual(machine.state, MyState.state2) - machine <- (.State1, "State2 deactivate") - XCTAssertEqual(machine.state, MyState.State1) + machine <- (.state1, "State2 deactivate") + XCTAssertEqual(machine.state, MyState.state1) - machine <- .State0 - XCTAssertEqual(machine.state, MyState.State0) + machine <- .state0 + XCTAssertEqual(machine.state, MyState.state0) - machine <- .State3 - XCTAssertEqual(machine.state, MyState.State0, "No 0 => 3.") + machine <- .state3 + XCTAssertEqual(machine.state, MyState.state0, "No 0 => 3.") } } diff --git a/Tests/MyEvent.swift b/Tests/MyEvent.swift index 15d16d6..bf86a3a 100644 --- a/Tests/MyEvent.swift +++ b/Tests/MyEvent.swift @@ -10,17 +10,17 @@ import SwiftState enum MyEvent: EventType { - case Event0, Event1 + case event0, event1 } enum StrEvent: EventType { - case Str(String) + case str(String) var hashValue: Int { switch self { - case .Str(let str): return str.hashValue + case .str(let str): return str.hashValue } } } @@ -28,7 +28,7 @@ enum StrEvent: EventType func == (lhs: StrEvent, rhs: StrEvent) -> Bool { switch (lhs, rhs) { - case let (.Str(str1), .Str(str2)): + case let (.str(str1), .str(str2)): return str1 == str2 // default: // return false diff --git a/Tests/MyState.swift b/Tests/MyState.swift index 104b37a..b1cc1db 100644 --- a/Tests/MyState.swift +++ b/Tests/MyState.swift @@ -10,17 +10,17 @@ import SwiftState enum MyState: StateType { - case State0, State1, State2, State3 + case state0, state1, state2, state3 } enum StrState: StateType { - case Str(String) + case str(String) var hashValue: Int { switch self { - case .Str(let str): return str.hashValue + case .str(let str): return str.hashValue } } } @@ -28,7 +28,7 @@ enum StrState: StateType func == (lhs: StrState, rhs: StrState) -> Bool { switch (lhs, rhs) { - case let (.Str(str1), .Str(str2)): + case let (.str(str1), .str(str2)): return str1 == str2 } } diff --git a/Tests/QiitaTests.swift b/Tests/QiitaTests.swift index ae16e79..5681945 100644 --- a/Tests/QiitaTests.swift +++ b/Tests/QiitaTests.swift @@ -15,8 +15,8 @@ import XCTest enum InputKey: StateType { - case None - case Key0, Key1, Key2, Key3, Key4, Key5, Key6, Key7, Key8, Key9 + case none + case key0, key1, key2, key3, key4, key5, key6, key7, key8, key9 } class QiitaTests: _TestCase @@ -25,32 +25,32 @@ class QiitaTests: _TestCase { var success = false - let machine = StateMachine(state: .None) { machine in + let machine = StateMachine(state: .none) { machine in // connect all states - machine.addRoute(.Any => .Any) + machine.addRoute(.any => .any) // success = true only when transitionChain 2 => 3 => 5 => 7 is fulfilled - machine.addRouteChain(.Key2 => .Key3 => .Key5 => .Key7) { context in + machine.addRouteChain(.key2 => .key3 => .key5 => .key7) { context in success = true return } } // tryState - machine <- .Key2 - machine <- .Key3 - machine <- .Key4 - machine <- .Key5 - machine <- .Key6 - machine <- .Key7 + machine <- .key2 + machine <- .key3 + machine <- .key4 + machine <- .key5 + machine <- .key6 + machine <- .key7 XCTAssertFalse(success) - machine <- .Key2 - machine <- .Key3 - machine <- .Key5 - machine <- .Key7 + machine <- .key2 + machine <- .key3 + machine <- .key5 + machine <- .key7 XCTAssertTrue(success) } diff --git a/Tests/RouteChainTests.swift b/Tests/RouteChainTests.swift index 2029e0f..ee0d592 100644 --- a/Tests/RouteChainTests.swift +++ b/Tests/RouteChainTests.swift @@ -15,10 +15,10 @@ class MachineChainTests: _TestCase { var invokeCount = 0 - let machine = StateMachine(state: .State0) { machine in + let machine = StateMachine(state: .state0) { machine in // add 0 => 1 => 2 - machine.addRouteChain(.State0 => .State1 => .State2) { context in + machine.addRouteChain(.state0 => .state1 => .state2) { context in invokeCount += 1 return } @@ -26,20 +26,20 @@ class MachineChainTests: _TestCase } // tryState 0 => 1 => 2 - machine <- .State1 - machine <- .State2 + machine <- .state1 + machine <- .state2 XCTAssertEqual(invokeCount, 1, "Handler should be performed.") // // reset: tryState 2 => 0 // - machine.addRoute(.State2 => .State0) // make sure to add routes - machine <- .State0 + machine.addRoute(.state2 => .state0) // make sure to add routes + machine <- .state0 // tryState 0 => 1 => 2 again - machine <- .State1 - machine <- .State2 + machine <- .state1 + machine <- .state2 XCTAssertEqual(invokeCount, 2, "Handler should be performed again.") } @@ -49,10 +49,10 @@ class MachineChainTests: _TestCase var flag = false var invokeCount = 0 - let machine = StateMachine(state: .State0) { machine in + let machine = StateMachine(state: .state0) { machine in // add 0 => 1 => 2 - machine.addRouteChain(.State0 => .State1 => .State2, condition: { _ in flag }) { context in + machine.addRouteChain(.state0 => .state1 => .state2, condition: { _ in flag }) { context in invokeCount += 1 return } @@ -60,67 +60,67 @@ class MachineChainTests: _TestCase } // tryState 0 => 1 => 2 - machine <- .State1 - machine <- .State2 + machine <- .state1 + machine <- .state2 XCTAssertEqual(invokeCount, 0, "Handler should NOT be performed because flag=false.") // // reset: tryState 2 => 0 // - machine.addRoute(.State2 => .State0) // make sure to add routes - machine <- .State0 + machine.addRoute(.state2 => .state0) // make sure to add routes + machine <- .state0 flag = true // tryState 0 => 1 => 2 - machine <- .State1 - machine <- .State2 + machine <- .state1 + machine <- .state2 XCTAssertEqual(invokeCount, 1, "Handler should be performed.") } func testAddRouteChain_failBySkipping() { - let machine = StateMachine(state: .State0) { machine in + let machine = StateMachine(state: .state0) { machine in // add 0 => 1 => 2 - machine.addRouteChain(.State0 => .State1 => .State2) { context in + machine.addRouteChain(.state0 => .state1 => .state2) { context in XCTFail("Handler should NOT be performed because 0 => 2 is skipping 1.") } } // tryState 0 => 2 directly (skipping 1) - machine <- .State2 + machine <- .state2 } func testAddRouteChain_failByHangingAround() { - let machine = StateMachine(state: .State0) { machine in + let machine = StateMachine(state: .state0) { machine in // add 0 => 1 => 2 - machine.addRouteChain(.State0 => .State1 => .State2) { context in + machine.addRouteChain(.state0 => .state1 => .state2) { context in XCTFail("Handler should NOT be performed because 0 => 1 => 3 => 2 is hanging around 3.") } - machine.addRoute(.State1 => .State3) // add 1 => 3 route for hanging around + machine.addRoute(.state1 => .state3) // add 1 => 3 route for hanging around } // tryState 0 => 1 => 3 => 2 (hanging around 3) - machine <- .State1 - machine <- .State3 - machine <- .State2 + machine <- .state1 + machine <- .state3 + machine <- .state2 } func testAddRouteChain_succeedByFailingHangingAround() { var invokeCount = 0 - let machine = StateMachine(state: .State0) { machine in + let machine = StateMachine(state: .state0) { machine in // add 0 => 1 => 2 - machine.addRouteChain(.State0 => .State1 => .State2) { context in + machine.addRouteChain(.state0 => .state1 => .state2) { context in invokeCount += 1 return } @@ -129,9 +129,9 @@ class MachineChainTests: _TestCase } // tryState 0 => 1 => 3 => 2 (cannot hang around 3) - machine <- .State1 - machine <- .State3 - machine <- .State2 + machine <- .state1 + machine <- .state3 + machine <- .state2 XCTAssertEqual(invokeCount, 1, "Handler should be performed because 1 => 3 is not registered, thus performing 0 => 1 => 2.") } @@ -140,10 +140,10 @@ class MachineChainTests: _TestCase { var invokeCount = 0 - let machine = StateMachine(state: .State0) { machine in + let machine = StateMachine(state: .state0) { machine in // add 0 => 1 => 2 => 0 (back home) => 2 - machine.addRouteChain(.State0 => .State1 => .State2 => .State0 => .State2) { context in + machine.addRouteChain(.state0 => .state1 => .state2 => .state0 => .state2) { context in invokeCount += 1 return } @@ -151,10 +151,10 @@ class MachineChainTests: _TestCase } // tryState 0 => 1 => 2 => 0 => 2 - machine <- .State1 - machine <- .State2 - machine <- .State0 - machine <- .State2 + machine <- .state1 + machine <- .state2 + machine <- .state0 + machine <- .state2 XCTAssertEqual(invokeCount, 1) } @@ -164,12 +164,12 @@ class MachineChainTests: _TestCase { var invokeCount = 0 - let machine = StateMachine(state: .State0) { machine in + let machine = StateMachine(state: .state0) { machine in - machine.addRoute(.Any => .Any) // connect all states + machine.addRoute(.any => .any) // connect all states // add 0 => 1 => 2 => 0 (back home) => 1 => 2 - machine.addRouteChain(.State0 => .State1 => .State2 => .State0 => .State1 => .State2) { context in + machine.addRouteChain(.state0 => .state1 => .state2 => .state0 => .state1 => .state2) { context in invokeCount += 1 return } @@ -177,24 +177,24 @@ class MachineChainTests: _TestCase } // tryState 0 => 1 => 2 => 0 => 1 => 0 => 2 - machine <- .State1 - machine <- .State2 - machine <- .State0 - machine <- .State1 - machine <- .State0 - machine <- .State2 + machine <- .state1 + machine <- .state2 + machine <- .state0 + machine <- .state1 + machine <- .state0 + machine <- .state2 XCTAssertEqual(invokeCount, 0) // reset to 0 - machine <- .State0 + machine <- .state0 // tryState 0 => 1 => 2 => 0 => 1 => 2 - machine <- .State1 - machine <- .State2 - machine <- .State0 - machine <- .State1 - machine <- .State2 + machine <- .state1 + machine <- .state2 + machine <- .state0 + machine <- .state1 + machine <- .state2 XCTAssertEqual(invokeCount, 1) } @@ -203,10 +203,10 @@ class MachineChainTests: _TestCase { var invokeCount = 0 - let machine = StateMachine(state: .State0) { machine in + let machine = StateMachine(state: .state0) { machine in // add 0 => 1 => 2 - let chainDisposable = machine.addRouteChain(.State0 => .State1 => .State2) { context in + let chainDisposable = machine.addRouteChain(.state0 => .state1 => .state2) { context in invokeCount += 1 return } @@ -218,10 +218,10 @@ class MachineChainTests: _TestCase // tryState 0 => 1 => 2 - machine <- .State1 + machine <- .state1 XCTAssertEqual(invokeCount, 0, "ChainHandler should NOT be performed.") - machine <- .State2 + machine <- .state2 XCTAssertEqual(invokeCount, 0, "ChainHandler should NOT be performed.") } @@ -229,11 +229,11 @@ class MachineChainTests: _TestCase { var errorCount = 0 - let machine = StateMachine(state: .State0) { machine in + let machine = StateMachine(state: .state0) { machine in - let transitionChain = MyState.State0 => .State1 => .State2 + let transitionChain = MyState.state0 => .state1 => .state2 - machine.addRoute(.Any => .Any) // connect all states + machine.addRoute(.any => .any) // connect all states // add 0 => 1 => 2 machine.addRouteChain(transitionChain) { context in @@ -250,13 +250,13 @@ class MachineChainTests: _TestCase } // tryState 0 (starting state) => 1 => 0 - machine <- .State1 + machine <- .state1 XCTAssertEqual(errorCount, 0, "0 => 1 is successful (still chaining), so chainErrorHandler should NOT be performed at this point.") - machine <- .State0 + machine <- .state0 XCTAssertEqual(errorCount, 1, "chainErrorHandler should be performed.") // tryState 0 (starting state) => 2 - machine <- .State2 + machine <- .state2 XCTAssertEqual(errorCount, 2, "chainErrorHandler should be performed again.") } @@ -264,11 +264,11 @@ class MachineChainTests: _TestCase { var errorCount = 0 - let machine = StateMachine(state: .State0) { machine in + let machine = StateMachine(state: .state0) { machine in - let transitionChain = MyState.State0 => .State1 => .State2 + let transitionChain = MyState.state0 => .state1 => .state2 - machine.addRoute(.Any => .Any) // connect all states + machine.addRoute(.any => .any) // connect all states // add 0 => 1 => 2 chainErrorHandler let chainErrorHandlerDisposable = machine.addChainErrorHandler(transitionChain) { context in @@ -282,12 +282,12 @@ class MachineChainTests: _TestCase } // tryState 0 (starting state) => 1 => 0 - machine <- .State1 - machine <- .State0 + machine <- .state1 + machine <- .state0 XCTAssertEqual(errorCount, 0, "Chain error, but chainErrorHandler should NOT be performed.") // tryState 0 (starting state) => 2 - machine <- .State2 + machine <- .state2 XCTAssertEqual(errorCount, 0, "Chain error, but chainErrorHandler should NOT be performed.") } diff --git a/Tests/RouteMappingTests.swift b/Tests/RouteMappingTests.swift index a04cb82..117be14 100644 --- a/Tests/RouteMappingTests.swift +++ b/Tests/RouteMappingTests.swift @@ -18,15 +18,15 @@ import XCTest private enum _State: StateType, Hashable { - case Pending - case Loading(Int) + case pending + case loading(Int) var hashValue: Int { switch self { - case .Pending: + case .pending: return "Pending".hashValue - case let .Loading(x): + case let .loading(x): return "Loading\(x)".hashValue } } @@ -35,26 +35,26 @@ private enum _State: StateType, Hashable private func == (lhs: _State, rhs: _State) -> Bool { switch (lhs, rhs) { - case (.Pending, .Pending): + case (.pending, .pending): return true - case let (.Loading(x1), .Loading(x2)): + case let (.loading(x1), .loading(x2)): return x1 == x2 default: return false } } -private enum _Event: EventType, Hashable +private enum _Event: SwiftState.EventType, Hashable { - case CancelAction - case LoadAction(Int) + case cancelAction + case loadAction(Int) var hashValue: Int { switch self { - case .CancelAction: + case .cancelAction: return "CancelAction".hashValue - case let .LoadAction(x): + case let .loadAction(x): return "LoadAction\(x)".hashValue } } @@ -63,9 +63,9 @@ private enum _Event: EventType, Hashable private func == (lhs: _Event, rhs: _Event) -> Bool { switch (lhs, rhs) { - case (.CancelAction, .CancelAction): + case (.cancelAction, .cancelAction): return true - case let (.LoadAction(x1), .LoadAction(x2)): + case let (.loadAction(x1), .loadAction(x2)): return x1 == x2 default: return false @@ -79,7 +79,7 @@ class RouteMappingTests: _TestCase { var count = 0 - let machine = StateMachine<_State, _Event>(state: .Pending) { machine in + let machine = StateMachine<_State, _Event>(state: .pending) { machine in machine.addRouteMapping { event, fromState, userInfo in // no routes for no event @@ -88,47 +88,47 @@ class RouteMappingTests: _TestCase } switch event { - case .CancelAction: + case .cancelAction: // can transit to `.Pending` if current state is not the same - return fromState == .Pending ? nil : .Pending - case .LoadAction(let actionId): + 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) + return fromState == .loading(actionId) ? nil : .loading(actionId) } } // increment `count` when any events i.e. `.CancelAction` and `.LoadAction(x)` succeed. - machine.addHandler(event: .Any) { event, transition, order, userInfo in + machine.addHandler(event: .any) { event, transition, order, userInfo in count += 1 } } // initial - XCTAssertEqual(machine.state, _State.Pending) + XCTAssertEqual(machine.state, _State.pending) XCTAssertEqual(count, 0) // CancelAction (to .Pending state, same as before) - machine <-! .CancelAction - XCTAssertEqual(machine.state, _State.Pending) + machine <-! .cancelAction + XCTAssertEqual(machine.state, _State.pending) XCTAssertEqual(count, 0, "`tryEvent()` failed, and `count` should not be incremented.") // LoadAction(1) (to .Loading(1) state) - machine <-! .LoadAction(1) - XCTAssertEqual(machine.state, _State.Loading(1)) + machine <-! .loadAction(1) + XCTAssertEqual(machine.state, _State.loading(1)) XCTAssertEqual(count, 1) // LoadAction(1) (same as before) - machine <-! .LoadAction(1) - XCTAssertEqual(machine.state, _State.Loading(1)) + machine <-! .loadAction(1) + XCTAssertEqual(machine.state, _State.loading(1)) XCTAssertEqual(count, 1, "`tryEvent()` failed, and `count` should not be incremented.") - machine <-! .LoadAction(2) - XCTAssertEqual(machine.state, _State.Loading(2)) + machine <-! .loadAction(2) + XCTAssertEqual(machine.state, _State.loading(2)) XCTAssertEqual(count, 2) - machine <-! .CancelAction - XCTAssertEqual(machine.state, _State.Pending) + machine <-! .cancelAction + XCTAssertEqual(machine.state, _State.pending) XCTAssertEqual(count, 3) } @@ -137,7 +137,7 @@ class RouteMappingTests: _TestCase { var count = 0 - let machine = StateMachine<_State, _Event>(state: .Pending) { machine in + let machine = StateMachine<_State, _Event>(state: .pending) { machine in // Add following routes: // - `.Pending => .Loading(1)` @@ -145,45 +145,45 @@ class RouteMappingTests: _TestCase // - `.Loading(x) => .Loading(x+100)` machine.addStateRouteMapping { fromState, userInfo in switch fromState { - case .Pending: - return [.Loading(1)] - case .Loading(let actionId): - return [.Loading(actionId+10), .Loading(actionId+100)] + case .pending: + return [.loading(1)] + case .loading(let actionId): + return [.loading(actionId+10), .loading(actionId+100)] } } // increment `count` when any events i.e. `.CancelAction` and `.LoadAction(x)` succeed. - machine.addHandler(.Any => .Any) { event, transition, order, userInfo in + machine.addHandler(.any => .any) { event, transition, order, userInfo in count += 1 } } // initial - XCTAssertEqual(machine.state, _State.Pending) + XCTAssertEqual(machine.state, _State.pending) XCTAssertEqual(count, 0) // .Loading(999) (fails) - machine <- .Loading(999) - XCTAssertEqual(machine.state, _State.Pending) + machine <- .loading(999) + XCTAssertEqual(machine.state, _State.pending) XCTAssertEqual(count, 0, "`tryState()` failed, and `count` should not be incremented.") // .Loading(1) - machine <- .Loading(1) - XCTAssertEqual(machine.state, _State.Loading(1)) + machine <- .loading(1) + XCTAssertEqual(machine.state, _State.loading(1)) XCTAssertEqual(count, 1) // .Loading(999) (fails) - machine <- .Loading(999) - XCTAssertEqual(machine.state, _State.Loading(1)) + machine <- .loading(999) + XCTAssertEqual(machine.state, _State.loading(1)) XCTAssertEqual(count, 1, "`tryState()` failed, and `count` should not be incremented.") - machine <- .Loading(11) - XCTAssertEqual(machine.state, _State.Loading(11)) + machine <- .loading(11) + XCTAssertEqual(machine.state, _State.loading(11)) XCTAssertEqual(count, 2) - machine <- .Loading(111) - XCTAssertEqual(machine.state, _State.Loading(111)) + machine <- .loading(111) + XCTAssertEqual(machine.state, _State.loading(111)) XCTAssertEqual(count, 3) } } diff --git a/Tests/RouteTests.swift b/Tests/RouteTests.swift index b0cb499..b02fa7f 100644 --- a/Tests/RouteTests.swift +++ b/Tests/RouteTests.swift @@ -13,19 +13,19 @@ class RouteTests: _TestCase { func testInit() { - let route = Route(transition: .State0 => .State1, condition: nil) - XCTAssertEqual(route.transition.fromState.rawValue, MyState.State0) - XCTAssertEqual(route.transition.toState.rawValue, MyState.State1) + let route = Route(transition: .state0 => .state1, condition: nil) + XCTAssertEqual(route.transition.fromState.rawValue, MyState.state0) + XCTAssertEqual(route.transition.toState.rawValue, MyState.state1) XCTAssertTrue(route.condition == nil) - let route2 = Route(transition: .State1 => .State2, condition: { _ in false }) - XCTAssertEqual(route2.transition.fromState.rawValue, MyState.State1) - XCTAssertEqual(route2.transition.toState.rawValue, MyState.State2) + let route2 = Route(transition: .state1 => .state2, condition: { _ in false }) + XCTAssertEqual(route2.transition.fromState.rawValue, MyState.state1) + XCTAssertEqual(route2.transition.toState.rawValue, MyState.state2) XCTAssertTrue(route2.condition != nil) - let route3 = Route(transition: .State2 => .State3, condition: { context in false }) - XCTAssertEqual(route3.transition.fromState.rawValue, MyState.State2) - XCTAssertEqual(route3.transition.toState.rawValue, MyState.State3) + let route3 = Route(transition: .state2 => .state3, condition: { context in false }) + XCTAssertEqual(route3.transition.fromState.rawValue, MyState.state2) + XCTAssertEqual(route3.transition.toState.rawValue, MyState.state3) XCTAssertTrue(route3.condition != nil) } } diff --git a/Tests/StateMachineEventTests.swift b/Tests/StateMachineEventTests.swift index b403255..278a1df 100644 --- a/Tests/StateMachineEventTests.swift +++ b/Tests/StateMachineEventTests.swift @@ -13,16 +13,16 @@ class StateMachineEventTests: _TestCase { func testCanTryEvent() { - let machine = StateMachine(state: .State0) + let machine = StateMachine(state: .state0) // add 0 => 1 & 1 => 2 // (NOTE: this is not chaining e.g. 0 => 1 => 2) - machine.addRoutes(event: .Event0, transitions: [ - .State0 => .State1, - .State1 => .State2, + machine.addRoutes(event: .event0, transitions: [ + .state0 => .state1, + .state1 => .state2, ]) - XCTAssertTrue(machine.canTryEvent(.Event0) != nil) + XCTAssertTrue(machine.canTryEvent(.event0) != nil) } //-------------------------------------------------- @@ -31,60 +31,60 @@ class StateMachineEventTests: _TestCase func testTryEvent() { - let machine = StateMachine(state: .State0) + let machine = StateMachine(state: .state0) // add 0 => 1 => 2 - machine.addRoutes(event: .Event0, transitions: [ - .State0 => .State1, - .State1 => .State2, + machine.addRoutes(event: .event0, transitions: [ + .state0 => .state1, + .state1 => .state2, ]) // tryEvent - machine <-! .Event0 - XCTAssertEqual(machine.state, MyState.State1) + machine <-! .event0 + XCTAssertEqual(machine.state, MyState.state1) // tryEvent - machine <-! .Event0 - XCTAssertEqual(machine.state, MyState.State2) + machine <-! .event0 + XCTAssertEqual(machine.state, MyState.state2) // tryEvent - machine <-! .Event0 - XCTAssertEqual(machine.state, MyState.State2, "Event0 doesn't have 2 => Any") + machine <-! .event0 + XCTAssertEqual(machine.state, MyState.state2, "Event0 doesn't have 2 => Any") } func testTryEvent_string() { - let machine = StateMachine(state: .State0) + let machine = StateMachine(state: .state0) // add 0 => 1 => 2 machine.addRoutes(event: "Run", transitions: [ - .State0 => .State1, - .State1 => .State2, + .state0 => .state1, + .state1 => .state2, ]) // tryEvent machine <-! "Run" - XCTAssertEqual(machine.state, MyState.State1) + XCTAssertEqual(machine.state, MyState.state1) // tryEvent machine <-! "Run" - XCTAssertEqual(machine.state, MyState.State2) + XCTAssertEqual(machine.state, MyState.state2) // tryEvent machine <-! "Run" - XCTAssertEqual(machine.state, MyState.State2, "Event=Run doesn't have 2 => Any") + XCTAssertEqual(machine.state, MyState.state2, "Event=Run doesn't have 2 => Any") } // https://github.com/ReactKit/SwiftState/issues/20 func testTryEvent_issue20() { - let machine = StateMachine(state: MyState.State2) { machine in - machine.addRoutes(event: .Event0, transitions: [.Any => .State0]) + let machine = StateMachine(state: MyState.state2) { machine in + machine.addRoutes(event: .event0, transitions: [.any => .state0]) } // tryEvent - machine <-! .Event0 - XCTAssertEqual(machine.state, MyState.State0) + machine <-! .event0 + XCTAssertEqual(machine.state, MyState.state0) } // https://github.com/ReactKit/SwiftState/issues/28 @@ -92,9 +92,9 @@ class StateMachineEventTests: _TestCase { var eventCount = 0 - let machine = StateMachine(state: .State0) { machine in - machine.addRoute(.State0 => .State1) - machine.addRoutes(event: .Event0, transitions: [.Any => .Any]) { _ in + let machine = StateMachine(state: .state0) { machine in + machine.addRoute(.state0 => .state1) + machine.addRoutes(event: .event0, transitions: [.any => .any]) { _ in eventCount += 1 } } @@ -102,38 +102,38 @@ class StateMachineEventTests: _TestCase XCTAssertEqual(eventCount, 0) // tryEvent - machine <-! .Event0 + machine <-! .event0 XCTAssertEqual(eventCount, 1) - XCTAssertEqual(machine.state, MyState.State0, "State should NOT be changed") + XCTAssertEqual(machine.state, MyState.state0, "State should NOT be changed") // tryEvent - machine <- .State1 - XCTAssertEqual(machine.state, MyState.State1, "State should be changed") + machine <- .state1 + XCTAssertEqual(machine.state, MyState.state1, "State should be changed") // tryEvent - machine <-! .Event0 + machine <-! .event0 XCTAssertEqual(eventCount, 2) - XCTAssertEqual(machine.state, MyState.State1, "State should NOT be changed") + XCTAssertEqual(machine.state, MyState.state1, "State should NOT be changed") } // Fix for transitioning of routes w/ multiple from-states // https://github.com/ReactKit/SwiftState/pull/32 func testTryEvent_issue32() { - let machine = StateMachine(state: .State0) { machine in - machine.addRoutes(event: .Event0, transitions: [ .State0 => .State1 ]) - machine.addRoutes(event: .Event1, routes: [ [ .State1, .State2 ] => .State3 ]) + let machine = StateMachine(state: .state0) { machine in + machine.addRoutes(event: .event0, transitions: [ .state0 => .state1 ]) + machine.addRoutes(event: .event1, routes: [ [ .state1, .state2 ] => .state3 ]) } - XCTAssertEqual(machine.state, MyState.State0) + XCTAssertEqual(machine.state, MyState.state0) // tryEvent - machine <-! .Event0 - XCTAssertEqual(machine.state, MyState.State1) + machine <-! .event0 + XCTAssertEqual(machine.state, MyState.state1) // tryEvent - machine <-! .Event1 - XCTAssertEqual(machine.state, MyState.State3) + machine <-! .event1 + XCTAssertEqual(machine.state, MyState.state3) } // MARK: hasRoute + event @@ -141,22 +141,22 @@ class StateMachineEventTests: _TestCase func testHasRoute_anyEvent() { ({ - let machine = StateMachine(state: .State0) { machine in - machine.addRoute(.State0 => .State1) - machine.addRoutes(event: .Any, transitions: [.State0 => .State1]) + let machine = StateMachine(state: .state0) { machine in + machine.addRoute(.state0 => .state1) + machine.addRoutes(event: .any, transitions: [.state0 => .state1]) } - let hasRoute = machine.hasRoute(event: .Event0, transition: .State0 => .State1) + let hasRoute = machine.hasRoute(event: .event0, transition: .state0 => .state1) XCTAssertTrue(hasRoute) })() ({ - let machine = StateMachine(state: .State0) { machine in - machine.addRoute(.State0 => .State1) - machine.addRoutes(event: .Any, transitions: [.State2 => .State3]) + let machine = StateMachine(state: .state0) { machine in + machine.addRoute(.state0 => .state1) + machine.addRoutes(event: .any, transitions: [.state2 => .state3]) } - let hasRoute = machine.hasRoute(event: .Event0, transition: .State0 => .State1) + let hasRoute = machine.hasRoute(event: .event0, transition: .state0 => .state1) XCTAssertFalse(hasRoute) })() } @@ -165,12 +165,12 @@ class StateMachineEventTests: _TestCase // https://github.com/ReactKit/SwiftState/pull/19 func testHasRoute_issue19() { - let machine = StateMachine(state: .State0) { machine in - machine.addRoute(.State0 => .State1) // no-event - machine.addRoutes(event: .Event0, transitions: [.State1 => .State2]) // with-event + let machine = StateMachine(state: .state0) { machine in + machine.addRoute(.state0 => .state1) // no-event + machine.addRoutes(event: .event0, transitions: [.state1 => .state2]) // with-event } - let hasRoute = machine.hasRoute(event: .Event0, transition: .State1 => .State2) + let hasRoute = machine.hasRoute(event: .event0, transition: .state1 => .state2) XCTAssertTrue(hasRoute) } @@ -180,85 +180,85 @@ class StateMachineEventTests: _TestCase func testAddRoute_tryState() { - let machine = StateMachine(state: .State0) { machine in + let machine = StateMachine(state: .state0) { machine in // add 0 => 1 & 1 => 2 // (NOTE: this is not chaining e.g. 0 => 1 => 2) - machine.addRoutes(event: .Event0, transitions: [ - .State0 => .State1, - .State1 => .State2, + machine.addRoutes(event: .event0, transitions: [ + .state0 => .state1, + .state1 => .state2, ]) } // tryState 0 => 1 - machine <- .State1 - XCTAssertEqual(machine.state, MyState.State1) + machine <- .state1 + XCTAssertEqual(machine.state, MyState.state1) // tryState 1 => 2 - machine <- .State2 - XCTAssertEqual(machine.state, MyState.State2) + machine <- .state2 + XCTAssertEqual(machine.state, MyState.state2) // tryState 2 => 3 - machine <- .State3 - XCTAssertEqual(machine.state, MyState.State2, "2 => 3 is not registered.") + machine <- .state3 + XCTAssertEqual(machine.state, MyState.state2, "2 => 3 is not registered.") } func testAddRoute_multiple() { - let machine = StateMachine(state: .State0) { machine in + let machine = StateMachine(state: .state0) { machine in // add 0 => 1 => 2 - machine.addRoutes(event: .Event0, transitions: [ - .State0 => .State1, - .State1 => .State2, + machine.addRoutes(event: .event0, transitions: [ + .state0 => .state1, + .state1 => .state2, ]) // add 2 => 1 => 0 - machine.addRoutes(event: .Event1, transitions: [ - .State2 => .State1, - .State1 => .State0, + machine.addRoutes(event: .event1, transitions: [ + .state2 => .state1, + .state1 => .state0, ]) } // initial - XCTAssertEqual(machine.state, MyState.State0) + XCTAssertEqual(machine.state, MyState.state0) // tryEvent - machine <-! .Event1 - XCTAssertEqual(machine.state, MyState.State0, "Event1 doesn't have 0 => Any.") + machine <-! .event1 + XCTAssertEqual(machine.state, MyState.state0, "Event1 doesn't have 0 => Any.") // tryEvent - machine <-! .Event0 - XCTAssertEqual(machine.state, MyState.State1) + machine <-! .event0 + XCTAssertEqual(machine.state, MyState.state1) // tryEvent - machine <-! .Event0 - XCTAssertEqual(machine.state, MyState.State2) + machine <-! .event0 + XCTAssertEqual(machine.state, MyState.state2) // tryEvent - machine <-! .Event0 - XCTAssertEqual(machine.state, MyState.State2, "Event0 doesn't have 2 => Any.") + machine <-! .event0 + XCTAssertEqual(machine.state, MyState.state2, "Event0 doesn't have 2 => Any.") // tryEvent - machine <-! .Event1 - XCTAssertEqual(machine.state, MyState.State1) + machine <-! .event1 + XCTAssertEqual(machine.state, MyState.state1) // tryEvent - machine <-! .Event1 - XCTAssertEqual(machine.state, MyState.State0) + machine <-! .event1 + XCTAssertEqual(machine.state, MyState.state0) } func testAddRoute_handler() { var invokeCount = 0 - let machine = StateMachine(state: .State0) { machine in + let machine = StateMachine(state: .state0) { machine in // add 0 => 1 => 2 - machine.addRoutes(event: .Event0, transitions: [ - .State0 => .State1, - .State1 => .State2, + machine.addRoutes(event: .event0, transitions: [ + .state0 => .state1, + .state1 => .state2, ], handler: { context in invokeCount += 1 return @@ -266,12 +266,12 @@ class StateMachineEventTests: _TestCase } // tryEvent - machine <-! .Event0 - XCTAssertEqual(machine.state, MyState.State1) + machine <-! .event0 + XCTAssertEqual(machine.state, MyState.state1) // tryEvent - machine <-! .Event0 - XCTAssertEqual(machine.state, MyState.State2) + machine <-! .event0 + XCTAssertEqual(machine.state, MyState.state2) XCTAssertEqual(invokeCount, 2) } @@ -280,15 +280,15 @@ class StateMachineEventTests: _TestCase { var invokeCount = 0 - let machine = StateMachine(state: .State0) { machine in + let machine = StateMachine(state: .state0) { machine in // add 0 => 1 => 2 - let routeDisposable = machine.addRoutes(event: .Event0, transitions: [ - .State0 => .State1, - .State1 => .State2, + let routeDisposable = machine.addRoutes(event: .event0, transitions: [ + .state0 => .state1, + .state1 => .state2, ]) - machine.addHandler(event: .Event0) { context in + machine.addHandler(event: .event0) { context in invokeCount += 1 return } @@ -299,11 +299,11 @@ class StateMachineEventTests: _TestCase } // initial - XCTAssertEqual(machine.state, MyState.State0) + XCTAssertEqual(machine.state, MyState.state0) // tryEvent - machine <-! .Event0 - XCTAssertEqual(machine.state, MyState.State0, "Route should be removed.") + machine <-! .event0 + XCTAssertEqual(machine.state, MyState.state0, "Route should be removed.") XCTAssertEqual(invokeCount, 0, "Handler should NOT be performed") } @@ -316,15 +316,15 @@ class StateMachineEventTests: _TestCase { var invokeCount = 0 - let machine = StateMachine(state: .State0) { machine in + let machine = StateMachine(state: .state0) { machine in // add 0 => 1 => 2 - machine.addRoutes(event: .Event0, transitions: [ - .State0 => .State1, - .State1 => .State2, + machine.addRoutes(event: .event0, transitions: [ + .state0 => .state1, + .state1 => .state2, ]) - machine.addHandler(event: .Event0) { context in + machine.addHandler(event: .event0) { context in invokeCount += 1 return } @@ -332,12 +332,12 @@ class StateMachineEventTests: _TestCase } // tryEvent - machine <-! .Event0 - XCTAssertEqual(machine.state, MyState.State1) + machine <-! .event0 + XCTAssertEqual(machine.state, MyState.state1) // tryEvent - machine <-! .Event0 - XCTAssertEqual(machine.state, MyState.State2) + machine <-! .event0 + XCTAssertEqual(machine.state, MyState.state2) XCTAssertEqual(invokeCount, 2) } @@ -346,15 +346,15 @@ class StateMachineEventTests: _TestCase { var invokeCount = 0 - let machine = StateMachine(state: .State0) { machine in + let machine = StateMachine(state: .state0) { machine in // add 0 => 1 => 2 - machine.addRoutes(event: .Event0, transitions: [ - .State0 => .State1, - .State1 => .State2, + machine.addRoutes(event: .event0, transitions: [ + .state0 => .state1, + .state1 => .state2, ]) - let handlerDisposable = machine.addHandler(event: .Event0) { context in + let handlerDisposable = machine.addHandler(event: .event0) { context in invokeCount += 1 return } @@ -365,12 +365,12 @@ class StateMachineEventTests: _TestCase } // tryEvent - machine <-! .Event0 - XCTAssertEqual(machine.state, MyState.State1, "0 => 1 should be succesful") + machine <-! .event0 + XCTAssertEqual(machine.state, MyState.state1, "0 => 1 should be succesful") // tryEvent - machine <-! .Event0 - XCTAssertEqual(machine.state, MyState.State2, "1 => 2 should be succesful") + machine <-! .event0 + XCTAssertEqual(machine.state, MyState.state2, "1 => 2 should be succesful") XCTAssertEqual(invokeCount, 0, "Handler should NOT be performed") } @@ -383,64 +383,64 @@ class StateMachineEventTests: _TestCase { var invokeCounts = [0, 0, 0, 0, 0, 0] - let machine = StateMachine(state: .State0) { machine in + let machine = StateMachine(state: .state0) { machine in // add 0 => 1 => 2 (event-based) - machine.addRoutes(event: .Event0, transitions: [ - .State0 => .State1, - .State1 => .State2, + machine.addRoutes(event: .event0, transitions: [ + .state0 => .state1, + .state1 => .state2, ]) // add 2 => 3 (state-based) - machine.addRoute(.State2 => .State3) + machine.addRoute(.state2 => .state3) // // addAnyHandler (for both event-based & state-based) // - machine.addAnyHandler(.State0 => .State1) { context in + machine.addAnyHandler(.state0 => .state1) { context in invokeCounts[0] += 1 } - machine.addAnyHandler(.State1 => .State2) { context in + machine.addAnyHandler(.state1 => .state2) { context in invokeCounts[1] += 1 } - machine.addAnyHandler(.State2 => .State3) { context in + machine.addAnyHandler(.state2 => .state3) { context in invokeCounts[2] += 1 } - machine.addAnyHandler(.Any => .State3) { context in + machine.addAnyHandler(.any => .state3) { context in invokeCounts[3] += 1 } - machine.addAnyHandler(.State0 => .Any) { context in + machine.addAnyHandler(.state0 => .any) { context in invokeCounts[4] += 1 } - machine.addAnyHandler(.Any => .Any) { context in + machine.addAnyHandler(.any => .any) { context in invokeCounts[5] += 1 } } // initial - XCTAssertEqual(machine.state, MyState.State0) + XCTAssertEqual(machine.state, MyState.state0) XCTAssertEqual(invokeCounts, [0, 0, 0, 0, 0, 0]) // tryEvent - machine <-! .Event0 - XCTAssertEqual(machine.state, MyState.State1) + machine <-! .event0 + XCTAssertEqual(machine.state, MyState.state1) XCTAssertEqual(invokeCounts, [1, 0, 0, 0, 1, 1]) // tryEvent - machine <-! .Event0 - XCTAssertEqual(machine.state, MyState.State2) + machine <-! .event0 + XCTAssertEqual(machine.state, MyState.state2) XCTAssertEqual(invokeCounts, [1, 1, 0, 0, 1, 2]) // tryState - machine <- .State3 - XCTAssertEqual(machine.state, MyState.State3) + machine <- .state3 + XCTAssertEqual(machine.state, MyState.state3) XCTAssertEqual(invokeCounts, [1, 1, 1, 1, 1, 3]) } diff --git a/Tests/StateMachineTests.swift b/Tests/StateMachineTests.swift index 432674e..fcd3006 100644 --- a/Tests/StateMachineTests.swift +++ b/Tests/StateMachineTests.swift @@ -13,9 +13,9 @@ class StateMachineTests: _TestCase { func testInit() { - let machine = StateMachine(state: .State0) + let machine = StateMachine(state: .state0) - XCTAssertEqual(machine.state, MyState.State0) + XCTAssertEqual(machine.state, MyState.state0) } //-------------------------------------------------- @@ -25,19 +25,19 @@ class StateMachineTests: _TestCase // machine <- state func testTryState() { - let machine = StateMachine(state: .State0) + let machine = StateMachine(state: .state0) // tryState 0 => 1, without registering any transitions - machine <- .State1 + machine <- .state1 - XCTAssertEqual(machine.state, MyState.State0, "0 => 1 should fail because transition is not added yet.") + XCTAssertEqual(machine.state, MyState.state0, "0 => 1 should fail because transition is not added yet.") // add 0 => 1 - machine.addRoute(.State0 => .State1) + machine.addRoute(.state0 => .state1) // tryState 0 => 1 - machine <- .State1 - XCTAssertEqual(machine.state, MyState.State1) + machine <- .state1 + XCTAssertEqual(machine.state, MyState.state1) } func testTryState_string() @@ -64,71 +64,71 @@ class StateMachineTests: _TestCase // add state1 => state2 func testAddRoute() { - let machine = StateMachine(state: .State0) { machine in - machine.addRoute(.State0 => .State1) + let machine = StateMachine(state: .state0) { machine in + machine.addRoute(.state0 => .state1) } - XCTAssertFalse(machine.hasRoute(.State0 => .State0)) - XCTAssertTrue(machine.hasRoute(.State0 => .State1)) // true - XCTAssertFalse(machine.hasRoute(.State0 => .State2)) - XCTAssertFalse(machine.hasRoute(.State1 => .State0)) - XCTAssertFalse(machine.hasRoute(.State1 => .State1)) - XCTAssertFalse(machine.hasRoute(.State1 => .State2)) + XCTAssertFalse(machine.hasRoute(.state0 => .state0)) + XCTAssertTrue(machine.hasRoute(.state0 => .state1)) // true + XCTAssertFalse(machine.hasRoute(.state0 => .state2)) + XCTAssertFalse(machine.hasRoute(.state1 => .state0)) + XCTAssertFalse(machine.hasRoute(.state1 => .state1)) + XCTAssertFalse(machine.hasRoute(.state1 => .state2)) } // add .Any => state func testAddRoute_fromAnyState() { - let machine = StateMachine(state: .State0) { machine in - machine.addRoute(.Any => .State1) // Any => State1 + let machine = StateMachine(state: .state0) { machine in + machine.addRoute(.any => .state1) // Any => State1 } - XCTAssertFalse(machine.hasRoute(.State0 => .State0)) - XCTAssertTrue(machine.hasRoute(.State0 => .State1)) // true - XCTAssertFalse(machine.hasRoute(.State0 => .State2)) - XCTAssertFalse(machine.hasRoute(.State1 => .State0)) - XCTAssertTrue(machine.hasRoute(.State1 => .State1)) // true - XCTAssertFalse(machine.hasRoute(.State1 => .State2)) + XCTAssertFalse(machine.hasRoute(.state0 => .state0)) + XCTAssertTrue(machine.hasRoute(.state0 => .state1)) // true + XCTAssertFalse(machine.hasRoute(.state0 => .state2)) + XCTAssertFalse(machine.hasRoute(.state1 => .state0)) + XCTAssertTrue(machine.hasRoute(.state1 => .state1)) // true + XCTAssertFalse(machine.hasRoute(.state1 => .state2)) } // add state => .Any func testAddRoute_toAnyState() { - let machine = StateMachine(state: .State0) { machine in - machine.addRoute(.State1 => .Any) // State1 => Any + let machine = StateMachine(state: .state0) { machine in + machine.addRoute(.state1 => .any) // State1 => Any } - XCTAssertFalse(machine.hasRoute(.State0 => .State0)) - XCTAssertFalse(machine.hasRoute(.State0 => .State1)) - XCTAssertFalse(machine.hasRoute(.State0 => .State2)) - XCTAssertTrue(machine.hasRoute(.State1 => .State0)) // true - XCTAssertTrue(machine.hasRoute(.State1 => .State1)) // true - XCTAssertTrue(machine.hasRoute(.State1 => .State2)) // true + XCTAssertFalse(machine.hasRoute(.state0 => .state0)) + XCTAssertFalse(machine.hasRoute(.state0 => .state1)) + XCTAssertFalse(machine.hasRoute(.state0 => .state2)) + XCTAssertTrue(machine.hasRoute(.state1 => .state0)) // true + XCTAssertTrue(machine.hasRoute(.state1 => .state1)) // true + XCTAssertTrue(machine.hasRoute(.state1 => .state2)) // true } // add .Any => .Any func testAddRoute_bothAnyState() { - let machine = StateMachine(state: .State0) { machine in - machine.addRoute(.Any => .Any) // Any => Any + let machine = StateMachine(state: .state0) { machine in + machine.addRoute(.any => .any) // Any => Any } - XCTAssertTrue(machine.hasRoute(.State0 => .State0)) // true - XCTAssertTrue(machine.hasRoute(.State0 => .State1)) // true - XCTAssertTrue(machine.hasRoute(.State0 => .State2)) // true - XCTAssertTrue(machine.hasRoute(.State1 => .State0)) // true - XCTAssertTrue(machine.hasRoute(.State1 => .State1)) // true - XCTAssertTrue(machine.hasRoute(.State1 => .State2)) // true + XCTAssertTrue(machine.hasRoute(.state0 => .state0)) // true + XCTAssertTrue(machine.hasRoute(.state0 => .state1)) // true + XCTAssertTrue(machine.hasRoute(.state0 => .state2)) // true + XCTAssertTrue(machine.hasRoute(.state1 => .state0)) // true + XCTAssertTrue(machine.hasRoute(.state1 => .state1)) // true + XCTAssertTrue(machine.hasRoute(.state1 => .state2)) // true } // add state0 => state0 func testAddRoute_sameState() { - let machine = StateMachine(state: .State0) { machine in - machine.addRoute(.State0 => .State0) + let machine = StateMachine(state: .state0) { machine in + machine.addRoute(.state0 => .state0) } - XCTAssertTrue(machine.hasRoute(.State0 => .State0)) + XCTAssertTrue(machine.hasRoute(.state0 => .state0)) } // add route + condition @@ -136,33 +136,33 @@ class StateMachineTests: _TestCase { var flag = false - let machine = StateMachine(state: .State0) { machine in + let machine = StateMachine(state: .state0) { machine in // add 0 => 1 - machine.addRoute(.State0 => .State1, condition: { _ in flag }) + machine.addRoute(.state0 => .state1, condition: { _ in flag }) } - XCTAssertFalse(machine.hasRoute(.State0 => .State1)) + XCTAssertFalse(machine.hasRoute(.state0 => .state1)) flag = true - XCTAssertTrue(machine.hasRoute(.State0 => .State1)) + XCTAssertTrue(machine.hasRoute(.state0 => .state1)) } // add route + condition + blacklist func testAddRoute_condition_blacklist() { - let machine = StateMachine(state: .State0) { machine in + let machine = StateMachine(state: .state0) { machine in // add 0 => Any, except 0 => 2 - machine.addRoute(.State0 => .Any, condition: { context in - return context.toState != .State2 + machine.addRoute(.state0 => .any, condition: { context in + return context.toState != .state2 }) } - XCTAssertTrue(machine.hasRoute(.State0 => .State0)) - XCTAssertTrue(machine.hasRoute(.State0 => .State1)) - XCTAssertFalse(machine.hasRoute(.State0 => .State2)) - XCTAssertTrue(machine.hasRoute(.State0 => .State3)) + XCTAssertTrue(machine.hasRoute(.state0 => .state0)) + XCTAssertTrue(machine.hasRoute(.state0 => .state1)) + XCTAssertFalse(machine.hasRoute(.state0 => .state2)) + XCTAssertTrue(machine.hasRoute(.state0 => .state3)) } // add route + handler @@ -170,11 +170,11 @@ class StateMachineTests: _TestCase { var invokedCount = 0 - let machine = StateMachine(state: .State0) { machine in + let machine = StateMachine(state: .state0) { machine in - machine.addRoute(.State0 => .State1) { context in - XCTAssertEqual(context.fromState, MyState.State0) - XCTAssertEqual(context.toState, MyState.State1) + machine.addRoute(.state0 => .state1) { context in + XCTAssertEqual(context.fromState, MyState.state0) + XCTAssertEqual(context.toState, MyState.state1) invokedCount += 1 } @@ -184,7 +184,7 @@ class StateMachineTests: _TestCase XCTAssertEqual(invokedCount, 0, "Transition has not started yet.") // tryState 0 => 1 - machine <- .State1 + machine <- .state1 XCTAssertEqual(invokedCount, 1) } @@ -195,41 +195,41 @@ class StateMachineTests: _TestCase var invokedCount = 0 var flag = false - let machine = StateMachine(state: .State0) { machine in + let machine = StateMachine(state: .state0) { machine in // add 0 => 1 without condition to guarantee 0 => 1 transition - machine.addRoute(.State0 => .State1) + machine.addRoute(.state0 => .state1) // add 0 => 1 with condition + conditionalHandler - machine.addRoute(.State0 => .State1, condition: { _ in flag }) { context in - XCTAssertEqual(context.fromState, MyState.State0) - XCTAssertEqual(context.toState, MyState.State1) + machine.addRoute(.state0 => .state1, condition: { _ in flag }) { context in + XCTAssertEqual(context.fromState, MyState.state0) + XCTAssertEqual(context.toState, MyState.state1) invokedCount += 1 } // add 1 => 0 for resetting state - machine.addRoute(.State1 => .State0) + machine.addRoute(.state1 => .state0) } // tryState 0 => 1 - machine <- .State1 + machine <- .state1 - XCTAssertEqual(machine.state, MyState.State1) + XCTAssertEqual(machine.state, MyState.state1) XCTAssertEqual(invokedCount, 0, "Conditional handler should NOT be performed because flag=false.") // tryState 1 => 0 (resetting to 0) - machine <- .State0 + machine <- .state0 - XCTAssertEqual(machine.state, MyState.State0) + XCTAssertEqual(machine.state, MyState.state0) flag = true // tryState 0 => 1 - machine <- .State1 + machine <- .state1 - XCTAssertEqual(machine.state, MyState.State1) + XCTAssertEqual(machine.state, MyState.state1) XCTAssertEqual(invokedCount, 1) } @@ -238,57 +238,57 @@ class StateMachineTests: _TestCase func testAddRoute_array_left() { - let machine = StateMachine(state: .State0) { machine in + let machine = StateMachine(state: .state0) { machine in // add 0 => 2 or 1 => 2 - machine.addRoute([.State0, .State1] => .State2) + machine.addRoute([.state0, .state1] => .state2) } - XCTAssertFalse(machine.hasRoute(.State0 => .State0)) - XCTAssertFalse(machine.hasRoute(.State0 => .State1)) - XCTAssertTrue(machine.hasRoute(.State0 => .State2)) - XCTAssertFalse(machine.hasRoute(.State1 => .State0)) - XCTAssertFalse(machine.hasRoute(.State1 => .State1)) - XCTAssertTrue(machine.hasRoute(.State1 => .State2)) + XCTAssertFalse(machine.hasRoute(.state0 => .state0)) + XCTAssertFalse(machine.hasRoute(.state0 => .state1)) + XCTAssertTrue(machine.hasRoute(.state0 => .state2)) + XCTAssertFalse(machine.hasRoute(.state1 => .state0)) + XCTAssertFalse(machine.hasRoute(.state1 => .state1)) + XCTAssertTrue(machine.hasRoute(.state1 => .state2)) } func testAddRoute_array_right() { - let machine = StateMachine(state: .State0) { machine in + let machine = StateMachine(state: .state0) { machine in // add 0 => 1 or 0 => 2 - machine.addRoute(.State0 => [.State1, .State2]) + machine.addRoute(.state0 => [.state1, .state2]) } - XCTAssertFalse(machine.hasRoute(.State0 => .State0)) - XCTAssertTrue(machine.hasRoute(.State0 => .State1)) - XCTAssertTrue(machine.hasRoute(.State0 => .State2)) - XCTAssertFalse(machine.hasRoute(.State1 => .State0)) - XCTAssertFalse(machine.hasRoute(.State1 => .State1)) - XCTAssertFalse(machine.hasRoute(.State1 => .State2)) + XCTAssertFalse(machine.hasRoute(.state0 => .state0)) + XCTAssertTrue(machine.hasRoute(.state0 => .state1)) + XCTAssertTrue(machine.hasRoute(.state0 => .state2)) + XCTAssertFalse(machine.hasRoute(.state1 => .state0)) + XCTAssertFalse(machine.hasRoute(.state1 => .state1)) + XCTAssertFalse(machine.hasRoute(.state1 => .state2)) } func testAddRoute_array_both() { - let machine = StateMachine(state: .State0) { machine in + let machine = StateMachine(state: .state0) { machine in // add 0 => 2 or 0 => 3 or 1 => 2 or 1 => 3 - machine.addRoute([MyState.State0, MyState.State1] => [MyState.State2, MyState.State3]) + machine.addRoute([MyState.state0, MyState.state1] => [MyState.state2, MyState.state3]) } - XCTAssertFalse(machine.hasRoute(.State0 => .State0)) - XCTAssertFalse(machine.hasRoute(.State0 => .State1)) - XCTAssertTrue(machine.hasRoute(.State0 => .State2)) - XCTAssertTrue(machine.hasRoute(.State0 => .State3)) - XCTAssertFalse(machine.hasRoute(.State1 => .State0)) - XCTAssertFalse(machine.hasRoute(.State1 => .State1)) - XCTAssertTrue(machine.hasRoute(.State1 => .State2)) - XCTAssertTrue(machine.hasRoute(.State1 => .State3)) - XCTAssertFalse(machine.hasRoute(.State2 => .State0)) - XCTAssertFalse(machine.hasRoute(.State2 => .State1)) - XCTAssertFalse(machine.hasRoute(.State2 => .State2)) - XCTAssertFalse(machine.hasRoute(.State2 => .State3)) - XCTAssertFalse(machine.hasRoute(.State3 => .State0)) - XCTAssertFalse(machine.hasRoute(.State3 => .State1)) - XCTAssertFalse(machine.hasRoute(.State3 => .State2)) - XCTAssertFalse(machine.hasRoute(.State3 => .State3)) + XCTAssertFalse(machine.hasRoute(.state0 => .state0)) + XCTAssertFalse(machine.hasRoute(.state0 => .state1)) + XCTAssertTrue(machine.hasRoute(.state0 => .state2)) + XCTAssertTrue(machine.hasRoute(.state0 => .state3)) + XCTAssertFalse(machine.hasRoute(.state1 => .state0)) + XCTAssertFalse(machine.hasRoute(.state1 => .state1)) + XCTAssertTrue(machine.hasRoute(.state1 => .state2)) + XCTAssertTrue(machine.hasRoute(.state1 => .state3)) + XCTAssertFalse(machine.hasRoute(.state2 => .state0)) + XCTAssertFalse(machine.hasRoute(.state2 => .state1)) + XCTAssertFalse(machine.hasRoute(.state2 => .state2)) + XCTAssertFalse(machine.hasRoute(.state2 => .state3)) + XCTAssertFalse(machine.hasRoute(.state3 => .state0)) + XCTAssertFalse(machine.hasRoute(.state3 => .state1)) + XCTAssertFalse(machine.hasRoute(.state3 => .state2)) + XCTAssertFalse(machine.hasRoute(.state3 => .state3)) } //-------------------------------------------------- @@ -297,30 +297,30 @@ class StateMachineTests: _TestCase func testRemoveRoute() { - let machine = StateMachine(state: .State0) + let machine = StateMachine(state: .state0) - let routeDisposable = machine.addRoute(.State0 => .State1) + let routeDisposable = machine.addRoute(.state0 => .state1) - XCTAssertTrue(machine.hasRoute(.State0 => .State1)) + XCTAssertTrue(machine.hasRoute(.state0 => .state1)) // remove route routeDisposable.dispose() - XCTAssertFalse(machine.hasRoute(.State0 => .State1)) + XCTAssertFalse(machine.hasRoute(.state0 => .state1)) } func testRemoveRoute_handler() { - let machine = StateMachine(state: .State0) + let machine = StateMachine(state: .state0) - let routeDisposable = machine.addRoute(.State0 => .State1, handler: { _ in }) + let routeDisposable = machine.addRoute(.state0 => .state1, handler: { _ in }) - XCTAssertTrue(machine.hasRoute(.State0 => .State1)) + XCTAssertTrue(machine.hasRoute(.state0 => .state1)) // remove route routeDisposable.dispose() - XCTAssertFalse(machine.hasRoute(.State0 => .State1)) + XCTAssertFalse(machine.hasRoute(.state0 => .state1)) } //-------------------------------------------------- @@ -331,14 +331,14 @@ class StateMachineTests: _TestCase { var invokedCount = 0 - let machine = StateMachine(state: .State0) { machine in + let machine = StateMachine(state: .state0) { machine in // add 0 => 1 - machine.addRoute(.State0 => .State1) + machine.addRoute(.state0 => .state1) - machine.addHandler(.State0 => .State1) { context in - XCTAssertEqual(context.fromState, MyState.State0) - XCTAssertEqual(context.toState, MyState.State1) + machine.addHandler(.state0 => .state1) { context in + XCTAssertEqual(context.fromState, MyState.state0) + XCTAssertEqual(context.toState, MyState.state1) invokedCount += 1 } @@ -349,7 +349,7 @@ class StateMachineTests: _TestCase XCTAssertEqual(invokedCount, 0, "Transition has not started yet.") // tryState 0 => 1 - machine <- .State1 + machine <- .state1 XCTAssertEqual(invokedCount, 1) } @@ -358,27 +358,27 @@ class StateMachineTests: _TestCase { var invokedCount = 0 - let machine = StateMachine(state: .State0) { machine in + let machine = StateMachine(state: .state0) { machine in // add 0 => 1 - machine.addRoute(.State0 => .State1) + machine.addRoute(.state0 => .state1) // order = 100 (default) - machine.addHandler(.State0 => .State1) { context in + machine.addHandler(.state0 => .state1) { context in XCTAssertEqual(invokedCount, 1) - XCTAssertEqual(context.fromState, MyState.State0) - XCTAssertEqual(context.toState, MyState.State1) + XCTAssertEqual(context.fromState, MyState.state0) + XCTAssertEqual(context.toState, MyState.state1) invokedCount += 1 } // order = 99 - machine.addHandler(.State0 => .State1, order: 99) { context in + machine.addHandler(.state0 => .state1, order: 99) { context in XCTAssertEqual(invokedCount, 0) - XCTAssertEqual(context.fromState, MyState.State0) - XCTAssertEqual(context.toState, MyState.State1) + XCTAssertEqual(context.fromState, MyState.state0) + XCTAssertEqual(context.toState, MyState.state1) invokedCount += 1 } @@ -388,7 +388,7 @@ class StateMachineTests: _TestCase XCTAssertEqual(invokedCount, 0) // tryState 0 => 1 - machine <- .State1 + machine <- .state1 XCTAssertEqual(invokedCount, 2) } @@ -399,26 +399,26 @@ class StateMachineTests: _TestCase var passed1 = false var passed2 = false - let machine = StateMachine(state: .State0) { machine in + let machine = StateMachine(state: .state0) { machine in // add 0 => 1 - machine.addRoute(.State0 => .State1) + machine.addRoute(.state0 => .state1) - machine.addHandler(.State0 => .State1) { context in + machine.addHandler(.state0 => .state1) { context in passed1 = true } // add 0 => 1 once more - machine.addRoute(.State0 => .State1) + machine.addRoute(.state0 => .state1) - machine.addHandler(.State0 => .State1) { context in + machine.addHandler(.state0 => .state1) { context in passed2 = true } } // tryState 0 => 1 - machine <- .State1 + machine <- .state1 XCTAssertTrue(passed1) XCTAssertTrue(passed2) @@ -428,15 +428,15 @@ class StateMachineTests: _TestCase { var passed = false - let machine = StateMachine(state: .State0) { machine in + let machine = StateMachine(state: .state0) { machine in - machine.addRoute(.State0 => .State1) + machine.addRoute(.state0 => .state1) - machine.addHandler(.State0 => .State1) { context in + machine.addHandler(.state0 => .state1) { context in // empty } - machine.addHandler(.State0 => .State1) { context in + machine.addHandler(.state0 => .state1) { context in passed = true } @@ -444,7 +444,7 @@ class StateMachineTests: _TestCase XCTAssertFalse(passed) - machine <- .State1 + machine <- .state1 XCTAssertTrue(passed) } @@ -457,19 +457,19 @@ class StateMachineTests: _TestCase { var passed = false - let machine = StateMachine(state: .State0) { machine in + let machine = StateMachine(state: .state0) { machine in // add 0 => 1 - machine.addRoute(.State0 => .State1) + machine.addRoute(.state0 => .state1) - let handlerDisposable = machine.addHandler(.State0 => .State1) { context in + let handlerDisposable = machine.addHandler(.state0 => .state1) { context in XCTFail("Should never reach here") } // add 0 => 1 once more - machine.addRoute(.State0 => .State1) + machine.addRoute(.state0 => .state1) - machine.addHandler(.State0 => .State1) { context in + machine.addHandler(.state0 => .state1) { context in passed = true } @@ -481,19 +481,19 @@ class StateMachineTests: _TestCase XCTAssertFalse(passed) // tryState 0 => 1 - machine <- .State1 + machine <- .state1 XCTAssertTrue(passed) } func testRemoveHandler_unregistered() { - let machine = StateMachine(state: .State0) + let machine = StateMachine(state: .state0) // add 0 => 1 - machine.addRoute(.State0 => .State1) + machine.addRoute(.state0 => .state1) - let handlerDisposable = machine.addHandler(.State0 => .State1) { context in + let handlerDisposable = machine.addHandler(.state0 => .state1) { context in // empty } @@ -510,17 +510,17 @@ class StateMachineTests: _TestCase { var passed = false - let machine = StateMachine(state: .State0) { machine in + let machine = StateMachine(state: .state0) { machine in // add 2 => 1 - machine.addRoute(.State2 => .State1) + machine.addRoute(.state2 => .state1) let handlerDisposable = machine.addErrorHandler { context in XCTFail("Should never reach here") } // add 2 => 1 once more - machine.addRoute(.State2 => .State1) + machine.addRoute(.state2 => .state1) machine.addErrorHandler { context in passed = true @@ -532,17 +532,17 @@ class StateMachineTests: _TestCase } // tryState 0 => 1 - machine <- .State1 + machine <- .state1 XCTAssertTrue(passed) } func testRemoveErrorHandler_unregistered() { - let machine = StateMachine(state: .State0) + let machine = StateMachine(state: .state0) // add 0 => 1 - machine.addRoute(.State0 => .State1) + machine.addRoute(.state0 => .state1) let handlerDisposable = machine.addErrorHandler { context in // empty @@ -565,30 +565,30 @@ class StateMachineTests: _TestCase { var success = false - let machine = StateMachine(state: .State0) { machine in + let machine = StateMachine(state: .state0) { machine in // add 0 => 1 => 2 => 3 - machine.addRouteChain(.State0 => .State1 => .State2 => .State3) { context in + machine.addRouteChain(.state0 => .state1 => .state2 => .state3) { context in success = true } } // initial - XCTAssertEqual(machine.state, MyState.State0) + XCTAssertEqual(machine.state, MyState.state0) // 0 => 1 - machine <- .State1 - XCTAssertEqual(machine.state, MyState.State1) + machine <- .state1 + XCTAssertEqual(machine.state, MyState.state1) XCTAssertFalse(success, "RouteChain is not completed yet.") // 1 => 2 - machine <- .State2 - XCTAssertEqual(machine.state, MyState.State2) + machine <- .state2 + XCTAssertEqual(machine.state, MyState.state2) XCTAssertFalse(success, "RouteChain is not completed yet.") // 2 => 3 - machine <- .State3 - XCTAssertEqual(machine.state, MyState.State3) + machine <- .state3 + XCTAssertEqual(machine.state, MyState.state3) XCTAssertTrue(success) } @@ -596,43 +596,43 @@ class StateMachineTests: _TestCase { var success = false - let machine = StateMachine(state: .State0) { machine in + let machine = StateMachine(state: .state0) { machine in // add all routes - machine.addRoute(.Any => .Any) + machine.addRoute(.any => .any) // add 0 => 1 => 2 => 3 - machine.addChainHandler(.State0 => .State1 => .State2 => .State3) { context in + machine.addChainHandler(.state0 => .state1 => .state2 => .state3) { context in success = true } } // initial - XCTAssertEqual(machine.state, MyState.State0) + XCTAssertEqual(machine.state, MyState.state0) // 0 => 1 - machine <- .State1 - XCTAssertEqual(machine.state, MyState.State1) + machine <- .state1 + XCTAssertEqual(machine.state, MyState.state1) XCTAssertFalse(success, "RouteChain is not completed yet.") // 1 => 2 - machine <- .State2 - XCTAssertEqual(machine.state, MyState.State2) + machine <- .state2 + XCTAssertEqual(machine.state, MyState.state2) XCTAssertFalse(success, "RouteChain is not completed yet.") // 2 => 2 (fails & resets chaining count) - machine <- .State2 - XCTAssertEqual(machine.state, MyState.State2, "State should not change.") + machine <- .state2 + XCTAssertEqual(machine.state, MyState.state2, "State should not change.") XCTAssertFalse(success, "RouteChain failed and reset count.") // 2 => 3 (chaining is failed) - machine <- .State3 - XCTAssertEqual(machine.state, MyState.State3) + machine <- .state3 + XCTAssertEqual(machine.state, MyState.state3) XCTAssertFalse(success, "RouteChain is already failed.") // go back to 0 & run 0 => 1 => 2 => 3 - machine <- .State0 <- .State1 <- .State2 <- .State3 - XCTAssertEqual(machine.state, MyState.State3) + machine <- .state0 <- .state1 <- .state2 <- .state3 + XCTAssertEqual(machine.state, MyState.state3) XCTAssertTrue(success, "RouteChain is resetted & should succeed its chaining.") } @@ -644,12 +644,12 @@ class StateMachineTests: _TestCase { var routeMappingDisposable: Disposable? - let machine = StateMachine(state: .State0) { machine in + let machine = StateMachine(state: .state0) { machine in // add 0 => 1 & 0 => 2 routeMappingDisposable = machine.addStateRouteMapping { fromState, userInfo -> [MyState]? in - if fromState == .State0 { - return [.State1, .State2] + if fromState == .state0 { + return [.state1, .state2] } else { return nil @@ -658,28 +658,28 @@ class StateMachineTests: _TestCase } - XCTAssertFalse(machine.hasRoute(.State0 => .State0)) - XCTAssertTrue(machine.hasRoute(.State0 => .State1)) - XCTAssertTrue(machine.hasRoute(.State0 => .State2)) - XCTAssertFalse(machine.hasRoute(.State1 => .State0)) - XCTAssertFalse(machine.hasRoute(.State1 => .State1)) - XCTAssertFalse(machine.hasRoute(.State1 => .State2)) - XCTAssertFalse(machine.hasRoute(.State2 => .State0)) - XCTAssertFalse(machine.hasRoute(.State2 => .State1)) - XCTAssertFalse(machine.hasRoute(.State2 => .State2)) + XCTAssertFalse(machine.hasRoute(.state0 => .state0)) + XCTAssertTrue(machine.hasRoute(.state0 => .state1)) + XCTAssertTrue(machine.hasRoute(.state0 => .state2)) + XCTAssertFalse(machine.hasRoute(.state1 => .state0)) + XCTAssertFalse(machine.hasRoute(.state1 => .state1)) + XCTAssertFalse(machine.hasRoute(.state1 => .state2)) + XCTAssertFalse(machine.hasRoute(.state2 => .state0)) + XCTAssertFalse(machine.hasRoute(.state2 => .state1)) + XCTAssertFalse(machine.hasRoute(.state2 => .state2)) // remove routeMapping routeMappingDisposable?.dispose() - XCTAssertFalse(machine.hasRoute(.State0 => .State0)) - XCTAssertFalse(machine.hasRoute(.State0 => .State1)) - XCTAssertFalse(machine.hasRoute(.State0 => .State2)) - XCTAssertFalse(machine.hasRoute(.State1 => .State0)) - XCTAssertFalse(machine.hasRoute(.State1 => .State1)) - XCTAssertFalse(machine.hasRoute(.State1 => .State2)) - XCTAssertFalse(machine.hasRoute(.State2 => .State0)) - XCTAssertFalse(machine.hasRoute(.State2 => .State1)) - XCTAssertFalse(machine.hasRoute(.State2 => .State2)) + XCTAssertFalse(machine.hasRoute(.state0 => .state0)) + XCTAssertFalse(machine.hasRoute(.state0 => .state1)) + XCTAssertFalse(machine.hasRoute(.state0 => .state2)) + XCTAssertFalse(machine.hasRoute(.state1 => .state0)) + XCTAssertFalse(machine.hasRoute(.state1 => .state1)) + XCTAssertFalse(machine.hasRoute(.state1 => .state2)) + XCTAssertFalse(machine.hasRoute(.state2 => .state0)) + XCTAssertFalse(machine.hasRoute(.state2 => .state1)) + XCTAssertFalse(machine.hasRoute(.state2 => .state2)) } func testAddStateRouteMapping_handler() @@ -687,13 +687,13 @@ class StateMachineTests: _TestCase var invokedCount = 0 var routeMappingDisposable: Disposable? - let machine = StateMachine(state: .State0) { machine in + let machine = StateMachine(state: .state0) { machine in // add 0 => 1 & 0 => 2 routeMappingDisposable = machine.addStateRouteMapping({ fromState, userInfo -> [MyState]? in - if fromState == .State0 { - return [.State1, .State2] + if fromState == .state0 { + return [.state1, .state2] } else { return nil @@ -706,20 +706,20 @@ class StateMachineTests: _TestCase } // initial - XCTAssertEqual(machine.state, MyState.State0) + XCTAssertEqual(machine.state, MyState.state0) XCTAssertEqual(invokedCount, 0) // 0 => 1 - machine <- .State1 - XCTAssertEqual(machine.state, MyState.State1) + machine <- .state1 + XCTAssertEqual(machine.state, MyState.state1) XCTAssertEqual(invokedCount, 1) // remove routeMapping routeMappingDisposable?.dispose() // 1 => 2 (fails) - machine <- .State1 - XCTAssertEqual(machine.state, MyState.State1) + machine <- .state1 + XCTAssertEqual(machine.state, MyState.state1) XCTAssertEqual(invokedCount, 1) } @@ -729,12 +729,12 @@ class StateMachineTests: _TestCase { var routeMappingDisposable: Disposable? - let machine = StateMachine(state: .State0) { machine in + let machine = StateMachine(state: .state0) { machine in // add 0 => 1 & 0 => 2 routeMappingDisposable = machine.addStateRouteMapping { fromState, userInfo -> [MyState]? in - if fromState == .State0 { - return [.State1, .State2] + if fromState == .state0 { + return [.state1, .state2] } else { return nil @@ -745,8 +745,8 @@ class StateMachineTests: _TestCase machine.addRouteMapping { event, fromState, userInfo -> MyState? in guard event == nil else { return nil } - if fromState == .State1 { - return .State0 + if fromState == .state1 { + return .state0 } else { return nil @@ -754,27 +754,27 @@ class StateMachineTests: _TestCase } } - XCTAssertFalse(machine.hasRoute(.State0 => .State0)) - XCTAssertTrue(machine.hasRoute(.State0 => .State1)) - XCTAssertTrue(machine.hasRoute(.State0 => .State2)) - XCTAssertTrue(machine.hasRoute(.State1 => .State0)) - XCTAssertFalse(machine.hasRoute(.State1 => .State1)) - XCTAssertFalse(machine.hasRoute(.State1 => .State2)) - XCTAssertFalse(machine.hasRoute(.State2 => .State0)) - XCTAssertFalse(machine.hasRoute(.State2 => .State1)) - XCTAssertFalse(machine.hasRoute(.State2 => .State2)) + XCTAssertFalse(machine.hasRoute(.state0 => .state0)) + XCTAssertTrue(machine.hasRoute(.state0 => .state1)) + XCTAssertTrue(machine.hasRoute(.state0 => .state2)) + XCTAssertTrue(machine.hasRoute(.state1 => .state0)) + XCTAssertFalse(machine.hasRoute(.state1 => .state1)) + XCTAssertFalse(machine.hasRoute(.state1 => .state2)) + XCTAssertFalse(machine.hasRoute(.state2 => .state0)) + XCTAssertFalse(machine.hasRoute(.state2 => .state1)) + XCTAssertFalse(machine.hasRoute(.state2 => .state2)) // remove routeMapping routeMappingDisposable?.dispose() - XCTAssertFalse(machine.hasRoute(.State0 => .State0)) - XCTAssertFalse(machine.hasRoute(.State0 => .State1)) - XCTAssertFalse(machine.hasRoute(.State0 => .State2)) - XCTAssertTrue(machine.hasRoute(.State1 => .State0)) - XCTAssertFalse(machine.hasRoute(.State1 => .State1)) - XCTAssertFalse(machine.hasRoute(.State1 => .State2)) - XCTAssertFalse(machine.hasRoute(.State2 => .State0)) - XCTAssertFalse(machine.hasRoute(.State2 => .State1)) - XCTAssertFalse(machine.hasRoute(.State2 => .State2)) + XCTAssertFalse(machine.hasRoute(.state0 => .state0)) + XCTAssertFalse(machine.hasRoute(.state0 => .state1)) + XCTAssertFalse(machine.hasRoute(.state0 => .state2)) + XCTAssertTrue(machine.hasRoute(.state1 => .state0)) + XCTAssertFalse(machine.hasRoute(.state1 => .state1)) + XCTAssertFalse(machine.hasRoute(.state1 => .state2)) + XCTAssertFalse(machine.hasRoute(.state2 => .state0)) + XCTAssertFalse(machine.hasRoute(.state2 => .state1)) + XCTAssertFalse(machine.hasRoute(.state2 => .state2)) } } diff --git a/Tests/StateTests.swift b/Tests/StateTests.swift index 855eb3e..d98e9a7 100644 --- a/Tests/StateTests.swift +++ b/Tests/StateTests.swift @@ -13,27 +13,27 @@ class StateTests: _TestCase { func testInit_state() { - let state = State(rawValue: .State0) - XCTAssertTrue(state == .State0) - XCTAssertTrue(.State0 == state) + let state = State(rawValue: .state0) + XCTAssertTrue(state == .state0) + XCTAssertTrue(.state0 == state) } func testInit_nil() { let state = State(rawValue: nil) - XCTAssertTrue(state == .Any) - XCTAssertTrue(.Any == state) + XCTAssertTrue(state == .any) + XCTAssertTrue(.any == state) } func testRawValue_state() { - let state = State.Some(.State0) - XCTAssertTrue(state.rawValue == .State0) + let state = State.some(.state0) + XCTAssertTrue(state.rawValue == .state0) } func testRawValue_any() { - let state = State.Any + let state = State.any XCTAssertTrue(state.rawValue == nil) } } diff --git a/Tests/TransitionChainTests.swift b/Tests/TransitionChainTests.swift index 333f077..523e838 100644 --- a/Tests/TransitionChainTests.swift +++ b/Tests/TransitionChainTests.swift @@ -14,79 +14,79 @@ class TransitionChainTests: _TestCase func testInit() { // 0 => 1 => 2 - var chain = MyState.State0 => .State1 => .State2 + var chain = MyState.state0 => .state1 => .state2 XCTAssertEqual(chain.states.count, 3) - XCTAssertTrue(chain.states[0] == .State0) - XCTAssertTrue(chain.states[1] == .State1) - XCTAssertTrue(chain.states[2] == .State2) + XCTAssertTrue(chain.states[0] == .state0) + XCTAssertTrue(chain.states[1] == .state1) + XCTAssertTrue(chain.states[2] == .state2) // (1 => 2) => 3 - chain = MyState.State1 => .State2 => .State3 + chain = MyState.state1 => .state2 => .state3 XCTAssertEqual(chain.states.count, 3) - XCTAssertTrue(chain.states[0] == .State1) - XCTAssertTrue(chain.states[1] == .State2) - XCTAssertTrue(chain.states[2] == .State3) + XCTAssertTrue(chain.states[0] == .state1) + XCTAssertTrue(chain.states[1] == .state2) + XCTAssertTrue(chain.states[2] == .state3) // 2 => (3 => 0) - chain = MyState.State2 => (.State3 => .State0) + chain = MyState.state2 => (.state3 => .state0) XCTAssertEqual(chain.states.count, 3) - XCTAssertTrue(chain.states[0] == .State2) - XCTAssertTrue(chain.states[1] == .State3) - XCTAssertTrue(chain.states[2] == .State0) + XCTAssertTrue(chain.states[0] == .state2) + XCTAssertTrue(chain.states[1] == .state3) + XCTAssertTrue(chain.states[2] == .state0) } func testAppend() { // 0 => 1 - let transition = MyState.State0 => .State1 + let transition = MyState.state0 => .state1 var chain = TransitionChain(transition: transition) XCTAssertEqual(chain.states.count, 2) - XCTAssertTrue(chain.states[0] == .State0) - XCTAssertTrue(chain.states[1] == .State1) + XCTAssertTrue(chain.states[0] == .state0) + XCTAssertTrue(chain.states[1] == .state1) // 0 => 1 => 2 - chain = chain => .State2 + chain = chain => .state2 XCTAssertEqual(chain.states.count, 3) - XCTAssertTrue(chain.states[0] == .State0) - XCTAssertTrue(chain.states[1] == .State1) - XCTAssertTrue(chain.states[2] == .State2) + XCTAssertTrue(chain.states[0] == .state0) + XCTAssertTrue(chain.states[1] == .state1) + XCTAssertTrue(chain.states[2] == .state2) // 0 => 1 => 2 => 3 - chain = chain => .State3 + chain = chain => .state3 XCTAssertEqual(chain.states.count, 4) - XCTAssertTrue(chain.states[0] == .State0) - XCTAssertTrue(chain.states[1] == .State1) - XCTAssertTrue(chain.states[2] == .State2) - XCTAssertTrue(chain.states[3] == .State3) + XCTAssertTrue(chain.states[0] == .state0) + XCTAssertTrue(chain.states[1] == .state1) + XCTAssertTrue(chain.states[2] == .state2) + XCTAssertTrue(chain.states[3] == .state3) } func testPrepend() { // 0 => 1 - let transition = MyState.State0 => .State1 + let transition = MyState.state0 => .state1 var chain = TransitionChain(transition: transition) XCTAssertEqual(chain.states.count, 2) - XCTAssertTrue(chain.states[0] == .State0) - XCTAssertTrue(chain.states[1] == .State1) + XCTAssertTrue(chain.states[0] == .state0) + XCTAssertTrue(chain.states[1] == .state1) // 2 => 0 => 1 - chain = .State2 => chain // same as prepend + chain = .state2 => chain // same as prepend XCTAssertEqual(chain.states.count, 3) - XCTAssertTrue(chain.states[0] == .State2) - XCTAssertTrue(chain.states[1] == .State0) - XCTAssertTrue(chain.states[2] == .State1) + XCTAssertTrue(chain.states[0] == .state2) + XCTAssertTrue(chain.states[1] == .state0) + XCTAssertTrue(chain.states[2] == .state1) // 3 => 2 => 0 => 1 - chain = .State3 => chain + chain = .state3 => chain XCTAssertEqual(chain.states.count, 4) - XCTAssertTrue(chain.states[0] == .State3) - XCTAssertTrue(chain.states[1] == .State2) - XCTAssertTrue(chain.states[2] == .State0) - XCTAssertTrue(chain.states[3] == .State1) + XCTAssertTrue(chain.states[0] == .state3) + XCTAssertTrue(chain.states[1] == .state2) + XCTAssertTrue(chain.states[2] == .state0) + XCTAssertTrue(chain.states[3] == .state1) } } diff --git a/Tests/TransitionTests.swift b/Tests/TransitionTests.swift index f9f4834..0f1a090 100644 --- a/Tests/TransitionTests.swift +++ b/Tests/TransitionTests.swift @@ -13,55 +13,55 @@ class TransitionTests: _TestCase { func testInit() { - let transition = Transition(fromState: .State0, toState: .State1) - XCTAssertEqual(transition.fromState.rawValue, MyState.State0) - XCTAssertEqual(transition.toState.rawValue, MyState.State1) + let transition = Transition(fromState: .state0, toState: .state1) + XCTAssertEqual(transition.fromState.rawValue, MyState.state0) + XCTAssertEqual(transition.toState.rawValue, MyState.state1) // shorthand - let transition2 = MyState.State1 => .State0 - XCTAssertEqual(transition2.fromState.rawValue, MyState.State1) - XCTAssertEqual(transition2.toState.rawValue, MyState.State0) + let transition2 = MyState.state1 => .state0 + XCTAssertEqual(transition2.fromState.rawValue, MyState.state1) + XCTAssertEqual(transition2.toState.rawValue, MyState.state0) } func testInit_fromAny() { - let transition = Transition(fromState: .Any, toState: .State1) + let transition = Transition(fromState: .any, toState: .state1) XCTAssertNil(transition.fromState.rawValue) - XCTAssertEqual(transition.toState.rawValue, MyState.State1) + XCTAssertEqual(transition.toState.rawValue, MyState.state1) // shorthand - let transition2 = .Any => MyState.State0 + let transition2 = .any => MyState.state0 XCTAssertNil(transition2.fromState.rawValue) - XCTAssertEqual(transition2.toState.rawValue, MyState.State0) + XCTAssertEqual(transition2.toState.rawValue, MyState.state0) } func testInit_toAny() { - let transition = Transition(fromState: .State0, toState: .Any) - XCTAssertEqual(transition.fromState.rawValue, MyState.State0) + let transition = Transition(fromState: .state0, toState: .any) + XCTAssertEqual(transition.fromState.rawValue, MyState.state0) XCTAssertNil(transition.toState.rawValue) // shorthand - let transition2 = MyState.State1 => .Any - XCTAssertEqual(transition2.fromState.rawValue, MyState.State1) + let transition2 = MyState.state1 => .any + XCTAssertEqual(transition2.fromState.rawValue, MyState.state1) XCTAssertNil(transition2.toState.rawValue) } func testNil() { // .Any => state - let transition = .Any => MyState.State0 - XCTAssertTrue(transition.fromState == .Any) - XCTAssertTrue(transition.toState == .State0) + let transition = .any => MyState.state0 + XCTAssertTrue(transition.fromState == .any) + XCTAssertTrue(transition.toState == .state0) // state => .Any - let transition2 = MyState.State0 => .Any - XCTAssertTrue(transition2.fromState == .State0) - XCTAssertTrue(transition2.toState == .Any) + let transition2 = MyState.state0 => .any + XCTAssertTrue(transition2.fromState == .state0) + XCTAssertTrue(transition2.toState == .any) // .Any => .Any - let transition3: Transition = .Any => .Any - XCTAssertTrue(transition3.fromState == .Any) - XCTAssertTrue(transition3.toState == .Any) + let transition3: Transition = .any => .any + XCTAssertTrue(transition3.fromState == .any) + XCTAssertTrue(transition3.toState == .any) } } From 4d7e2fdc5128bf3b408507366bda8934773ffa8c Mon Sep 17 00:00:00 2001 From: Matt Prowse Date: Fri, 2 Sep 2016 08:51:29 +1000 Subject: [PATCH 02/27] Changes for Xcode 8 beta compatibility. --- Sources/Disposable.swift | 2 +- Sources/Machine.swift | 12 ++++++------ Sources/StateMachine.swift | 8 ++++---- Sources/Transition.swift | 2 +- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/Sources/Disposable.swift b/Sources/Disposable.swift index fe9489e..24c9b70 100644 --- a/Sources/Disposable.swift +++ b/Sources/Disposable.swift @@ -34,7 +34,7 @@ public final class ActionDisposable: Disposable { } /// Initializes the disposable to run the given action upon disposal. - public init(action: () -> ()) { + public init(action: (() -> ())) { self.action = action } diff --git a/Sources/Machine.swift b/Sources/Machine.swift index f8917d8..aa9a48f 100644 --- a/Sources/Machine.swift +++ b/Sources/Machine.swift @@ -28,7 +28,7 @@ public class Machine /// Closure-based route, mainly for `tryEvent()` (and also works for subclass's `tryState()`). /// - Returns: Preferred `toState`. - public typealias RouteMapping = (event: E?, fromState: S, userInfo: Any?) -> S? + public typealias RouteMapping = (_ event: E?, _ fromState: S, _ userInfo: Any?) -> S? internal typealias _RouteDict = [Transition : [String : Condition?]] @@ -142,7 +142,7 @@ public class Machine private func _hasRouteMappingInDict(event: E?, fromState: S, toState: S?, userInfo: Any? = nil) -> S? { for mapping in self._routeMappings.values { - if let preferredToState = mapping(event: event, fromState: fromState, userInfo: userInfo), + if let preferredToState = mapping(event, fromState, userInfo), preferredToState == toState || toState == nil { return preferredToState @@ -218,7 +218,7 @@ public class Machine let validHandlerInfos = [ self._handlers[.some(event)], self._handlers[.any] ] .filter { $0 != nil } .map { $0! } - .flatten() + .joined() return validHandlerInfos.sorted { info1, info2 in return info1.order < info2.order @@ -389,7 +389,7 @@ public class Machine let handlerDisposable = self._addHandler(event: .any, order: order) { context in - guard let preferredToState = routeMapping(event: context.event, fromState: context.fromState, userInfo: context.userInfo), + guard let preferredToState = routeMapping(context.event, context.fromState, context.userInfo), preferredToState == context.toState else { return @@ -518,7 +518,7 @@ public class Machine // MARK: `<-!` (tryEvent) -infix operator <-! { associativity left } +infix operator <-! : AdditionPrecedence @discardableResult public func <-! (machine: Machine, event: E) -> Machine @@ -552,7 +552,7 @@ internal func _createUniqueString() -> String { var uniqueString: String = "" for _ in 1...8 { - uniqueString += String(UnicodeScalar(_random(0xD800))) // 0xD800 = 55296 = 15.755bit + uniqueString += String(describing: UnicodeScalar(_random(0xD800))) // 0xD800 = 55296 = 15.755bit } return uniqueString } diff --git a/Sources/StateMachine.swift b/Sources/StateMachine.swift index 1b22e17..88822aa 100644 --- a/Sources/StateMachine.swift +++ b/Sources/StateMachine.swift @@ -16,7 +16,7 @@ public final class StateMachine: Machine { /// Closure-based routes for `tryState()`. /// - Returns: Multiple `toState`s from single `fromState`, similar to `.State0 => [.State1, .State2]` - public typealias StateRouteMapping = (fromState: S, userInfo: Any?) -> [S]? + public typealias StateRouteMapping = (_ fromState: S, _ userInfo: Any?) -> [S]? private lazy var _routes: _RouteDict = [:] private lazy var _routeMappings: [String : StateRouteMapping] = [:] // NOTE: `StateRouteMapping`, not `RouteMapping` @@ -99,7 +99,7 @@ public final class StateMachine: Machine private func _hasRouteMappingInDict(fromState: S, toState: S, userInfo: Any? = nil) -> S? { for mapping in self._routeMappings.values { - if let preferredToStates = mapping(fromState: fromState, userInfo: userInfo) { + if let preferredToStates = mapping(fromState, userInfo) { return preferredToStates.contains(toState) ? toState : nil } } @@ -507,7 +507,7 @@ public final class StateMachine: Machine guard context.event == nil else { return } - guard let preferredToStates = routeMapping(fromState: context.fromState, userInfo: context.userInfo), preferredToStates.contains(context.toState) else + guard let preferredToStates = routeMapping(context.fromState, context.userInfo), preferredToStates.contains(context.toState) else { return } @@ -543,7 +543,7 @@ public final class StateMachine: Machine // MARK: `<-` (tryState) -infix operator <- { associativity left } +infix operator <- : AdditionPrecedence @discardableResult public func <- (machine: StateMachine, state: S) -> StateMachine diff --git a/Sources/Transition.swift b/Sources/Transition.swift index c84ead1..f8a8ec4 100644 --- a/Sources/Transition.swift +++ b/Sources/Transition.swift @@ -52,7 +52,7 @@ public func == (left: Transition, right: Transition) -> Bool // MARK: - Custom Operators //-------------------------------------------------- -infix operator => { associativity left } +infix operator => : AdditionPrecedence /// e.g. .State0 => .State1 public func => (left: State, right: State) -> Transition From f0c70e31dab4b3c3b2063068a55999186de5ccbe Mon Sep 17 00:00:00 2001 From: Matt Prowse Date: Mon, 5 Sep 2016 09:50:28 +1000 Subject: [PATCH 03/27] Lowercased enum cases in documentation and comments. --- README.md | 132 +++++++++++++-------------- Sources/EventType.swift | 2 +- Sources/Machine.swift | 4 +- Sources/Route.swift | 8 +- Sources/StateMachine.swift | 4 +- Sources/StateType.swift | 2 +- Sources/Transition.swift | 6 +- Sources/TransitionChain.swift | 6 +- Tests/BasicTests.swift | 2 +- Tests/HierarchicalMachineTests.swift | 10 +- Tests/RouteChainTests.swift | 2 +- Tests/RouteMappingTests.swift | 24 ++--- Tests/StateMachineTests.swift | 6 +- Tests/TransitionTests.swift | 6 +- 14 files changed, 107 insertions(+), 107 deletions(-) diff --git a/README.md b/README.md index 4a5db53..29fbad5 100644 --- a/README.md +++ b/README.md @@ -10,20 +10,20 @@ Elegant state machine for Swift. ```swift enum MyState: StateType { - case State0, State1, State2 + case state0, state1, state2 } ``` ```swift // setup state machine -let machine = StateMachine(state: .State0) { machine in +let machine = StateMachine(state: .state0) { machine in - machine.addRoute(.State0 => .State1) - machine.addRoute(.Any => .State2) { context in print("Any => 2, msg=\(context.userInfo)") } - machine.addRoute(.State2 => .Any) { context in print("2 => Any, msg=\(context.userInfo)") } + machine.addRoute(.state0 => .state1) + machine.addRoute(.any => .state2) { context in print("Any => 2, msg=\(context.userInfo)") } + machine.addRoute(.state2 => .any) { context in print("2 => Any, msg=\(context.userInfo)") } // add handler (`context = (event, fromState, toState, userInfo)`) - machine.addHandler(.State0 => .State1) { context in + machine.addHandler(.state0 => .state1) { context in print("0 => 1") } @@ -34,21 +34,21 @@ let machine = StateMachine(state: .State0) { machine in } // initial -XCTAssertEqual(machine.state, MyState.State0) +XCTAssertEqual(machine.state, MyState.state0) // tryState 0 => 1 => 2 => 1 => 0 -machine <- .State1 -XCTAssertEqual(machine.state, MyState.State1) +machine <- .state1 +XCTAssertEqual(machine.state, MyState.state1) -machine <- (.State2, "Hello") -XCTAssertEqual(machine.state, MyState.State2) +machine <- (.state2, "Hello") +XCTAssertEqual(machine.state, MyState.state2) -machine <- (.State1, "Bye") -XCTAssertEqual(machine.state, MyState.State1) +machine <- (.state1, "Bye") +XCTAssertEqual(machine.state, MyState.state1) -machine <- .State0 // fail: no 1 => 0 -XCTAssertEqual(machine.state, MyState.State1) +machine <- .state0 // fail: no 1 => 0 +XCTAssertEqual(machine.state, MyState.state1) ``` This will print: @@ -57,7 +57,7 @@ This will print: 0 => 1 Any => 2, msg=Optional("Hello") 2 => Any, msg=Optional("Bye") -[ERROR] State1 => State0 +[ERROR] state1 => state0 ``` ### Transition by Event @@ -66,39 +66,39 @@ Use `<-!` operator to try transition by `Event` rather than specifying target `S ```swift enum MyEvent: EventType { - case Event0, Event1 + case event0, event1 } ``` ```swift -let machine = StateMachine(state: .State0) { machine in +let machine = StateMachine(state: .state0) { machine in // add 0 => 1 => 2 - machine.addRoutes(event: .Event0, transitions: [ - .State0 => .State1, - .State1 => .State2, + machine.addRoutes(event: .event0, transitions: [ + .state0 => .state1, + .state1 => .state2, ]) // add event handler - machine.addHandler(event: .Event0) { context in - print(".Event0 triggered!") + machine.addHandler(event: .event0) { context in + print(".event0 triggered!") } } // initial -XCTAssertEqual(machine.state, MyState.State0) +XCTAssertEqual(machine.state, MyState.state0) // tryEvent -machine <-! .Event0 -XCTAssertEqual(machine.state, MyState.State1) +machine <-! .event0 +XCTAssertEqual(machine.state, MyState.state1) // tryEvent -machine <-! .Event0 -XCTAssertEqual(machine.state, MyState.State2) +machine <-! .event0 +XCTAssertEqual(machine.state, MyState.state2) // tryEvent (fails) -machine <-! .Event0 -XCTAssertEqual(machine.state, MyState.State2, "Event0 doesn't have 2 => Any") +machine <-! .event0 +XCTAssertEqual(machine.state, MyState.state2, "event0 doesn't have 2 => Any") ``` If there is no `Event`-based transition, use built-in `NoEvent` instead. @@ -116,25 +116,25 @@ For example: ```swift enum StrState: StateType { - case Str(String) ... + case str(String) ... } enum StrEvent: EventType { - case Str(String) ... + case str(String) ... } -let machine = Machine(state: .Str("initial")) { machine in +let machine = Machine(state: .str("initial")) { machine in machine.addRouteMapping { event, fromState, userInfo -> StrState? in // no route for no-event guard let event = event else { return nil } switch (event, fromState) { - case (.Str("gogogo"), .Str("initial")): - return .Str("Phase 1") - case (.Str("gogogo"), .Str("Phase 1")): - return .Str("Phase 2") - case (.Str("finish"), .Str("Phase 2")): - return .Str("end") + case (.str("gogogo"), .str("initial")): + return .str("Phase 1") + case (.str("gogogo"), .str("Phase 1")): + return .str("Phase 2") + case (.str("finish"), .str("Phase 2")): + return .str("end") default: return nil } @@ -143,31 +143,31 @@ let machine = Machine(state: .Str("initial")) { machine in } // initial -XCTAssertEqual(machine.state, StrState.Str("initial")) +XCTAssertEqual(machine.state, StrState.str("initial")) // tryEvent (fails) -machine <-! .Str("go?") -XCTAssertEqual(machine.state, StrState.Str("initial"), "No change.") +machine <-! .str("go?") +XCTAssertEqual(machine.state, StrState.str("initial"), "No change.") // tryEvent -machine <-! .Str("gogogo") -XCTAssertEqual(machine.state, StrState.Str("Phase 1")) +machine <-! .str("gogogo") +XCTAssertEqual(machine.state, StrState.str("Phase 1")) // tryEvent (fails) -machine <-! .Str("finish") -XCTAssertEqual(machine.state, StrState.Str("Phase 1"), "No change.") +machine <-! .str("finish") +XCTAssertEqual(machine.state, StrState.str("Phase 1"), "No change.") // tryEvent -machine <-! .Str("gogogo") -XCTAssertEqual(machine.state, StrState.Str("Phase 2")) +machine <-! .str("gogogo") +XCTAssertEqual(machine.state, StrState.str("Phase 2")) // tryEvent (fails) -machine <-! .Str("gogogo") -XCTAssertEqual(machine.state, StrState.Str("Phase 2"), "No change.") +machine <-! .str("gogogo") +XCTAssertEqual(machine.state, StrState.str("Phase 2"), "No change.") // tryEvent -machine <-! .Str("finish") -XCTAssertEqual(machine.state, StrState.Str("end")) +machine <-! .str("finish") +XCTAssertEqual(machine.state, StrState.str("end")) ``` This behaves very similar to JavaScript's safe state-container [rackt/Redux](https://github.com/rackt/redux), where `RouteMapping` can be interpretted as `Redux.Reducer`. @@ -178,22 +178,22 @@ For more examples, please see XCTest cases. ## Features - Easy Swift syntax - - Transition: `.State0 => .State1`, `[.State0, .State1] => .State2` - - Try state: `machine <- .State1` - - Try state + messaging: `machine <- (.State1, "GoGoGo")` - - Try event: `machine <-! .Event1` + - Transition: `.state0 => .state1`, `[.state0, .state1] => .state2` + - Try state: `machine <- .state1` + - Try state + messaging: `machine <- (.state1, "GoGoGo")` + - Try event: `machine <-! .event1` - Highly flexible transition routing - Using `Condition` - - Using `.Any` state - - Entry handling: `.Any => .SomeState` - - Exit handling: `.SomeState => .Any` - - Blacklisting: `.Any => .Any` + `Condition` - - Using `.Any` event + - Using `.any` state + - Entry handling: `.any => .someState` + - Exit handling: `.someState => .any` + - Blacklisting: `.any => .any` + `Condition` + - Using `.any` event - Route Mapping (closure-based routing): [#36](https://github.com/ReactKit/SwiftState/pull/36) - Success/Error handlers with `order: UInt8` (more flexible than before/after handlers) - Removable routes and handlers using `Disposable` -- Route Chaining: `.State0 => .State1 => .State2` +- Route Chaining: `.state0 => .state1 => .state2` - Hierarchical State Machine: [#10](https://github.com/ReactKit/SwiftState/pull/10) @@ -201,17 +201,17 @@ For more examples, please see XCTest cases. Term | Type | Description ------------- | ----------------------------- | ------------------------------------------ -State | `StateType` (protocol) | Mostly enum, describing each state e.g. `.State0`. +State | `StateType` (protocol) | Mostly enum, describing each state e.g. `.state0`. Event | `EventType` (protocol) | Name for route-group. Transition can be fired via `Event` instead of explicitly targeting next `State`. State Machine | `Machine` | State transition manager which can register `Route`/`RouteMapping` and `Handler` separately for variety of transitions. -Transition | `Transition` | `From-` and `to-` states represented as `.State1 => .State2`. Also, `.Any` can be used to represent _any state_. +Transition | `Transition` | `From-` and `to-` states represented as `.state1 => .state2`. Also, `.any` can be used to represent _any state_. Route | `Route` | `Transition` + `Condition`. Condition | `Context -> Bool` | Closure for validating transition. If condition returns `false`, transition will fail and associated handlers will not be invoked. Route Mapping | `(event: E?, fromState: S, userInfo: Any?) -> S?` | Another way of defining routes **using closure instead of transition arrows (`=>`)**. This is useful when state & event are enum with associated values. Return value (`S?`) means preferred-`toState`, where passing `nil` means no routes available. See [#36](https://github.com/ReactKit/SwiftState/pull/36) for more info. -State Route Mapping | `(fromState: S, userInfo: Any?) -> [S]?` | Another way of defining routes **using closure instead of transition arrows (`=>`)**. This is useful when state is enum with associated values. Return value (`[S]?`) means multiple `toState`s from single `fromState` (synonym for multiple routing e.g. `.State0 => [.State1, .State2]`). See [#36](https://github.com/ReactKit/SwiftState/pull/36) for more info. +State Route Mapping | `(fromState: S, userInfo: Any?) -> [S]?` | Another way of defining routes **using closure instead of transition arrows (`=>`)**. This is useful when state is enum with associated values. Return value (`[S]?`) means multiple `toState`s from single `fromState` (synonym for multiple routing e.g. `.state0 => [.state1, .state2]`). See [#36](https://github.com/ReactKit/SwiftState/pull/36) for more info. Handler | `Context -> Void` | Transition callback invoked when state has been changed successfully. Context | `(event: E?, fromState: S, toState: S, userInfo: Any?)` | Closure argument for `Condition` & `Handler`. -Chain | `TransitionChain` / `RouteChain` | Group of continuous routes represented as `.State1 => .State2 => .State3` +Chain | `TransitionChain` / `RouteChain` | Group of continuous routes represented as `.state1 => .state2 => .state3` ## Related Articles diff --git a/Sources/EventType.swift b/Sources/EventType.swift index 2601960..e696cb6 100644 --- a/Sources/EventType.swift +++ b/Sources/EventType.swift @@ -10,7 +10,7 @@ public protocol EventType: Hashable {} // MARK: Event -/// `EventType` wrapper for handling `.Any` event. +/// `EventType` wrapper for handling `.any` event. public enum Event { case some(E) diff --git a/Sources/Machine.swift b/Sources/Machine.swift index aa9a48f..269f3e5 100644 --- a/Sources/Machine.swift +++ b/Sources/Machine.swift @@ -73,7 +73,7 @@ public class Machine guard let fromState = transition.fromState.rawValue, let toState = transition.toState.rawValue else { - assertionFailure("State = `.Any` is not supported for `hasRoute()` (always returns `false`)") + assertionFailure("State = `.any` is not supported for `hasRoute()` (always returns `false`)") return false } @@ -164,7 +164,7 @@ public class Machine for (transition, keyConditionDict) in routeDict { if transition.fromState == .some(self.state) || transition.fromState == .any { for (_, condition) in keyConditionDict { - // if toState is `.Any`, always treat as identity transition + // if toState is `.any`, always treat as identity transition let toState = transition.toState.rawValue ?? self.state if _canPassCondition(condition, forEvent: event, fromState: self.state, toState: toState, userInfo: userInfo) { diff --git a/Sources/Route.swift b/Sources/Route.swift index ea5c854..3d8b3fe 100644 --- a/Sources/Route.swift +++ b/Sources/Route.swift @@ -23,10 +23,10 @@ public struct Route // MARK: - Custom Operators //-------------------------------------------------- -/// e.g. [.State0, .State1] => .State, allowing [0 => 2, 1 => 2] +/// e.g. [.state0, .state1] => .state, allowing [0 => 2, 1 => 2] public func => (leftStates: [S], right: State) -> Route { - // NOTE: don't reuse ".Any => .Any + condition" for efficiency + // NOTE: don't reuse ".any => .any + condition" for efficiency return Route(transition: .any => right, condition: { context -> Bool in return leftStates.contains(context.fromState) }) @@ -37,7 +37,7 @@ public func => (leftStates: [S], right: S) -> Route< return leftStates => .some(right) } -/// e.g. .State0 => [.State1, .State], allowing [0 => 1, 0 => 2] +/// e.g. .state0 => [.state1, .state], allowing [0 => 1, 0 => 2] public func => (left: State, rightStates: [S]) -> Route { return Route(transition: left => .any, condition: { context -> Bool in @@ -50,7 +50,7 @@ public func => (left: S, rightStates: [S]) -> Route< return .some(left) => rightStates } -/// e.g. [.State0, .State1] => [.State, .State3], allowing [0 => 2, 0 => 3, 1 => 2, 1 => 3] +/// e.g. [.state0, .state1] => [.state, .state3], allowing [0 => 2, 0 => 3, 1 => 2, 1 => 3] public func => (leftStates: [S], rightStates: [S]) -> Route { return Route(transition: .any => .any, condition: { context -> Bool in diff --git a/Sources/StateMachine.swift b/Sources/StateMachine.swift index 88822aa..2ee39a5 100644 --- a/Sources/StateMachine.swift +++ b/Sources/StateMachine.swift @@ -15,7 +15,7 @@ public final class StateMachine: Machine { /// Closure-based routes for `tryState()`. - /// - Returns: Multiple `toState`s from single `fromState`, similar to `.State0 => [.State1, .State2]` + /// - Returns: Multiple `toState`s from single `fromState`, similar to `.state0 => [.state1, .state2]` public typealias StateRouteMapping = (_ fromState: S, _ userInfo: Any?) -> [S]? private lazy var _routes: _RouteDict = [:] @@ -52,7 +52,7 @@ public final class StateMachine: Machine guard let fromState = transition.fromState.rawValue, let toState = transition.toState.rawValue else { - assertionFailure("State = `.Any` is not supported for `hasRoute()` (always returns `false`)") + assertionFailure("State = `.any` is not supported for `hasRoute()` (always returns `false`)") return false } diff --git a/Sources/StateType.swift b/Sources/StateType.swift index 8a24252..fc801a8 100644 --- a/Sources/StateType.swift +++ b/Sources/StateType.swift @@ -10,7 +10,7 @@ public protocol StateType: Hashable {} // MARK: State -/// `StateType` wrapper for handling `.Any` state. +/// `StateType` wrapper for handling `.any` state. public enum State { case some(S) diff --git a/Sources/Transition.swift b/Sources/Transition.swift index f8a8ec4..8827b81 100644 --- a/Sources/Transition.swift +++ b/Sources/Transition.swift @@ -7,8 +7,8 @@ // /// -/// "From-" and "to-" states represented as `.State1 => .State2`. -/// Also, `.Any` can be used to represent _any state_. +/// "From-" and "to-" states represented as `.state1 => .state2`. +/// Also, `.any` can be used to represent _any state_. /// public struct Transition: Hashable { @@ -54,7 +54,7 @@ public func == (left: Transition, right: Transition) -> Bool infix operator => : AdditionPrecedence -/// e.g. .State0 => .State1 +/// e.g. .state0 => .state1 public func => (left: State, right: State) -> Transition { return Transition(fromState: left, toState: right) diff --git a/Sources/TransitionChain.swift b/Sources/TransitionChain.swift index 31b957b..76c77b6 100644 --- a/Sources/TransitionChain.swift +++ b/Sources/TransitionChain.swift @@ -6,7 +6,7 @@ // Copyright (c) 2014年 Yasuhiro Inami. All rights reserved. // -/// Group of continuous `Transition`s represented as `.State1 => .State2 => .State3`. +/// Group of continuous `Transition`s represented as `.state1 => .state2 => .state3`. public struct TransitionChain { public private(set) var states: [State] @@ -37,7 +37,7 @@ public struct TransitionChain // MARK: - Custom Operators //-------------------------------------------------- -// e.g. (.State0 => .State1) => .State +// e.g. (.state0 => .state1) => .state public func => (left: Transition, right: State) -> TransitionChain { return TransitionChain(states: [left.fromState, left.toState]) => right @@ -58,7 +58,7 @@ public func => (left: TransitionChain, right: S) -> TransitionC return left => .some(right) } -// e.g. .State0 => (.State1 => .State) +// e.g. .state0 => (.state1 => .state) public func => (left: State, right: Transition) -> TransitionChain { return left => TransitionChain(states: [right.fromState, right.toState]) diff --git a/Tests/BasicTests.swift b/Tests/BasicTests.swift index ce4d41c..0aab84a 100644 --- a/Tests/BasicTests.swift +++ b/Tests/BasicTests.swift @@ -61,7 +61,7 @@ class BasicTests: _TestCase // add event handler machine.addHandler(event: .event0) { context in - print(".Event0 triggered!") + print(".event0 triggered!") } } diff --git a/Tests/HierarchicalMachineTests.swift b/Tests/HierarchicalMachineTests.swift index de76c17..aa50914 100644 --- a/Tests/HierarchicalMachineTests.swift +++ b/Tests/HierarchicalMachineTests.swift @@ -55,14 +55,14 @@ class HierarchicalMachineTests: _TestCase /// - mainMachine /// - MainState0 (initial) /// - subMachine1 - /// - SubState0 - /// - SubState1 + /// - subState0 + /// - subState1 /// - subMachine2 - /// - SubState0 - /// - SubState1 + /// - subState0 + /// - subState1 /// /// - Warning: - /// This is a naive implementation and easily lose consistency when `subMachine.state` is changed directly, e.g. `subMachine1 <- .SubState1`. + /// This is a naive implementation and easily lose consistency when `subMachine.state` is changed directly, e.g. `subMachine1 <- .subState1`. /// private var mainMachine: StateMachine<_MainState, NoEvent>? diff --git a/Tests/RouteChainTests.swift b/Tests/RouteChainTests.swift index ee0d592..edd5546 100644 --- a/Tests/RouteChainTests.swift +++ b/Tests/RouteChainTests.swift @@ -124,7 +124,7 @@ class MachineChainTests: _TestCase invokeCount += 1 return } - // machine.addRoute(.State1 => .State3) // comment-out: 1 => 3 is not possible + // machine.addRoute(.state1 => .state3) // comment-out: 1 => 3 is not possible } diff --git a/Tests/RouteMappingTests.swift b/Tests/RouteMappingTests.swift index 117be14..ddf765f 100644 --- a/Tests/RouteMappingTests.swift +++ b/Tests/RouteMappingTests.swift @@ -89,15 +89,15 @@ class RouteMappingTests: _TestCase switch event { case .cancelAction: - // can transit to `.Pending` if current state is not the same + // 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 + // 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. + // increment `count` when any events i.e. `.cancelAction` and `.loadAction(x)` succeed. machine.addHandler(event: .any) { event, transition, order, userInfo in count += 1 } @@ -108,12 +108,12 @@ class RouteMappingTests: _TestCase XCTAssertEqual(machine.state, _State.pending) XCTAssertEqual(count, 0) - // CancelAction (to .Pending state, same as before) + // CancelAction (to .pending state, same as before) machine <-! .cancelAction XCTAssertEqual(machine.state, _State.pending) XCTAssertEqual(count, 0, "`tryEvent()` failed, and `count` should not be incremented.") - // LoadAction(1) (to .Loading(1) state) + // LoadAction(1) (to .loading(1) state) machine <-! .loadAction(1) XCTAssertEqual(machine.state, _State.loading(1)) XCTAssertEqual(count, 1) @@ -140,9 +140,9 @@ class RouteMappingTests: _TestCase let machine = StateMachine<_State, _Event>(state: .pending) { machine in // Add following routes: - // - `.Pending => .Loading(1)` - // - `.Loading(x) => .Loading(x+10)` - // - `.Loading(x) => .Loading(x+100)` + // - `.pending => .loading(1)` + // - `.loading(x) => .loading(x+10)` + // - `.loading(x) => .loading(x+100)` machine.addStateRouteMapping { fromState, userInfo in switch fromState { case .pending: @@ -152,7 +152,7 @@ class RouteMappingTests: _TestCase } } - // increment `count` when any events i.e. `.CancelAction` and `.LoadAction(x)` succeed. + // increment `count` when any events i.e. `.cancelAction` and `.loadAction(x)` succeed. machine.addHandler(.any => .any) { event, transition, order, userInfo in count += 1 } @@ -163,17 +163,17 @@ class RouteMappingTests: _TestCase XCTAssertEqual(machine.state, _State.pending) XCTAssertEqual(count, 0) - // .Loading(999) (fails) + // .loading(999) (fails) machine <- .loading(999) XCTAssertEqual(machine.state, _State.pending) XCTAssertEqual(count, 0, "`tryState()` failed, and `count` should not be incremented.") - // .Loading(1) + // .loading(1) machine <- .loading(1) XCTAssertEqual(machine.state, _State.loading(1)) XCTAssertEqual(count, 1) - // .Loading(999) (fails) + // .loading(999) (fails) machine <- .loading(999) XCTAssertEqual(machine.state, _State.loading(1)) XCTAssertEqual(count, 1, "`tryState()` failed, and `count` should not be incremented.") diff --git a/Tests/StateMachineTests.swift b/Tests/StateMachineTests.swift index fcd3006..83b6595 100644 --- a/Tests/StateMachineTests.swift +++ b/Tests/StateMachineTests.swift @@ -76,7 +76,7 @@ class StateMachineTests: _TestCase XCTAssertFalse(machine.hasRoute(.state1 => .state2)) } - // add .Any => state + // add .any => state func testAddRoute_fromAnyState() { let machine = StateMachine(state: .state0) { machine in @@ -91,7 +91,7 @@ class StateMachineTests: _TestCase XCTAssertFalse(machine.hasRoute(.state1 => .state2)) } - // add state => .Any + // add state => .any func testAddRoute_toAnyState() { let machine = StateMachine(state: .state0) { machine in @@ -106,7 +106,7 @@ class StateMachineTests: _TestCase XCTAssertTrue(machine.hasRoute(.state1 => .state2)) // true } - // add .Any => .Any + // add .any => .any func testAddRoute_bothAnyState() { let machine = StateMachine(state: .state0) { machine in diff --git a/Tests/TransitionTests.swift b/Tests/TransitionTests.swift index 0f1a090..e4e05af 100644 --- a/Tests/TransitionTests.swift +++ b/Tests/TransitionTests.swift @@ -49,17 +49,17 @@ class TransitionTests: _TestCase func testNil() { - // .Any => state + // .any => state let transition = .any => MyState.state0 XCTAssertTrue(transition.fromState == .any) XCTAssertTrue(transition.toState == .state0) - // state => .Any + // state => .any let transition2 = MyState.state0 => .any XCTAssertTrue(transition2.fromState == .state0) XCTAssertTrue(transition2.toState == .any) - // .Any => .Any + // .any => .any let transition3: Transition = .any => .any XCTAssertTrue(transition3.fromState == .any) XCTAssertTrue(transition3.toState == .any) From 2736a7bd626fbe0795eb1ac6e05da23b7f2c6374 Mon Sep 17 00:00:00 2001 From: Matt Prowse Date: Mon, 5 Sep 2016 09:55:04 +1000 Subject: [PATCH 04/27] Update documentation to reflect Xcode 8 beta 6 changes to RouteMapping and StateRouteMapping. --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 29fbad5..6828e24 100644 --- a/README.md +++ b/README.md @@ -108,9 +108,9 @@ If there is no `Event`-based transition, use built-in `NoEvent` instead. Above examples use _arrow-style routing_ which are easy to understand, but it lacks in ability to handle **state & event enums with associated values**. In such cases, use either of the following functions to apply _closure-style routing_: - `machine.addRouteMapping(routeMapping)` - - `RouteMapping`: `(event: E?, fromState: S, userInfo: Any?) -> S?` + - `RouteMapping`: `(_ event: E?, _ fromState: S, _ userInfo: Any?) -> S?` - `machine.addStateRouteMapping(stateRouteMapping)` - - `StateRouteMapping`: `(fromState: S, userInfo: Any?) -> [S]?` + - `StateRouteMapping`: `(_ fromState: S, _ userInfo: Any?) -> [S]?` For example: From 08368b5d25ccd4e9e06ae11b38d114dc992d6f15 Mon Sep 17 00:00:00 2001 From: Alexander Belyavskiy Date: Fri, 9 Sep 2016 08:39:19 +0300 Subject: [PATCH 05/27] Adds @escaping where required for Swift 3.0 GM --- Sources/Disposable.swift | 2 +- Sources/Machine.swift | 20 ++++++++++---------- Sources/StateMachine.swift | 26 +++++++++++++------------- Sources/_HandlerInfo.swift | 2 +- 4 files changed, 25 insertions(+), 25 deletions(-) diff --git a/Sources/Disposable.swift b/Sources/Disposable.swift index 24c9b70..3689560 100644 --- a/Sources/Disposable.swift +++ b/Sources/Disposable.swift @@ -34,7 +34,7 @@ public final class ActionDisposable: Disposable { } /// Initializes the disposable to run the given action upon disposal. - public init(action: (() -> ())) { + public init(action: @escaping (() -> ())) { self.action = action } diff --git a/Sources/Machine.swift b/Sources/Machine.swift index 269f3e5..8054ac1 100644 --- a/Sources/Machine.swift +++ b/Sources/Machine.swift @@ -293,26 +293,26 @@ public class Machine // MARK: addRoutes(event:) + conditional handler @discardableResult - public func addRoutes(event: E, transitions: [Transition], condition: Condition? = nil, handler: Handler) -> Disposable + public func addRoutes(event: E, transitions: [Transition], condition: Condition? = nil, handler: @escaping Handler) -> Disposable { return self.addRoutes(event: .some(event), transitions: transitions, condition: condition, handler: handler) } @discardableResult - public func addRoutes(event: Event, transitions: [Transition], condition: Condition? = nil, handler: Handler) -> Disposable + public func addRoutes(event: Event, transitions: [Transition], condition: Condition? = nil, handler: @escaping Handler) -> Disposable { let routes = transitions.map { Route(transition: $0, condition: condition) } return self.addRoutes(event: event, routes: routes, handler: handler) } @discardableResult - public func addRoutes(event: E, routes: [Route], handler: Handler) -> Disposable + public func addRoutes(event: E, routes: [Route], handler: @escaping Handler) -> Disposable { return self.addRoutes(event: .some(event), routes: routes, handler: handler) } @discardableResult - public func addRoutes(event: Event, routes: [Route], handler: Handler) -> Disposable + public func addRoutes(event: Event, routes: [Route], handler: @escaping Handler) -> Disposable { let routeDisposable = self.addRoutes(event: event, routes: routes) let handlerDisposable = self.addHandler(event: event, handler: handler) @@ -367,7 +367,7 @@ public class Machine // MARK: addRouteMapping @discardableResult - public func addRouteMapping(_ routeMapping: RouteMapping) -> Disposable + public func addRouteMapping(_ routeMapping: @escaping RouteMapping) -> Disposable { let key = _createUniqueString() @@ -383,7 +383,7 @@ public class Machine // MARK: addRouteMapping + conditional handler @discardableResult - public func addRouteMapping(_ routeMapping: RouteMapping, order: HandlerOrder = _defaultOrder, handler: Machine.Handler) -> Disposable + public func addRouteMapping(_ routeMapping: @escaping RouteMapping, order: HandlerOrder = _defaultOrder, handler: @escaping Machine.Handler) -> Disposable { let routeDisposable = self.addRouteMapping(routeMapping) @@ -425,13 +425,13 @@ public class Machine // MARK: addHandler(event:) @discardableResult - public func addHandler(event: E, order: HandlerOrder = _defaultOrder, handler: Machine.Handler) -> Disposable + public func addHandler(event: E, order: HandlerOrder = _defaultOrder, handler: @escaping Machine.Handler) -> Disposable { return self.addHandler(event: .some(event), order: order, handler: handler) } @discardableResult - public func addHandler(event: Event, order: HandlerOrder = _defaultOrder, handler: Machine.Handler) -> Disposable + public func addHandler(event: Event, order: HandlerOrder = _defaultOrder, handler: @escaping Machine.Handler) -> Disposable { return self._addHandler(event: event, order: order) { context in // skip if not event-based transition @@ -446,7 +446,7 @@ public class Machine } @discardableResult - private func _addHandler(event: Event, order: HandlerOrder = _defaultOrder, handler: Handler) -> Disposable + private func _addHandler(event: Event, order: HandlerOrder = _defaultOrder, handler: @escaping Handler) -> Disposable { if self._handlers[event] == nil { self._handlers[event] = [] @@ -470,7 +470,7 @@ public class Machine // MARK: addErrorHandler @discardableResult - public func addErrorHandler(order: HandlerOrder = _defaultOrder, handler: Handler) -> Disposable + public func addErrorHandler(order: HandlerOrder = _defaultOrder, handler: @escaping Handler) -> Disposable { let key = _createUniqueString() diff --git a/Sources/StateMachine.swift b/Sources/StateMachine.swift index 2ee39a5..654d9cd 100644 --- a/Sources/StateMachine.swift +++ b/Sources/StateMachine.swift @@ -215,14 +215,14 @@ public final class StateMachine: Machine // MARK: addRoute (no-event) + conditional handler @discardableResult - public func addRoute(_ transition: Transition, condition: Condition? = nil, handler: Handler) -> Disposable + public func addRoute(_ transition: Transition, condition: Condition? = nil, handler: @escaping Handler) -> Disposable { let route = Route(transition: transition, condition: condition) return self.addRoute(route, handler: handler) } @discardableResult - public func addRoute(_ route: Route, handler: Handler) -> Disposable + public func addRoute(_ route: Route, handler: @escaping Handler) -> Disposable { let transition = route.transition let condition = route.condition @@ -278,7 +278,7 @@ public final class StateMachine: Machine /// Add `handler` that is called when `tryState()` succeeds for target `transition`. /// - Note: `handler` will not be invoked for `tryEvent()`. @discardableResult - public func addHandler(_ transition: Transition, order: HandlerOrder = _defaultOrder, handler: Handler) -> Disposable + public func addHandler(_ transition: Transition, order: HandlerOrder = _defaultOrder, handler: @escaping Handler) -> Disposable { if self._handlers[transition] == nil { self._handlers[transition] = [] @@ -322,7 +322,7 @@ public final class StateMachine: Machine /// Add `handler` that is called when either `tryEvent()` or `tryState()` succeeds for target `transition`. @discardableResult - public func addAnyHandler(_ transition: Transition, order: HandlerOrder = _defaultOrder, handler: Handler) -> Disposable + public func addAnyHandler(_ transition: Transition, order: HandlerOrder = _defaultOrder, handler: @escaping Handler) -> Disposable { let disposable1 = self.addHandler(transition, order: order, handler: handler) let disposable2 = self.addHandler(event: .any, order: order) { context in @@ -351,14 +351,14 @@ public final class StateMachine: Machine // MARK: addRouteChain + conditional handler @discardableResult - public func addRouteChain(_ chain: TransitionChain, condition: Condition? = nil, handler: Handler) -> Disposable + public func addRouteChain(_ chain: TransitionChain, condition: Condition? = nil, handler: @escaping Handler) -> Disposable { let routeChain = RouteChain(transitionChain: chain, condition: condition) return self.addRouteChain(routeChain, handler: handler) } @discardableResult - public func addRouteChain(_ chain: RouteChain, handler: Handler) -> Disposable + public func addRouteChain(_ chain: RouteChain, handler: @escaping Handler) -> Disposable { let routeDisposables = chain.routes.map { self.addRoute($0) } let handlerDisposable = self.addChainHandler(chain, handler: handler) @@ -372,13 +372,13 @@ public final class StateMachine: Machine // MARK: addChainHandler @discardableResult - public func addChainHandler(_ chain: TransitionChain, order: HandlerOrder = _defaultOrder, handler: Handler) -> Disposable + public func addChainHandler(_ chain: TransitionChain, order: HandlerOrder = _defaultOrder, handler: @escaping Handler) -> Disposable { return self.addChainHandler(RouteChain(transitionChain: chain), order: order, handler: handler) } @discardableResult - public func addChainHandler(_ chain: RouteChain, order: HandlerOrder = _defaultOrder, handler: Handler) -> Disposable + public func addChainHandler(_ chain: RouteChain, order: HandlerOrder = _defaultOrder, handler: @escaping Handler) -> Disposable { return self._addChainHandler(chain, order: order, handler: handler, isError: false) } @@ -386,19 +386,19 @@ public final class StateMachine: Machine // MARK: addChainErrorHandler @discardableResult - public func addChainErrorHandler(_ chain: TransitionChain, order: HandlerOrder = _defaultOrder, handler: Handler) -> Disposable + public func addChainErrorHandler(_ chain: TransitionChain, order: HandlerOrder = _defaultOrder, handler: @escaping Handler) -> Disposable { return self.addChainErrorHandler(RouteChain(transitionChain: chain), order: order, handler: handler) } @discardableResult - public func addChainErrorHandler(_ chain: RouteChain, order: HandlerOrder = _defaultOrder, handler: Handler) -> Disposable + public func addChainErrorHandler(_ chain: RouteChain, order: HandlerOrder = _defaultOrder, handler: @escaping Handler) -> Disposable { return self._addChainHandler(chain, order: order, handler: handler, isError: true) } @discardableResult - private func _addChainHandler(_ chain: RouteChain, order: HandlerOrder = _defaultOrder, handler: Handler, isError: Bool) -> Disposable + private func _addChainHandler(_ chain: RouteChain, order: HandlerOrder = _defaultOrder, handler: @escaping Handler, isError: Bool) -> Disposable { var handlerDisposables: [Disposable] = [] @@ -483,7 +483,7 @@ public final class StateMachine: Machine // MARK: addStateRouteMapping @discardableResult - public func addStateRouteMapping(_ routeMapping: StateRouteMapping) -> Disposable + public func addStateRouteMapping(_ routeMapping: @escaping StateRouteMapping) -> Disposable { let key = _createUniqueString() @@ -499,7 +499,7 @@ public final class StateMachine: Machine // MARK: addStateRouteMapping + conditional handler @discardableResult - public func addStateRouteMapping(_ routeMapping: StateRouteMapping, handler: Handler) -> Disposable + public func addStateRouteMapping(_ routeMapping: @escaping StateRouteMapping, handler: @escaping Handler) -> Disposable { let routeDisposable = self.addStateRouteMapping(routeMapping) diff --git a/Sources/_HandlerInfo.swift b/Sources/_HandlerInfo.swift index 706b867..530a02b 100644 --- a/Sources/_HandlerInfo.swift +++ b/Sources/_HandlerInfo.swift @@ -12,7 +12,7 @@ internal final class _HandlerInfo internal let key: String internal let handler: Machine.Handler - internal init(order: HandlerOrder, key: String, handler: Machine.Handler) + internal init(order: HandlerOrder, key: String, handler: @escaping Machine.Handler) { self.order = order self.key = key From 468d23e17fe8fa46eeede0edf940fe187b494301 Mon Sep 17 00:00:00 2001 From: George Malayil Philip Date: Thu, 16 Feb 2017 20:04:54 +0530 Subject: [PATCH 06/27] Switched to arc4random_uniform() for random number generation on watchOS and tvOS as well. --- Sources/_Random.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/_Random.swift b/Sources/_Random.swift index 1d159f7..cf79e73 100644 --- a/Sources/_Random.swift +++ b/Sources/_Random.swift @@ -14,7 +14,7 @@ import Glibc internal func _random(_ upperBound: Int) -> Int { - #if os(OSX) || os(iOS) + #if os(OSX) || os(iOS) || os(watchOS) || os(tvOS) return Int(arc4random_uniform(UInt32(upperBound))) #else return Int(random() % upperBound) From 7a5da5b9b5da8a5ef51bba7f755b89323250484e Mon Sep 17 00:00:00 2001 From: Adam Yanalunas Date: Wed, 27 Sep 2017 15:09:24 -0700 Subject: [PATCH 07/27] Update project to Xcode 9 suggested settings, fix warnings --- Sources/EventType.swift | 6 +++--- Sources/Machine.swift | 12 ++++++------ Sources/Route.swift | 10 +++++----- Sources/StateMachine.swift | 4 ++-- Sources/StateType.swift | 6 +++--- Sources/Transition.swift | 10 +++++----- Sources/TransitionChain.swift | 16 ++++++++-------- SwiftState.xcodeproj/project.pbxproj | 14 +++++++++++++- .../xcshareddata/xcschemes/SwiftState.xcscheme | 4 +++- Tests/BasicTests.swift | 4 ++-- Tests/MiscTests.swift | 16 ++++++++-------- 11 files changed, 58 insertions(+), 44 deletions(-) diff --git a/Sources/EventType.swift b/Sources/EventType.swift index e696cb6..7266065 100644 --- a/Sources/EventType.swift +++ b/Sources/EventType.swift @@ -49,7 +49,7 @@ extension Event: RawRepresentable } } -public func == (lhs: Event, rhs: Event) -> Bool +public func == (lhs: Event, rhs: Event) -> Bool { switch (lhs, rhs) { case let (.some(x1), .some(x2)) where x1 == x2: @@ -61,7 +61,7 @@ public func == (lhs: Event, rhs: Event) -> Bool } } -public func == (lhs: Event, rhs: E) -> Bool +public func == (lhs: Event, rhs: E) -> Bool { switch lhs { case .some(let x): @@ -71,7 +71,7 @@ public func == (lhs: Event, rhs: E) -> Bool } } -public func == (lhs: E, rhs: Event) -> Bool +public func == (lhs: E, rhs: Event) -> Bool { switch rhs { case .some(let x): diff --git a/Sources/Machine.swift b/Sources/Machine.swift index 8054ac1..8d18824 100644 --- a/Sources/Machine.swift +++ b/Sources/Machine.swift @@ -521,14 +521,14 @@ public class Machine infix operator <-! : AdditionPrecedence @discardableResult -public func <-! (machine: Machine, event: E) -> Machine +public func <-! (machine: Machine, event: E) -> Machine { machine.tryEvent(event) return machine } @discardableResult -public func <-! (machine: Machine, tuple: (E, Any?)) -> Machine +public func <-! (machine: Machine, tuple: (E, Any?)) -> Machine { machine.tryEvent(tuple.0, userInfo: tuple.1) return machine @@ -557,7 +557,7 @@ internal func _createUniqueString() -> String return uniqueString } -internal func _validTransitions(fromState: S, toState: S) -> [Transition] +internal func _validTransitions(fromState: S, toState: S) -> [Transition] { return [ fromState => toState, @@ -567,12 +567,12 @@ internal func _validTransitions(fromState: S, toState: S) -> [Tran ] } -internal func _canPassCondition(_ condition: Machine.Condition?, forEvent event: E?, fromState: S, toState: S, userInfo: Any?) -> Bool +internal func _canPassCondition(_ condition: Machine.Condition?, forEvent event: E?, fromState: S, toState: S, userInfo: Any?) -> Bool { return condition?((event, fromState, toState, userInfo)) ?? true } -internal func _insertHandlerIntoArray(_ handlerInfos: inout [_HandlerInfo], newHandlerInfo: _HandlerInfo) +internal func _insertHandlerIntoArray(_ handlerInfos: inout [_HandlerInfo], newHandlerInfo: _HandlerInfo) { var index = handlerInfos.count @@ -586,7 +586,7 @@ internal func _insertHandlerIntoArray(_ handlerInfos handlerInfos.insert(newHandlerInfo, at: index) } -internal func _removeHandlerFromArray(_ handlerInfos: inout [_HandlerInfo], removingHandlerID: _HandlerID) -> Bool +internal func _removeHandlerFromArray(_ handlerInfos: inout [_HandlerInfo], removingHandlerID: _HandlerID) -> Bool { for i in 0.. //-------------------------------------------------- /// e.g. [.state0, .state1] => .state, allowing [0 => 2, 1 => 2] -public func => (leftStates: [S], right: State) -> Route +public func => (leftStates: [S], right: State) -> Route { // NOTE: don't reuse ".any => .any + condition" for efficiency return Route(transition: .any => right, condition: { context -> Bool in @@ -32,26 +32,26 @@ public func => (leftStates: [S], right: State) -> }) } -public func => (leftStates: [S], right: S) -> Route +public func => (leftStates: [S], right: S) -> Route { return leftStates => .some(right) } /// e.g. .state0 => [.state1, .state], allowing [0 => 1, 0 => 2] -public func => (left: State, rightStates: [S]) -> Route +public func => (left: State, rightStates: [S]) -> Route { return Route(transition: left => .any, condition: { context -> Bool in return rightStates.contains(context.toState) }) } -public func => (left: S, rightStates: [S]) -> Route +public func => (left: S, rightStates: [S]) -> Route { return .some(left) => rightStates } /// e.g. [.state0, .state1] => [.state, .state3], allowing [0 => 2, 0 => 3, 1 => 2, 1 => 3] -public func => (leftStates: [S], rightStates: [S]) -> Route +public func => (leftStates: [S], rightStates: [S]) -> Route { return Route(transition: .any => .any, condition: { context -> Bool in return leftStates.contains(context.fromState) && rightStates.contains(context.toState) diff --git a/Sources/StateMachine.swift b/Sources/StateMachine.swift index 654d9cd..ae8ff6e 100644 --- a/Sources/StateMachine.swift +++ b/Sources/StateMachine.swift @@ -546,14 +546,14 @@ public final class StateMachine: Machine infix operator <- : AdditionPrecedence @discardableResult -public func <- (machine: StateMachine, state: S) -> StateMachine +public func <- (machine: StateMachine, state: S) -> StateMachine { machine.tryState(state) return machine } @discardableResult -public func <- (machine: StateMachine, tuple: (S, Any?)) -> StateMachine +public func <- (machine: StateMachine, tuple: (S, Any?)) -> StateMachine { machine.tryState(tuple.0, userInfo: tuple.1) return machine diff --git a/Sources/StateType.swift b/Sources/StateType.swift index fc801a8..bf5e278 100644 --- a/Sources/StateType.swift +++ b/Sources/StateType.swift @@ -49,7 +49,7 @@ extension State: RawRepresentable } } -public func == (lhs: State, rhs: State) -> Bool +public func == (lhs: State, rhs: State) -> Bool { switch (lhs, rhs) { case let (.some(x1), .some(x2)) where x1 == x2: @@ -61,7 +61,7 @@ public func == (lhs: State, rhs: State) -> Bool } } -public func == (lhs: State, rhs: S) -> Bool +public func == (lhs: State, rhs: S) -> Bool { switch lhs { case .some(let x): @@ -71,7 +71,7 @@ public func == (lhs: State, rhs: S) -> Bool } } -public func == (lhs: S, rhs: State) -> Bool +public func == (lhs: S, rhs: State) -> Bool { switch rhs { case .some(let x): diff --git a/Sources/Transition.swift b/Sources/Transition.swift index 8827b81..0cba79d 100644 --- a/Sources/Transition.swift +++ b/Sources/Transition.swift @@ -43,7 +43,7 @@ public struct Transition: Hashable } // for Transition Equatable -public func == (left: Transition, right: Transition) -> Bool +public func == (left: Transition, right: Transition) -> Bool { return left.fromState == right.fromState && left.toState == right.toState } @@ -55,22 +55,22 @@ public func == (left: Transition, right: Transition) -> Bool infix operator => : AdditionPrecedence /// e.g. .state0 => .state1 -public func => (left: State, right: State) -> Transition +public func => (left: State, right: State) -> Transition { return Transition(fromState: left, toState: right) } -public func => (left: State, right: S) -> Transition +public func => (left: State, right: S) -> Transition { return left => .some(right) } -public func => (left: S, right: State) -> Transition +public func => (left: S, right: State) -> Transition { return .some(left) => right } -public func => (left: S, right: S) -> Transition +public func => (left: S, right: S) -> Transition { return .some(left) => .some(right) } diff --git a/Sources/TransitionChain.swift b/Sources/TransitionChain.swift index 76c77b6..c0fa93e 100644 --- a/Sources/TransitionChain.swift +++ b/Sources/TransitionChain.swift @@ -38,43 +38,43 @@ public struct TransitionChain //-------------------------------------------------- // e.g. (.state0 => .state1) => .state -public func => (left: Transition, right: State) -> TransitionChain +public func => (left: Transition, right: State) -> TransitionChain { return TransitionChain(states: [left.fromState, left.toState]) => right } -public func => (left: Transition, right: S) -> TransitionChain +public func => (left: Transition, right: S) -> TransitionChain { return left => .some(right) } -public func => (left: TransitionChain, right: State) -> TransitionChain +public func => (left: TransitionChain, right: State) -> TransitionChain { return TransitionChain(states: left.states + [right]) } -public func => (left: TransitionChain, right: S) -> TransitionChain +public func => (left: TransitionChain, right: S) -> TransitionChain { return left => .some(right) } // e.g. .state0 => (.state1 => .state) -public func => (left: State, right: Transition) -> TransitionChain +public func => (left: State, right: Transition) -> TransitionChain { return left => TransitionChain(states: [right.fromState, right.toState]) } -public func => (left: S, right: Transition) -> TransitionChain +public func => (left: S, right: Transition) -> TransitionChain { return .some(left) => right } -public func => (left: State, right: TransitionChain) -> TransitionChain +public func => (left: State, right: TransitionChain) -> TransitionChain { return TransitionChain(states: [left] + right.states) } -public func => (left: S, right: TransitionChain) -> TransitionChain +public func => (left: S, right: TransitionChain) -> TransitionChain { return .some(left) => right } diff --git a/SwiftState.xcodeproj/project.pbxproj b/SwiftState.xcodeproj/project.pbxproj index 3552690..bcd8b06 100644 --- a/SwiftState.xcodeproj/project.pbxproj +++ b/SwiftState.xcodeproj/project.pbxproj @@ -312,7 +312,7 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 0700; - LastUpgradeCheck = 0800; + LastUpgradeCheck = 0900; ORGANIZATIONNAME = "Yasuhiro Inami"; TargetAttributes = { 1FA61FFF1996601000460108 = { @@ -427,14 +427,20 @@ CLANG_CXX_LIBRARY = "libc++"; CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; @@ -475,14 +481,20 @@ CLANG_CXX_LIBRARY = "libc++"; CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; diff --git a/SwiftState.xcodeproj/xcshareddata/xcschemes/SwiftState.xcscheme b/SwiftState.xcodeproj/xcshareddata/xcschemes/SwiftState.xcscheme index 9e845a5..156778f 100644 --- a/SwiftState.xcodeproj/xcshareddata/xcschemes/SwiftState.xcscheme +++ b/SwiftState.xcodeproj/xcshareddata/xcschemes/SwiftState.xcscheme @@ -1,6 +1,6 @@ @@ -56,6 +57,7 @@ buildConfiguration = "Debug" selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" + language = "" launchStyle = "0" useCustomWorkingDirectory = "NO" ignoresPersistentStateOnLaunch = "NO" diff --git a/Tests/BasicTests.swift b/Tests/BasicTests.swift index 0aab84a..8f97c83 100644 --- a/Tests/BasicTests.swift +++ b/Tests/BasicTests.swift @@ -17,8 +17,8 @@ class BasicTests: _TestCase let machine = StateMachine(state: .state0) { machine in machine.addRoute(.state0 => .state1) - machine.addRoute(.any => .state2) { context in print("Any => 2, msg=\(context.userInfo)") } - machine.addRoute(.state2 => .any) { context in print("2 => Any, msg=\(context.userInfo)") } + machine.addRoute(.any => .state2) { context in print("Any => 2, msg=\(String(describing: context.userInfo))") } + machine.addRoute(.state2 => .any) { context in print("2 => Any, msg=\(String(describing: context.userInfo))") } // add handler (`context = (event, fromState, toState, userInfo)`) machine.addHandler(.state0 => .state1) { context in diff --git a/Tests/MiscTests.swift b/Tests/MiscTests.swift index 71ac2d5..7909237 100644 --- a/Tests/MiscTests.swift +++ b/Tests/MiscTests.swift @@ -17,8 +17,8 @@ class MiscTests: _TestCase let machine = StateMachine(state: ".State0") { machine in machine.addRoute(".State0" => ".State1") - machine.addRoute(.any => ".State2") { context in print("Any => 2, msg=\(context.userInfo)") } - machine.addRoute(".State2" => .any) { context in print("2 => Any, msg=\(context.userInfo)") } + machine.addRoute(.any => ".State2") { context in print("Any => 2, msg=\(String(describing: context.userInfo))") } + machine.addRoute(".State2" => .any) { context in print("2 => Any, msg=\(String(describing: context.userInfo))") } // add handler (handlerContext = (event, transition, order, userInfo)) machine.addHandler(".State0" => ".State1") { context in @@ -54,8 +54,8 @@ class MiscTests: _TestCase let machine = StateMachine(state: .str("0")) { machine in machine.addRoute(.str("0") => .str("1")) - machine.addRoute(.any => .str("2")) { context in print("Any => 2, msg=\(context.userInfo)") } - machine.addRoute(.str("2") => .any) { context in print("2 => Any, msg=\(context.userInfo)") } + machine.addRoute(.any => .str("2")) { context in print("Any => 2, msg=\(String(describing: context.userInfo))") } + machine.addRoute(.str("2") => .any) { context in print("2 => Any, msg=\(String(describing: context.userInfo))") } // add handler (handlerContext = (event, transition, order, userInfo)) machine.addHandler(.str("0") => .str("1")) { context in @@ -151,10 +151,10 @@ class MiscTests: _TestCase print("[Entry 1] \(context.fromState) => \(context.toState)") } $0.addHandler(.any => .state2) { context in - print("[Entry 2] \(context.fromState) => \(context.toState), userInfo = \(context.userInfo)") + print("[Entry 2] \(context.fromState) => \(context.toState), userInfo = \(String(describing: context.userInfo))") } $0.addHandler(.any => .state2) { context in - print("[Entry 2b] \(context.fromState) => \(context.toState), userInfo = \(context.userInfo)") + print("[Entry 2b] \(context.fromState) => \(context.toState), userInfo = \(String(describing: context.userInfo))") } // add exit handlers @@ -165,10 +165,10 @@ class MiscTests: _TestCase print("[Exit 1] \(context.fromState) => \(context.toState)") } $0.addHandler(.state2 => .any) { context in - print("[Exit 2] \(context.fromState) => \(context.toState), userInfo = \(context.userInfo)") + print("[Exit 2] \(context.fromState) => \(context.toState), userInfo = \(String(describing: context.userInfo))") } $0.addHandler(.state2 => .any) { context in - print("[Exit 2b] \(context.fromState) => \(context.toState), userInfo = \(context.userInfo)") + print("[Exit 2b] \(context.fromState) => \(context.toState), userInfo = \(String(describing: context.userInfo))") } } From d6d4d223dab665017052d42397886119d17df4cc Mon Sep 17 00:00:00 2001 From: Adam Yanalunas Date: Wed, 27 Sep 2017 15:16:33 -0700 Subject: [PATCH 08/27] Update project and code to Swift 4 --- Sources/Machine.swift | 12 ++++++------ Sources/StateMachine.swift | 14 +++++++------- SwiftState.xcodeproj/project.pbxproj | 12 ++++++------ 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/Sources/Machine.swift b/Sources/Machine.swift index 8d18824..80b7f7a 100644 --- a/Sources/Machine.swift +++ b/Sources/Machine.swift @@ -383,7 +383,7 @@ public class Machine // MARK: addRouteMapping + conditional handler @discardableResult - public func addRouteMapping(_ routeMapping: @escaping RouteMapping, order: HandlerOrder = _defaultOrder, handler: @escaping Machine.Handler) -> Disposable + public func addRouteMapping(_ routeMapping: @escaping RouteMapping, order: HandlerOrder = DefaultOrder, handler: @escaping Machine.Handler) -> Disposable { let routeDisposable = self.addRouteMapping(routeMapping) @@ -425,13 +425,13 @@ public class Machine // MARK: addHandler(event:) @discardableResult - public func addHandler(event: E, order: HandlerOrder = _defaultOrder, handler: @escaping Machine.Handler) -> Disposable + public func addHandler(event: E, order: HandlerOrder = DefaultOrder, handler: @escaping Machine.Handler) -> Disposable { return self.addHandler(event: .some(event), order: order, handler: handler) } @discardableResult - public func addHandler(event: Event, order: HandlerOrder = _defaultOrder, handler: @escaping Machine.Handler) -> Disposable + public func addHandler(event: Event, order: HandlerOrder = DefaultOrder, handler: @escaping Machine.Handler) -> Disposable { return self._addHandler(event: event, order: order) { context in // skip if not event-based transition @@ -446,7 +446,7 @@ public class Machine } @discardableResult - private func _addHandler(event: Event, order: HandlerOrder = _defaultOrder, handler: @escaping Handler) -> Disposable + private func _addHandler(event: Event, order: HandlerOrder = DefaultOrder, handler: @escaping Handler) -> Disposable { if self._handlers[event] == nil { self._handlers[event] = [] @@ -470,7 +470,7 @@ public class Machine // MARK: addErrorHandler @discardableResult - public func addErrorHandler(order: HandlerOrder = _defaultOrder, handler: @escaping Handler) -> Disposable + public func addErrorHandler(order: HandlerOrder = DefaultOrder, handler: @escaping Handler) -> Disposable { let key = _createUniqueString() @@ -541,7 +541,7 @@ public func <-! (machine: Machine, tuple: (E, Any?)) -> Machine: Machine /// Add `handler` that is called when `tryState()` succeeds for target `transition`. /// - Note: `handler` will not be invoked for `tryEvent()`. @discardableResult - public func addHandler(_ transition: Transition, order: HandlerOrder = _defaultOrder, handler: @escaping Handler) -> Disposable + public func addHandler(_ transition: Transition, order: HandlerOrder = DefaultOrder, handler: @escaping Handler) -> Disposable { if self._handlers[transition] == nil { self._handlers[transition] = [] @@ -322,7 +322,7 @@ public final class StateMachine: Machine /// Add `handler` that is called when either `tryEvent()` or `tryState()` succeeds for target `transition`. @discardableResult - public func addAnyHandler(_ transition: Transition, order: HandlerOrder = _defaultOrder, handler: @escaping Handler) -> Disposable + public func addAnyHandler(_ transition: Transition, order: HandlerOrder = DefaultOrder, handler: @escaping Handler) -> Disposable { let disposable1 = self.addHandler(transition, order: order, handler: handler) let disposable2 = self.addHandler(event: .any, order: order) { context in @@ -372,13 +372,13 @@ public final class StateMachine: Machine // MARK: addChainHandler @discardableResult - public func addChainHandler(_ chain: TransitionChain, order: HandlerOrder = _defaultOrder, handler: @escaping Handler) -> Disposable + public func addChainHandler(_ chain: TransitionChain, order: HandlerOrder = DefaultOrder, handler: @escaping Handler) -> Disposable { return self.addChainHandler(RouteChain(transitionChain: chain), order: order, handler: handler) } @discardableResult - public func addChainHandler(_ chain: RouteChain, order: HandlerOrder = _defaultOrder, handler: @escaping Handler) -> Disposable + public func addChainHandler(_ chain: RouteChain, order: HandlerOrder = DefaultOrder, handler: @escaping Handler) -> Disposable { return self._addChainHandler(chain, order: order, handler: handler, isError: false) } @@ -386,19 +386,19 @@ public final class StateMachine: Machine // MARK: addChainErrorHandler @discardableResult - public func addChainErrorHandler(_ chain: TransitionChain, order: HandlerOrder = _defaultOrder, handler: @escaping Handler) -> Disposable + public func addChainErrorHandler(_ chain: TransitionChain, order: HandlerOrder = DefaultOrder, handler: @escaping Handler) -> Disposable { return self.addChainErrorHandler(RouteChain(transitionChain: chain), order: order, handler: handler) } @discardableResult - public func addChainErrorHandler(_ chain: RouteChain, order: HandlerOrder = _defaultOrder, handler: @escaping Handler) -> Disposable + public func addChainErrorHandler(_ chain: RouteChain, order: HandlerOrder = DefaultOrder, handler: @escaping Handler) -> Disposable { return self._addChainHandler(chain, order: order, handler: handler, isError: true) } @discardableResult - private func _addChainHandler(_ chain: RouteChain, order: HandlerOrder = _defaultOrder, handler: @escaping Handler, isError: Bool) -> Disposable + private func _addChainHandler(_ chain: RouteChain, order: HandlerOrder = DefaultOrder, handler: @escaping Handler, isError: Bool) -> Disposable { var handlerDisposables: [Disposable] = [] diff --git a/SwiftState.xcodeproj/project.pbxproj b/SwiftState.xcodeproj/project.pbxproj index bcd8b06..a3f913f 100644 --- a/SwiftState.xcodeproj/project.pbxproj +++ b/SwiftState.xcodeproj/project.pbxproj @@ -317,11 +317,11 @@ TargetAttributes = { 1FA61FFF1996601000460108 = { CreatedOnToolsVersion = 6.0; - LastSwiftMigration = 0800; + LastSwiftMigration = 0900; }; 1FA6200A1996601000460108 = { CreatedOnToolsVersion = 6.0; - LastSwiftMigration = 0800; + LastSwiftMigration = 0900; }; }; }; @@ -534,7 +534,7 @@ PRODUCT_BUNDLE_IDENTIFIER = "com.inamiy.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(PROJECT_NAME)"; SKIP_INSTALL = YES; - SWIFT_VERSION = 3.0; + SWIFT_VERSION = 4.0; }; name = Debug; }; @@ -554,7 +554,7 @@ PRODUCT_BUNDLE_IDENTIFIER = "com.inamiy.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(PROJECT_NAME)"; SKIP_INSTALL = YES; - SWIFT_VERSION = 3.0; + SWIFT_VERSION = 4.0; }; name = Release; }; @@ -574,7 +574,7 @@ INFOPLIST_FILE = Tests/Info.plist; PRODUCT_BUNDLE_IDENTIFIER = "com.inamiy.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 3.0; + SWIFT_VERSION = 4.0; }; name = Debug; }; @@ -590,7 +590,7 @@ INFOPLIST_FILE = Tests/Info.plist; PRODUCT_BUNDLE_IDENTIFIER = "com.inamiy.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 3.0; + SWIFT_VERSION = 4.0; }; name = Release; }; From 6347e6c35e3effe6a0d3626ef5d421090228cdf3 Mon Sep 17 00:00:00 2001 From: Adam Yanalunas Date: Wed, 27 Sep 2017 15:16:49 -0700 Subject: [PATCH 09/27] Update CircleCI to 4.0 branch --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6828e24..1751bff 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -SwiftState [![Circle CI](https://circleci.com/gh/ReactKit/SwiftState/tree/swift%2F2.0.svg?style=svg)](https://circleci.com/gh/ReactKit/SwiftState/tree/swift%2F2.0) +SwiftState [![Circle CI](https://circleci.com/gh/ReactKit/SwiftState/tree/swift%2F4.0.svg?style=svg)](https://circleci.com/gh/ReactKit/SwiftState/tree/swift%2F4.0) ========== Elegant state machine for Swift. From 2bdf6fabe831214d23055d1a7ee5608003befc11 Mon Sep 17 00:00:00 2001 From: Adam Yanalunas Date: Thu, 28 Sep 2017 18:44:28 -0700 Subject: [PATCH 10/27] Revert DefaultOrder property name to _defaultOrder --- Sources/Machine.swift | 12 ++++++------ Sources/StateMachine.swift | 14 +++++++------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/Sources/Machine.swift b/Sources/Machine.swift index 80b7f7a..ff22ab7 100644 --- a/Sources/Machine.swift +++ b/Sources/Machine.swift @@ -383,7 +383,7 @@ public class Machine // MARK: addRouteMapping + conditional handler @discardableResult - public func addRouteMapping(_ routeMapping: @escaping RouteMapping, order: HandlerOrder = DefaultOrder, handler: @escaping Machine.Handler) -> Disposable + public func addRouteMapping(_ routeMapping: @escaping RouteMapping, order: HandlerOrder = _defaultOrder, handler: @escaping Machine.Handler) -> Disposable { let routeDisposable = self.addRouteMapping(routeMapping) @@ -425,13 +425,13 @@ public class Machine // MARK: addHandler(event:) @discardableResult - public func addHandler(event: E, order: HandlerOrder = DefaultOrder, handler: @escaping Machine.Handler) -> Disposable + public func addHandler(event: E, order: HandlerOrder = _defaultOrder, handler: @escaping Machine.Handler) -> Disposable { return self.addHandler(event: .some(event), order: order, handler: handler) } @discardableResult - public func addHandler(event: Event, order: HandlerOrder = DefaultOrder, handler: @escaping Machine.Handler) -> Disposable + public func addHandler(event: Event, order: HandlerOrder = _defaultOrder, handler: @escaping Machine.Handler) -> Disposable { return self._addHandler(event: event, order: order) { context in // skip if not event-based transition @@ -446,7 +446,7 @@ public class Machine } @discardableResult - private func _addHandler(event: Event, order: HandlerOrder = DefaultOrder, handler: @escaping Handler) -> Disposable + private func _addHandler(event: Event, order: HandlerOrder = _defaultOrder, handler: @escaping Handler) -> Disposable { if self._handlers[event] == nil { self._handlers[event] = [] @@ -470,7 +470,7 @@ public class Machine // MARK: addErrorHandler @discardableResult - public func addErrorHandler(order: HandlerOrder = DefaultOrder, handler: @escaping Handler) -> Disposable + public func addErrorHandler(order: HandlerOrder = _defaultOrder, handler: @escaping Handler) -> Disposable { let key = _createUniqueString() @@ -541,7 +541,7 @@ public func <-! (machine: Machine, tuple: (E, Any?)) -> Machine: Machine /// Add `handler` that is called when `tryState()` succeeds for target `transition`. /// - Note: `handler` will not be invoked for `tryEvent()`. @discardableResult - public func addHandler(_ transition: Transition, order: HandlerOrder = DefaultOrder, handler: @escaping Handler) -> Disposable + public func addHandler(_ transition: Transition, order: HandlerOrder = _defaultOrder, handler: @escaping Handler) -> Disposable { if self._handlers[transition] == nil { self._handlers[transition] = [] @@ -322,7 +322,7 @@ public final class StateMachine: Machine /// Add `handler` that is called when either `tryEvent()` or `tryState()` succeeds for target `transition`. @discardableResult - public func addAnyHandler(_ transition: Transition, order: HandlerOrder = DefaultOrder, handler: @escaping Handler) -> Disposable + public func addAnyHandler(_ transition: Transition, order: HandlerOrder = _defaultOrder, handler: @escaping Handler) -> Disposable { let disposable1 = self.addHandler(transition, order: order, handler: handler) let disposable2 = self.addHandler(event: .any, order: order) { context in @@ -372,13 +372,13 @@ public final class StateMachine: Machine // MARK: addChainHandler @discardableResult - public func addChainHandler(_ chain: TransitionChain, order: HandlerOrder = DefaultOrder, handler: @escaping Handler) -> Disposable + public func addChainHandler(_ chain: TransitionChain, order: HandlerOrder = _defaultOrder, handler: @escaping Handler) -> Disposable { return self.addChainHandler(RouteChain(transitionChain: chain), order: order, handler: handler) } @discardableResult - public func addChainHandler(_ chain: RouteChain, order: HandlerOrder = DefaultOrder, handler: @escaping Handler) -> Disposable + public func addChainHandler(_ chain: RouteChain, order: HandlerOrder = _defaultOrder, handler: @escaping Handler) -> Disposable { return self._addChainHandler(chain, order: order, handler: handler, isError: false) } @@ -386,19 +386,19 @@ public final class StateMachine: Machine // MARK: addChainErrorHandler @discardableResult - public func addChainErrorHandler(_ chain: TransitionChain, order: HandlerOrder = DefaultOrder, handler: @escaping Handler) -> Disposable + public func addChainErrorHandler(_ chain: TransitionChain, order: HandlerOrder = _defaultOrder, handler: @escaping Handler) -> Disposable { return self.addChainErrorHandler(RouteChain(transitionChain: chain), order: order, handler: handler) } @discardableResult - public func addChainErrorHandler(_ chain: RouteChain, order: HandlerOrder = DefaultOrder, handler: @escaping Handler) -> Disposable + public func addChainErrorHandler(_ chain: RouteChain, order: HandlerOrder = _defaultOrder, handler: @escaping Handler) -> Disposable { return self._addChainHandler(chain, order: order, handler: handler, isError: true) } @discardableResult - private func _addChainHandler(_ chain: RouteChain, order: HandlerOrder = DefaultOrder, handler: @escaping Handler, isError: Bool) -> Disposable + private func _addChainHandler(_ chain: RouteChain, order: HandlerOrder = _defaultOrder, handler: @escaping Handler, isError: Bool) -> Disposable { var handlerDisposables: [Disposable] = [] From a986cb7c3b3ce3935d0043194f3c794ef3d03a03 Mon Sep 17 00:00:00 2001 From: Adam Yanalunas Date: Thu, 28 Sep 2017 18:44:41 -0700 Subject: [PATCH 11/27] Remove CircleCI badge from README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1751bff..590a353 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -SwiftState [![Circle CI](https://circleci.com/gh/ReactKit/SwiftState/tree/swift%2F4.0.svg?style=svg)](https://circleci.com/gh/ReactKit/SwiftState/tree/swift%2F4.0) +SwiftState ========== Elegant state machine for Swift. From 74756b2b86d18b4fa1cc2b4e98eb059fbaefb3e9 Mon Sep 17 00:00:00 2001 From: John Foulkes Date: Fri, 7 Apr 2017 20:40:21 +0100 Subject: [PATCH 12/27] Renamed Tests -> SwiftStateTests for SPM --- SwiftState.xcodeproj/project.pbxproj | 184 ++++++++---------- Tests/{ => SwiftStateTests}/BasicTests.swift | 0 Tests/{ => SwiftStateTests}/EventTests.swift | 0 .../HierarchicalMachineTests.swift | 0 Tests/{ => SwiftStateTests}/Info.plist | 0 .../{ => SwiftStateTests}/MachineTests.swift | 0 Tests/{ => SwiftStateTests}/MiscTests.swift | 0 Tests/{ => SwiftStateTests}/MyEvent.swift | 0 Tests/{ => SwiftStateTests}/MyState.swift | 0 Tests/{ => SwiftStateTests}/QiitaTests.swift | 0 .../RouteChainTests.swift | 0 .../RouteMappingTests.swift | 0 Tests/{ => SwiftStateTests}/RouteTests.swift | 0 .../StateMachineEventTests.swift | 0 .../StateMachineTests.swift | 0 Tests/{ => SwiftStateTests}/StateTests.swift | 0 .../String+TestExt.swift | 0 .../TransitionChainTests.swift | 0 .../TransitionTests.swift | 0 Tests/{ => SwiftStateTests}/_TestCase.swift | 0 20 files changed, 84 insertions(+), 100 deletions(-) rename Tests/{ => SwiftStateTests}/BasicTests.swift (100%) rename Tests/{ => SwiftStateTests}/EventTests.swift (100%) rename Tests/{ => SwiftStateTests}/HierarchicalMachineTests.swift (100%) rename Tests/{ => SwiftStateTests}/Info.plist (100%) rename Tests/{ => SwiftStateTests}/MachineTests.swift (100%) rename Tests/{ => SwiftStateTests}/MiscTests.swift (100%) rename Tests/{ => SwiftStateTests}/MyEvent.swift (100%) rename Tests/{ => SwiftStateTests}/MyState.swift (100%) rename Tests/{ => SwiftStateTests}/QiitaTests.swift (100%) rename Tests/{ => SwiftStateTests}/RouteChainTests.swift (100%) rename Tests/{ => SwiftStateTests}/RouteMappingTests.swift (100%) rename Tests/{ => SwiftStateTests}/RouteTests.swift (100%) rename Tests/{ => SwiftStateTests}/StateMachineEventTests.swift (100%) rename Tests/{ => SwiftStateTests}/StateMachineTests.swift (100%) rename Tests/{ => SwiftStateTests}/StateTests.swift (100%) rename Tests/{ => SwiftStateTests}/String+TestExt.swift (100%) rename Tests/{ => SwiftStateTests}/TransitionChainTests.swift (100%) rename Tests/{ => SwiftStateTests}/TransitionTests.swift (100%) rename Tests/{ => SwiftStateTests}/_TestCase.swift (100%) diff --git a/SwiftState.xcodeproj/project.pbxproj b/SwiftState.xcodeproj/project.pbxproj index 3552690..76796f2 100644 --- a/SwiftState.xcodeproj/project.pbxproj +++ b/SwiftState.xcodeproj/project.pbxproj @@ -7,19 +7,12 @@ objects = { /* Begin PBXBuildFile section */ - 1F198C5C19972320001C3700 /* QiitaTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F198C5B19972320001C3700 /* QiitaTests.swift */; }; - 1F1F74C31C0A02EA00675EAA /* HierarchicalMachineTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F1F74C11C0A02E700675EAA /* HierarchicalMachineTests.swift */; }; - 1F27771E1BE68D1D00C57CC9 /* RouteMappingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F27771C1BE68C7F00C57CC9 /* RouteMappingTests.swift */; }; - 1F4336AD1C17113F00E7C1AC /* StateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F4336AC1C17113F00E7C1AC /* StateTests.swift */; }; - 1F4336B01C17115700E7C1AC /* EventTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F4336AF1C17115700E7C1AC /* EventTests.swift */; }; 1F532C541C12B5BC00D3813A /* Disposable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F532C531C12B5BC00D3813A /* Disposable.swift */; }; 1F532C571C12BBBB00D3813A /* _HandlerInfo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F532C561C12BBBB00D3813A /* _HandlerInfo.swift */; }; - 1F532C611C12D7BD00D3813A /* MiscTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F532C601C12D7BD00D3813A /* MiscTests.swift */; }; 1F532C641C12EA8000D3813A /* _Random.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F532C631C12EA8000D3813A /* _Random.swift */; }; 1F70FB661BF0F46000E5AC8C /* _RouteID.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F70FB651BF0F46000E5AC8C /* _RouteID.swift */; }; 1F70FB6C1BF0F47700E5AC8C /* _RouteMappingID.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F70FB6B1BF0F47700E5AC8C /* _RouteMappingID.swift */; }; 1F70FB6F1BF0F59600E5AC8C /* _HandlerID.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F70FB6E1BF0F59600E5AC8C /* _HandlerID.swift */; }; - 1F70FB791BF0FB7000E5AC8C /* String+TestExt.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F70FB761BF0FB2400E5AC8C /* String+TestExt.swift */; }; 1FA620061996601000460108 /* SwiftState.h in Headers */ = {isa = PBXBuildFile; fileRef = 1FA620051996601000460108 /* SwiftState.h */; settings = {ATTRIBUTES = (Public, ); }; }; 1FA620201996606300460108 /* EventType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FA620191996606200460108 /* EventType.swift */; }; 1FA620221996606300460108 /* Route.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FA6201B1996606300460108 /* Route.swift */; }; @@ -27,20 +20,27 @@ 1FA620241996606300460108 /* Transition.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FA6201D1996606300460108 /* Transition.swift */; }; 1FA620251996606300460108 /* TransitionChain.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FA6201E1996606300460108 /* TransitionChain.swift */; }; 1FA620261996606300460108 /* StateType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FA6201F1996606300460108 /* StateType.swift */; }; - 1FA62030199660CA00460108 /* _TestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FA62027199660CA00460108 /* _TestCase.swift */; }; - 1FA62031199660CA00460108 /* BasicTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FA62028199660CA00460108 /* BasicTests.swift */; }; - 1FA62032199660CA00460108 /* MyState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FA62029199660CA00460108 /* MyState.swift */; }; - 1FA62033199660CA00460108 /* RouteChainTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FA6202A199660CA00460108 /* RouteChainTests.swift */; }; - 1FA62034199660CA00460108 /* StateMachineEventTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FA6202B199660CA00460108 /* StateMachineEventTests.swift */; }; - 1FA62035199660CA00460108 /* StateMachineTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FA6202C199660CA00460108 /* StateMachineTests.swift */; }; - 1FA62036199660CA00460108 /* RouteTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FA6202D199660CA00460108 /* RouteTests.swift */; }; - 1FA62037199660CA00460108 /* TransitionChainTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FA6202E199660CA00460108 /* TransitionChainTests.swift */; }; - 1FA62038199660CA00460108 /* TransitionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FA6202F199660CA00460108 /* TransitionTests.swift */; }; - 1FB1EC8A199E515B00ABD937 /* MyEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FB1EC89199E515B00ABD937 /* MyEvent.swift */; }; 1FF692041996625900E3CE40 /* SwiftState.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1FA620001996601000460108 /* SwiftState.framework */; }; 4836FF5A1C0EFD700038B7D2 /* StateMachine.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4836FF591C0EFD700038B7D2 /* StateMachine.swift */; }; 483F35571C0EB192007C70D7 /* Machine.swift in Sources */ = {isa = PBXBuildFile; fileRef = 483F35561C0EB192007C70D7 /* Machine.swift */; }; - 4876510F1C0ECBEB005961AC /* MachineTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4876510E1C0ECBEB005961AC /* MachineTests.swift */; }; + 5243E3DC1E9821E900D031A9 /* _TestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5243E3B61E9821E200D031A9 /* _TestCase.swift */; }; + 5243E3DD1E9821E900D031A9 /* BasicTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5243E3B71E9821E200D031A9 /* BasicTests.swift */; }; + 5243E3DE1E9821E900D031A9 /* EventTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5243E3B81E9821E200D031A9 /* EventTests.swift */; }; + 5243E3DF1E9821E900D031A9 /* HierarchicalMachineTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5243E3B91E9821E200D031A9 /* HierarchicalMachineTests.swift */; }; + 5243E3E11E9821E900D031A9 /* MachineTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5243E3BB1E9821E200D031A9 /* MachineTests.swift */; }; + 5243E3E21E9821E900D031A9 /* MiscTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5243E3BC1E9821E200D031A9 /* MiscTests.swift */; }; + 5243E3E31E9821E900D031A9 /* MyEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5243E3BD1E9821E200D031A9 /* MyEvent.swift */; }; + 5243E3E41E9821E900D031A9 /* MyState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5243E3BE1E9821E200D031A9 /* MyState.swift */; }; + 5243E3E51E9821E900D031A9 /* QiitaTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5243E3BF1E9821E200D031A9 /* QiitaTests.swift */; }; + 5243E3E61E9821E900D031A9 /* RouteChainTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5243E3C01E9821E200D031A9 /* RouteChainTests.swift */; }; + 5243E3E71E9821E900D031A9 /* RouteMappingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5243E3C11E9821E200D031A9 /* RouteMappingTests.swift */; }; + 5243E3E81E9821E900D031A9 /* RouteTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5243E3C21E9821E200D031A9 /* RouteTests.swift */; }; + 5243E3E91E9821E900D031A9 /* StateMachineEventTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5243E3C31E9821E200D031A9 /* StateMachineEventTests.swift */; }; + 5243E3EA1E9821E900D031A9 /* StateMachineTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5243E3C41E9821E200D031A9 /* StateMachineTests.swift */; }; + 5243E3EB1E9821E900D031A9 /* StateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5243E3C51E9821E200D031A9 /* StateTests.swift */; }; + 5243E3EC1E9821E900D031A9 /* String+TestExt.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5243E3C61E9821E200D031A9 /* String+TestExt.swift */; }; + 5243E3ED1E9821E900D031A9 /* TransitionChainTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5243E3C71E9821E200D031A9 /* TransitionChainTests.swift */; }; + 5243E3EE1E9821E900D031A9 /* TransitionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5243E3C81E9821E200D031A9 /* TransitionTests.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -54,39 +54,21 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ - 1F198C5B19972320001C3700 /* QiitaTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = QiitaTests.swift; sourceTree = ""; }; - 1F1F74C11C0A02E700675EAA /* HierarchicalMachineTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HierarchicalMachineTests.swift; sourceTree = ""; }; - 1F27771C1BE68C7F00C57CC9 /* RouteMappingTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RouteMappingTests.swift; sourceTree = ""; }; - 1F4336AC1C17113F00E7C1AC /* StateTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StateTests.swift; sourceTree = ""; }; - 1F4336AF1C17115700E7C1AC /* EventTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EventTests.swift; sourceTree = ""; }; 1F532C531C12B5BC00D3813A /* Disposable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Disposable.swift; sourceTree = ""; }; 1F532C561C12BBBB00D3813A /* _HandlerInfo.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = _HandlerInfo.swift; sourceTree = ""; }; - 1F532C601C12D7BD00D3813A /* MiscTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MiscTests.swift; sourceTree = ""; }; 1F532C631C12EA8000D3813A /* _Random.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = _Random.swift; sourceTree = ""; }; 1F70FB651BF0F46000E5AC8C /* _RouteID.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = _RouteID.swift; sourceTree = ""; }; 1F70FB6B1BF0F47700E5AC8C /* _RouteMappingID.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = _RouteMappingID.swift; sourceTree = ""; }; 1F70FB6E1BF0F59600E5AC8C /* _HandlerID.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = _HandlerID.swift; sourceTree = ""; }; - 1F70FB761BF0FB2400E5AC8C /* String+TestExt.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "String+TestExt.swift"; sourceTree = ""; }; 1FA620001996601000460108 /* SwiftState.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SwiftState.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 1FA620051996601000460108 /* SwiftState.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SwiftState.h; sourceTree = ""; }; 1FA6200B1996601000460108 /* SwiftStateTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SwiftStateTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; - 1FA6200E1996601000460108 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 1FA620191996606200460108 /* EventType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EventType.swift; sourceTree = ""; }; 1FA6201B1996606300460108 /* Route.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Route.swift; sourceTree = ""; }; 1FA6201C1996606300460108 /* RouteChain.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RouteChain.swift; sourceTree = ""; }; 1FA6201D1996606300460108 /* Transition.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Transition.swift; sourceTree = ""; }; 1FA6201E1996606300460108 /* TransitionChain.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TransitionChain.swift; sourceTree = ""; }; 1FA6201F1996606300460108 /* StateType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StateType.swift; sourceTree = ""; }; - 1FA62027199660CA00460108 /* _TestCase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = _TestCase.swift; sourceTree = ""; }; - 1FA62028199660CA00460108 /* BasicTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BasicTests.swift; sourceTree = ""; }; - 1FA62029199660CA00460108 /* MyState.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MyState.swift; sourceTree = ""; }; - 1FA6202A199660CA00460108 /* RouteChainTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RouteChainTests.swift; sourceTree = ""; }; - 1FA6202B199660CA00460108 /* StateMachineEventTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StateMachineEventTests.swift; sourceTree = ""; }; - 1FA6202C199660CA00460108 /* StateMachineTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StateMachineTests.swift; sourceTree = ""; }; - 1FA6202D199660CA00460108 /* RouteTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RouteTests.swift; sourceTree = ""; }; - 1FA6202E199660CA00460108 /* TransitionChainTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TransitionChainTests.swift; sourceTree = ""; }; - 1FA6202F199660CA00460108 /* TransitionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TransitionTests.swift; sourceTree = ""; }; - 1FB1EC89199E515B00ABD937 /* MyEvent.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MyEvent.swift; sourceTree = ""; }; 1FD79F961C1736D600CE7060 /* UniversalFramework_Base.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = UniversalFramework_Base.xcconfig; sourceTree = ""; }; 1FD79F971C1736D600CE7060 /* UniversalFramework_Framework.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = UniversalFramework_Framework.xcconfig; sourceTree = ""; }; 1FD79F981C1736D600CE7060 /* UniversalFramework_Test.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = UniversalFramework_Test.xcconfig; sourceTree = ""; }; @@ -95,7 +77,25 @@ 1FD79FA61C17412600CE7060 /* Release.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Release.xcconfig; sourceTree = ""; }; 4836FF591C0EFD700038B7D2 /* StateMachine.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StateMachine.swift; sourceTree = ""; }; 483F35561C0EB192007C70D7 /* Machine.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Machine.swift; sourceTree = ""; }; - 4876510E1C0ECBEB005961AC /* MachineTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MachineTests.swift; sourceTree = ""; }; + 5243E3B61E9821E200D031A9 /* _TestCase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = _TestCase.swift; sourceTree = ""; }; + 5243E3B71E9821E200D031A9 /* BasicTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BasicTests.swift; sourceTree = ""; }; + 5243E3B81E9821E200D031A9 /* EventTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EventTests.swift; sourceTree = ""; }; + 5243E3B91E9821E200D031A9 /* HierarchicalMachineTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HierarchicalMachineTests.swift; sourceTree = ""; }; + 5243E3BA1E9821E200D031A9 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + 5243E3BB1E9821E200D031A9 /* MachineTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MachineTests.swift; sourceTree = ""; }; + 5243E3BC1E9821E200D031A9 /* MiscTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MiscTests.swift; sourceTree = ""; }; + 5243E3BD1E9821E200D031A9 /* MyEvent.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MyEvent.swift; sourceTree = ""; }; + 5243E3BE1E9821E200D031A9 /* MyState.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MyState.swift; sourceTree = ""; }; + 5243E3BF1E9821E200D031A9 /* QiitaTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = QiitaTests.swift; sourceTree = ""; }; + 5243E3C01E9821E200D031A9 /* RouteChainTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RouteChainTests.swift; sourceTree = ""; }; + 5243E3C11E9821E200D031A9 /* RouteMappingTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RouteMappingTests.swift; sourceTree = ""; }; + 5243E3C21E9821E200D031A9 /* RouteTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RouteTests.swift; sourceTree = ""; }; + 5243E3C31E9821E200D031A9 /* StateMachineEventTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StateMachineEventTests.swift; sourceTree = ""; }; + 5243E3C41E9821E200D031A9 /* StateMachineTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StateMachineTests.swift; sourceTree = ""; }; + 5243E3C51E9821E200D031A9 /* StateTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StateTests.swift; sourceTree = ""; }; + 5243E3C61E9821E200D031A9 /* String+TestExt.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "String+TestExt.swift"; sourceTree = ""; }; + 5243E3C71E9821E200D031A9 /* TransitionChainTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TransitionChainTests.swift; sourceTree = ""; }; + 5243E3C81E9821E200D031A9 /* TransitionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TransitionTests.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -143,9 +143,9 @@ 1FA61FF61996601000460108 = { isa = PBXGroup; children = ( + 5243E3B51E9821E200D031A9 /* SwiftStateTests */, 1FD79F9E1C173C6C00CE7060 /* Configurations */, 1FA620021996601000460108 /* Sources */, - 1FA6200C1996601000460108 /* Tests */, 1FA620011996601000460108 /* Products */, ); sourceTree = ""; @@ -173,38 +173,6 @@ path = Sources; sourceTree = ""; }; - 1FA6200C1996601000460108 /* Tests */ = { - isa = PBXGroup; - children = ( - 1FA62027199660CA00460108 /* _TestCase.swift */, - 1FB1EC8D199E609900ABD937 /* State & Event */, - 1FA62028199660CA00460108 /* BasicTests.swift */, - 1F532C601C12D7BD00D3813A /* MiscTests.swift */, - 1F198C5B19972320001C3700 /* QiitaTests.swift */, - 4876510E1C0ECBEB005961AC /* MachineTests.swift */, - 1FA6202C199660CA00460108 /* StateMachineTests.swift */, - 1FA6202B199660CA00460108 /* StateMachineEventTests.swift */, - 1F4336AC1C17113F00E7C1AC /* StateTests.swift */, - 1F4336AF1C17115700E7C1AC /* EventTests.swift */, - 1FA6202F199660CA00460108 /* TransitionTests.swift */, - 1FA6202E199660CA00460108 /* TransitionChainTests.swift */, - 1FA6202D199660CA00460108 /* RouteTests.swift */, - 1FA6202A199660CA00460108 /* RouteChainTests.swift */, - 1F27771C1BE68C7F00C57CC9 /* RouteMappingTests.swift */, - 1F1F74C11C0A02E700675EAA /* HierarchicalMachineTests.swift */, - 1FA6200D1996601000460108 /* Supporting Files */, - ); - path = Tests; - sourceTree = ""; - }; - 1FA6200D1996601000460108 /* Supporting Files */ = { - isa = PBXGroup; - children = ( - 1FA6200E1996601000460108 /* Info.plist */, - ); - name = "Supporting Files"; - sourceTree = ""; - }; 1FB1EC88199E4ABC00ABD937 /* Protocols */ = { isa = PBXGroup; children = ( @@ -214,16 +182,6 @@ name = Protocols; sourceTree = ""; }; - 1FB1EC8D199E609900ABD937 /* State & Event */ = { - isa = PBXGroup; - children = ( - 1FA62029199660CA00460108 /* MyState.swift */, - 1FB1EC89199E515B00ABD937 /* MyEvent.swift */, - 1F70FB761BF0FB2400E5AC8C /* String+TestExt.swift */, - ); - name = "State & Event"; - sourceTree = ""; - }; 1FD79F931C1736D600CE7060 /* mrackwitz/xcconfigs (for target) */ = { isa = PBXGroup; children = ( @@ -255,6 +213,32 @@ path = Configurations; sourceTree = ""; }; + 5243E3B51E9821E200D031A9 /* SwiftStateTests */ = { + isa = PBXGroup; + children = ( + 5243E3B61E9821E200D031A9 /* _TestCase.swift */, + 5243E3B71E9821E200D031A9 /* BasicTests.swift */, + 5243E3B81E9821E200D031A9 /* EventTests.swift */, + 5243E3B91E9821E200D031A9 /* HierarchicalMachineTests.swift */, + 5243E3BA1E9821E200D031A9 /* Info.plist */, + 5243E3BB1E9821E200D031A9 /* MachineTests.swift */, + 5243E3BC1E9821E200D031A9 /* MiscTests.swift */, + 5243E3BD1E9821E200D031A9 /* MyEvent.swift */, + 5243E3BE1E9821E200D031A9 /* MyState.swift */, + 5243E3BF1E9821E200D031A9 /* QiitaTests.swift */, + 5243E3C01E9821E200D031A9 /* RouteChainTests.swift */, + 5243E3C11E9821E200D031A9 /* RouteMappingTests.swift */, + 5243E3C21E9821E200D031A9 /* RouteTests.swift */, + 5243E3C31E9821E200D031A9 /* StateMachineEventTests.swift */, + 5243E3C41E9821E200D031A9 /* StateMachineTests.swift */, + 5243E3C51E9821E200D031A9 /* StateTests.swift */, + 5243E3C61E9821E200D031A9 /* String+TestExt.swift */, + 5243E3C71E9821E200D031A9 /* TransitionChainTests.swift */, + 5243E3C81E9821E200D031A9 /* TransitionTests.swift */, + ); + path = SwiftStateTests; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXHeadersBuildPhase section */ @@ -386,24 +370,24 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - 1FB1EC8A199E515B00ABD937 /* MyEvent.swift in Sources */, - 1F198C5C19972320001C3700 /* QiitaTests.swift in Sources */, - 1F70FB791BF0FB7000E5AC8C /* String+TestExt.swift in Sources */, - 4876510F1C0ECBEB005961AC /* MachineTests.swift in Sources */, - 1FA62038199660CA00460108 /* TransitionTests.swift in Sources */, - 1FA62033199660CA00460108 /* RouteChainTests.swift in Sources */, - 1FA62030199660CA00460108 /* _TestCase.swift in Sources */, - 1F1F74C31C0A02EA00675EAA /* HierarchicalMachineTests.swift in Sources */, - 1F4336AD1C17113F00E7C1AC /* StateTests.swift in Sources */, - 1FA62036199660CA00460108 /* RouteTests.swift in Sources */, - 1F532C611C12D7BD00D3813A /* MiscTests.swift in Sources */, - 1FA62031199660CA00460108 /* BasicTests.swift in Sources */, - 1FA62034199660CA00460108 /* StateMachineEventTests.swift in Sources */, - 1FA62035199660CA00460108 /* StateMachineTests.swift in Sources */, - 1FA62037199660CA00460108 /* TransitionChainTests.swift in Sources */, - 1F27771E1BE68D1D00C57CC9 /* RouteMappingTests.swift in Sources */, - 1F4336B01C17115700E7C1AC /* EventTests.swift in Sources */, - 1FA62032199660CA00460108 /* MyState.swift in Sources */, + 5243E3DF1E9821E900D031A9 /* HierarchicalMachineTests.swift in Sources */, + 5243E3E61E9821E900D031A9 /* RouteChainTests.swift in Sources */, + 5243E3EC1E9821E900D031A9 /* String+TestExt.swift in Sources */, + 5243E3E21E9821E900D031A9 /* MiscTests.swift in Sources */, + 5243E3EE1E9821E900D031A9 /* TransitionTests.swift in Sources */, + 5243E3DD1E9821E900D031A9 /* BasicTests.swift in Sources */, + 5243E3E31E9821E900D031A9 /* MyEvent.swift in Sources */, + 5243E3EA1E9821E900D031A9 /* StateMachineTests.swift in Sources */, + 5243E3E91E9821E900D031A9 /* StateMachineEventTests.swift in Sources */, + 5243E3E81E9821E900D031A9 /* RouteTests.swift in Sources */, + 5243E3DE1E9821E900D031A9 /* EventTests.swift in Sources */, + 5243E3DC1E9821E900D031A9 /* _TestCase.swift in Sources */, + 5243E3E11E9821E900D031A9 /* MachineTests.swift in Sources */, + 5243E3E51E9821E900D031A9 /* QiitaTests.swift in Sources */, + 5243E3EB1E9821E900D031A9 /* StateTests.swift in Sources */, + 5243E3E71E9821E900D031A9 /* RouteMappingTests.swift in Sources */, + 5243E3ED1E9821E900D031A9 /* TransitionChainTests.swift in Sources */, + 5243E3E41E9821E900D031A9 /* MyState.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -559,7 +543,7 @@ "DEBUG=1", "$(inherited)", ); - INFOPLIST_FILE = Tests/Info.plist; + INFOPLIST_FILE = SwiftStateTests/Info.plist; PRODUCT_BUNDLE_IDENTIFIER = "com.inamiy.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_VERSION = 3.0; @@ -575,7 +559,7 @@ "$(DEVELOPER_FRAMEWORKS_DIR)", "$(inherited)", ); - INFOPLIST_FILE = Tests/Info.plist; + INFOPLIST_FILE = SwiftStateTests/Info.plist; PRODUCT_BUNDLE_IDENTIFIER = "com.inamiy.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_VERSION = 3.0; diff --git a/Tests/BasicTests.swift b/Tests/SwiftStateTests/BasicTests.swift similarity index 100% rename from Tests/BasicTests.swift rename to Tests/SwiftStateTests/BasicTests.swift diff --git a/Tests/EventTests.swift b/Tests/SwiftStateTests/EventTests.swift similarity index 100% rename from Tests/EventTests.swift rename to Tests/SwiftStateTests/EventTests.swift diff --git a/Tests/HierarchicalMachineTests.swift b/Tests/SwiftStateTests/HierarchicalMachineTests.swift similarity index 100% rename from Tests/HierarchicalMachineTests.swift rename to Tests/SwiftStateTests/HierarchicalMachineTests.swift diff --git a/Tests/Info.plist b/Tests/SwiftStateTests/Info.plist similarity index 100% rename from Tests/Info.plist rename to Tests/SwiftStateTests/Info.plist diff --git a/Tests/MachineTests.swift b/Tests/SwiftStateTests/MachineTests.swift similarity index 100% rename from Tests/MachineTests.swift rename to Tests/SwiftStateTests/MachineTests.swift diff --git a/Tests/MiscTests.swift b/Tests/SwiftStateTests/MiscTests.swift similarity index 100% rename from Tests/MiscTests.swift rename to Tests/SwiftStateTests/MiscTests.swift diff --git a/Tests/MyEvent.swift b/Tests/SwiftStateTests/MyEvent.swift similarity index 100% rename from Tests/MyEvent.swift rename to Tests/SwiftStateTests/MyEvent.swift diff --git a/Tests/MyState.swift b/Tests/SwiftStateTests/MyState.swift similarity index 100% rename from Tests/MyState.swift rename to Tests/SwiftStateTests/MyState.swift diff --git a/Tests/QiitaTests.swift b/Tests/SwiftStateTests/QiitaTests.swift similarity index 100% rename from Tests/QiitaTests.swift rename to Tests/SwiftStateTests/QiitaTests.swift diff --git a/Tests/RouteChainTests.swift b/Tests/SwiftStateTests/RouteChainTests.swift similarity index 100% rename from Tests/RouteChainTests.swift rename to Tests/SwiftStateTests/RouteChainTests.swift diff --git a/Tests/RouteMappingTests.swift b/Tests/SwiftStateTests/RouteMappingTests.swift similarity index 100% rename from Tests/RouteMappingTests.swift rename to Tests/SwiftStateTests/RouteMappingTests.swift diff --git a/Tests/RouteTests.swift b/Tests/SwiftStateTests/RouteTests.swift similarity index 100% rename from Tests/RouteTests.swift rename to Tests/SwiftStateTests/RouteTests.swift diff --git a/Tests/StateMachineEventTests.swift b/Tests/SwiftStateTests/StateMachineEventTests.swift similarity index 100% rename from Tests/StateMachineEventTests.swift rename to Tests/SwiftStateTests/StateMachineEventTests.swift diff --git a/Tests/StateMachineTests.swift b/Tests/SwiftStateTests/StateMachineTests.swift similarity index 100% rename from Tests/StateMachineTests.swift rename to Tests/SwiftStateTests/StateMachineTests.swift diff --git a/Tests/StateTests.swift b/Tests/SwiftStateTests/StateTests.swift similarity index 100% rename from Tests/StateTests.swift rename to Tests/SwiftStateTests/StateTests.swift diff --git a/Tests/String+TestExt.swift b/Tests/SwiftStateTests/String+TestExt.swift similarity index 100% rename from Tests/String+TestExt.swift rename to Tests/SwiftStateTests/String+TestExt.swift diff --git a/Tests/TransitionChainTests.swift b/Tests/SwiftStateTests/TransitionChainTests.swift similarity index 100% rename from Tests/TransitionChainTests.swift rename to Tests/SwiftStateTests/TransitionChainTests.swift diff --git a/Tests/TransitionTests.swift b/Tests/SwiftStateTests/TransitionTests.swift similarity index 100% rename from Tests/TransitionTests.swift rename to Tests/SwiftStateTests/TransitionTests.swift diff --git a/Tests/_TestCase.swift b/Tests/SwiftStateTests/_TestCase.swift similarity index 100% rename from Tests/_TestCase.swift rename to Tests/SwiftStateTests/_TestCase.swift From dbc9547624916952a76fa6cf4fc1f509da7ebf75 Mon Sep 17 00:00:00 2001 From: David Whetstone Date: Mon, 2 Apr 2018 15:11:54 -0700 Subject: [PATCH 13/27] Fix incorrect test path --- SwiftState.xcodeproj/project.pbxproj | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/SwiftState.xcodeproj/project.pbxproj b/SwiftState.xcodeproj/project.pbxproj index bd7e074..643c163 100644 --- a/SwiftState.xcodeproj/project.pbxproj +++ b/SwiftState.xcodeproj/project.pbxproj @@ -236,7 +236,8 @@ 5243E3C71E9821E200D031A9 /* TransitionChainTests.swift */, 5243E3C81E9821E200D031A9 /* TransitionTests.swift */, ); - path = SwiftStateTests; + name = SwiftStateTests; + path = Tests/SwiftStateTests; sourceTree = ""; }; /* End PBXGroup section */ @@ -555,7 +556,7 @@ "DEBUG=1", "$(inherited)", ); - INFOPLIST_FILE = SwiftStateTests/Info.plist; + INFOPLIST_FILE = Tests/SwiftStateTests/Info.plist; PRODUCT_BUNDLE_IDENTIFIER = "com.inamiy.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_VERSION = 4.0; @@ -571,7 +572,7 @@ "$(DEVELOPER_FRAMEWORKS_DIR)", "$(inherited)", ); - INFOPLIST_FILE = SwiftStateTests/Info.plist; + INFOPLIST_FILE = Tests/SwiftStateTests/Info.plist; PRODUCT_BUNDLE_IDENTIFIER = "com.inamiy.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_VERSION = 4.0; From bd79d8e50b0945df88c9a22ee6bddb0506df1e8c Mon Sep 17 00:00:00 2001 From: David Whetstone Date: Mon, 2 Apr 2018 15:29:27 -0700 Subject: [PATCH 14/27] Replace Machine.Context tuple with a struct MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Allows Machine.Context to be extended. Tuples, as non-nominal types, can’t be extended. --- Sources/Machine.swift | 10 ++++++++-- Tests/SwiftStateTests/BasicTests.swift | 4 ++-- Tests/SwiftStateTests/HierarchicalMachineTests.swift | 8 ++++---- Tests/SwiftStateTests/MachineTests.swift | 2 +- Tests/SwiftStateTests/MiscTests.swift | 8 ++++---- Tests/SwiftStateTests/RouteMappingTests.swift | 4 ++-- 6 files changed, 21 insertions(+), 15 deletions(-) diff --git a/Sources/Machine.swift b/Sources/Machine.swift index ff22ab7..23aa909 100644 --- a/Sources/Machine.swift +++ b/Sources/Machine.swift @@ -17,7 +17,13 @@ public class Machine { /// Closure argument for `Condition` & `Handler`. - public typealias Context = (event: E?, fromState: S, toState: S, userInfo: Any?) + public struct Context + { + public let event: E? + public let fromState: S + public let toState: S + public let userInfo: Any? + } /// Closure for validating transition. /// If condition returns `false`, transition will fail and associated handlers will not be invoked. @@ -569,7 +575,7 @@ internal func _validTransitions(fromState: S, toState: S) -> [Transition] internal func _canPassCondition(_ condition: Machine.Condition?, forEvent event: E?, fromState: S, toState: S, userInfo: Any?) -> Bool { - return condition?((event, fromState, toState, userInfo)) ?? true + return condition?(Machine.Context(event: event, fromState: fromState, toState: toState, userInfo: userInfo)) ?? true } internal func _insertHandlerIntoArray(_ handlerInfos: inout [_HandlerInfo], newHandlerInfo: _HandlerInfo) diff --git a/Tests/SwiftStateTests/BasicTests.swift b/Tests/SwiftStateTests/BasicTests.swift index 8f97c83..7dd88fa 100644 --- a/Tests/SwiftStateTests/BasicTests.swift +++ b/Tests/SwiftStateTests/BasicTests.swift @@ -26,8 +26,8 @@ class BasicTests: _TestCase } // add errorHandler - machine.addErrorHandler { event, fromState, toState, userInfo in - print("[ERROR] \(fromState) => \(toState)") + machine.addErrorHandler { context in + print("[ERROR] \(context.fromState) => \(context.toState)") } } diff --git a/Tests/SwiftStateTests/HierarchicalMachineTests.swift b/Tests/SwiftStateTests/HierarchicalMachineTests.swift index aa50914..ac6ab7a 100644 --- a/Tests/SwiftStateTests/HierarchicalMachineTests.swift +++ b/Tests/SwiftStateTests/HierarchicalMachineTests.swift @@ -92,9 +92,9 @@ class HierarchicalMachineTests: _TestCase let mainMachine = StateMachine<_MainState, NoEvent>(state: .mainState0) { mainMachine in // add routes & handle for same-subMachine internal transitions - mainMachine.addRoute(.any => .any, condition: { _, fromState, toState, userInfo in + mainMachine.addRoute(.any => .any, condition: { context in - switch (fromState, toState) { + switch (context.fromState, context.toState) { case let (.subMachine1(state1), .subMachine1(state2)): return subMachine1.hasRoute(fromState: state1, toState: state2) case let (.subMachine2(state1), .subMachine2(state2)): @@ -103,8 +103,8 @@ class HierarchicalMachineTests: _TestCase return false } - }, handler: { _, fromState, toState, userInfo in - switch (fromState, toState) { + }, handler: { context in + switch (context.fromState, context.toState) { case let (.subMachine1, .subMachine1(state2)): subMachine1 <- state2 case let (.subMachine2, .subMachine2(state2)): diff --git a/Tests/SwiftStateTests/MachineTests.swift b/Tests/SwiftStateTests/MachineTests.swift index 1414e19..3995481 100644 --- a/Tests/SwiftStateTests/MachineTests.swift +++ b/Tests/SwiftStateTests/MachineTests.swift @@ -341,7 +341,7 @@ class MachineTests: _TestCase let machine = Machine(state: .state0) { machine in machine.addRoutes(event: .event0, transitions: [ .state0 => .state1 ]) - machine.addErrorHandler { event, fromState, toState, userInfo in + machine.addErrorHandler { _ in invokeCount += 1 } } diff --git a/Tests/SwiftStateTests/MiscTests.swift b/Tests/SwiftStateTests/MiscTests.swift index 7909237..5b1fe90 100644 --- a/Tests/SwiftStateTests/MiscTests.swift +++ b/Tests/SwiftStateTests/MiscTests.swift @@ -26,8 +26,8 @@ class MiscTests: _TestCase } // add errorHandler - machine.addErrorHandler { event, fromState, toState, userInfo in - print("[ERROR] \(fromState) => \(toState)") + machine.addErrorHandler { context in + print("[ERROR] \(context.fromState) => \(context.toState)") } } @@ -63,8 +63,8 @@ class MiscTests: _TestCase } // add errorHandler - machine.addErrorHandler { event, fromState, toState, userInfo in - print("[ERROR] \(fromState) => \(toState)") + machine.addErrorHandler { context in + print("[ERROR] \(context.fromState) => \(context.toState)") } } diff --git a/Tests/SwiftStateTests/RouteMappingTests.swift b/Tests/SwiftStateTests/RouteMappingTests.swift index ddf765f..a0b8f38 100644 --- a/Tests/SwiftStateTests/RouteMappingTests.swift +++ b/Tests/SwiftStateTests/RouteMappingTests.swift @@ -98,7 +98,7 @@ class RouteMappingTests: _TestCase } // increment `count` when any events i.e. `.cancelAction` and `.loadAction(x)` succeed. - machine.addHandler(event: .any) { event, transition, order, userInfo in + machine.addHandler(event: .any) { _ in count += 1 } @@ -153,7 +153,7 @@ class RouteMappingTests: _TestCase } // increment `count` when any events i.e. `.cancelAction` and `.loadAction(x)` succeed. - machine.addHandler(.any => .any) { event, transition, order, userInfo in + machine.addHandler(.any => .any) { _ in count += 1 } From 28f4c2a4474c6fbb883ca932118414693e745d16 Mon Sep 17 00:00:00 2001 From: Amadeu Andrade Date: Wed, 4 Jul 2018 11:10:04 +0200 Subject: [PATCH 15/27] Bump version to 4.2.0 This adds the tag version and updates the podspec --- Sources/Info.plist | 2 +- SwiftState.podspec | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/Info.plist b/Sources/Info.plist index 0cefffe..b237a65 100644 --- a/Sources/Info.plist +++ b/Sources/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 4.1.0 + 4.2.0 CFBundleSignature ???? CFBundleVersion diff --git a/SwiftState.podspec b/SwiftState.podspec index cb2cbdd..5ddbe9b 100644 --- a/SwiftState.podspec +++ b/SwiftState.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'SwiftState' - s.version = '4.1.0' + s.version = '4.2.0' s.license = { :type => 'MIT' } s.homepage = 'https://github.com/ReactKit/SwiftState' s.authors = { 'Yasuhiro Inami' => 'inamiy@gmail.com' } From 327774759b0bde0251a7061343a2d2ef9ba07410 Mon Sep 17 00:00:00 2001 From: Konya Kirsten Date: Thu, 14 Mar 2019 09:36:47 +0100 Subject: [PATCH 16/27] Add Build Option 'Require Only App-Extension-Safe API' --- SwiftState.xcodeproj/project.pbxproj | 2 ++ 1 file changed, 2 insertions(+) diff --git a/SwiftState.xcodeproj/project.pbxproj b/SwiftState.xcodeproj/project.pbxproj index 643c163..c9bd54a 100644 --- a/SwiftState.xcodeproj/project.pbxproj +++ b/SwiftState.xcodeproj/project.pbxproj @@ -507,6 +507,7 @@ isa = XCBuildConfiguration; baseConfigurationReference = 1FD79F971C1736D600CE7060 /* UniversalFramework_Framework.xcconfig */; buildSettings = { + APPLICATION_EXTENSION_API_ONLY = YES; CLANG_ENABLE_MODULES = YES; COMBINE_HIDPI_IMAGES = YES; DEFINES_MODULE = YES; @@ -527,6 +528,7 @@ isa = XCBuildConfiguration; baseConfigurationReference = 1FD79F971C1736D600CE7060 /* UniversalFramework_Framework.xcconfig */; buildSettings = { + APPLICATION_EXTENSION_API_ONLY = YES; CLANG_ENABLE_MODULES = YES; COMBINE_HIDPI_IMAGES = YES; DEFINES_MODULE = YES; From 917b3be9dcdfea4e962d7dd5ec869a7bbbfac5cd Mon Sep 17 00:00:00 2001 From: Emanuele Rudel Date: Sat, 15 Jun 2019 23:17:25 -0700 Subject: [PATCH 17/27] Update for Swift 5.0 --- Sources/EventType.swift | 3 +-- Sources/Transition.swift | 5 +++-- SwiftState.xcodeproj/project.pbxproj | 20 +++++++++++++------ .../xcschemes/SwiftState.xcscheme | 8 +++----- .../HierarchicalMachineTests.swift | 8 ++++---- Tests/SwiftStateTests/MyEvent.swift | 4 ++-- Tests/SwiftStateTests/MyState.swift | 4 ++-- Tests/SwiftStateTests/RouteMappingTests.swift | 12 +++++------ 8 files changed, 35 insertions(+), 29 deletions(-) diff --git a/Sources/EventType.swift b/Sources/EventType.swift index 7266065..b531f41 100644 --- a/Sources/EventType.swift +++ b/Sources/EventType.swift @@ -86,9 +86,8 @@ public func == (lhs: E, rhs: Event) -> Bool /// Useful for creating StateMachine without events, i.e. `StateMachine`. public enum NoEvent: EventType { - public var hashValue: Int + public func hash(into hasher: inout Hasher) { - return 0 } } diff --git a/Sources/Transition.swift b/Sources/Transition.swift index 0cba79d..445b183 100644 --- a/Sources/Transition.swift +++ b/Sources/Transition.swift @@ -36,9 +36,10 @@ public struct Transition: Hashable self.init(fromState: .some(fromState), toState: .some(toState)) } - public var hashValue: Int + public func hash(into hasher: inout Hasher) { - return self.fromState.hashValue &+ self.toState.hashValue.byteSwapped + hasher.combine(fromState.hashValue) + hasher.combine(toState.hashValue.byteSwapped) } } diff --git a/SwiftState.xcodeproj/project.pbxproj b/SwiftState.xcodeproj/project.pbxproj index 643c163..3e261d5 100644 --- a/SwiftState.xcodeproj/project.pbxproj +++ b/SwiftState.xcodeproj/project.pbxproj @@ -297,7 +297,7 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 0700; - LastUpgradeCheck = 0900; + LastUpgradeCheck = 1020; ORGANIZATIONNAME = "Yasuhiro Inami"; TargetAttributes = { 1FA61FFF1996601000460108 = { @@ -312,10 +312,11 @@ }; buildConfigurationList = 1FA61FFA1996601000460108 /* Build configuration list for PBXProject "SwiftState" */; compatibilityVersion = "Xcode 3.2"; - developmentRegion = English; + developmentRegion = en; hasScannedForEncodings = 0; knownRegions = ( en, + Base, ); mainGroup = 1FA61FF61996601000460108; productRefGroup = 1FA620011996601000460108 /* Products */; @@ -408,6 +409,7 @@ baseConfigurationReference = 1FD79FA51C17412600CE7060 /* Debug.xcconfig */; buildSettings = { ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES; CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; CLANG_CXX_LIBRARY = "libc++"; CLANG_ENABLE_MODULES = YES; @@ -416,12 +418,14 @@ CLANG_WARN_BOOL_CONVERSION = YES; CLANG_WARN_COMMA = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; @@ -462,6 +466,7 @@ baseConfigurationReference = 1FD79FA61C17412600CE7060 /* Release.xcconfig */; buildSettings = { ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES; CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; CLANG_CXX_LIBRARY = "libc++"; CLANG_ENABLE_MODULES = YES; @@ -470,12 +475,14 @@ CLANG_WARN_BOOL_CONVERSION = YES; CLANG_WARN_COMMA = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; @@ -498,6 +505,7 @@ GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; MTL_ENABLE_DEBUG_INFO = NO; + SWIFT_COMPILATION_MODE = wholemodule; VERSIONING_SYSTEM = "apple-generic"; VERSION_INFO_PREFIX = ""; }; @@ -519,7 +527,7 @@ PRODUCT_BUNDLE_IDENTIFIER = "com.inamiy.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(PROJECT_NAME)"; SKIP_INSTALL = YES; - SWIFT_VERSION = 4.0; + SWIFT_VERSION = 5.0; }; name = Debug; }; @@ -539,7 +547,7 @@ PRODUCT_BUNDLE_IDENTIFIER = "com.inamiy.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(PROJECT_NAME)"; SKIP_INSTALL = YES; - SWIFT_VERSION = 4.0; + SWIFT_VERSION = 5.0; }; name = Release; }; @@ -559,7 +567,7 @@ INFOPLIST_FILE = Tests/SwiftStateTests/Info.plist; PRODUCT_BUNDLE_IDENTIFIER = "com.inamiy.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 4.0; + SWIFT_VERSION = 5.0; }; name = Debug; }; @@ -575,7 +583,7 @@ INFOPLIST_FILE = Tests/SwiftStateTests/Info.plist; PRODUCT_BUNDLE_IDENTIFIER = "com.inamiy.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 4.0; + SWIFT_VERSION = 5.0; }; name = Release; }; diff --git a/SwiftState.xcodeproj/xcshareddata/xcschemes/SwiftState.xcscheme b/SwiftState.xcodeproj/xcshareddata/xcschemes/SwiftState.xcscheme index 156778f..49e1b54 100644 --- a/SwiftState.xcodeproj/xcshareddata/xcschemes/SwiftState.xcscheme +++ b/SwiftState.xcodeproj/xcshareddata/xcschemes/SwiftState.xcscheme @@ -1,6 +1,6 @@ + codeCoverageEnabled = "YES" + shouldUseLaunchSchemeArgsEnv = "YES"> @@ -57,7 +56,6 @@ buildConfiguration = "Debug" selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" - language = "" launchStyle = "0" useCustomWorkingDirectory = "NO" ignoresPersistentStateOnLaunch = "NO" diff --git a/Tests/SwiftStateTests/HierarchicalMachineTests.swift b/Tests/SwiftStateTests/HierarchicalMachineTests.swift index ac6ab7a..aa18eba 100644 --- a/Tests/SwiftStateTests/HierarchicalMachineTests.swift +++ b/Tests/SwiftStateTests/HierarchicalMachineTests.swift @@ -15,15 +15,15 @@ private enum _MainState: StateType case subMachine1(_SubState) case subMachine2(_SubState) - var hashValue: Int + func hash(into hasher: inout Hasher) { switch self { case .mainState0: - return "MainState0".hashValue + hasher.combine("MainState0".hashValue) case let .subMachine1(state): - return "SubMachine1-\(state)".hashValue + hasher.combine("SubMachine1-\(state)".hashValue) case let .subMachine2(state): - return "SubMachine2-\(state)".hashValue + hasher.combine("SubMachine2-\(state)".hashValue) } } } diff --git a/Tests/SwiftStateTests/MyEvent.swift b/Tests/SwiftStateTests/MyEvent.swift index bf86a3a..563a78a 100644 --- a/Tests/SwiftStateTests/MyEvent.swift +++ b/Tests/SwiftStateTests/MyEvent.swift @@ -17,10 +17,10 @@ enum StrEvent: EventType { case str(String) - var hashValue: Int + func hash(into hasher: inout Hasher) { switch self { - case .str(let str): return str.hashValue + case .str(let str): hasher.combine(str.hashValue) } } } diff --git a/Tests/SwiftStateTests/MyState.swift b/Tests/SwiftStateTests/MyState.swift index b1cc1db..7425bb6 100644 --- a/Tests/SwiftStateTests/MyState.swift +++ b/Tests/SwiftStateTests/MyState.swift @@ -17,10 +17,10 @@ enum StrState: StateType { case str(String) - var hashValue: Int + func hash(into hasher: inout Hasher) { switch self { - case .str(let str): return str.hashValue + case .str(let str): hasher.combine(str.hashValue) } } } diff --git a/Tests/SwiftStateTests/RouteMappingTests.swift b/Tests/SwiftStateTests/RouteMappingTests.swift index a0b8f38..a92ed06 100644 --- a/Tests/SwiftStateTests/RouteMappingTests.swift +++ b/Tests/SwiftStateTests/RouteMappingTests.swift @@ -21,13 +21,13 @@ private enum _State: StateType, Hashable case pending case loading(Int) - var hashValue: Int + func hash(into hasher: inout Hasher) { switch self { case .pending: - return "Pending".hashValue + hasher.combine("Pending".hashValue) case let .loading(x): - return "Loading\(x)".hashValue + hasher.combine("Loading\(x)".hashValue) } } } @@ -49,13 +49,13 @@ private enum _Event: SwiftState.EventType, Hashable case cancelAction case loadAction(Int) - var hashValue: Int + func hash(into hasher: inout Hasher) { switch self { case .cancelAction: - return "CancelAction".hashValue + hasher.combine("CancelAction".hashValue) case let .loadAction(x): - return "LoadAction\(x)".hashValue + hasher.combine("LoadAction\(x)".hashValue) } } } From 9f4c19aafcde4ab4725ed09cc544d03e559b63a6 Mon Sep 17 00:00:00 2001 From: Yasuhiro Inami Date: Mon, 17 Jun 2019 01:51:30 +0900 Subject: [PATCH 18/27] [Xcode] Add IDEWorkspaceChecks.plist --- .../xcshareddata/IDEWorkspaceChecks.plist | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 SwiftState.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist diff --git a/SwiftState.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/SwiftState.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 0000000..18d9810 --- /dev/null +++ b/SwiftState.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + From 1c53ce12ca939bae2b329196d24a3b636acca3e3 Mon Sep 17 00:00:00 2001 From: Yasuhiro Inami Date: Mon, 17 Jun 2019 01:51:42 +0900 Subject: [PATCH 19/27] Update podspec --- SwiftState.podspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SwiftState.podspec b/SwiftState.podspec index 5ddbe9b..b527f04 100644 --- a/SwiftState.podspec +++ b/SwiftState.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'SwiftState' - s.version = '4.2.0' + s.version = '5.0.0' s.license = { :type => 'MIT' } s.homepage = 'https://github.com/ReactKit/SwiftState' s.authors = { 'Yasuhiro Inami' => 'inamiy@gmail.com' } From e7f32fd6ab2386a317f439b6385f2dabd6db4fb7 Mon Sep 17 00:00:00 2001 From: Yasuhiro Inami Date: Mon, 17 Jun 2019 02:03:27 +0900 Subject: [PATCH 20/27] Update xcconfig --- Configurations/Base.xcconfig | 4 +++- SwiftState.xcodeproj/project.pbxproj | 4 ---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/Configurations/Base.xcconfig b/Configurations/Base.xcconfig index a8a1138..522fbff 100644 --- a/Configurations/Base.xcconfig +++ b/Configurations/Base.xcconfig @@ -10,4 +10,6 @@ CODE_SIGN_IDENTITY[sdk=iphoneos*] = iPhone Developer; MACOSX_DEPLOYMENT_TARGET = 10.9; IPHONEOS_DEPLOYMENT_TARGET = 8.0; WATCHOS_DEPLOYMENT_TARGET = 2.0; -TVOS_DEPLOYMENT_TARGET = 9.0; \ No newline at end of file +TVOS_DEPLOYMENT_TARGET = 9.0; + +SWIFT_VERSION = 5.0 diff --git a/SwiftState.xcodeproj/project.pbxproj b/SwiftState.xcodeproj/project.pbxproj index 66629a2..a8453b6 100644 --- a/SwiftState.xcodeproj/project.pbxproj +++ b/SwiftState.xcodeproj/project.pbxproj @@ -528,7 +528,6 @@ PRODUCT_BUNDLE_IDENTIFIER = "com.inamiy.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(PROJECT_NAME)"; SKIP_INSTALL = YES; - SWIFT_VERSION = 5.0; }; name = Debug; }; @@ -549,7 +548,6 @@ PRODUCT_BUNDLE_IDENTIFIER = "com.inamiy.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(PROJECT_NAME)"; SKIP_INSTALL = YES; - SWIFT_VERSION = 5.0; }; name = Release; }; @@ -569,7 +567,6 @@ INFOPLIST_FILE = Tests/SwiftStateTests/Info.plist; PRODUCT_BUNDLE_IDENTIFIER = "com.inamiy.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 5.0; }; name = Debug; }; @@ -585,7 +582,6 @@ INFOPLIST_FILE = Tests/SwiftStateTests/Info.plist; PRODUCT_BUNDLE_IDENTIFIER = "com.inamiy.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 5.0; }; name = Release; }; From 8cb92f38ccf34a8b3aa81c5c5cd90ebddaa976f8 Mon Sep 17 00:00:00 2001 From: Yasuhiro Inami Date: Mon, 17 Jun 2019 02:03:54 +0900 Subject: [PATCH 21/27] Update podspec --- SwiftState.podspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SwiftState.podspec b/SwiftState.podspec index b527f04..5443b3b 100644 --- a/SwiftState.podspec +++ b/SwiftState.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'SwiftState' - s.version = '5.0.0' + s.version = '6.0.0' s.license = { :type => 'MIT' } s.homepage = 'https://github.com/ReactKit/SwiftState' s.authors = { 'Yasuhiro Inami' => 'inamiy@gmail.com' } From fbc36eab93a59a368e5f24a3b4902f1b941b1d39 Mon Sep 17 00:00:00 2001 From: Srinivas Prabhu Date: Tue, 8 Dec 2020 18:37:11 +0530 Subject: [PATCH 22/27] spm support --- Package.swift | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Package.swift b/Package.swift index f0b256b..1fd0a49 100644 --- a/Package.swift +++ b/Package.swift @@ -10,4 +10,12 @@ import PackageDescription let package = Package( name: "SwiftState" + name: "SwiftState", + platforms: [.iOS(.v11),.watchOS(*)], + products: [ + .library(name: "SwiftState", targets: ["SwiftState"]) + ], + targets: [ + .target(name: "SwiftState", path: "Sources") + ] ) From 1c5b64de18cc44561c4061b885dcb3c6da5af558 Mon Sep 17 00:00:00 2001 From: Srinivas Prabhu Date: Tue, 8 Dec 2020 18:42:53 +0530 Subject: [PATCH 23/27] compiler error fix --- Package.swift | 1 - 1 file changed, 1 deletion(-) diff --git a/Package.swift b/Package.swift index 1fd0a49..ebc1a95 100644 --- a/Package.swift +++ b/Package.swift @@ -9,7 +9,6 @@ import PackageDescription let package = Package( - name: "SwiftState" name: "SwiftState", platforms: [.iOS(.v11),.watchOS(*)], products: [ From 9f3f97b9e065d9cc9f6d29d40ff6e6a7ae82e157 Mon Sep 17 00:00:00 2001 From: Srinivas Prabhu Date: Tue, 8 Dec 2020 18:47:36 +0530 Subject: [PATCH 24/27] swift package manager support --- Package.swift | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/Package.swift b/Package.swift index ebc1a95..b4f6daa 100644 --- a/Package.swift +++ b/Package.swift @@ -1,20 +1,28 @@ -// -// Package.swift -// SwiftState -// -// Created by Yasuhiro Inami on 2015-12-05. -// Copyright © 2015 Yasuhiro Inami. All rights reserved. -// +// swift-tools-version:5.3 +// The swift-tools-version declares the minimum version of Swift required to build this package. import PackageDescription let package = Package( name: "SwiftState", - platforms: [.iOS(.v11),.watchOS(*)], products: [ - .library(name: "SwiftState", targets: ["SwiftState"]) + // Products define the executables and libraries a package produces, and make them visible to other packages. + .library( + name: "SwiftState", + targets: ["SwiftState"]), + ], + dependencies: [ + // Dependencies declare other packages that this package depends on. + // .package(url: /* package url */, from: "1.0.0"), ], targets: [ - .target(name: "SwiftState", path: "Sources") + // Targets are the basic building blocks of a package. A target can define a module or a test suite. + // Targets can depend on other targets in this package, and on products in packages this package depends on. + .target( + name: "SwiftState", + dependencies: []), + .testTarget( + name: "SwiftStateTests", + dependencies: ["SwiftState"]), ] ) From 3a49bb88f58b764ff2b2db42146fade2ff683dd8 Mon Sep 17 00:00:00 2001 From: Srinivas Prabhu Date: Tue, 8 Dec 2020 18:53:10 +0530 Subject: [PATCH 25/27] Attempt to fix spm issue --- Sources/{_HandlerID.swift => HandlerID.swift} | 0 Sources/{_HandlerInfo.swift => HandlerInfo.swift} | 0 Sources/{_Random.swift => Random.swift} | 0 Sources/{_RouteID.swift => RouteID.swift} | 0 Sources/{_RouteMappingID.swift => RouteMappingID.swift} | 0 5 files changed, 0 insertions(+), 0 deletions(-) rename Sources/{_HandlerID.swift => HandlerID.swift} (100%) rename Sources/{_HandlerInfo.swift => HandlerInfo.swift} (100%) rename Sources/{_Random.swift => Random.swift} (100%) rename Sources/{_RouteID.swift => RouteID.swift} (100%) rename Sources/{_RouteMappingID.swift => RouteMappingID.swift} (100%) diff --git a/Sources/_HandlerID.swift b/Sources/HandlerID.swift similarity index 100% rename from Sources/_HandlerID.swift rename to Sources/HandlerID.swift diff --git a/Sources/_HandlerInfo.swift b/Sources/HandlerInfo.swift similarity index 100% rename from Sources/_HandlerInfo.swift rename to Sources/HandlerInfo.swift diff --git a/Sources/_Random.swift b/Sources/Random.swift similarity index 100% rename from Sources/_Random.swift rename to Sources/Random.swift diff --git a/Sources/_RouteID.swift b/Sources/RouteID.swift similarity index 100% rename from Sources/_RouteID.swift rename to Sources/RouteID.swift diff --git a/Sources/_RouteMappingID.swift b/Sources/RouteMappingID.swift similarity index 100% rename from Sources/_RouteMappingID.swift rename to Sources/RouteMappingID.swift From 84886b8259b9a093d8445abf39c91f6461c6ad32 Mon Sep 17 00:00:00 2001 From: Srinivas Prabhu Date: Tue, 8 Dec 2020 19:01:31 +0530 Subject: [PATCH 26/27] update --- Sources/{HandlerID.swift => _HandlerID.swift} | 0 Sources/{HandlerInfo.swift => _HandlerInfo.swift} | 0 Sources/{Random.swift => _Random.swift} | 0 Sources/{RouteID.swift => _RouteID.swift} | 0 Sources/{RouteMappingID.swift => _RouteMappingID.swift} | 0 5 files changed, 0 insertions(+), 0 deletions(-) rename Sources/{HandlerID.swift => _HandlerID.swift} (100%) rename Sources/{HandlerInfo.swift => _HandlerInfo.swift} (100%) rename Sources/{Random.swift => _Random.swift} (100%) rename Sources/{RouteID.swift => _RouteID.swift} (100%) rename Sources/{RouteMappingID.swift => _RouteMappingID.swift} (100%) diff --git a/Sources/HandlerID.swift b/Sources/_HandlerID.swift similarity index 100% rename from Sources/HandlerID.swift rename to Sources/_HandlerID.swift diff --git a/Sources/HandlerInfo.swift b/Sources/_HandlerInfo.swift similarity index 100% rename from Sources/HandlerInfo.swift rename to Sources/_HandlerInfo.swift diff --git a/Sources/Random.swift b/Sources/_Random.swift similarity index 100% rename from Sources/Random.swift rename to Sources/_Random.swift diff --git a/Sources/RouteID.swift b/Sources/_RouteID.swift similarity index 100% rename from Sources/RouteID.swift rename to Sources/_RouteID.swift diff --git a/Sources/RouteMappingID.swift b/Sources/_RouteMappingID.swift similarity index 100% rename from Sources/RouteMappingID.swift rename to Sources/_RouteMappingID.swift From 43aa9c04d60b0be5d3140f0e8a23e6670371f51b Mon Sep 17 00:00:00 2001 From: Srinivas Prabhu Date: Tue, 8 Dec 2020 19:07:26 +0530 Subject: [PATCH 27/27] Updated source path in spm file --- Package.swift | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Package.swift b/Package.swift index b4f6daa..8c11573 100644 --- a/Package.swift +++ b/Package.swift @@ -20,9 +20,11 @@ let package = Package( // Targets can depend on other targets in this package, and on products in packages this package depends on. .target( name: "SwiftState", - dependencies: []), + dependencies: [], + path:"Sources"), .testTarget( name: "SwiftStateTests", - dependencies: ["SwiftState"]), + dependencies: ["SwiftState"], + path:"Sources"), ] )