From 9500e288397de5710151f32ced2e9a2903766a2e Mon Sep 17 00:00:00 2001 From: mike Date: Fri, 04 Dec 2009 22:25:14 +0000 Subject: Boy scout : Fixing tests for properties' values (cherry picked from commit 9f985564d6ade807a9182fcb7411388ed863e311) --- (limited to 'tests/constraintstests.py') 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) -- cgit v0.9.1