From f72cb15d47a1844218aed65826dfae9919e8f688 Mon Sep 17 00:00:00 2001 From: mike Date: Tue, 10 Mar 2009 18:56:30 +0000 Subject: TutoriusV1 : Adding a sample usage case tests for FSM --- diff --git a/src/sugar/tutorius/core.py b/src/sugar/tutorius/core.py index f817ba9..a699bbb 100644 --- a/src/sugar/tutorius/core.py +++ b/src/sugar/tutorius/core.py @@ -113,6 +113,8 @@ class State: this state @param tutorial The higher level container of the state """ + self.name = name + self._actions = action_list # Unused for now @@ -257,8 +259,7 @@ class FiniteStateMachine(State): state.set_tutorial(tutorial) else: raise RuntimeWarning(\ - "The FSM %s is already associated with a tutorial."%self.name\ - ) + "The FSM %s is already associated with a tutorial."%self.name) def setup(self): """ @@ -328,6 +329,9 @@ class FiniteStateMachine(State): for action in self.actions: action.undo() + # TODO : It might be nice to have a start() and stop() method for the + # FSM. + #Unused for now ## def verify(self): ## """Verify if the current state passes its tests""" diff --git a/src/sugar/tutorius/tests/coretests.py b/src/sugar/tutorius/tests/coretests.py index ed5a7c0..086c833 100644 --- a/src/sugar/tutorius/tests/coretests.py +++ b/src/sugar/tutorius/tests/coretests.py @@ -76,7 +76,7 @@ class CountAction(Action): class TriggerEventFilter(EventFilter): """ - This event filter can be triggered by simply calling its execute function. + This event filter can be triggered by simply calling its do_callback function. Used to fake events and see the effect on the FSM. """ @@ -93,6 +93,22 @@ class TriggerEventFilter(EventFilter): def _inner_cb(self, event_filter): self.toggle_on_callback = not self.toggle_on_callback +class FakeEventFilter(TriggerEventFilter): + """ + This is a fake event that is connected to the tutorial. + + The difference between this one and the TriggerEventFilter is that the + tutorial's set_state will be called on the callback. + + Do not forget to add the do_callback() function. + """ + def set_tutorial(self, tutorial): + self.tutorial = tutorial + + def _inner_cb(self, event_filter): + self.toggle_on_callback = not self.toggle_on_callback + self.tutorial.set_state(event_filter.get_next_state()) + class OnceWrapperTests(unittest.TestCase): def test_onceaction_toggle(self): """ @@ -192,6 +208,48 @@ class StateTest(unittest.TestCase): except : pass - +class FSMTest(unittest.TestCase): + """ + This class needs to text the interface and functionality of the Finite + State Machine. + """ + + def test_sample_usage(self): + act_init = TrueWhileActiveAction() + act_second = TrueWhileActiveAction() + + event_init = FakeEventFilter("SECOND") + + content = { + "INIT": State("INIT", action_list=[act_init],event_filter_list=[event_init]), + "SECOND": State("SECOND", action_list=[act_second]) + } + + fsm = FiniteStateMachine("SampleUsage", state_dict=content) + + assert fsm is not None, "Unable to create FSM" + + tut = Tutorial("SampleUsageTutorial", fsm) + + tut.attach(None) + event_init.set_tutorial(tut) + + assert fsm.current_state.name == "INIT", "Unable to set state to initial state" + + assert act_init.active, "FSM did not call the state's action DO properly" + + # Trigger the event of the INIT state + event_init.do_callback() + + assert act_init.active == False, "FSM did not teardown INIT properly" + + assert fsm.current_state.name == "SECOND", "FSM did not switch to SECOND state" + + assert act_second.active == True, "FSM did not setup SECOND properly" + + tut.detach() + + assert act_second.active == False, "FSM did not teardown SECOND properly" + if __name__ == "__main__": unittest.main() -- cgit v0.9.1