def test_event_trigger_no_initial_state(self): state_machine = StateMachine("sm") exit_cb = MagicMock() entry_cb = MagicMock() initial_state = State("initial_state") second_state = State("second_state") initial_state.on_exit(exit_cb) second_state.on_entry(entry_cb) event = Event("event") state_machine.add_state(initial_state) state_machine.add_state(second_state) state_machine.add_event(event) state_machine.add_transition(initial_state, second_state, event) with pytest.raises(ValueError): state_machine.trigger_event(event, "data")
def test_event_trigger(self): state_machine = StateMachine("sm") exit_cb = MagicMock() entry_cb = MagicMock() initial_state = State("initial_state") second_state = State("second_state") initial_state.on_exit(exit_cb) second_state.on_entry(entry_cb) event = Event("event") state_machine.add_state(initial_state, initial_state=True) state_machine.add_state(second_state) state_machine.add_event(event) state_machine.add_transition(initial_state, second_state, event) state_machine.start("data") exit_cb.assert_not_called() entry_cb.assert_not_called() state_machine.trigger_event(event, "data") exit_cb.assert_called_once_with("data") entry_cb.assert_called_once_with("data")
def test_event_trigger_propagate_but_no_child_sm(self): state_machine = StateMachine("sm") exit_cb = MagicMock() entry_cb = MagicMock() initial_state = State("initial_state") second_state = State("second_state") initial_state.on_exit(exit_cb) second_state.on_entry(entry_cb) event = Event("event") state_machine.add_state(initial_state, initial_state=True) state_machine.add_state(second_state) state_machine.add_event(event) state_machine.add_transition(initial_state, second_state, event) state_machine.start("data") exit_cb.assert_not_called() entry_cb.assert_not_called() state_machine.trigger_event(event, "data", propagate=True) # no child fsm in current state, so the event is caught in this fsm assert state_machine.current_state == second_state
def test_exit_callback(self): exit_sm_cb = MagicMock() state_machine = StateMachine("sm") state_machine.on_exit(exit_sm_cb) initial_state = State("initial_state") second_state = State("second_state") exit_state_error = ExitState("Error") event = Event("event") error_event = Event("error") state_machine.add_state(initial_state, initial_state=True) state_machine.add_state(second_state) state_machine.add_state(exit_state_error) state_machine.add_event(event) state_machine.add_event(error_event) state_machine.add_transition(initial_state, second_state, event) state_machine.add_transition(second_state, exit_state_error, error_event) state_machine.start("data") assert state_machine.current_state == initial_state state_machine.trigger_event(event, "data") assert state_machine.current_state == second_state state_machine.trigger_event(error_event, "data") assert state_machine.current_state == exit_state_error exit_sm_cb.assert_called_once_with(exit_state_error, "data")