Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_multiple_models(self):
class Model(object):
pass
s1, s2 = Model(), Model()
m = MachineFactory.get_predefined(nested=True)(model=[s1, s2], states=['A', 'B', 'C'],
initial='A')
self.assertEqual(len(m.models), 2)
m.add_transition('advance', 'A', 'B')
self.assertNotEqual(s1.advance, s2.advance)
s1.advance()
self.assertEqual(s1.state, 'B')
self.assertEqual(s2.state, 'A')
def setUp(self):
super(TestDiagramsLockedNested, self).setUp()
self.machine_cls = MachineFactory.get_predefined(graph=True, nested=True, locked=True)
from unittest import TestCase, skipIf
try:
import enum
except ImportError:
enum = None
from transitions.extensions import MachineFactory
@skipIf(enum is None, "enum is not available")
class TestEnumsAsStates(TestCase):
machine_cls = MachineFactory.get_predefined()
def setUp(self):
class States(enum.Enum):
RED = 1
YELLOW = 2
GREEN = 3
self.States = States
def test_pass_enums_as_states(self):
m = self.machine_cls(states=self.States, initial=self.States.YELLOW)
assert m.state == self.States.YELLOW
assert m.is_RED() is False
assert m.is_YELLOW() is True
assert m.is_RED() is False
def setUp(self):
states = ['A', 'B', {'name': 'C', 'children': ['1', '2', {'name': '3', 'children': ['a', 'b', 'c']}]},
'D', 'E', 'F']
machine_cls = MachineFactory.get_predefined(nested=True)
self.stuff = Stuff(states, machine_cls)
def setUp(self):
self.event_list = []
self.s1 = DummyModel()
self.c1 = TestContext(event_list=self.event_list)
self.c2 = TestContext(event_list=self.event_list)
self.c3 = TestContext(event_list=self.event_list)
self.c4 = TestContext(event_list=self.event_list)
self.stuff = Stuff(machine_cls=MachineFactory.get_predefined(locked=True), extra_kwargs={
'machine_context': [self.c1, self.c2]
})
self.stuff.machine.add_model(self.s1, model_context=[self.c3, self.c4])
del self.event_list[:]
self.stuff.machine.add_transition('forward', 'A', 'B')
machine = self.stuff.machine_cls(states=states, transitions=transitions, auto_transitions=False)
trans = machine.get_triggers('caffeinated{0}dithering'.format(state_separator))
self.assertEqual(len(trans), 3)
self.assertTrue('relax' in trans)
def test_internal_transitions(self):
s = self.stuff
s.machine.add_transition('internal', 'A', None, prepare='increase_level')
s.internal()
self.assertEqual(s.state, 'A')
self.assertEqual(s.level, 2)
class TestNestedStateEnums(TestEnumsAsStates):
machine_cls = MachineFactory.get_predefined(nested=True)
def test_nested_enums(self):
states = ['A', 'B', {'name': 'C', 'children': self.States, 'initial': self.States.GREEN}]
m = self.machine_cls(states=states, initial='A')
m.to_C()
self.assertEqual(m.state, self.States.GREEN)
@skipIf(pgv is None, 'NestedGraph diagram test requires graphviz')
class TestWithGraphTransitions(TestTransitions):
def setUp(self):
State.separator = state_separator
states = ['A', 'B', {'name': 'C', 'children': ['1', '2', {'name': '3', 'children': ['a', 'b', 'c']}]},
'D', 'E', 'F']
class CounterContext(object):
def __init__(self):
self.counter = 0
self.level = 0
self.max = 0
super(CounterContext, self).__init__()
def __enter__(self):
self.counter += 1
self.level += 1
self.max = max(self.level, self.max)
def __exit__(self, *exc):
self.level -= 1
M = MachineFactory.get_predefined(locked=True)
c = CounterContext()
m = M(states=['A', 'B', 'C', 'D'], transitions=[['reset', '*', 'A']], initial='A', machine_context=c)
m.get_triggers('A')
self.assertEqual(c.max, 1) # was 3 before
self.assertEqual(c.counter, 4) # was 72 (!) before
def test_multiple_models(self):
class Model(object):
pass
s1, s2 = Model(), Model()
m = MachineFactory.get_predefined(nested=True)(model=[s1, s2], states=['A', 'B', 'C'],
initial='A')
self.assertEqual(len(m.models), 2)
m.add_transition('advance', 'A', 'B')
self.assertNotEqual(s1.advance, s2.advance)
s1.advance()
self.assertEqual(s1.state, 'B')
self.assertEqual(s2.state, 'A')
def setUp(self):
self.factory = MachineFactory()