diff options
author | Jason Gerard DeRose <jderose@redhat.com> | 2009-02-06 12:36:49 -0700 |
---|---|---|
committer | Rob Crittenden <rcritten@redhat.com> | 2009-02-17 16:03:08 -0500 |
commit | b5b2e55be589bc97ba030d0ad070d787c646ddd0 (patch) | |
tree | 87a9509b9ef3843a841252ed9d60537e072381b3 /tests | |
parent | 18cecdc515f04853a316fa74b1247878ae77ba15 (diff) | |
download | freeipa-b5b2e55be589bc97ba030d0ad070d787c646ddd0.tar.gz freeipa-b5b2e55be589bc97ba030d0ad070d787c646ddd0.tar.xz freeipa-b5b2e55be589bc97ba030d0ad070d787c646ddd0.zip |
Add pattern matching to Str and Bytes
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_ipalib/test_parameters.py | 70 |
1 files changed, 69 insertions, 1 deletions
diff --git a/tests/test_ipalib/test_parameters.py b/tests/test_ipalib/test_parameters.py index 70430714e..816f24c94 100644 --- a/tests/test_ipalib/test_parameters.py +++ b/tests/test_ipalib/test_parameters.py @@ -21,6 +21,7 @@ Test the `ipalib.parameters` module. """ +import re from types import NoneType from inspect import isclass from tests.util import raises, ClassChecker, read_only @@ -632,7 +633,7 @@ class test_Data(ClassChecker): assert o.minlength is None assert o.maxlength is None assert o.length is None - assert not hasattr(o, 'pattern') + assert o.pattern is None # Test mixing length with minlength or maxlength: o = self.cls('my_data', length=5) @@ -687,6 +688,7 @@ class test_Bytes(ClassChecker): assert o.maxlength is None assert o.length is None assert o.pattern is None + assert o.re is None # Test mixing length with minlength or maxlength: o = self.cls('my_bytes', length=5) @@ -804,6 +806,39 @@ class test_Bytes(ClassChecker): assert dummy.called() is True dummy.reset() + def test_rule_pattern(self): + """ + Test the `ipalib.parameters.Bytes._rule_pattern` method. + """ + # Test our assumptions about Python re module and Unicode: + pat = '\w+$' + r = re.compile(pat) + assert r.match('Hello_World') is not None + assert r.match(utf8_bytes) is None + assert r.match(binary_bytes) is None + + # Create instance: + o = self.cls('my_bytes', pattern=pat) + assert o.pattern is pat + rule = o._rule_pattern + translation = u'pattern=%(pattern)r' + dummy = dummy_ugettext(translation) + + # Test with passing values: + for value in ('HELLO', 'hello', 'Hello_World'): + assert rule(dummy, value) is None + assert dummy.called() is False + + # Test with failing values: + for value in ('Hello!', 'Hello World', utf8_bytes, binary_bytes): + assert_equal( + rule(dummy, value), + translation % dict(pattern=pat), + ) + assert_equal(dummy.message, 'must match pattern "%(pattern)s"') + assert dummy.called() is True + dummy.reset() + class test_Str(ClassChecker): """ @@ -921,6 +956,39 @@ class test_Str(ClassChecker): assert dummy.called() is True dummy.reset() + def test_rule_pattern(self): + """ + Test the `ipalib.parameters.Str._rule_pattern` method. + """ + # Test our assumptions about Python re module and Unicode: + pat = '\w{5}$' + r1 = re.compile(pat) + r2 = re.compile(pat, re.UNICODE) + assert r1.match(unicode_str) is None + assert r2.match(unicode_str) is not None + + # Create instance: + o = self.cls('my_str', pattern=pat) + assert o.pattern is pat + rule = o._rule_pattern + translation = u'pattern=%(pattern)r' + dummy = dummy_ugettext(translation) + + # Test with passing values: + for value in (u'HELLO', u'hello', unicode_str): + assert rule(dummy, value) is None + assert dummy.called() is False + + # Test with failing values: + for value in (u'H LLO', u'***lo', unicode_str + unicode_str): + assert_equal( + rule(dummy, value), + translation % dict(pattern=pat), + ) + assert_equal(dummy.message, 'must match pattern "%(pattern)s"') + assert dummy.called() is True + dummy.reset() + class test_Password(ClassChecker): """ |