Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authormike <michael.jmontcalm@gmail.com>2009-12-04 22:25:14 (GMT)
committer mike <michael.jmontcalm@gmail.com>2009-12-04 22:27:21 (GMT)
commit9500e288397de5710151f32ced2e9a2903766a2e (patch)
tree17dd1765f9acf148faae4f70f6523e245ae5b463 /tests
parent9a44da4488a0ff00150eb5cb114f74ba560b96a6 (diff)
Boy scout : Fixing tests for properties' values
(cherry picked from commit 9f985564d6ade807a9182fcb7411388ed863e311)
Diffstat (limited to 'tests')
-rw-r--r--tests/constraintstests.py107
-rw-r--r--tests/propertiestests.py94
2 files changed, 177 insertions, 24 deletions
diff --git a/tests/constraintstests.py b/tests/constraintstests.py
index a5ccf26..f4318f8 100644
--- a/tests/constraintstests.py
+++ b/tests/constraintstests.py
@@ -36,6 +36,113 @@ class ValueConstraintTest(unittest.TestCase):
assert cons.limit == 12
+def try_good_values(constraint, type):
+ if type == "int":
+ constraint.validate(1)
+ if type == "float":
+ constraint.validate(1.2)
+ if type == "str":
+ constraint.validate("This is a string")
+ if type == "tuple":
+ constraint.validate((1,2.0,'3',[4], (5,)))
+ if type == "list":
+ constraint.validate([1,2.0,'3',[4],(5,)])
+ if type == "bool":
+ constraint.validate(True)
+ constraint.validate(False)
+
+def try_wrong_values(testcase, constraint, type):
+ if type != "int":
+ testcase.assertRaises(TypeConstraintError, constraint.validate, 1)
+ if type != "float":
+ testcase.assertRaises(TypeConstraintError, constraint.validate, 1.2)
+ if type != "str":
+ testcase.assertRaises(TypeConstraintError, constraint.validate, "1")
+ if type != "tuple":
+ testcase.assertRaises(TypeConstraintError, constraint.validate, (1,))
+ if type != "list":
+ testcase.assertRaises(TypeConstraintError, constraint.validate, [1,])
+ if type != "bool":
+ testcase.assertRaises(TypeConstraintError, constraint.validate, True)
+
+class TypeConstraintsTest(unittest.TestCase):
+ """
+ Tests all the type constraints at the same time to ensure that they
+ only allow a single type of item.
+ """
+ def test_int_type(self):
+ constraint = IntTypeConstraint()
+ try_good_values(constraint, 'int')
+ try_wrong_values(self, constraint, 'int')
+
+ def test_float_type(self):
+ constraint = FloatTypeConstraint()
+ try_good_values(constraint, 'float')
+ try_wrong_values(self, constraint, 'float')
+
+ def test_string_type(self):
+ constraint = StringTypeConstraint()
+ try_good_values(constraint, 'str')
+ try_wrong_values(self, constraint, 'str')
+
+ def test_tuple_type(self):
+ constraint = TupleTypeConstraint()
+ try_good_values(constraint, 'tuple')
+ try_wrong_values(self, constraint, 'tuple')
+
+ def test_list_type(self):
+ constraint = ListTypeConstraint()
+ try_good_values(constraint, 'list')
+ try_wrong_values(self, constraint, 'list')
+
+ def test_bool_type(self):
+ constraint = BoolTypeConstraint()
+ try_good_values(constraint, 'bool')
+ try_wrong_values(self, constraint, 'bool')
+
+class MultiTypeConstraintsTest(unittest.TestCase):
+ """
+ Tests all the type constraints at the same time to ensure that they
+ only allow the specified types of item.
+ """
+ def _try_good_values(self, constraint, types_list):
+ if "int" in types_list:
+ constraint.validate(1)
+ if "float" in types_list:
+ constraint.validate(1.0)
+ if "str" in types_list:
+ constraint.validate("This is a string.")
+ if "tuple" in types_list:
+ constraint.validate((1, 2.0, '3', [4], (5,)))
+ if "list" in types_list:
+ constraint.validate([1, 2.0, '3', [4], (5,)])
+ if "bool" in types_list:
+ constraint.validate(True)
+
+ def _try_wrong_values(self, constraint, types_list):
+ if "int" not in types_list:
+ self.assertRaises(TypeConstraintError, constraint.validate, 1)
+ if "float" not in types_list:
+ self.assertRaises(TypeConstraintError, constraint.validate, 1.0)
+ if "str" not in types_list:
+ self.assertRaises(TypeConstraintError, constraint.validate, "1")
+ if "tuple" not in types_list:
+ self.assertRaises(TypeConstraintError, constraint.validate, (1,))
+ if "list" not in types_list:
+ self.assertRaises(TypeConstraintError, constraint.validate, [1,])
+ if "bool" not in types_list:
+ self.assertRaises(TypeConstraintError, constraint.validate, True)
+
+ def test_array_type(self):
+ constraint = ArrayTypeConstraint()
+ self._try_good_values(constraint, ['tuple', 'list'])
+ self._try_wrong_values(constraint, ['tuple', 'list'])
+
+ def test_sequence_type(self):
+ constraint = SequenceTypeConstraint()
+ self._try_good_values(constraint, ['list', 'tuple', 'str'])
+ self._try_wrong_values(constraint, ['list', 'tuple', 'str'])
+
class UpperLimitConstraintTest(unittest.TestCase):
def test_empty_constraint(self):
cons = UpperLimitConstraint(None)
diff --git a/tests/propertiestests.py b/tests/propertiestests.py
index 49d2312..55c3818 100644
--- a/tests/propertiestests.py
+++ b/tests/propertiestests.py
@@ -29,36 +29,35 @@ def try_wrong_values(obj):
try:
obj.prop = 3
assert False, "Able to insert int value in property of type %s"%typ
- except ValueError:
+ except TypeConstraintError:
pass
if typ != "float":
try:
obj.prop = 1.1
assert False, "Able to insert float value in property of type %s"%typ
- except ValueError:
+ except TypeConstraintError:
pass
- if typ != "string":
+ if typ != "string" and typ != "sequence":
try:
obj.prop = "Fake string"
assert False, "Able to insert string value in property of type %s"%typ
- except ValueError:
+ except TypeConstraintError:
pass
- if typ != "array":
+ if typ != "array" and typ != "color" and typ != "sequence":
try:
obj.prop = [1, 2000, 3, 400]
assert False, "Able to insert array value in property of type %s"%typ
- except ValueError:
+ except TypeConstraintError:
pass
- if typ != "color":
+ if typ != "color" and typ != "sequence" and typ != "array":
try:
obj.prop = [1,2,3]
- if typ != "array":
- assert False, "Able to insert color value in property of type %s"%typ
- except ValueError:
+ assert False, "Able to insert color value in property of type %s"%typ
+ except TypeConstraintError:
pass
if typ != "boolean":
@@ -66,7 +65,7 @@ def try_wrong_values(obj):
obj.prop = True
if typ != "boolean":
assert False, "Able to set boolean value in property of type %s"%typ
- except ValueError:
+ except TypeConstraintError:
pass
class BasePropertyTest(unittest.TestCase):
@@ -216,7 +215,7 @@ class TIntPropertyTest(unittest.TestCase):
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"
+ assert len(cons) == 3, "Not enough constraints on the int property"
obj.prop = 12
@@ -271,22 +270,22 @@ class TIntPropertyTest(unittest.TestCase):
class TFloatPropertyTest(unittest.TestCase):
def test_float_property(self):
class klass(TPropContainer):
- prop = TFloatProperty(22)
+ prop = TFloatProperty(22.0)
obj = klass()
- assert obj.prop == 22, "Could not set value on property via constructor"
+ assert obj.prop == 22.0, "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"
+ assert len(cons) == 3, "Not enough constraints on the float property"
- obj.prop = 12
+ obj.prop = 12.0
- assert obj.prop == 12, "Could not set value"
+ assert obj.prop == 12.0, "Could not set value"
def test_wrong_values(self):
class klass(TPropContainer):
- prop = TFloatProperty(33)
+ prop = TFloatProperty(33.0)
obj = klass()
# Try setting values of other types
@@ -315,7 +314,7 @@ class TFloatPropertyTest(unittest.TestCase):
def test_failing_constructor(self):
try:
- prop = TFloatProperty(100, 0, 20)
+ prop = TFloatProperty(100.0, 0, 20)
assert False, "Creation of the property should fail."
except UpperLimitConstraintError:
pass
@@ -323,7 +322,7 @@ class TFloatPropertyTest(unittest.TestCase):
assert False, "Wrong exception type on failed constructor"
try:
- prop = TFloatProperty(-100, 0, 20)
+ prop = TFloatProperty(-100.0, 0, 20)
assert False, "Creation of the property should fail."
except LowerLimitConstraintError:
pass
@@ -419,6 +418,56 @@ class TArrayPropertyTest(unittest.TestCase):
except MinSizeConstraintError:
pass
+class TSequencePropertyTest(unittest.TestCase):
+ def test_basic_array(self):
+ class klass(TPropContainer):
+ prop = TSequenceProperty([1, 2, 3, 4])
+ obj = klass()
+
+ assert obj.prop == (1,2,3,4), "Unable to set initial value via constructor"
+
+ assert klass.prop.type == "sequence", "Wrong type for sequence : %s"%klass.prop.type
+
+ def test_wrong_values(self):
+ class klass(TPropContainer):
+ prop = TSequenceProperty([1,2,3,4,5])
+ obj = klass()
+
+ try_wrong_values(obj)
+
+ def test_size_limits(self):
+ class klass(TPropContainer):
+ prop = TSequenceProperty([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 = TSequenceProperty([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 = TSequenceProperty([100, 0, 20], None, 2)
+ assert False, "Creation of the property should fail."
+ except MaxSizeConstraintError:
+ pass
+ try:
+ prop = TSequenceProperty([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):
@@ -471,7 +520,7 @@ class TBooleanPropertyTest(unittest.TestCase):
try:
prop = TBooleanProperty(64)
assert False, "Creation of the property should fail with non-boolean value"
- except BooleanConstraintError:
+ except TypeConstraintError:
pass
except:
assert False, "Wrong exception type on failed constructor"
@@ -497,9 +546,6 @@ class TEnumPropertyTest(unittest.TestCase):
except EnumConstraintError:
pass
- def test_wrong_type(self):
- try_wrong_values(self.obj)
-
class TFilePropertyTest(unittest.TestCase):
root_folder = "/tmp/tutorius"