From 92a2332105b8fa693fc37fff585ea4611bee638c Mon Sep 17 00:00:00 2001 From: Jean-Christophe Savard Date: Mon, 30 Mar 2009 17:38:36 +0000 Subject: Core (bug 341760) : TutoSerializer, 2nd version (fonction save_tutorial added) + Tests : added serializertests.py --- (limited to 'src/sugar/tutorius/tests') diff --git a/src/sugar/tutorius/tests/run-tests.py b/src/sugar/tutorius/tests/run-tests.py index 473e083..182d7b2 100755 --- a/src/sugar/tutorius/tests/run-tests.py +++ b/src/sugar/tutorius/tests/run-tests.py @@ -28,6 +28,7 @@ if __name__=='__main__': import gtkutilstests import overlaytests import linear_creatortests + import serializertests suite = unittest.TestSuite() suite.addTests(unittest.findTestCases(coretests)) @@ -35,6 +36,7 @@ if __name__=='__main__': suite.addTests(unittest.findTestCases(gtkutilstests)) suite.addTests(unittest.findTestCases(overlaytests)) suite.addTests(unittest.findTestCases(linear_creatortests)) + suite.addTests(unittest.findTestCases(serializertests)) runner = unittest.TextTestRunner() runner.run(suite) diff --git a/src/sugar/tutorius/tests/serializertests.py b/src/sugar/tutorius/tests/serializertests.py new file mode 100644 index 0000000..7a31602 --- /dev/null +++ b/src/sugar/tutorius/tests/serializertests.py @@ -0,0 +1,121 @@ +# Copyright (C) 2009, Tutorius.org +# Copyright (C) 2009, Jean-Christophe Savard +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +""" +Serialization Tests + +This module contains all the tests that pertain to the usage of the Tutorius +Serializer object. This means testing saving a tutorial dictionary to a .tml +file, loading the list of tutorials for this activity and building chosen +tutorial. +""" + +import unittest + +import logging +import linecache +import os +import cPickle as pickle + +from sugar.tutorius import gtkutils, overlayer +from sugar.tutorius.core import Tutorial, State, FiniteStateMachine +from sugar.tutorius.actions import DialogMessage, OnceWrapper, BubbleMessage +from sugar.tutorius.filters import GtkWidgetEventFilter, TimerEvent +from sugar.tutorius.tutoserialize import TutoSerializer + + +# Helper classes to help testing + + + + +class SerializerTest(unittest.TestCase): + """ + This class has to test the Serializer methods as well as the expected + functionality. + """ + + def test_pickle_integrity(self): + """ + Validates content is uncorrupted trough a pickle file save/load. + """ + + # Sample valid FSM dict + sampleDict = { + "INIT":State("INIT", + action_list=[ + OnceWrapper(BubbleMessage(message="Welcome to the text editor tutorial!\n\n Click on the canvas and type a letter.", pos=[100,100], tailpos=[-10,-20])), + ], + event_filter_list=[ + GtkWidgetEventFilter("TEXT","0.0.0.1.0.0.0","key-press-event"), + TimerEvent("LOST",15), + ], + ), + "LOST":State("LOST", + action_list=[BubbleMessage("Click in the canvas and type on your keyboard", [400, 400]),], + event_filter_list=[ + GtkWidgetEventFilter("TEXT","0.0.0.1.0.0.0","key-press-event"), + TimerEvent("INIT",5), + ], + ), + "TEXT":State("TEXT", + action_list=[OnceWrapper(BubbleMessage(" You can type more letters if you want!\n\n" + + "To proceed to the next step, select your text.\n\n Click and drag over the text!", [200,150])),], + event_filter_list=[ + GtkWidgetEventFilter("SELECTED","0.0.0.1.0.0","text-selected"), + ], + ), + } + + testpath = "/tmp/testdata/" + + # Create testdata/ folder if no exists + if not os.path.exists(testpath): + os.mkdir(testpath) + + serialize = TutoSerializer() + + # Make the class believe the test is in a activity path + os.environ["SUGAR_ACTIVITY_ROOT"] = testpath + + fsm = FiniteStateMachine("Test", state_dict=sampleDict) + + serialize.save_tutorial("Test", "Test", fsm, "serializeTest") + + fileDict = serialize.load_tuto_list() + + for filekey, tutorial in fileDict.items(): + if filekey == "Test": + reformedTuto = serialize.build_tutorial(filekey) + + reformedfsm = reformedTuto.get("Test").state_machine + + #Tests + assert reformedfsm._states.get("INIT").name == fsm._states.get("INIT").name, \ + 'FSM underlying dictionary differ from original to pickled/reformed one' + assert reformedfsm._states.get("LOST").name == fsm._states.get("LOST").name, \ + 'FSM underlying dictionary differ from original to pickled/reformed one' + assert reformedfsm._states.get("TEXT").name == fsm._states.get("TEXT").name, \ + 'FSM underlying dictionary differ from original to pickled/reformed one' + + + os.remove(testpath + "serializeTest.tml") + os.rmdir(testpath) + os.rmdir("/tmp") + + +if __name__ == "__main__": + unittest.main() \ No newline at end of file -- cgit v0.9.1