From e0b900894fcc884fbde26ba78fa61aedec3843e9 Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Thu, 18 Sep 2008 23:15:34 +0000 Subject: [PATCH] 300: Added Command.max_args instance attribute; added corresponding unit tests --- ipalib/public.py | 5 ++++- ipalib/tests/test_public.py | 25 ++++++++++++++++++++----- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/ipalib/public.py b/ipalib/public.py index f3626fdfa..c2624f672 100644 --- a/ipalib/public.py +++ b/ipalib/public.py @@ -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 diff --git a/ipalib/tests/test_public.py b/ipalib/tests/test_public.py index 29531d69a..80151fa08 100644 --- a/ipalib/tests/test_public.py +++ b/ipalib/tests/test_public.py @@ -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.