107: Some cleanup in cmd; added unit tests for cmd.default() method

This commit is contained in:
Jason Gerard DeRose 2008-08-11 17:37:33 +00:00
parent 8aee8e060c
commit fd6c215d59
2 changed files with 40 additions and 10 deletions

View File

@ -130,7 +130,8 @@ class option(plugable.Plugin):
class cmd(plugable.Plugin):
__public__ = frozenset((
'normalize',
'autofill',
'default',
'validate',
'__call__',
'get_doc',
'options',
@ -182,11 +183,6 @@ class cmd(plugable.Plugin):
def normalize(self, **kw):
return dict(self.normalize_iter(kw))
def validate(self, **kw):
for (key, value) in kw.items():
if key in self.options:
self.options.validate(value)
def default(self, **kw):
d = {}
for opt in self.options:
@ -198,11 +194,16 @@ class cmd(plugable.Plugin):
kw.update(d)
return kw
def validate(self, **kw):
for (key, value) in kw.items():
if key in self.options:
self.options.validate(value)
def __call__(self, **kw):
(args, kw) = self.normalize(*args, **kw)
(args, kw) = self.autofill(*args, **kw)
self.validate(*args, **kw)
self.execute(*args, **kw)
kw = self.normalize(**kw)
kw = self.default(**kw)
self.validate(**kw)
self.execute(**kw)

View File

@ -167,6 +167,13 @@ class test_cmd(ClassChecker):
class my_option(public.option):
def normalize(self, value):
return super(my_option, self).normalize(value).lower()
@public.rule
def my_rule(self, value):
if value != self.name:
return 'must equal %s' % name
def default(self, **kw):
return kw['default_from']
class option0(my_option):
pass
class option1(my_option):
@ -221,6 +228,28 @@ class test_cmd(ClassChecker):
sub = self.subcls()
assert sub.normalize(**kw) == norm
def test_default(self):
"""
Tests the `default` method.
"""
assert 'default' in self.cls.__public__ # Public
no_fill = dict(
option0='value0',
option1='value1',
whatever='hello world',
)
fill = dict(
default_from='the default',
)
filled = dict(
option0='the default',
option1='the default',
default_from='the default',
)
sub = self.subcls()
assert sub.default(**no_fill) == no_fill
assert sub.default(**fill) == filled
def test_obj():
cls = public.obj