# Copyright (C) 2009, Tutorius.org # # 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 import unittest from sugar.tutorius.constraints import * from sugar.tutorius.properties import * # Helper function to test the wrong types on a property, given its type def try_wrong_values(prop): if prop.type != "int": try: prop.set(3) assert False, "Able to insert int value in property of type %s"%prop.type except: pass if prop.type != "float": try: prop.set(1.1) assert False, "Able to insert float value in property of type %s"%prop.type except: pass if prop.type != "string": try: prop.set("Fake string") assert False, "Able to insert string value in property of type %s"%prop.type except: pass if prop.type != "array": try: prop.set([1, 2000, 3, 400]) assert False, "Able to insert array value in property of type %s"%prop.type except: pass if prop.type != "color": try: prop.set([1,2,3]) if prop.type != "array": assert False, "Able to insert color value in property of type %s"%prop.type except: pass if prop.type != "boolean": try: prop.set(True) if prop.type != "boolean": assert False, "Able to set boolean value in property of type %s"%prop.type except: pass class BasePropertyTest(unittest.TestCase): def test_base_class(self): prop = TutoriusProperty() assert prop.value == None, "There should not be an initial value in the base property" assert prop.type == None, "There should be no type associated with the base property" assert prop.get_constraints() == [], "There should be no constraints on the base property" prop.set(2) assert prop.value == 2, "Unable to set a value on base class" class TIntPropertyTest(unittest.TestCase): def test_int_property(self): prop = TIntProperty(22) assert prop.value == 22, "Could not set value on property via constructor" assert prop.type == "int", "Wrong type on int property : %s" % prop.type cons = prop.get_constraints() assert len(cons) == 2, "Not enough constraints on the int property" prop.set(12) assert prop.value == 12, "Could not set value" def test_wrong_values(self): prop = TIntProperty(33) # Try setting values of other types try_wrong_values(prop) def test_limit_constructor(self): prop = TIntProperty(22, 0, 30) try: prop.set(-22) assert False, "Assigning an out-of-range value should trigger LowerLimitConstraint" except LowerLimitConstraintError: pass except Exception: assert False, "Wrong exception triggered by assignation" try: prop.set(222) assert False, "Assigning an out-of-range value should trigger UpperLimitConstraint" except UpperLimitConstraintError: pass except Exception: assert False, "Wrong exception triggered by assignation" def test_failing_constructor(self): try: prop = TIntProperty(100, 0, 20) assert False, "Creation of the property should fail." except UpperLimitConstraintError: pass except: assert False, "Wrong exception type on failed constructor" try: prop = TIntProperty(-100, 0, 20) assert False, "Creation of the property should fail." except LowerLimitConstraintError: pass except: assert False, "Wrong exception type on failed constructor" class TFloatPropertyTest(unittest.TestCase): def test_float_property(self): prop = TFloatProperty(22) assert prop.value == 22, "Could not set value on property via constructor" assert prop.type == "float", "Wrong type on float property : %s" % prop.type cons = prop.get_constraints() assert len(cons) == 2, "Not enough constraints on the float property" prop.set(12) assert prop.value == 12, "Could not set value" def test_wrong_values(self): prop = TFloatProperty(33) # Try setting values of other types try_wrong_values(prop) def test_limit_constructor(self): prop = TFloatProperty(22.4, 0.1, 30.5223) try: prop.set(-22.8) assert False, "Assigning an out-of-range value should trigger LowerLimitConstraint" except LowerLimitConstraintError: pass except Exception: assert False, "Wrong exception triggered by assignation" try: prop.set(222.2) assert False, "Assigning an out-of-range value should trigger UpperLimitConstraint" except UpperLimitConstraintError: pass except Exception: assert False, "Wrong exception triggered by assignation" def test_failing_constructor(self): try: prop = TFloatProperty(100, 0, 20) assert False, "Creation of the property should fail." except UpperLimitConstraintError: pass except: assert False, "Wrong exception type on failed constructor" try: prop = TFloatProperty(-100, 0, 20) assert False, "Creation of the property should fail." except LowerLimitConstraintError: pass except: assert False, "Wrong exception type on failed constructor" class TStringPropertyTest(unittest.TestCase): def test_basic_string(self): prop = TStringProperty("Starter string") assert prop.value == "Starter string", "Could not set string value via constructor" assert prop.type == "string", "Wrong type for string property : %s" % prop.type def test_size_limit(self): prop = TStringProperty("Small", 10) try: prop.set("My string is too big!") assert False, "String should not set to longer than max size" except SizeConstraintError: pass except: assert False, "Wrong exception type thrown" def test_wrong_values(self): prop = TStringProperty("Beginning") try_wrong_values(prop) def test_failing_constructor(self): try: prop = TStringProperty("This is normal", 5) assert False, "Creation of the property should fail." except SizeConstraintError: pass except: assert False, "Wrong exception type on failed constructor" class TArrayPropertyTest(unittest.TestCase): def test_basic_array(self): prop = TArrayProperty([1, 2, 3, 4]) assert prop.value == [1,2,3,4], "Unable to set initial value via constructor" assert prop.type == "array", "Wrong type for array : %s"%prop.type def test_wrong_values(self): prop = TArrayProperty([1,2,3,4,5]) try_wrong_values(prop) def test_size_limit(self): prop = TArrayProperty([1,2], 4) try: prop.set([1,2,4,5,6,7]) assert False, "Size limit constraint was not properly applied" except SizeConstraintError: pass except: assert False, "Wrong type of exception thrown" def test_failing_constructor(self): try: prop = TArrayProperty([100, 0, 20], 2) assert False, "Creation of the property should fail." except SizeConstraintError: pass except: assert False, "Wrong exception type on failed constructor" class TColorPropertyTest(unittest.TestCase): def test_basic_color(self): prop = TColorProperty(20, 40, 60) assert prop.value == [20, 40, 60], "Could not set initial value with constructor" assert prop.type == "color", "Wrong type on color : %s"%prop.type def test_wrong_values(self): prop = TColorProperty(250, 250, 250) try_wrong_values(prop) def test_failing_constructor(self): try: prop = TColorProperty(0, "str", 0) assert False, "Creation of the property should fail." except ColorTypeError: pass except: assert False, "Wrong exception type on failed constructor" class TBooleanPropertyTest(unittest.TestCase): def setUp(self): self.prop = TBooleanProperty(False) def test_basic_boolean(self): assert self.prop.value == False, "Could not set initial value via constructor" self.prop.set(True) assert self.prop.value == True, "Could not change the value via set" self.prop.set(False) assert self.prop.value == False, "Could not change the value via set" def test_wrong_types(self): try_wrong_values(self.prop) def test_failing_constructor(self): try: prop = TBooleanProperty(64) assert False, "Creation of the property should fail with non-boolean value" except BooleanConstraintError: pass except: assert False, "Wrong exception type on failed constructor" class TEnumPropertyTest(unittest.TestCase): def setUp(self): self.prop = TEnumProperty("hello", [1, 2, "hello", "world", True, None]) def test_basic_enum(self): assert self.prop.value == "hello", "Could not set initial value on property" self.prop.set(True) assert self.prop.value, "Could not change the value via set" try: self.prop.set(4) assert False, "Switched to a value outside the enum" except EnumConstraintError: pass def test_wrong_type(self): try_wrong_values(self.prop) class TFilePropertyTest(unittest.TestCase): def setUp(self): self.prop = TFileProperty("propertiestests.py") def test_basic_file(self): assert self.prop.value == "propertiestests.py", "Could not set initial value" self.prop.set("run-tests.py") assert self.prop.value == "run-tests.py", "Could not change value" try: self.prop.set("unknown/file/on/disk.gif") assert False, "An exception should be thrown on unknown file" except FileConstraintError: pass if __name__ == "__main__": unittest.main()