From aefa4024904ec92034d108d94768a388e8f1a9f6 Mon Sep 17 00:00:00 2001 From: mike Date: Thu, 26 Mar 2009 20:39:40 +0000 Subject: TutoriusV2 : Added test for set_state with same name twice - corrected core behavior in this case --- (limited to 'src/sugar/tutorius') diff --git a/src/sugar/tutorius/core.py b/src/sugar/tutorius/core.py index 901820f..4d53684 100644 --- a/src/sugar/tutorius/core.py +++ b/src/sugar/tutorius/core.py @@ -277,11 +277,8 @@ class FiniteStateMachine(State): self._states = state_dict or {} self.start_state_name = start_state_name - # If we have a filled input dictionary - if len(self._states) > 0: - self.current_state = self._states[self.start_state_name] - else: - self.current_state = None + # Set the current state to None - we are not executing anything yet + self.current_state = None # Register the actions for the FSM - They will be processed at the # FSM level, meaning that when the FSM will start, it will first @@ -357,6 +354,13 @@ class FiniteStateMachine(State): # state by that name - we must ignore this state change request as # it will be done elsewhere in the hierarchy (or it's just bogus). return + + if self.current_state != None: + if new_state_name == self.current_state.name: + # If we already are in this state, we do not need to change + # anything in the current state - By design, a state may not point + # to itself + return new_state = self._states[new_state_name] diff --git a/src/sugar/tutorius/tests/coretests.py b/src/sugar/tutorius/tests/coretests.py index f9125ce..1d864f9 100644 --- a/src/sugar/tutorius/tests/coretests.py +++ b/src/sugar/tutorius/tests/coretests.py @@ -18,7 +18,7 @@ Core Tests This module contains all the tests that pertain to the usage of the Tutorius -Core. This means that the the Finite State Machine, States and all the +Core. This means that the Event Filters, the Finite State Machine and all the related elements and interfaces are tested here. Usage of actions and event filters is tested, but not the concrete actions @@ -29,7 +29,8 @@ and event filters. Those are in their separate test module import unittest import logging -from sugar.tutorius.actions import Action +import sugar.activity +from sugar.tutorius.actions import Action, OnceWrapper from sugar.tutorius.core import * from sugar.tutorius.filters import * @@ -49,6 +50,14 @@ class SimpleTutorial(Tutorial): def set_state(self, name): self.current_state_name = name +class TutorialWithFSM(Tutorial): + """ + Fake tutorial, but associated with a FSM. + """ + def __init__(self, start_name="INIT", fsm=None): + Tutorial.__init__(self, start_name, fsm) + self.activity = activity.Activity() + class TrueWhileActiveAction(Action): """ This action's active member is set to True after a do and to False after @@ -356,7 +365,6 @@ class FSMTest(unittest.TestCase): except Exception: assert False, "Removing a non-existing state dit not throw the right kind of exception" - # Now try removing the second state fsm.remove_state("second") @@ -375,6 +383,33 @@ class FSMTest(unittest.TestCase): assert "second" not in fsm.get_following_states("third"),\ "The link to second from third still exists after removal" + def test_set_same_state(self): + fsm = FiniteStateMachine("Set same state") + + st1 = State("INIT") + st1.add_action(CountAction()) + + fsm.add_state(st1) + + tut = SimpleTutorial() + + fsm.set_tutorial(tut) + + fsm.set_state("INIT") + + assert fsm.get_state_by_name("INIT").get_action_list()[0].do_count == 1, \ + "The action was not triggered on 'INIT'" + + fsm.set_state("INIT") + + do_count = fsm.get_state_by_name("INIT").get_action_list()[0].do_count + assert fsm.get_state_by_name("INIT").get_action_list()[0].do_count == 1, \ + "The action was triggered a second time, do_count = %d"%do_count + + undo_count = fsm.get_state_by_name("INIT").get_action_list()[0].undo_count + assert fsm.get_state_by_name("INIT").get_action_list()[0].undo_count == 0,\ + "The action has been undone unappropriately, undo_count = %d"%undo_count + class FSMExplorationTests(unittest.TestCase): def setUp(self): self.buildFSM() @@ -429,6 +464,7 @@ class FSMExplorationTests(unittest.TestCase): self.validate_previous_states("Third", ("INIT", "Second")) self.validate_previous_states("Fourth", ("Second")) + if __name__ == "__main__": unittest.main() -- cgit v0.9.1