New Param: added missing unit tests for TypeError and ValueError cases in parse_param_spec()

This commit is contained in:
Jason Gerard DeRose 2008-12-18 01:57:39 -07:00
parent 99363131df
commit cb2f294cfe
3 changed files with 14 additions and 3 deletions

View File

@ -26,7 +26,7 @@ All constants centralised in one file.
NULLS = (None, '', u'', tuple(), []) NULLS = (None, '', u'', tuple(), [])
TYPE_ERROR = '%s: need a %r; got %r (a %r)' TYPE_ERROR = '%s: need a %r; got %r (which is a %r)'
CALLABLE_ERROR = '%s: need a callable; got %r (a %r)' CALLABLE_ERROR = '%s: need a callable; got %r (a %r)'

View File

@ -161,10 +161,12 @@ def parse_param_spec(spec):
:param spec: A spec string. :param spec: A spec string.
""" """
if type(spec) is not str: if type(spec) is not str:
raise_TypeError(spec, str, 'spec') raise TypeError(
TYPE_ERROR % ('spec', str, spec, type(spec))
)
if len(spec) < 2: if len(spec) < 2:
raise ValueError( raise ValueError(
'param spec must be at least 2 characters; got %r' % spec 'spec must be at least 2 characters; got %r' % spec
) )
_map = { _map = {
'?': dict(required=False, multivalue=False), '?': dict(required=False, multivalue=False),

View File

@ -92,9 +92,18 @@ def test_parse_param_spec():
assert f('name?') == ('name', dict(required=False, multivalue=False)) assert f('name?') == ('name', dict(required=False, multivalue=False))
assert f('name*') == ('name', dict(required=False, multivalue=True)) assert f('name*') == ('name', dict(required=False, multivalue=True))
assert f('name+') == ('name', dict(required=True, multivalue=True)) assert f('name+') == ('name', dict(required=True, multivalue=True))
# Make sure other "funny" endings are *not* treated special: # Make sure other "funny" endings are *not* treated special:
assert f('name^') == ('name^', dict(required=True, multivalue=False)) assert f('name^') == ('name^', dict(required=True, multivalue=False))
# Test that TypeError is raised if spec isn't an str:
e = raises(TypeError, f, u'name?')
assert str(e) == TYPE_ERROR % ('spec', str, u'name?', unicode)
# Test that ValueError is raised if len(spec) < 2:
e = raises(ValueError, f, 'n')
assert str(e) == "spec must be at least 2 characters; got 'n'"
class test_Param(ClassChecker): class test_Param(ClassChecker):
""" """