def test_enter_exit_state(self): mock = MagicMock() def callback(state, event, instance, other_state): mock() states = [ 'A', 'B', { 'name': 'C', 'on_enter': callback, 'on_exit': callback }, 'D' ] transitions = [['A', 'C', 'go'], ['C', 'D', 'go']] m = Stuff.machine m.add_states(states=states, initial='A') m.add_transitions(transitions) s = Stuff() dispatch(s, Event('go')) self.assertEqual(s.state, 'C') self.assertTrue(mock.called) self.assertEqual(mock.call_count, 1) dispatch(s, Event('go')) self.assertEqual(s.state, 'D') self.assertEqual(mock.call_count, 2)
def test_example_one(self): states = ['standing', 'walking', {'name': 'caffeinated'}] transitions = [['standing', 'walking', 'walk'], ['walking', 'standing', 'stop'], ['*', 'caffeinated', 'drink'], ['caffeinated', 'standing', 'relax']] machine = Stuff.machine machine.add_states(states=states, initial='standing') machine.add_transitions(transitions) s = Stuff() dispatch(s, Event('walk')) dispatch(s, Event('stop')) dispatch(s, Event('drink')) self.assertEqual(s.state, 'caffeinated') with self.assertRaises(error.InvalidTransition): dispatch(s, Event('stop', raise_invalid_transition=True)) dispatch(s, Event('relax')) self.assertEqual(s.state, 'standing')
def test_dispatch(self): mock = MagicMock() def callback(state, event, instance): mock() state = State('State1') state.handlers['advance'] = callback states = [state, 'State2', {'name': 'State3'}] transitions = [ { 'event': 'advance', 'from_state': 'State1', 'to_state': 'State2', 'conditions': '!is_manager', 'after': callback }, { 'event': 'advance', 'from_state': 'State1', 'to_state': 'State3', 'conditions': 'is_manager', 'after': 'on_advance' }, ] m = Stuff.machine m.add_states(states) m.add_transitions(transitions) m.set_initial_state('State1') s = Stuff() s.is_manager = False dispatch(s, Event('advance')) self.assertEqual(s.state, 'State2') self.assertTrue(mock.called) self.assertEqual(mock.call_count, 2) s = Stuff() s.is_manager = True s.on_advance = callback dispatch(s, Event('advance')) self.assertEqual(s.state, 'State3') self.assertEqual(mock.call_count, 4)
def test_dispatch(self): mock = MagicMock() def callback(state, event, instance): mock() def master(state, event, instance): return instance.is_manager state = NestedState('A') state.handlers['advance'] = callback states = [state, 'B', {'name': 'C', 'children': ['1', '2']}] transitions = [ {'event': 'advance', 'from_state': 'A', 'to_state': 'B', 'conditions': '!is_manager', 'after': callback}, {'event': 'advance', 'from_state': 'A', 'to_state': 'C.1', 'conditions': 'is_manager', 'after': 'on_advance'}, {'event': 'advance', 'from_state': 'B', 'to_state': 'C.2', 'conditions': master}, ] m = Stuff.machine m.add_states(states) m.add_transitions(transitions) m.set_initial_state('A') s = Stuff() s.is_manager = False dispatch(s, Event('advance')) self.assertEqual(s.state, 'B') self.assertTrue(mock.called) self.assertEqual(mock.call_count, 2) s.is_manager = True dispatch(s, Event('advance')) self.assertEqual(s.state, 'C.2') s = Stuff() s.is_manager = True s.on_advance = callback dispatch(s, Event('advance')) self.assertEqual(s.state, 'C.1')