diff options
author | Jason Gerard DeRose <jderose@redhat.com> | 2008-09-10 21:34:29 +0000 |
---|---|---|
committer | Jason Gerard DeRose <jderose@redhat.com> | 2008-09-10 21:34:29 +0000 |
commit | 7c40226500daa4a27f4430ef7f94ec4520ab72e5 (patch) | |
tree | d9f2947f93cec56c0b940612553f4801d8a832ff | |
parent | 100492d98a199169a985086c20746dea7ff1fd3e (diff) | |
download | freeipa.git-7c40226500daa4a27f4430ef7f94ec4520ab72e5.tar.gz freeipa.git-7c40226500daa4a27f4430ef7f94ec4520ab72e5.tar.xz freeipa.git-7c40226500daa4a27f4430ef7f94ec4520ab72e5.zip |
286: Finished Command.args_to_kw(); finished unit tests for args_to_kw()
-rw-r--r-- | ipalib/public.py | 26 | ||||
-rw-r--r-- | ipalib/tests/test_public.py | 13 |
2 files changed, 27 insertions, 12 deletions
diff --git a/ipalib/public.py b/ipalib/public.py index 8ca97a36..4c0255ef 100644 --- a/ipalib/public.py +++ b/ipalib/public.py @@ -352,15 +352,14 @@ class Command(plugable.Plugin): def args_to_kw(self, *args): Args = tuple(self.args()) - if len(args) > len(Args): - if len(Args) > 0 and not Args[-1].multivalue: + if len(Args) == 0 and len(args) > 0: + raise errors.ArgumentError(self, 'takes no arguments') + if len(args) > len(Args) and not Args[-1].multivalue: if len(Args) == 1: error = 'takes at most 1 argument' else: error = 'takes at most %d arguments' % len(Args) raise errors.ArgumentError(self, error) - else: - raise errors.ArgumentError(self, 'takes no arguments') MinArgs = sum(int(A.required) for A in Args) if len(args) < MinArgs: if MinArgs == 1: @@ -368,14 +367,17 @@ class Command(plugable.Plugin): else: error = 'takes at least %d arguments' % MinArgs raise errors.ArgumentError(self, error) - for (i, Arg) in enumerate(Args): - pass - - - - - - + return dict(self.__args_to_kw_iter(args)) + + def __args_to_kw_iter(self, args): + for (i, Arg) in enumerate(self.args()): + if len(args) > i: + if Arg.multivalue: + yield (Arg.name, args[i:]) + else: + yield (Arg.name, args[i]) + else: + assert not Arg.required class Object(plugable.Plugin): diff --git a/ipalib/tests/test_public.py b/ipalib/tests/test_public.py index 6662b9d0..8bff5a19 100644 --- a/ipalib/tests/test_public.py +++ b/ipalib/tests/test_public.py @@ -551,6 +551,19 @@ class test_Command(ClassChecker): assert 'execute' in self.cls.__public__ # Public def test_args_to_kw(self): + 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) + + 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,)) + assert o.args_to_kw(1, 2, 3) == dict(one=1, two=(2, 3)) + + o = self.__get_instance(args=('one', 'two+')) + 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' |