def test_transition_definitions(self): states = ['A', 'B', 'C', 'D'] # Define with list of dictionaries transitions = [{ 'trigger': 'walk', 'source': 'A', 'dest': 'B' }, { 'trigger': 'run', 'source': 'B', 'dest': 'C' }, { 'trigger': 'sprint', 'source': 'C', 'dest': 'D' }] m = Machine(states=states, transitions=transitions, initial='A') m.walk() self.assertEquals(m.state, 'B') # Define with list of lists transitions = [['walk', 'A', 'B'], ['run', 'B', 'C'], ['sprint', 'C', 'D']] m = Machine(states=states, transitions=transitions, initial='A') m.to_C() m.sprint() self.assertEquals(m.state, 'D')
def test_use_machine_as_model(self): states = ['A', 'B', 'C', 'D'] m = Machine(states=states, initial='A') m.add_transition('move', 'A', 'B') m.add_transition('move_to_C', 'B', 'C') m.move() self.assertEquals(m.state, 'B')
def test_silent_wrong_transition(self): state = ['state_one', 'state_two'] m = Machine(None, state) m.add_transition(trigger='advance', source='B', dest='C', before='extract_message') m.to_state_one() m.advance() self.assertEqual(m.state, 'state_one')
def test_init_machine_with_hella_arguments(self): states = [ State('State1'), 'State2', { 'name': 'State3', 'on_enter': 'hello_world' } ] transitions = [{ 'trigger': 'advance', 'source': 'State2', 'dest': 'State3' }] s = Stuff() Machine(model=s, states=states, transitions=transitions, initial='State2') s.advance() self.assertEquals(s.message, 'Hello World!')
def test_send_event_data(self): states = ['A', 'B', 'C', 'D'] s = Stuff() # First pass positional and keyword args directly to the callback m = Machine(model=s, states=states, initial='A', send_event=False) m.add_transition(trigger='advance', source='A', dest='B', before='set_message') s.advance(message='Hallo. My name is Inigo Montoya.') self.assertTrue(s.message.startswith('Hallo.')) # Now wrap arguments in an EventData instance m.send_event = True m.add_transition(trigger='advance', source='B', dest='C', before='extract_message') s.advance(message='You killed my father. Prepare to die.') self.assertTrue(s.message.startswith('You'))
def __init__(self, states, initial='A'): self.state = None Machine.__init__(self, states=states, initial=initial)
def test_ordered_transitions(self): states = ['beginning', 'middle', 'end'] m = Machine(None, states) m.add_ordered_transitions() self.assertEquals(m.state, 'initial') m.next_state() self.assertEquals(m.state, 'beginning') m.next_state() m.next_state() self.assertEquals(m.state, 'end') m.next_state() self.assertEquals(m.state, 'initial') # Include initial state in loop m = Machine(None, states) m.add_ordered_transitions(loop_includes_initial=False) m.to_end() m.next_state() self.assertEquals(m.state, 'beginning') # Test user-determined sequence and trigger name m = Machine(None, states, initial='beginning') m.add_ordered_transitions(['end', 'beginning'], trigger='advance') m.advance() self.assertEquals(m.state, 'end') m.advance() self.assertEquals(m.state, 'beginning') # Via init argument m = Machine(None, states, initial='beginning', ordered_transitions=True) m.next_state() self.assertEquals(m.state, 'middle')
def test_auto_transitions(self): states = ['A', {'name': 'B'}, State(name='C')] m = Machine(None, states, initial='A', auto_transitions=True) m.to_B() self.assertEquals(m.state, 'B') m.to_C() self.assertEquals(m.state, 'C') m.to_A() self.assertEquals(m.state, 'A') # Should fail if auto transitions is off... m = Machine(None, states, initial='A', auto_transitions=False) with self.assertRaises(TypeError): m.to_C()
def __init__(self): self.state = None states = ['A', 'B', 'C', 'D', 'E', 'F'] self.machine = Machine(self, states=states, initial='A')