diff options
author | Jason Gerard DeRose <jderose@redhat.com> | 2008-08-27 21:52:13 +0000 |
---|---|---|
committer | Jason Gerard DeRose <jderose@redhat.com> | 2008-08-27 21:52:13 +0000 |
commit | e6cecfdcf299db564a9055ad69b1c0bc75d4af31 (patch) | |
tree | 7f69f301a46077e6200240b616d0e60aa4a0c4fb /ipalib | |
parent | 89ea3acd0add73049a9ff7e73a0fdd646c7c9894 (diff) | |
download | freeipa.git-e6cecfdcf299db564a9055ad69b1c0bc75d4af31.tar.gz freeipa.git-e6cecfdcf299db564a9055ad69b1c0bc75d4af31.tar.xz freeipa.git-e6cecfdcf299db564a9055ad69b1c0bc75d4af31.zip |
204: Fixed logic error in check_min_max(); started work on argument validation for Unicode
Diffstat (limited to 'ipalib')
-rw-r--r-- | ipalib/ipa_types.py | 24 | ||||
-rw-r--r-- | ipalib/tests/test_ipa_types.py | 19 |
2 files changed, 26 insertions, 17 deletions
diff --git a/ipalib/ipa_types.py b/ipalib/ipa_types.py index a03b8822..d94a44fc 100644 --- a/ipalib/ipa_types.py +++ b/ipalib/ipa_types.py @@ -21,6 +21,7 @@ Type system for coercing and normalizing input values. """ +import re from plugable import ReadOnly, lock import errors @@ -31,9 +32,9 @@ def check_min_max(min_value, max_value, min_name, max_name): for (name, value) in [(min_name, min_value), (max_name, max_value)]: if not (value is None or type(value) is int): raise TypeError( - '`%s` must be an int or None, got: %r' % (name, value) + '%s must be an int or None, got: %r' % (name, value) ) - if None not in (min_value, max_value) and min_value >= max_value: + if None not in (min_value, max_value) and min_value > max_value: d = dict( k0=min_name, v0=min_value, @@ -41,7 +42,7 @@ def check_min_max(min_value, max_value, min_name, max_name): v1=max_value, ) raise ValueError( - '%(k1)s > %(k0)s: %(k0)s=%(v0)r, %(k1)s=%(v1)r' % d + '%(k0)s > %(k1)s: %(k0)s=%(v0)r, %(k1)s=%(v1)r' % d ) @@ -82,17 +83,16 @@ class Int(Type): class Unicode(Type): - def __init__(self, min_length=None, max_length=None, pattern=None): - integers = (min_length, max_length) - for i in integers: - if not (i is None or type(i) is int): - raise TypeError('Must be an int or None: %r' % i) - if None not in integers and min_value >= max_value: + def __init__(self, length=None,min_length=None, max_length=None, pattern=None): + check_min_max(min_length, max_length, 'min_length', 'max_length') + if min_length is not None and min_length < 0: raise ValueError( - 'min_value not less than max_value: %r, %r' % ( - min_value, max_value - ) + 'min_length must zero or greater, got: %r' % min_length ) self.min_length = min_length self.max_length = max_length self.pattern = pattern + if pattern is None: + self.regex = None + else: + self.regex = re.compile(pattern) diff --git a/ipalib/tests/test_ipa_types.py b/ipalib/tests/test_ipa_types.py index 87063ef6..03346684 100644 --- a/ipalib/tests/test_ipa_types.py +++ b/ipalib/tests/test_ipa_types.py @@ -47,9 +47,9 @@ def test_check_min_max(): ] for value in fail_type: e = raises(TypeError, f, value, None, 'low', 'high') - assert str(e) == '`low` must be an int or None, got: %r' % value + assert str(e) == 'low must be an int or None, got: %r' % value e = raises(TypeError, f, None, value, 'low', 'high') - assert str(e) == '`high` must be an int or None, got: %r' % value + assert str(e) == 'high must be an int or None, got: %r' % value fail_value = [ (10, 5), (-5, -10), @@ -57,7 +57,7 @@ def test_check_min_max(): ] for (l, h) in fail_value: e = raises(ValueError, f, l, h, 'low', 'high') - assert str(e) == 'high > low: low=%r, high=%r' % (l, h) + assert str(e) == 'low > high: low=%r, high=%r' % (l, h) class test_Type(ClassChecker): @@ -104,11 +104,11 @@ class test_Int(ClassChecker): for value in fail_type: e = raises(TypeError, self.cls, min_value=value) assert str(e) == ( - '`min_value` must be an int or None, got: %r' % value + 'min_value must be an int or None, got: %r' % value ) e = raises(TypeError, self.cls, max_value=value) assert str(e) == ( - '`max_value` must be an int or None, got: %r' % value + 'max_value must be an int or None, got: %r' % value ) fail_value = [ @@ -135,6 +135,15 @@ class test_Unicode(ClassChecker): def test_init(self): o = self.cls() + assert o.name == 'Unicode' assert o.min_length is None assert o.max_length is None assert o.pattern is None + assert o.regex is None + + okay = ( + (0, 5, r'(hello|world)'), + (8, 8, r'\d{4}'), + ) + for (l, h, pat) in okay: + o = self.cls(min_length=l, max_length=h, pattern=pat) |