Skip to content

Commit

Permalink
Merge pull request #19 from okhowang/master
Browse files Browse the repository at this point in the history
fix initial transition with multiple sub states
  • Loading branch information
qmuntal committed Apr 14, 2021
2 parents 6e7b011 + 5eded4a commit e166aa0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
6 changes: 3 additions & 3 deletions statemachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,12 +437,12 @@ func (sm *StateMachine) enterState(ctx context.Context, sr *stateRepresentation,
}
// Recursively enter substates that have an initial transition
if sr.HasInitialState {
isValidForInitialState := len(sr.Substates) != 0
isValidForInitialState := false
for _, substate := range sr.Substates {
// Verify that the target state is a substate
// Check if state has substate(s), and if an initial transition(s) has been set up.
if substate.State != sr.InitialTransitionTarget {
isValidForInitialState = false
if substate.State == sr.InitialTransitionTarget {
isValidForInitialState = true
break
}
}
Expand Down
9 changes: 9 additions & 0 deletions statemachine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1261,6 +1261,15 @@ func TestStateMachine_InitialTransition_DoNotAllowTransitionToSelf(t *testing.T)
})
}

func TestStateMachine_InitialTransition_WithMultipleSubStates(t *testing.T) {
sm := NewStateMachine(stateA)
sm.Configure(stateA).Permit(triggerX, stateB)
sm.Configure(stateB).InitialTransition(stateC)
sm.Configure(stateC).SubstateOf(stateB)
sm.Configure(stateD).SubstateOf(stateB)
assert.NoError(t, sm.Fire(triggerX))
}

func TestStateMachine_InitialTransition_DoNotAllowTransitionToAnotherSuperstate(t *testing.T) {
sm := NewStateMachine(stateA)

Expand Down

0 comments on commit e166aa0

Please sign in to comment.