def test_with_initial_state(self): state_machine = StateMachine("sm") initial_state = State("initial_state") entry_cb = MagicMock() initial_state.on_entry(entry_cb) state_machine.add_state(initial_state, initial_state=True) state_machine.start("data") assert state_machine.current_state == initial_state entry_cb.assert_called_once_with("data") assert state_machine.is_running()
def test_self_transition_no_condition(self): entry_callback = MagicMock() exit_callback = MagicMock() source_state = State("source") source_state.on_exit(exit_callback) source_state.on_entry(entry_callback) event = Event("event") transition = SelfTransition(source_state, event) transition("data") entry_callback.assert_called_once_with("data") exit_callback.assert_called_once_with("data")
def test_null_transition_condition_false(self): entry_callback = MagicMock() exit_callback = MagicMock() source_state = State("source") source_state.on_exit(exit_callback) source_state.on_entry(entry_callback) event = Event("event") transition = NullTransition(source_state, event) condition_callback = MagicMock(return_value=False) transition.add_condition(condition_callback) transition("data") entry_callback.assert_not_called() exit_callback.assert_not_called() condition_callback.assert_called_once_with("data")
def test_self_transition_condition_true(self): entry_callback = MagicMock() exit_callback = MagicMock() source_state = State("source") source_state.on_exit(exit_callback) source_state.on_entry(entry_callback) event = Event("event") transition = SelfTransition(source_state, event) condition_callback = MagicMock(return_value=True) transition.add_condition(condition_callback) transition("data") entry_callback.assert_called_once_with("data") exit_callback.assert_called_once_with("data") condition_callback.assert_called_once_with("data")
def create_child_fsm(): state_machine = StateMachine("child_sm") exit_cb = MagicMock() entry_cb = MagicMock() initial_state = State("child_initial_state") second_state = State("child_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) return state_machine
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_on_entry(self): callback = MagicMock() state = State("state") state.on_entry(callback) state.start("data") callback.assert_called_once_with("data")