Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/sugar/tutorius/tests
diff options
context:
space:
mode:
authormike <michael.jmontcalm@gmail.com>2009-03-19 18:14:02 (GMT)
committer mike <michael.jmontcalm@gmail.com>2009-03-19 18:14:02 (GMT)
commit55eb43ce0aef7de69da1f15eec3dfa31f5b8d8f0 (patch)
tree2ab938f8d6dfc855aa238c5032d208e0a25193d1 /src/sugar/tutorius/tests
parent79ffdf4c4c38e0064f25db06685af9156b39e679 (diff)
TutoriusV2 : Adding more tests for the FSM (add_state, find_state,
remove_state)
Diffstat (limited to 'src/sugar/tutorius/tests')
-rw-r--r--src/sugar/tutorius/tests/coretests.py108
1 files changed, 104 insertions, 4 deletions
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()