Skip to content

Commit

Permalink
simplify get_transitions in HSM; also add tests for it (#479)
Browse files Browse the repository at this point in the history
  • Loading branch information
aleneum committed Oct 20, 2020
1 parent 795b72a commit ea4ed0e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
23 changes: 23 additions & 0 deletions tests/test_nesting.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,29 @@ def test_get_triggers(self):
self.assertEqual(len(trans), 3)
self.assertTrue('relax' in trans)

def test_get_nested_transitions(self):
seperator = self.state_cls.separator
states = ['A', {'name': 'B', 'children': ['1', '2', {'name': '3', 'children': ['a', 'b'],
'transitions': [['inner', 'a', 'b'],
['inner', 'b', 'a']]}],
'transitions': [['mid', '1', '1'],
['mid', '2', '3'],
['mid', '3', '1'],
['mid2', '2', '3'],
['mid_loop', '1', '1']]}]
transitions = [['outer', 'A', 'B'],
['outer', ['A', 'B'], 'C']]
machine = self.stuff.machine_cls(states=states, transitions=transitions, initial='A', auto_transitions=False)
self.assertEqual(10, len(machine.get_transitions()))
self.assertEqual(2, len(machine.get_transitions(source='A')))
self.assertEqual(2, len(machine.get_transitions('inner')))
self.assertEqual(3, len(machine.get_transitions('mid')))
self.assertEqual(3, len(machine.get_transitions(dest='B{0}1'.format(seperator))))
self.assertEqual(2, len(machine.get_transitions(source='B{0}2'.format(seperator),
dest='B{0}3'.format(seperator))))
self.assertEqual(1, len(machine.get_transitions(source='B{0}3{0}a'.format(seperator),
dest='B{0}3{0}b'.format(seperator))))

def test_internal_transitions(self):
s = self.stuff
s.machine.add_transition('internal', 'A', None, prepare='increase_level')
Expand Down
3 changes: 3 additions & 0 deletions tests/test_nesting_legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ def test_add_nested_state(self):
def test_child_condition_persistence(self):
pass # not supported by legacy machine

def test_get_nested_transitions(self):
pass # not supported by legacy machine


class TestReuseLegacySeparatorDefault(TestReuseSeparatorBase):

Expand Down
9 changes: 1 addition & 8 deletions transitions/extensions/nesting.py
Original file line number Diff line number Diff line change
Expand Up @@ -659,14 +659,7 @@ def get_transitions(self, trigger="", source="*", dest="*"):
with self():
source_path = [] if source == "*" else source.split(self.state_cls.separator)
dest_path = [] if dest == "*" else dest.split(self.state_cls.separator)
if source_path:
transitions = []
while source_path:
transitions.extend(self.get_nested_transitions(trigger, source_path, dest_path))
source_path.pop()
return transitions
else:
return self.get_nested_transitions(trigger, None, dest_path)
return self.get_nested_transitions(trigger, source_path, dest_path)

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

0 comments on commit ea4ed0e

Please sign in to comment.