Exemplo n.º 1
0
    def test_garbage_collection(self):

        states = ['A', 'B', 'C', 'D', 'E', 'F']
        machine = Machine(model=[], states=states, initial='A', name='Test Machine')
        machine.add_transition('advance', 'A', 'B')
        machine.add_transition('advance', 'B', 'C')
        machine.add_transition('advance', 'C', 'D')

        s1 = Dummy()
        s2 = Dummy()

        s2_collected = threading.Event()
        s2_proxy = weakref.proxy(s2, lambda _: s2_collected.set())

        machine.add_model([s1, s2])

        self.assertTrue(s1.is_A())
        self.assertTrue(s2.is_A())
        s1.advance()

        self.assertTrue(s1.is_B())
        self.assertTrue(s2.is_A())

        self.assertFalse(s2_collected.is_set())
        machine.remove_model(s2)
        del s2
        gc.collect()
        self.assertTrue(s2_collected.is_set())

        s3 = Dummy()
        machine.add_model(s3)
        s3.advance()
        s3.advance()
        self.assertTrue(s3.is_C())
Exemplo n.º 2
0
    def test_add_model_initial_state(self):
        states = ['A', 'B', 'C', 'D', 'E', 'F']
        machine = Machine(model=[],
                          states=states,
                          initial='A',
                          name='Test Machine')
        machine.add_transition('advance', 'A', 'B')
        machine.add_transition('advance', 'B', 'C')
        machine.add_transition('advance', 'C', 'D')

        s1 = Dummy()
        s2 = Dummy()
        s3 = Dummy()

        machine.add_model(s1)
        machine.add_model(s2, initial='B')
        machine.add_model(s3, initial='C')

        s1.advance()
        s2.advance()
        s3.advance()

        self.assertTrue(s1.is_B())
        self.assertTrue(s2.is_C())
        self.assertTrue(s3.is_D())
Exemplo n.º 3
0
 def test_dispatch(self):
     s1, s2 = Stuff(), Stuff()
     states = ['A', 'B', 'C']
     m = Machine(model=s1, states=states, ignore_invalid_triggers=True,
                 initial=states[0], transitions=[['go', 'A', 'B'], ['go', 'B', 'C']])
     m.add_model(s2, initial='B')
     m.dispatch('go')
     self.assertEqual(s1.state, 'B')
     self.assertEqual(s2.state, 'C')
Exemplo n.º 4
0
 def test_dispatch(self):
     s1, s2 = Stuff(), Stuff()
     states = ['A', 'B', 'C']
     m = Machine(model=s1, states=states, ignore_invalid_triggers=True,
                 initial=states[0], transitions=[['go', 'A', 'B'], ['go', 'B', 'C']])
     m.add_model(s2, initial='B')
     m.dispatch('go')
     self.assertEqual(s1.state, 'B')
     self.assertEqual(s2.state, 'C')
Exemplo n.º 5
0
class ChatBotBrains(object):

    states = ['size', 'pay', 'check', 'thanks']

    def __init__(self):

        self.machine = Machine(model=None,
                               states=ChatBotBrains.states,
                               initial='size')

        self.machine.add_transition('next', 'size', 'pay')

        self.machine.add_transition('next', 'pay', 'check')

        self.machine.add_transition('next', 'check', 'thanks')

        self.machine.add_transition('next', 'thanks', 'size')

    def add_model(self, model):
        self.machine.add_model(model)
Exemplo n.º 6
0
    def test_add_model_initial_state(self):
        states = ['A', 'B', 'C', 'D', 'E', 'F']
        machine = Machine(model=[], states=states, initial='A', name='Test Machine')
        machine.add_transition('advance', 'A', 'B')
        machine.add_transition('advance', 'B', 'C')
        machine.add_transition('advance', 'C', 'D')

        s1 = Dummy()
        s2 = Dummy()
        s3 = Dummy()

        machine.add_model(s1)
        machine.add_model(s2, initial='B')
        machine.add_model(s3, initial='C')

        s1.advance()
        s2.advance()
        s3.advance()

        self.assertTrue(s1.is_B())
        self.assertTrue(s2.is_C())
        self.assertTrue(s3.is_D())
Exemplo n.º 7
0
    def test_add_model_no_initial_state(self):
        states = ['A', 'B', 'C', 'D', 'E', 'F']
        machine = Machine(states=states, name='Test Machine', add_self=False)
        machine.add_transition('advance', 'A', 'B')
        machine.add_transition('advance', 'B', 'C')
        machine.add_transition('advance', 'C', 'D')

        s1 = Dummy()
        s2 = Dummy()
        s3 = Dummy()

        with self.assertRaises(MachineError):
            machine.add_model(s1)
        machine.add_model(s1, initial='A')
        machine.add_model(s2, initial='B')
        machine.add_model(s3, initial='C')

        s1.advance()
        s2.advance()
        s3.advance()

        self.assertTrue(s1.is_B())
        self.assertTrue(s2.is_C())
        self.assertTrue(s3.is_D())