From 55eb43ce0aef7de69da1f15eec3dfa31f5b8d8f0 Mon Sep 17 00:00:00 2001 From: mike Date: Thu, 19 Mar 2009 18:14:02 +0000 Subject: TutoriusV2 : Adding more tests for the FSM (add_state, find_state, remove_state) --- (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 297a7c3..6b86cb9 100644 --- a/src/sugar/tutorius/tests/coretests.py +++ b/src/sugar/tutorius/tests/coretests.py @@ -250,8 +250,110 @@ class FSMTest(unittest.TestCase): tut.detach() assert act_second.active == False, "FSM did not teardown SECOND properly" - + + + def test_state_insert(self): + """ + This is a simple test to insert, then find a state. + """ + st1 = State("FakeState") + + fsm = FiniteStateMachine("StateInsertTest") + + fsm.add_state(st1) + + inserted_state = fsm.get_state_by_name(st1.name) + + assert inserted_state is st1, "Inserting, then fetching a state did not work" + + # Make sure we cannot insert it twice + try : + fsm.add_state(st1) + assert False, "No error raised on addition of an already present state" + except KeyError: + pass + + def test_state_find_by_name(self): + """ + Tests the interface for fetching a state by name. + - Basic functionnality + - Non-existent state + """ + + st1 = State("INIT") + + st2 = State("second") + + fsm = FiniteStateMachine("StateFindTest") + + fsm.add_state(st1) + fsm.add_state(st2) + + # Test the fetch by name + fetched_st1 = fsm.get_state_by_name(st1.name) + + assert fetched_st1 is st1, "Fetched state is not the same as the inserted one" + + fetched_st2 = fsm.get_state_by_name(st2.name) + + assert fetched_st2 is st2, "Fetched state is not the same as the inserted one" + + try: + fsm.get_state_by_name("no such state") + assert False, "Did not get a KeyError on non-existing key search" + except KeyError: + pass + except Exception: + assert False, "Did not get the right error on non-existing key search" + + def test_state_removal(self): + """ + This test removes a state from the FSM. It also verifies that the links + from other states going into the removed state are gone. + """ + st1 = State("INIT", event_filter_list=[TriggerEventFilter("second")]) + st2 = State("second", event_filter_list=[TriggerEventFilter("third")]) + st3 = State("third", event_filter_list=[TriggerEventFilter("second")]) + + fsm = FiniteStateMachine("StateRemovalTest") + + fsm.add_state(st1) + fsm.add_state(st2) + fsm.add_state(st3) + + # First tests - Removing a non-existing state and make sure we get a + # KeyError + try: + fsm.remove_state("Non-existing") + assert False, "Removing a non-existing state did not throw a KeyError" + except KeyError: + pass + 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") + + # Make sure it cannot be fetched + try : + fetched_state = fsm.get_state_by_name("second") + assert False, "The supposedly removed state is still present in the FSM" + except KeyError: + pass + + # Make sure that there is no link to the removed state in the rest + # of the FSM + assert "second" not in fsm.get_following_states("INIT"),\ + "The link to second from INIT still exists after removal" + + assert "second" not in fsm.get_following_states("third"),\ + "The link to second from third still exists after removal" + class FSMExplorationTests(unittest.TestCase): + def setUp(self): + self.buildFSM() + def buildFSM(self): """ Create a sample FSM to play with in the rest of the tests. @@ -288,7 +390,6 @@ class FSMExplorationTests(unittest.TestCase): (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")) @@ -296,7 +397,6 @@ class FSMExplorationTests(unittest.TestCase): self.validate_following_states("Third", ()) def test_get_previous_states(self): - self.buildFSM() self.validate_previous_states("INIT", ()) self.validate_previous_states("Second", ("INIT")) @@ -304,6 +404,6 @@ 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