Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/tests/constraintstests.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/constraintstests.py')
-rw-r--r--tests/constraintstests.py107
1 files changed, 107 insertions, 0 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)