summaryrefslogtreecommitdiffstats
path: root/ipalib
diff options
context:
space:
mode:
authorJason Gerard DeRose <jderose@redhat.com>2008-09-18 23:53:23 +0000
committerJason Gerard DeRose <jderose@redhat.com>2008-09-18 23:53:23 +0000
commitf29c827d06cb455709d3b07baf727913381709ca (patch)
tree029618fe0cc03cf8a9114bbc95b99f2fe67bf4db /ipalib
parente0b900894fcc884fbde26ba78fa61aedec3843e9 (diff)
downloadfreeipa-f29c827d06cb455709d3b07baf727913381709ca.tar.gz
freeipa-f29c827d06cb455709d3b07baf727913381709ca.tar.xz
freeipa-f29c827d06cb455709d3b07baf727913381709ca.zip
301: Command.args_to_kw() now raises ArgumentError if more args than max_args are given
Diffstat (limited to 'ipalib')
-rw-r--r--ipalib/public.py8
-rw-r--r--ipalib/tests/test_public.py18
2 files changed, 24 insertions, 2 deletions
diff --git a/ipalib/public.py b/ipalib/public.py
index c2624f672..967f88ce3 100644
--- a/ipalib/public.py
+++ b/ipalib/public.py
@@ -388,6 +388,14 @@ class Command(plugable.Plugin):
yield None
def args_to_kw(self, *values):
+ if self.max_args is not None and len(values) > self.max_args:
+ if self.max_args == 0:
+ raise errors.ArgumentError(self, 'takes no arguments')
+ if self.max_args == 1:
+ raise errors.ArgumentError(self, 'takes at most 1 argument')
+ raise errors.ArgumentError(self,
+ 'takes at most %d arguments' % len(self.args)
+ )
return dict(self.__args_to_kw_iter(values))
def __args_to_kw_iter(self, values):
diff --git a/ipalib/tests/test_public.py b/ipalib/tests/test_public.py
index 80151fa08..939d59e8e 100644
--- a/ipalib/tests/test_public.py
+++ b/ipalib/tests/test_public.py
@@ -600,6 +600,9 @@ class test_Command(ClassChecker):
assert str(e) == 'example takes at least 2 arguments'
def test_args_to_kw(self):
+ """
+ Test the `public.Command.args_to_kw` method.
+ """
o = self.get_instance(args=('one', 'two?'))
assert o.args_to_kw(1) == dict(one=1)
assert o.args_to_kw(1, 2) == dict(one=1, two=2)
@@ -614,9 +617,21 @@ class test_Command(ClassChecker):
assert o.args_to_kw(1, 2) == dict(one=1, two=(2,))
assert o.args_to_kw(1, 2, 3) == dict(one=1, two=(2, 3))
+ o = self.get_instance()
+ e = raises(errors.ArgumentError, o.args_to_kw, 1)
+ assert str(e) == 'example takes no arguments'
+
+ o = self.get_instance(args=('one?',))
+ e = raises(errors.ArgumentError, o.args_to_kw, 1, 2)
+ assert str(e) == 'example takes at most 1 argument'
+
+ o = self.get_instance(args=('one', 'two?'))
+ e = raises(errors.ArgumentError, o.args_to_kw, 1, 2, 3)
+ assert str(e) == 'example takes at most 2 arguments'
+
def test_kw_to_args(self):
"""
- Tests the `public.Command.kw_to_arg` method.
+ Tests the `public.Command.kw_to_args` method.
"""
o = self.get_instance(args=('one', 'two?'))
assert o.kw_to_args() == (None, None)
@@ -627,7 +642,6 @@ class test_Command(ClassChecker):
('One', 'Two')
-
class test_Object(ClassChecker):
"""
Tests the `public.Object` class.