105: Added a default implementation of cmd.get_options; added corresponding unit tests

This commit is contained in:
Jason Gerard DeRose
2008-08-11 00:21:12 +00:00
parent f6b69a5905
commit 879133d28a
2 changed files with 43 additions and 12 deletions

View File

@@ -23,6 +23,7 @@ and UI all use.
"""
import re
import inspect
import plugable
import errors
@@ -134,7 +135,8 @@ class cmd(plugable.Plugin):
'opt',
))
__opt = None
__options = None
option_classes = tuple()
def get_doc(self, _):
"""
@@ -149,19 +151,21 @@ class cmd(plugable.Plugin):
def get_options(self):
"""
Returns iterable with opt_proxy objects used to create the opt
NameSpace when __get_opt() is called.
Returns iterable with option proxy objects used to create the option
NameSpace when __get_option() is called.
"""
raise NotImplementedError('%s.get_options()' % self.name)
for cls in self.option_classes:
assert inspect.isclass(cls)
yield plugable.Proxy(option, cls())
def __get_opt(self):
def __get_options(self):
"""
Returns the NameSpace containing opt_proxy objects.
Returns the NameSpace containing the option proxy objects.
"""
if self.__opt is None:
self.__opt = plugable.NameSpace(self.get_options())
if self.__options is None:
self.__options = plugable.NameSpace(self.get_options())
return self.__opt
opt = property(__get_opt)
options = property(__get_options)
def normalize_iter(self, kw):
for (key, value) in kw.items():

View File

@@ -157,9 +157,36 @@ class test_option(ClassChecker):
assert self.cls().default() is None
def test_cmd():
cls = public.cmd
assert issubclass(cls, plugable.Plugin)
class test_cmd(ClassChecker):
"""
Tests the `cmd` class.
"""
_cls = public.cmd
def get_subcls(self):
class option0(public.option):
pass
class option1(public.option):
pass
class example(self.cls):
option_classes = (option0, option1)
return example
def test_class(self):
assert self.cls.__bases__ == (plugable.Plugin,)
assert type(self.cls.options) == property
def test_get_options(self):
"""
Tests the `get_options` method.
"""
assert list(self.cls().get_options()) == []
sub = self.subcls()
for (i, proxy) in enumerate(sub.get_options()):
assert isinstance(proxy, plugable.Proxy)
assert read_only(proxy, 'name') == 'option%d' % i
assert proxy.implements(public.option)
assert i == 1
def test_obj():