339: Added parse_param_spec() function and corresponding unit tests

This commit is contained in:
Jason Gerard DeRose 2008-09-24 17:55:29 +00:00
parent ac7bdf8790
commit 11a07008b8
2 changed files with 44 additions and 0 deletions

View File

@ -84,6 +84,38 @@ class DefaultFrom(plugable.ReadOnly):
return None
def parse_param_spec(spec):
"""
Parse param spec to get name, required, and multivalue.
The ``spec`` string determines the param name, whether the param is
required, and whether the param is multivalue according the following
syntax:
name => required=True, multivalue=False
name? => required=False, multivalue=False
name+ => required=True, multivalue=True
name* => required=False, multivalue=True
:param spec: A spec string.
"""
if type(spec) is not str:
raise_TypeError(spec, str, 'spec')
if len(spec) < 2:
raise ValueError(
'param spec must be at least 2 characters; got %r' % spec
)
_map = {
'?': dict(required=False, multivalue=False),
'*': dict(required=False, multivalue=True),
'+': dict(required=True, multivalue=True),
}
end = spec[-1]
if end in _map:
return (spec[:-1], _map[end])
return (spec, dict(required=True, multivalue=False))
class Param(plugable.ReadOnly):
__nones = (None, '', tuple(), [])

View File

@ -111,6 +111,18 @@ class test_DefaultFrom(ClassChecker):
assert o(**kw_copy) is None
def test_parse_param_spec():
"""
Test the `frontend.parse_param_spec` function.
"""
f = frontend.parse_param_spec
assert f('name') == ('name', dict(required=True, 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=True, multivalue=True))
class test_Param(ClassChecker):
"""
Test the `frontend.Param` class.