Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tryEvent() by never tryState(.AnyState) #29

Merged
merged 2 commits into from
Jul 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion SwiftState/StateMachine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,13 @@ public class StateMachine<S: StateType, E: StateEventType>

public func tryEvent(event: Event, userInfo: Any? = nil) -> Bool
{
if let toState = self.canTryEvent(event) {
if var toState = self.canTryEvent(event) {

// current state should not be changed if `toState == nil`
if toState == nil {
toState = self.state
}

self._tryState(toState, userInfo: userInfo, forEvent: event)
return true
}
Expand Down
25 changes: 25 additions & 0 deletions SwiftStateTests/StateMachineEventTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,31 @@ class StateMachineEventTests: _TestCase
XCTAssertEqual(machine.state, MyState.State0)
}

/// https://github.com/ReactKit/SwiftState/issues/28
func testTryEvent_issue28()
{
var eventCount = 0
let machine = StateMachine<MyState, MyEvent>(state: .State0) { machine in
machine.addRoute(.State0 => .State1)
machine.addRouteEvent(.Event0, transitions: [nil => nil]) { _ in
eventCount++
}
}

XCTAssertEqual(eventCount, 0)

machine <-! .Event0
XCTAssertEqual(eventCount, 1)
XCTAssertEqual(machine.state, MyState.State0, "State should NOT be changed")

machine <- .State1
XCTAssertEqual(machine.state, MyState.State1, "State should be changed")

machine <-! .Event0
XCTAssertEqual(eventCount, 2)
XCTAssertEqual(machine.state, MyState.State1, "State should NOT be changed")
}

func testTryEvent_string()
{
let machine = StateMachine<MyState, String>(state: .State0)
Expand Down