Skip to content

Commit ea4ed0e

Browse files
committed
simplify get_transitions in HSM; also add tests for it (#479)
1 parent 795b72a commit ea4ed0e

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

tests/test_nesting.py

+23
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,29 @@ def test_get_triggers(self):
425425
self.assertEqual(len(trans), 3)
426426
self.assertTrue('relax' in trans)
427427

428+
def test_get_nested_transitions(self):
429+
seperator = self.state_cls.separator
430+
states = ['A', {'name': 'B', 'children': ['1', '2', {'name': '3', 'children': ['a', 'b'],
431+
'transitions': [['inner', 'a', 'b'],
432+
['inner', 'b', 'a']]}],
433+
'transitions': [['mid', '1', '1'],
434+
['mid', '2', '3'],
435+
['mid', '3', '1'],
436+
['mid2', '2', '3'],
437+
['mid_loop', '1', '1']]}]
438+
transitions = [['outer', 'A', 'B'],
439+
['outer', ['A', 'B'], 'C']]
440+
machine = self.stuff.machine_cls(states=states, transitions=transitions, initial='A', auto_transitions=False)
441+
self.assertEqual(10, len(machine.get_transitions()))
442+
self.assertEqual(2, len(machine.get_transitions(source='A')))
443+
self.assertEqual(2, len(machine.get_transitions('inner')))
444+
self.assertEqual(3, len(machine.get_transitions('mid')))
445+
self.assertEqual(3, len(machine.get_transitions(dest='B{0}1'.format(seperator))))
446+
self.assertEqual(2, len(machine.get_transitions(source='B{0}2'.format(seperator),
447+
dest='B{0}3'.format(seperator))))
448+
self.assertEqual(1, len(machine.get_transitions(source='B{0}3{0}a'.format(seperator),
449+
dest='B{0}3{0}b'.format(seperator))))
450+
428451
def test_internal_transitions(self):
429452
s = self.stuff
430453
s.machine.add_transition('internal', 'A', None, prepare='increase_level')

tests/test_nesting_legacy.py

+3
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ def test_add_nested_state(self):
9494
def test_child_condition_persistence(self):
9595
pass # not supported by legacy machine
9696

97+
def test_get_nested_transitions(self):
98+
pass # not supported by legacy machine
99+
97100

98101
class TestReuseLegacySeparatorDefault(TestReuseSeparatorBase):
99102

transitions/extensions/nesting.py

+1-8
Original file line numberDiff line numberDiff line change
@@ -659,14 +659,7 @@ def get_transitions(self, trigger="", source="*", dest="*"):
659659
with self():
660660
source_path = [] if source == "*" else source.split(self.state_cls.separator)
661661
dest_path = [] if dest == "*" else dest.split(self.state_cls.separator)
662-
if source_path:
663-
transitions = []
664-
while source_path:
665-
transitions.extend(self.get_nested_transitions(trigger, source_path, dest_path))
666-
source_path.pop()
667-
return transitions
668-
else:
669-
return self.get_nested_transitions(trigger, None, dest_path)
662+
return self.get_nested_transitions(trigger, source_path, dest_path)
670663

671664
def get_triggers(self, *args):
672665
""" Extends transitions.core.Machine.get_triggers to also include parent state triggers. """

0 commit comments

Comments
 (0)