Skip to content

Commit

Permalink
Merge 9255271 into 070b450
Browse files Browse the repository at this point in the history
  • Loading branch information
thedrow committed Mar 25, 2020
2 parents 070b450 + 9255271 commit 7d37431
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
18 changes: 18 additions & 0 deletions tests/test_nesting.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from transitions.extensions.nesting import NestedState
from transitions.extensions import MachineFactory
from transitions.core import Enum

from unittest import skipIf
from .test_core import TestTransitions as TestsCore
Expand Down Expand Up @@ -230,6 +231,23 @@ def test_add_nested_state(self):
with self.assertRaises(ValueError):
m.add_state(m.states['A'])

def test_add_nested_enums_as_nested_state(self):
from transitions.extensions.nesting_legacy import HierarchicalMachine
if self.machine_cls is HierarchicalMachine:
self.skipTest("Converting enums to nested states is not supported on the legacy HierarchicalMachine")

class Foo(Enum):
A = 0
B = 1

class Bar(Enum):
FOO = Foo
C = 2

m = self.stuff.machine_cls(states=Bar, initial='A')

self.assertEqual(sorted(m.states['FOO'].states.keys()), ['A', 'B'])

def test_enter_exit_nested_state(self):
State = self.state_cls
mock = MagicMock()
Expand Down
5 changes: 4 additions & 1 deletion transitions/extensions/nesting.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from six import string_types

from ..core import State, Machine, Transition, Event, listify, MachineError, Enum, EventData
from ..core import State, Machine, Transition, Event, listify, MachineError, Enum, EnumMeta, EventData

_LOGGER = logging.getLogger(__name__)
_LOGGER.addHandler(logging.NullHandler())
Expand Down Expand Up @@ -400,6 +400,9 @@ def add_ordered_transitions(self, states=None, trigger='next_state',
def add_states(self, states, on_enter=None, on_exit=None, ignore_invalid_triggers=None, **kwargs):
remap = kwargs.pop('remap', None)
for state in listify(states):
if isinstance(state, Enum) and isinstance(state.value, EnumMeta):
state = {'name': state.name, 'children': state.value}

if isinstance(state, string_types):
if remap is not None and state in remap:
return
Expand Down

0 comments on commit 7d37431

Please sign in to comment.