Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/tests/propertiestests.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/propertiestests.py')
-rw-r--r--tests/propertiestests.py402
1 files changed, 402 insertions, 0 deletions
diff --git a/tests/propertiestests.py b/tests/propertiestests.py
new file mode 100644
index 0000000..46346c4
--- /dev/null
+++ b/tests/propertiestests.py
@@ -0,0 +1,402 @@
+# 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(obj):
+ typ = type(obj).prop.type
+ if typ != "int":
+ try:
+ obj.prop = 3
+ assert False, "Able to insert int value in property of type %s"%typ
+ except:
+ pass
+
+ if typ != "float":
+ try:
+ obj.prop = 1.1
+ assert False, "Able to insert float value in property of type %s"%typ
+ except:
+ pass
+
+ if typ != "string":
+ try:
+ obj.prop = "Fake string"
+ assert False, "Able to insert string value in property of type %s"%typ
+ except:
+ pass
+
+ if typ != "array":
+ try:
+ obj.prop = [1, 2000, 3, 400]
+ assert False, "Able to insert array value in property of type %s"%typ
+ except:
+ pass
+
+ if typ != "color":
+ try:
+ obj.prop = [1,2,3]
+ if typ != "array":
+ assert False, "Able to insert color value in property of type %s"%typ
+ except:
+ pass
+
+ if typ != "boolean":
+ try:
+ obj.prop = True
+ if typ != "boolean":
+ assert False, "Able to set boolean value in property of type %s"%typ
+ except:
+ pass
+
+class BasePropertyTest(unittest.TestCase):
+ def test_base_class(self):
+ class klass(TPropContainer):
+ prop = TutoriusProperty()
+ obj = klass()
+
+ assert klass.prop.default == None, "There should not be an initial value in the base property"
+
+ assert klass.prop.type == None, "There should be no type associated with the base property"
+
+ assert klass.prop.get_constraints() == [], "There should be no constraints on the base property"
+
+ obj.prop = 2
+
+ assert obj.prop == 2, "Unable to set a value on base class"
+
+class TIntPropertyTest(unittest.TestCase):
+ def test_int_property(self):
+ class klass(TPropContainer):
+ prop = TIntProperty(22)
+ obj = klass()
+
+ assert obj.prop == 22, "Could not set value on property via constructor"
+
+ assert klass.prop.type == "int", "Wrong type on int property : %s" % prop.type
+ cons = klass.prop.get_constraints()
+ assert len(cons) == 2, "Not enough constraints on the int property"
+
+ obj.prop = 12
+
+ assert obj.prop == 12, "Could not set value"
+
+ def test_wrong_values(self):
+ class klass(TPropContainer):
+ prop = TIntProperty(33)
+ obj = klass()
+
+ # Try setting values of other types
+ try_wrong_values(obj)
+
+ def test_limit_constructor(self):
+ class klass(TPropContainer):
+ prop = TIntProperty(22, 0, 30)
+ obj = klass()
+
+ try:
+ obj.prop = -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:
+ obj.prop = 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):
+ class klass(TPropContainer):
+ prop = TFloatProperty(22)
+ obj = klass()
+
+ assert obj.prop == 22, "Could not set value on property via constructor"
+
+ assert klass.prop.type == "float", "Wrong type on float property : %s" % klass.prop.type
+ cons = klass.prop.get_constraints()
+ assert len(cons) == 2, "Not enough constraints on the float property"
+
+ obj.prop = 12
+
+ assert obj.prop == 12, "Could not set value"
+
+ def test_wrong_values(self):
+ class klass(TPropContainer):
+ prop = TFloatProperty(33)
+ obj = klass()
+
+ # Try setting values of other types
+ try_wrong_values(obj)
+
+ def test_limit_constructor(self):
+ class klass(TPropContainer):
+ prop = TFloatProperty(22.4, 0.1, 30.5223)
+ obj = klass()
+
+ try:
+ obj.prop = -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:
+ obj.prop = 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):
+ class klass(TPropContainer):
+ prop = TStringProperty("Starter string")
+ obj = klass()
+
+ assert obj.prop == "Starter string", "Could not set string value via constructor"
+
+ assert klass.prop.type == "string", "Wrong type for string property : %s" % klass.prop.type
+
+ def test_size_limit(self):
+ class klass(TPropContainer):
+ prop = TStringProperty("Small", 10)
+ obj = klass()
+
+ try:
+ obj.prop = "My string is too big!"
+ assert False, "String should not set to longer than max size"
+ except MaxSizeConstraintError:
+ pass
+ except:
+ assert False, "Wrong exception type thrown"
+
+ def test_wrong_values(self):
+ class klass(TPropContainer):
+ prop = TStringProperty("Beginning")
+ obj = klass()
+
+ try_wrong_values(obj)
+
+ def test_failing_constructor(self):
+ try:
+ prop = TStringProperty("This is normal", 5)
+ assert False, "Creation of the property should fail."
+ except MaxSizeConstraintError:
+ pass
+ except:
+ assert False, "Wrong exception type on failed constructor"
+
+class TArrayPropertyTest(unittest.TestCase):
+ def test_basic_array(self):
+ class klass(TPropContainer):
+ prop = TArrayProperty([1, 2, 3, 4])
+ obj = klass()
+
+ assert obj.prop == [1,2,3,4], "Unable to set initial value via constructor"
+
+ assert klass.prop.type == "array", "Wrong type for array : %s"%klass.prop.type
+
+ def test_wrong_values(self):
+ class klass(TPropContainer):
+ prop = TArrayProperty([1,2,3,4,5])
+ obj = klass()
+
+ try_wrong_values(obj)
+
+ def test_size_limits(self):
+ class klass(TPropContainer):
+ prop = TArrayProperty([1,2], None, 4)
+ obj = klass()
+
+ try:
+ obj.prop = [1,2,4,5,6,7]
+ assert False, "Maximum size limit constraint was not properly applied"
+ except MaxSizeConstraintError:
+ pass
+
+ class klass(TPropContainer):
+ prop = TArrayProperty([1,2,3,4], 2)
+ obj = klass()
+
+ try:
+ obj.prop = [1]
+ assert False, "Minimum size limit constraint was not properly applied"
+ except MinSizeConstraintError:
+ pass
+
+ def test_failing_constructor(self):
+ try:
+ prop = TArrayProperty([100, 0, 20], None, 2)
+ assert False, "Creation of the property should fail."
+ except MaxSizeConstraintError:
+ pass
+ try:
+ prop = TArrayProperty([100, 0, 20], 4, None)
+ assert False, "Creation of the property should fail."
+ except MinSizeConstraintError:
+ pass
+
+class TColorPropertyTest(unittest.TestCase):
+ def test_basic_color(self):
+ class klass(TPropContainer):
+ prop = TColorProperty(20, 40, 60)
+ obj = klass()
+
+ assert obj.prop == [20, 40, 60], "Could not set initial value with constructor"
+
+ assert klass.prop.type == "color", "Wrong type on color : %s"%klass.prop.type
+
+ def test_wrong_values(self):
+ class klass(TPropContainer):
+ prop = TColorProperty(250, 250, 250)
+ obj = klass()
+
+ try_wrong_values(obj)
+
+ 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):
+ class klass(TPropContainer):
+ prop = TBooleanProperty(False)
+ self.obj = klass()
+
+ def test_basic_boolean(self):
+ assert self.obj.prop == False, "Could not set initial value via constructor"
+
+ assert self.obj.__class__.prop.type == "boolean", "Wrong type for TBooleanProperty : %s"%self.obj.__class__.prop.type
+
+ self.obj.prop = True
+
+ assert self.obj.prop == True, "Could not change the value via set"
+
+ self.obj.prop = False
+
+ assert self.obj.prop == False, "Could not change the value via set"
+
+ def test_wrong_types(self):
+ try_wrong_values(self.obj)
+
+ 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):
+ class klass(TPropContainer):
+ prop = TEnumProperty("hello", [1, 2, "hello", "world", True, None])
+ self.obj = klass()
+
+ def test_basic_enum(self):
+ assert self.obj.prop == "hello", "Could not set initial value on property"
+
+ assert type(self.obj).prop.type == "enum", "Wrong type for TEnumProperty : %s"%type(self.obj).prop.type
+
+ self.obj.prop = True
+
+ assert self.obj.prop, "Could not change the value via set"
+
+ try:
+ self.obj.prop = 4
+ assert False, "Switched to a value outside the enum"
+ except EnumConstraintError:
+ pass
+
+ def test_wrong_type(self):
+ try_wrong_values(self.obj)
+
+class TFilePropertyTest(unittest.TestCase):
+ def setUp(self):
+ class klass(TPropContainer):
+ prop = TFileProperty("propertiestests.py")
+ self.obj = klass()
+
+ def test_basic_file(self):
+ assert self.obj.prop == "propertiestests.py", "Could not set initial value"
+
+ assert type(self.obj).prop.type == "file", "Wrong type for TFileProperty : %s"%type(self.obj).prop.type
+
+ self.obj.prop = "run-tests.py"
+
+ assert self.obj.prop == "run-tests.py", "Could not change value"
+
+ try:
+ self.obj.prop = "unknown/file/on/disk.gif"
+ assert False, "An exception should be thrown on unknown file"
+ except FileConstraintError:
+ pass
+
+if __name__ == "__main__":
+ unittest.main()
+