Skip to content

Commit

Permalink
fix may_trigger for nested and async
Browse files Browse the repository at this point in the history
see #594
  • Loading branch information
aleneum committed May 7, 2024
1 parent c72e801 commit 99d69d2
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 4 deletions.
15 changes: 15 additions & 0 deletions tests/test_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,21 @@ async def run():

asyncio.run(run())

def test_may_transition_internal(self):
states = ['A', 'B', 'C']
d = DummyModel()
_ = self.machine_cls(model=d, states=states, transitions=[["go", "A", "B"], ["wait", "B", None]],
initial='A', auto_transitions=False)

async def run():
assert await d.may_go()
assert not await d.may_wait()
await d.go()
assert not await d.may_go()
assert await d.may_wait()

asyncio.run(run())


@skipIf(asyncio is None or (pgv is None and gv is None), "AsyncGraphMachine requires asyncio and (py)gaphviz")
class TestAsyncGraphMachine(TestAsync):
Expand Down
3 changes: 3 additions & 0 deletions tests/test_nesting.py
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,7 @@ def test_machine_may_transitions(self):
transitions = [
{'trigger': 'walk', 'source': 'A', 'dest': 'B'},
{'trigger': 'run', 'source': 'B', 'dest': 'C'},
{'trigger': 'wait', 'source': 'B', 'dest': None},
{'trigger': 'run_fast', 'source': 'C', 'dest': 'C{0}1'.format(self.separator)},
{'trigger': 'sprint', 'source': 'C', 'dest': 'D'}
]
Expand All @@ -920,11 +921,13 @@ def test_machine_may_transitions(self):
assert not m.may_run()
assert not m.may_run_fast()
assert not m.may_sprint()
assert not m.may_wait()

m.walk()
assert not m.may_walk()
assert m.may_run()
assert not m.may_run_fast()
assert m.may_wait()

m.run()
assert m.may_run_fast()
Expand Down
2 changes: 1 addition & 1 deletion transitions/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,7 @@ def _can_trigger(self, model, trigger, *args, **kwargs):
continue
for transition in self.events[trigger_name].transitions[state]:
try:
_ = transition.source if transition.dest is None else self.get_state(transition.dest)
_ = self.get_state(transition.dest) if transition.dest is not None else transition.source
except ValueError:
continue

Expand Down
4 changes: 2 additions & 2 deletions transitions/extensions/asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ async def _can_trigger(self, model, trigger, *args, **kwargs):
continue
for transition in self.events[trigger_name].transitions[state]:
try:
_ = self.get_state(transition.dest)
_ = self.get_state(transition.dest) if transition.dest is not None else transition.source
except ValueError:
continue
await self.callbacks(self.prepare_event, evt)
Expand Down Expand Up @@ -559,7 +559,7 @@ async def _can_trigger_nested(self, model, trigger, path, *args, **kwargs):
state_name = self.state_cls.separator.join(source_path)
for transition in self.events[trigger].transitions.get(state_name, []):
try:
_ = self.get_state(transition.dest)
_ = self.get_state(transition.dest) if transition.dest is not None else transition.source
except ValueError:
continue
await self.callbacks(self.prepare_event, evt)
Expand Down
2 changes: 1 addition & 1 deletion transitions/extensions/nesting.py
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ def _can_trigger_nested(self, model, trigger, path, *args, **kwargs):
state_name = self.state_cls.separator.join(source_path)
for transition in self.events[trigger].transitions.get(state_name, []):
try:
_ = self.get_state(transition.dest)
_ = self.get_state(transition.dest) if transition.dest is not None else transition.source
except ValueError:
continue
self.callbacks(self.prepare_event, evt)
Expand Down

0 comments on commit 99d69d2

Please sign in to comment.