def test_bind_handlers_by_name_default_behaviour(self): target = Mock() sm = StateMachine({ 'initial': 'foo', 'transitions': { ('foo', 'bar'): lambda: True } }) sm.bind_handlers_by_name(target) # First cycle enters and executes initial state, but forces delta T to zero sm.process(1.0) target._on_entry_foo.assert_called_once_with(0) target._in_state_foo.assert_called_once_with(0) target._on_entry_foo.reset_mock() target._in_state_foo.reset_mock() # Second cycle transitions due to lambda: True above sm.process(2.0) target._on_exit_foo.assert_called_once_with(2.0) target._on_entry_bar.assert_called_once_with(2.0) target._in_state_bar.assert_called_once_with(2.0) target._on_exit_foo.reset_mock() target._on_entry_bar.reset_mock() target._in_state_bar.reset_mock() # Third cycle only does an in_state in bar sm.process(3.0) target._in_state_bar.assert_called_once_with(3.0) target._on_entry_bar.assert_not_called() target._on_exit_bar.assert_not_called()
def test_bind_handlers_by_name_default_behaviour(self): target = Mock() sm = StateMachine({ 'initial': 'foo', 'transitions': { ('foo', 'bar'): lambda: True } }) sm.bind_handlers_by_name(target) # First cycle enters and executes initial state, but forces delta T to zero sm.process(1.0) target._on_entry_foo.assert_called_once_with(0) target._in_state_foo.assert_called_once_with(0) target._on_entry_foo.reset_mock() target._in_state_foo.reset_mock() # Second cycle transitions due to lambda: True above sm.process(2.0) target._on_exit_foo.assert_called_once_with(2.0) target._on_entry_bar.assert_called_once_with(2.0) target._in_state_bar.assert_called_once_with(2.0) target._on_exit_foo.reset_mock() target._on_entry_bar.reset_mock() target._in_state_bar.reset_mock() # Third cycle only does an in_state in bar sm.process(3.0) target._in_state_bar.assert_called_once_with(3.0) target._on_entry_bar.assert_not_called() target._on_exit_bar.assert_not_called()
def test_bind_handlers_by_name_override(self): # first target will accept any event call first = Mock() # second target will 'override' the events for bar second = Mock(spec=['_on_entry_bar', '_in_state_bar', '_on_exit_bar']) sm = StateMachine({ 'initial': 'foo', 'transitions': { ('foo', 'bar'): lambda: True } }) sm.bind_handlers_by_name(first) sm.bind_handlers_by_name(second, override=True) # First cycle enters and executes initial state, but forces delta T to zero sm.process(1.0) first._on_entry_foo.assert_called_once_with(0) first._in_state_foo.assert_called_once_with(0) first._on_entry_foo.reset_mock() first._in_state_foo.reset_mock() # Second cycle transitions due to lambda: True above sm.process(2.0) first._on_exit_foo.assert_called_once_with(2.0) second._on_entry_bar.assert_called_once_with(2.0) second._in_state_bar.assert_called_once_with(2.0) first._on_exit_foo.reset_mock() second._on_entry_bar.reset_mock() second._in_state_bar.reset_mock() # Third cycle only does an in_state in bar sm.process(3.0) second._in_state_bar.assert_called_once_with(3.0) second._on_entry_bar.assert_not_called() second._on_exit_bar.assert_not_called() # 'bar' events should not have been called on 'first' first._on_entry_bar.assert_not_called() first._in_state_bar.assert_not_called() first._on_exit_bar.assert_not_called()
def test_bind_handlers_by_name_override(self): # first target will accept any event call first = Mock() # second target will 'override' the events for bar second = Mock(spec=['_on_entry_bar', '_in_state_bar', '_on_exit_bar']) sm = StateMachine({ 'initial': 'foo', 'transitions': { ('foo', 'bar'): lambda: True } }) sm.bind_handlers_by_name(first) sm.bind_handlers_by_name(second, override=True) # First cycle enters and executes initial state, but forces delta T to zero sm.process(1.0) first._on_entry_foo.assert_called_once_with(0) first._in_state_foo.assert_called_once_with(0) first._on_entry_foo.reset_mock() first._in_state_foo.reset_mock() # Second cycle transitions due to lambda: True above sm.process(2.0) first._on_exit_foo.assert_called_once_with(2.0) second._on_entry_bar.assert_called_once_with(2.0) second._in_state_bar.assert_called_once_with(2.0) first._on_exit_foo.reset_mock() second._on_entry_bar.reset_mock() second._in_state_bar.reset_mock() # Third cycle only does an in_state in bar sm.process(3.0) second._in_state_bar.assert_called_once_with(3.0) second._on_entry_bar.assert_not_called() second._on_exit_bar.assert_not_called() # 'bar' events should not have been called on 'first' first._on_entry_bar.assert_not_called() first._in_state_bar.assert_not_called() first._on_exit_bar.assert_not_called()
def test_bind_handlers_by_name_custom_prefix(self): target = Mock() sm = StateMachine({ 'initial': 'foo', 'transitions': { ('foo', 'bar'): lambda: True } }) sm.bind_handlers_by_name(target, prefix={ 'on_entry': 'enter_', 'in_state': 'do_', 'on_exit': 'exit_', }) # First cycle enters and executes initial state, but forces delta T to zero sm.process(1.0) target.enter_foo.assert_called_once_with(0) target.do_foo.assert_called_once_with(0) target.enter_foo.reset_mock() target.do_foo.reset_mock() # Second cycle transitions due to lambda: True above sm.process(2.0) target.exit_foo.assert_called_once_with(2.0) target.enter_bar.assert_called_once_with(2.0) target.do_bar.assert_called_once_with(2.0) target.exit_foo.reset_mock() target.enter_bar.reset_mock() target.do_bar.reset_mock() # Third cycle only does an in_state in bar sm.process(3.0) target.do_bar.assert_called_once_with(3.0) target.enter_bar.assert_not_called() target.exit_bar.assert_not_called()
def test_bind_handlers_by_name_custom_prefix(self): target = Mock() sm = StateMachine({ 'initial': 'foo', 'transitions': { ('foo', 'bar'): lambda: True } }) sm.bind_handlers_by_name(target, prefix={ 'on_entry': 'enter_', 'in_state': 'do_', 'on_exit': 'exit_', }) # First cycle enters and executes initial state, but forces delta T to zero sm.process(1.0) target.enter_foo.assert_called_once_with(0) target.do_foo.assert_called_once_with(0) target.enter_foo.reset_mock() target.do_foo.reset_mock() # Second cycle transitions due to lambda: True above sm.process(2.0) target.exit_foo.assert_called_once_with(2.0) target.enter_bar.assert_called_once_with(2.0) target.do_bar.assert_called_once_with(2.0) target.exit_foo.reset_mock() target.enter_bar.reset_mock() target.do_bar.reset_mock() # Third cycle only does an in_state in bar sm.process(3.0) target.do_bar.assert_called_once_with(3.0) target.enter_bar.assert_not_called() target.exit_bar.assert_not_called()
def test_bind_handlers_by_name_custom_prefix(self): target = Mock() sm = StateMachine( {"initial": "foo", "transitions": {("foo", "bar"): lambda: True}} ) sm.bind_handlers_by_name( target, prefix={ "on_entry": "enter_", "in_state": "do_", "on_exit": "exit_", }, ) # First cycle enters and executes initial state, but forces delta T to zero sm.process(1.0) target.enter_foo.assert_called_once_with(0) target.do_foo.assert_called_once_with(0) target.enter_foo.reset_mock() target.do_foo.reset_mock() # Second cycle transitions due to lambda: True above sm.process(2.0) target.exit_foo.assert_called_once_with(2.0) target.enter_bar.assert_called_once_with(2.0) target.do_bar.assert_called_once_with(2.0) target.exit_foo.reset_mock() target.enter_bar.reset_mock() target.do_bar.reset_mock() # Third cycle only does an in_state in bar sm.process(3.0) target.do_bar.assert_called_once_with(3.0) target.enter_bar.assert_not_called() target.exit_bar.assert_not_called()