236: Ported pubic.Command to new Option; updated corresponding unit tests

This commit is contained in:
Jason Gerard DeRose 2008-09-02 23:40:44 +00:00
parent c9662adcf7
commit 5bfbbe3c38
2 changed files with 35 additions and 53 deletions

View File

@ -160,28 +160,16 @@ class Option(plugable.ReadOnly):
class Command(plugable.Plugin):
__public__ = frozenset((
'normalize',
'get_default',
'normalize',
'validate',
'execute',
'__call__',
'get_doc',
'options',
'Option',
))
__Option = None
options = tuple()
def get_doc(self, _):
"""
Returns the gettext translated doc-string for this command.
For example:
>>> def get_doc(self, _):
>>> return _('add new user')
"""
raise NotImplementedError('%s.get_doc()' % self.name)
def get_options(self):
return self.options
@ -196,21 +184,21 @@ class Command(plugable.Plugin):
return self.__Option
Option = property(__get_Option)
def normalize_iter(self, kw):
def __normalize_iter(self, kw):
for (key, value) in kw.items():
if key in self.options:
if key in self.Option:
yield (
key, self.options[key].normalize(value)
key, self.Option[key].normalize(value)
)
else:
yield (key, value)
def normalize(self, **kw):
self.print_call('normalize', kw, 1)
return dict(self.normalize_iter(kw))
return dict(self.__normalize_iter(kw))
def get_default_iter(self, kw):
for option in self.options():
def __get_default_iter(self, kw):
for option in self.Option():
if option.name not in kw:
value = option.get_default(**kw)
if value is not None:
@ -218,17 +206,17 @@ class Command(plugable.Plugin):
def get_default(self, **kw):
self.print_call('default', kw, 1)
return dict(self.get_default_iter(kw))
return dict(self.__get_default_iter(kw))
def validate(self, **kw):
self.print_call('validate', kw, 1)
for opt in self.options():
value = kw.get(opt.name, None)
for option in self.Option():
value = kw.get(option.name, None)
if value is None:
if opt.required:
raise errors.RequirementError(opt.name)
if option.required:
raise errors.RequirementError(option.name)
continue
opt.validate(value)
option.validate(value)
def execute(self, **kw):
self.print_call('execute', kw, 1)

View File

@ -304,7 +304,7 @@ class test_Option(ClassChecker):
assert o.get_values() == values
class dont_Command(ClassChecker):
class test_Command(ClassChecker):
"""
Tests the `public.Command` class.
"""
@ -326,11 +326,6 @@ class dont_Command(ClassChecker):
normalize = lambda value: value.lower()
type_ = ipa_types.Unicode()
class option0(my_option):
pass
class option1(my_option):
required = True
class example(self.cls):
options = (
public.Option('option0', 'Option zero', type_,
@ -349,7 +344,7 @@ class dont_Command(ClassChecker):
def test_class(self):
assert self.cls.__bases__ == (plugable.Plugin,)
assert type(self.cls.options) == property
assert self.cls.options == tuple()
def test_get_options(self):
"""
@ -357,27 +352,26 @@ class dont_Command(ClassChecker):
"""
assert list(self.cls().get_options()) == []
sub = self.subcls()
for (i, proxy) in enumerate(sub.get_options()):
assert isinstance(proxy, plugable.PluginProxy)
assert read_only(proxy, 'name') == 'option%d' % i
assert proxy.implements(public.Option)
for (i, option) in enumerate(sub.get_options()):
assert isinstance(option, public.Option)
assert read_only(option, 'name') == 'option%d' % i
assert i == 1
def test_options(self):
def test_Option(self):
"""
Tests the `public.Command.options` property.
Tests the `public.Command.Option` property.
"""
assert 'options' in self.cls.__public__ # Public
assert 'Option' in self.cls.__public__ # Public
sub = self.subcls()
options = sub.options
assert type(options) == plugable.NameSpace
assert len(options) == 2
O = sub.Option
assert type(O) is plugable.NameSpace
assert len(O) == 2
for name in ('option0', 'option1'):
assert name in options
proxy = options[name]
assert getattr(options, name) is proxy
assert isinstance(proxy, plugable.PluginProxy)
assert proxy.name == name
assert name in O
option = O[name]
assert getattr(O, name) is option
assert isinstance(option, public.Option)
assert option.name == name
def test_normalize(self):
"""
@ -385,9 +379,9 @@ class dont_Command(ClassChecker):
"""
assert 'normalize' in self.cls.__public__ # Public
kw = dict(
option0='OPTION0',
option1='OPTION1',
option2='option2',
option0=u'OPTION0',
option1=u'OPTION1',
option2=u'option2',
)
norm = dict((k, v.lower()) for (k, v) in kw.items())
sub = self.subcls()
@ -424,8 +418,8 @@ class dont_Command(ClassChecker):
# Check with valid args
okay = dict(
option0='option0',
option1='option1',
option0=u'option0',
option1=u'option1',
another_option='some value',
)
sub.validate(**okay)