diff options
author | mike <michael.jmontcalm@gmail.com> | 2009-12-04 22:25:14 (GMT) |
---|---|---|
committer | Vincent Vinet <vince.vinet@gmail.com> | 2009-12-06 20:32:16 (GMT) |
commit | 065357ba57135e37fefff302db894e691924705d (patch) | |
tree | 71d98458580194c6d38013bf23e037eeb31b0380 /tutorius/constraints.py | |
parent | 21daa86bcb386240a1902c0bbad960285c900bcf (diff) |
Boy scout : Fixing tests for properties' values
(cherry picked from commit 9f985564d6ade807a9182fcb7411388ed863e311)
Diffstat (limited to 'tutorius/constraints.py')
-rw-r--r-- | tutorius/constraints.py | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/tutorius/constraints.py b/tutorius/constraints.py index cd71167..9ac1ca4 100644 --- a/tutorius/constraints.py +++ b/tutorius/constraints.py @@ -47,6 +47,96 @@ class Constraint(): """ raise NotImplementedError("Unable to validate a base Constraint") +class TypeConstraintError(ConstraintException): + pass + +class TypeConstraint(Constraint): + """ + Base class for ensuring that all the values have a specific Python type. + """ + def __init__(self, expected_type): + """ + Creates a new type constraint to restrict accepted values to a certain type. + @param expected_type A string describing the name of the type to enforce + """ + self._expected_type = expected_type + + def validate(self, value): + if self._expected_type == type(value).__name__: + return value + raise TypeConstraintError("Wrong type for value %s, got type %s; expected %s" % \ + (str(value), + type(value).__name__, + self._expected_type)) + +class IntTypeConstraint(TypeConstraint): + """ + Forces the value to be a Python int. + """ + def __init__(self): + TypeConstraint.__init__(self, 'int') + +class FloatTypeConstraint(TypeConstraint): + """ + Forces the value to be a Python float. + """ + def __init__(self): + TypeConstraint.__init__(self, 'float') + +class StringTypeConstraint(TypeConstraint): + """ + Forces the value to be a Python string. + """ + def __init__(self): + TypeConstraint.__init__(self, 'str') + +class TupleTypeConstraint(TypeConstraint): + """ + Forces the value to be a Python tuple. + """ + def __init__(self): + TypeConstraint.__init__(self, 'tuple') + +class ListTypeConstraint(TypeConstraint): + """ + Forces the value to be a Python list. + """ + def __init__(self): + TypeConstraint.__init__(self, 'list') + +class BoolTypeConstraint(TypeConstraint): + def __init__(self): + TypeConstraint.__init__(self, 'bool') + +class MultiTypeConstraint(Constraint): + """ + Base class for ensuring that all the values have a specific Python type. + """ + def __init__(self, expected_types_list): + """ + Creates a new type constraint to restrict accepted values to a certain type. + @param expected_types_list A list of strings describing the possible names + of the types to enforce. + """ + self._expected_types_list = expected_types_list + + def validate(self, value): + if type(value).__name__ in self._expected_types_list: + return value + raise TypeConstraintError("Wrong type for value %s, got type %s; expected one in %s" % \ + (str(value), + type(value).__name__, + str(self._expected_types_list))) + +class ArrayTypeConstraint(MultiTypeConstraint): + """ Forces the value to either be a list or a tuple.""" + def __init__(self): + MultiTypeConstraint.__init__(self, ['list', 'tuple']) + +class SequenceTypeConstraint(MultiTypeConstraint): + def __init__(self): + MultiTypeConstraint.__init__(self, ['list', 'tuple', 'str']) + class ValueConstraint(Constraint): """ A value constraint contains a _limit member that can be used in a children |