From 79ffdf4c4c38e0064f25db06685af9156b39e679 Mon Sep 17 00:00:00 2001 From: mike Date: Wed, 18 Mar 2009 19:04:33 +0000 Subject: TutoriusV2 : Correcting constructors for States and FSMs, exploration tests --- (limited to 'src/sugar/tutorius/tests/coretests.py') diff --git a/src/sugar/tutorius/tests/coretests.py b/src/sugar/tutorius/tests/coretests.py index 086c833..297a7c3 100644 --- a/src/sugar/tutorius/tests/coretests.py +++ b/src/sugar/tutorius/tests/coretests.py @@ -100,7 +100,7 @@ class FakeEventFilter(TriggerEventFilter): 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. + Do not forget to add the do_callback() after creating the object. """ def set_tutorial(self, tutorial): self.tutorial = tutorial @@ -251,5 +251,59 @@ class FSMTest(unittest.TestCase): assert act_second.active == False, "FSM did not teardown SECOND properly" +class FSMExplorationTests(unittest.TestCase): + def buildFSM(self): + """ + Create a sample FSM to play with in the rest of the tests. + """ + st1 = State("INIT") + st1.add_action(CountAction()) + st1.add_event_filter(TriggerEventFilter("Second")) + st1.add_event_filter(TriggerEventFilter("Third")) + + st2 = State("Second") + st2.add_action(TrueWhileActiveAction()) + st2.add_event_filter(TriggerEventFilter("Third")) + st2.add_event_filter(TriggerEventFilter("Fourth")) + + st3 = State("Third") + st3.add_action(CountAction()) + st3.add_action(TrueWhileActiveAction()) + + self.fsm = FiniteStateMachine("ExplorationTestingMachine") + self.fsm.add_state(st1) + self.fsm.add_state(st2) + self.fsm.add_state(st3) + + def validate_following_states(self, in_name, out_name_list): + nextStates = self.fsm.get_following_states(in_name) + assert list(nextStates).sort() == list(out_name_list).sort(), \ + "The following states for %s are wrong : got %s"%\ + (in_name, str(nextStates)) + + def validate_previous_states(self, in_name, out_name_list): + prevStates = self.fsm.get_previous_states(in_name) + assert list(prevStates).sort() == list(out_name_list).sort(), \ + "The following states for %s are wrong : got %s"%\ + (in_name, str(prevStates)) + + def test_get_following_states(self): + self.buildFSM() + self.validate_following_states("INIT", ('Second', 'Third')) + + self.validate_following_states("Second", ("Third", "Fourth")) + + self.validate_following_states("Third", ()) + + def test_get_previous_states(self): + self.buildFSM() + self.validate_previous_states("INIT", ()) + + self.validate_previous_states("Second", ("INIT")) + + self.validate_previous_states("Third", ("INIT", "Second")) + + self.validate_previous_states("Fourth", ("Second")) + if __name__ == "__main__": unittest.main() -- cgit v0.9.1