Skip to content

Commit

Permalink
History's memory cannot target itself
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreDecan committed Jan 21, 2016
1 parent 635dc26 commit 32dcdc2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
4 changes: 3 additions & 1 deletion sismic/model/statechart.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,13 +514,15 @@ def _validate_compoundstate_initial(self):

def _validate_historystate_memory(self):
"""
Checks that every *HistoryStateMixin*'s memory refer to one of its parent's children
Checks that every *HistoryStateMixin*'s memory refer to one of its parent's children, except itself.
:return: True
:raise StatechartError:
"""
for name, state in self._states.items():
if isinstance(state, HistoryStateMixin) and state.memory:
if state.memory == state.name:
raise StatechartError('Initial memory {} of {} cannot target itself'.format(state.memory, state))
if state.memory not in self._states:
raise StatechartError('Initial memory {} of {} does not exist'.format(state.memory, state))
if state.memory not in self.children_for(self.parent_for(name)):
Expand Down
34 changes: 24 additions & 10 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,27 +109,41 @@ def test_history_not_in_compound(self):

class ValidateTests(unittest.TestCase):
# REMARK: "Positive" tests are already done during io.import_from_yaml!
def test_history_memory(self):
def test_history_memory_not_child(self):
statechart = io.import_from_yaml(open('tests/yaml/history.yaml'))

statechart.state_for('loop.H').memory = 'pause'
with self.assertRaises(exceptions.StatechartError):
statechart._validate_historystate_memory()

def test_history_memory_unknown(self):
statechart = io.import_from_yaml(open('tests/yaml/history.yaml'))
memory = statechart.state_for('loop.H').memory

statechart.state_for('loop.H').memory = 'unknown'
with self.assertRaises(exceptions.StatechartError) as cm:
with self.assertRaises(exceptions.StatechartError):
statechart._validate_historystate_memory()

statechart.state_for('loop.H').memory = memory
statechart._validate_historystate_memory()
def test_history_memory_self(self):
statechart = io.import_from_yaml(open('tests/yaml/history.yaml'))

statechart.state_for('loop.H').memory = 'loop.H'
with self.assertRaises(exceptions.StatechartError):
statechart._validate_historystate_memory()

def test_compound_initial(self):
def test_compound_initial_unknown(self):
statechart = io.import_from_yaml(open('tests/yaml/composite.yaml'))
initial = statechart.state_for('s1b').initial

statechart.state_for('s1b').initial = 'unknown'
with self.assertRaises(exceptions.StatechartError) as cm:
with self.assertRaises(exceptions.StatechartError):
statechart._validate_compoundstate_initial()

def test_compound_initial_not_child(self):
statechart = io.import_from_yaml(open('tests/yaml/composite.yaml'))

statechart.state_for('s1b').initial = 's1'
with self.assertRaises(exceptions.StatechartError):
statechart._validate_compoundstate_initial()

statechart.state_for('s1b').initial = initial
statechart._validate_compoundstate_initial()


class TransitionsTests(unittest.TestCase):
Expand Down

0 comments on commit 32dcdc2

Please sign in to comment.