252: Added Command.convert() method; added corresponding unit tests

This commit is contained in:
Jason Gerard DeRose 2008-09-04 02:30:40 +00:00
parent 5cdb182ae8
commit cf7e4c1038
2 changed files with 30 additions and 0 deletions

View File

@ -173,6 +173,7 @@ class Option(plugable.ReadOnly):
class Command(plugable.Plugin):
__public__ = frozenset((
'get_default',
'convert',
'normalize',
'validate',
'execute',
@ -196,6 +197,16 @@ class Command(plugable.Plugin):
return self.__Option
Option = property(__get_Option)
def __convert_iter(self, kw):
for (key, value) in kw.iteritems():
if key in self.Option:
yield (key, self.Option[key].convert(value))
else:
yield (key, value)
def convert(self, **kw):
return dict(self.__convert_iter(kw))
def __normalize_iter(self, kw):
for (key, value) in kw.items():
if key in self.Option:

View File

@ -385,6 +385,25 @@ class test_Command(ClassChecker):
assert isinstance(option, public.Option)
assert option.name == name
def test_convert(self):
"""
Tests the `public.Command.convert` method.
"""
assert 'convert' in self.cls.__public__ # Public
kw = dict(
option0='option0',
option1='option1',
whatever=False,
also=object,
)
expected = dict(kw)
expected.update(dict(option0=u'option0', option1=u'option1'))
o = self.subcls()
for (key, value) in o.convert(**kw).iteritems():
v = expected[key]
assert value == v
assert type(value) is type(v)
def test_normalize(self):
"""
Tests the `public.Command.normalize` method.