300: Added Command.max_args instance attribute; added corresponding unit tests

This commit is contained in:
Jason Gerard DeRose
2008-09-18 23:15:34 +00:00
parent ef0d7a71ab
commit e0b900894f
2 changed files with 24 additions and 6 deletions

View File

@@ -229,12 +229,15 @@ class Command(plugable.Plugin):
'options',
'group_args',
))
__Option = None
takes_options = tuple()
takes_args = tuple()
def __init__(self):
self.args = plugable.NameSpace(self.__check_args(), sort=False)
if len(self.args) == 0 or not self.args[-1].multivalue:
self.max_args = len(self.args)
else:
self.max_args = None
self.options = plugable.NameSpace(self.__check_options(), sort=False)
self.params = plugable.NameSpace(
tuple(self.args()) + tuple(self.options()), sort=False

View File

@@ -371,11 +371,6 @@ class test_Command(ClassChecker):
)
return example
def test_class(self):
assert self.cls.__bases__ == (plugable.Plugin,)
assert self.cls.takes_options == tuple()
assert self.cls.takes_args == tuple()
def get_instance(self, args=tuple(), options=tuple()):
"""
Helper method used to test args and options.
@@ -385,6 +380,11 @@ class test_Command(ClassChecker):
takes_options = options
return example()
def test_class(self):
assert self.cls.__bases__ == (plugable.Plugin,)
assert self.cls.takes_options == tuple()
assert self.cls.takes_args == tuple()
def test_get_args(self):
"""
Tests the `public.Command.get_args` method.
@@ -436,6 +436,21 @@ class test_Command(ClassChecker):
e = raises(ValueError, self.get_instance, args=('arg1+', 'arg2'))
assert str(e) == 'arg2: only final argument can be multivalue'
def test_max_args(self):
"""
Test the ``Command.max_args`` instance attribute.
"""
o = self.get_instance()
assert o.max_args == 0
o = self.get_instance(args=('one?',))
assert o.max_args == 1
o = self.get_instance(args=('one', 'two?'))
assert o.max_args == 2
o = self.get_instance(args=('one', 'multi+',))
assert o.max_args is None
o = self.get_instance(args=('one', 'multi*',))
assert o.max_args is None
def test_options(self):
"""
Tests the ``Command.options`` instance attribute.