From 89656ae9070ae5937379d3410e69fd6712affd55 Mon Sep 17 00:00:00 2001 From: Joshua Harlow Date: Wed, 24 Jun 2015 14:23:11 -0700 Subject: [PATCH] Disallow adding transitions from terminal states Terminal states are by there very nature terminal and can not have transitions to other states on any kind of event so on add make sure we disallow those types of transitions to even be created in the first place. Change-Id: I70321e9482a0d053a8cb0bc89a766b5fbc82f69e --- automaton/machines.py | 4 ++++ automaton/tests/test_fsm.py | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/automaton/machines.py b/automaton/machines.py index 2c5bc76..5a75010 100644 --- a/automaton/machines.py +++ b/automaton/machines.py @@ -190,6 +190,10 @@ class FiniteMachine(object): raise excp.NotFound("Can not add a transition on event '%s' that" " ends in a undefined state '%s'" % (event, end)) + if self._states[start]['terminal']: + raise excp.InvalidState("Can not add a transition on event '%s'" + " that starts in the terminal state '%s'" + % (event, start)) self._transitions[start][event] = _Jump(end, self._states[end]['on_enter'], self._states[start]['on_exit']) diff --git a/automaton/tests/test_fsm.py b/automaton/tests/test_fsm.py index 0b7e43f..4b4f7d4 100644 --- a/automaton/tests/test_fsm.py +++ b/automaton/tests/test_fsm.py @@ -60,6 +60,12 @@ class FSMTest(testcase.TestCase): m.add_state('unknown') self.assertIn('unknown', m) + def test_no_add_transition_terminal(self): + m = self._create_fsm('up') + m.add_state('down', terminal=True) + self.assertRaises(excp.InvalidState, + m.add_transition, 'down', 'up', 'jump') + def test_duplicate_state(self): m = self._create_fsm('unknown') self.assertRaises(excp.Duplicate, m.add_state, 'unknown')