Skip to content

Commit

Permalink
test proper subclassing for hierarchicalstatemachine (#510)
Browse files Browse the repository at this point in the history
  • Loading branch information
aleneum committed Feb 13, 2021
1 parent a900d7d commit 25ef493
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Bugfix #512: Use `model_attribute` consistently in `AsyncMachine` (thanks @thedrow)
- Testing now treats most warnings as errors (thanks @thedrow)
- As a consequence, `pygraphviz.Agraph` in `diagrams_pygraphviz` are now copied by `transitions` since `AGraph.copy` as of version `1.6` does not close temporary files appropriately
- `HierarchicalMachine` now checks whether `state_cls`, `event_cls` and `transition_cls` have been subclassed from nested base classes (e.g. `NestedState`) to prevent hard to debug inheritance errors

## 0.8.6 (December 2020)

Expand Down
16 changes: 16 additions & 0 deletions tests/test_nesting.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,22 @@ def on_exit_C_1(self):
model.to_C()
self.assertTrue(model.is_C_1())

def test_correct_subclassing(self):
from transitions.core import State

class WrongStateClass(self.machine_cls):
state_cls = State

class MyNestedState(NestedState):
pass

class CorrectStateClass(self.machine_cls):
state_cls = MyNestedState

with self.assertRaises(AssertionError):
m = WrongStateClass()
m = CorrectStateClass()


class TestSeparatorsBase(TestCase):

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 @@ -100,6 +100,9 @@ def test_child_condition_persistence(self):
def test_get_nested_transitions(self):
pass # not supported by legacy machine

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


class TestReuseLegacySeparatorDefault(TestReuseSeparatorBase):

Expand Down
3 changes: 3 additions & 0 deletions transitions/extensions/nesting.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,9 @@ class HierarchicalMachine(Machine):
event_cls = NestedEvent

def __init__(self, *args, **kwargs):
assert issubclass(self.state_cls, NestedState)
assert issubclass(self.event_cls, NestedEvent)
assert issubclass(self.transition_cls, NestedTransition)
self._stack = []
self.scoped = self
_super(HierarchicalMachine, self).__init__(*args, **kwargs)
Expand Down

0 comments on commit 25ef493

Please sign in to comment.