Skip to content

Commit

Permalink
test legacy reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
aleneum committed Mar 18, 2020
1 parent 70f42eb commit f59c595
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 16 deletions.
60 changes: 58 additions & 2 deletions tests/test_nesting_legacy.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
from .test_nesting import TestTransitions, Stuff
from transitions.extensions.nesting_legacy import HierarchicalMachine, NestedState
from .test_reuse import TestTransitions as TestReuse
from transitions.extensions.nesting_legacy import HierarchicalMachine

try:
from unittest.mock import MagicMock
except ImportError:
from mock import MagicMock


class TestNestedLegacy(TestTransitions):
Expand All @@ -8,7 +14,7 @@ def setUp(self):
super(TestNestedLegacy, self).setUp()
self.machine_cls = HierarchicalMachine
self.stuff = Stuff(self.states, self.machine_cls)
self.state_cls = NestedState
self.state_cls = self.machine_cls.state_cls

def test_add_custom_state(self):
s = self.stuff
Expand Down Expand Up @@ -51,3 +57,53 @@ def test_transitioning(self):

def test_nested_definitions(self):
pass


class TestReuseLegacy(TestReuse):

def setUp(self):
super(TestReuseLegacy, self).setUp()
self.machine_cls = HierarchicalMachine
self.stuff = Stuff(self.states, self.machine_cls)
self.state_cls = self.machine_cls.state_cls

def test_reuse_self_reference(self):
separator = self.state_cls.separator

class Nested(self.machine_cls):

def __init__(self, parent):
self.parent = parent
self.mock = MagicMock()
states = ['1', '2']
transitions = [{'trigger': 'finish', 'source': '*', 'dest': '2', 'after': self.print_msg}]
super(Nested, self).__init__(states=states, transitions=transitions, initial='1')

def print_msg(self):
self.mock()
self.parent.print_top()

class Top(self.machine_cls):

def print_msg(self):
self.mock()

def __init__(self):
self.nested = Nested(self)
self.mock = MagicMock()

states = ['A', {'name': 'B', 'children': self.nested}]
transitions = [dict(trigger='print_top', source='*', dest='=', after=self.print_msg),
dict(trigger='to_nested', source='*', dest='B{0}1'.format(separator))]

super(Top, self).__init__(states=states, transitions=transitions, initial='A')

top_machine = Top()
self.assertEqual(top_machine, top_machine.nested.parent)

top_machine.to_nested()
top_machine.finish()
self.assertTrue(top_machine.mock.called)
self.assertTrue(top_machine.nested.mock.called)
self.assertIsNot(top_machine.nested.get_state('2').on_enter,
top_machine.get_state('B{0}2'.format(separator)).on_enter)
28 changes: 14 additions & 14 deletions tests/test_reuse.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

from transitions import MachineError
from transitions.extensions import MachineFactory
from transitions.extensions.nesting import NestedState as State
from .utils import Stuff
from .test_nesting import default_separator

from unittest import TestCase

Expand All @@ -15,22 +15,19 @@
except ImportError:
from mock import MagicMock

nested_separator = State.separator


class TestTransitions(TestCase):

def setUp(self):
states = ['A', 'B',
{'name': 'C', 'children': ['1', '2', {'name': '3', 'children': ['a', 'b', 'c']}]},
'D', 'E', 'F']
self.states = ['A', 'B',
{'name': 'C', 'children': ['1', '2', {'name': '3', 'children': ['a', 'b', 'c']}]},
'D', 'E', 'F']
self.machine_cls = MachineFactory.get_predefined(nested=True)
self.stuff = Stuff(states, self.machine_cls)

def tearDown(self):
pass
self.state_cls = self.machine_cls.state_cls
self.stuff = Stuff(self.states, self.machine_cls)

def test_blueprint_reuse(self):
State = self.state_cls
states = ['1', '2', '3']
transitions = [
{'trigger': 'increase', 'source': '1', 'dest': '2'},
Expand Down Expand Up @@ -82,6 +79,7 @@ def test_blueprint_initial_false(self):
self.assertEqual(parent.state, 'c')

def test_blueprint_remap(self):
State = self.state_cls
states = ['1', '2', '3', 'finished']
transitions = [
{'trigger': 'increase', 'source': '1', 'dest': '2'},
Expand Down Expand Up @@ -157,12 +155,13 @@ def test_wrong_nesting(self):
m.to_B.A()

def test_custom_separator(self):
State.separator = '.'
self.state_cls.separator = '.'
self.tearDown()
self.setUp()
self.test_wrong_nesting()

def test_example_reuse(self):
State = self.state_cls
count_states = ['1', '2', '3', 'done']
count_trans = [
['increase', '1', '2'],
Expand Down Expand Up @@ -240,11 +239,12 @@ def preparation(self):

m_model = Model()
m = self.machine_cls(m_model, states=["A", "B", {"name": "NEST", "children": ms}])
m_model.to('NEST%sC' % State.separator)
m_model.to('NEST%sC' % self.state_cls.separator)
m_model.go()
self.assertTrue(m_model.prepared)

def test_reuse_self_reference(self):
separator = self.state_cls.separator

class Nested(self.machine_cls):

Expand All @@ -270,7 +270,7 @@ def __init__(self):

states = ['A', {'name': 'B', 'children': self.nested}]
transitions = [dict(trigger='print_top', source='*', dest='=', after=self.print_msg),
dict(trigger='to_nested', source='*', dest='B{0}1'.format(State.separator))]
dict(trigger='to_nested', source='*', dest='B{0}1'.format(separator))]

super(Top, self).__init__(states=states, transitions=transitions, initial='A')

Expand All @@ -282,4 +282,4 @@ def __init__(self):
self.assertTrue(top_machine.mock.called)
self.assertTrue(top_machine.nested.mock.called)
self.assertIs(top_machine.nested.get_state('2').on_enter,
top_machine.get_state('B{0}2'.format(State.separator)).on_enter)
top_machine.get_state('B{0}2'.format(separator)).on_enter)

0 comments on commit f59c595

Please sign in to comment.