From: Stavros Sachtouris Date: Tue, 16 Jul 2013 11:57:56 +0000 (+0300) Subject: Initialize unittests for argument.Argument X-Git-Tag: debian/0.10-1wheezy~34^2~2 X-Git-Url: https://code.grnet.gr/git/kamaki/commitdiff_plain/5286e2c3b6e0fe9228110522cf7e31d04302421f Initialize unittests for argument.Argument Refs: #4058 Also, make argument package a directory --- diff --git a/kamaki/cli/argument.py b/kamaki/cli/argument/__init__.py similarity index 92% rename from kamaki/cli/argument.py rename to kamaki/cli/argument/__init__.py index 04c3883..de636f8 100644 --- a/kamaki/cli/argument.py +++ b/kamaki/cli/argument/__init__.py @@ -57,29 +57,27 @@ log = getLogger(__name__) class Argument(object): """An argument that can be parsed from command line or otherwise. - This is the general Argument class. It is suggested to extent this + This is the top-level Argument class. It is suggested to extent this class into more specific argument types. """ def __init__(self, arity, help=None, parsed_name=None, default=None): self.arity = int(arity) - - if help: - self.help = help - if parsed_name: - self.parsed_name = parsed_name - assert self.parsed_name, 'No parsed name for argument %s' % self - self.default = default + self.help = '%s' % help or '' + self.parsed_name = parsed_name + self.default = default or (None if self.arity else False) @property def parsed_name(self): - """the string which will be recognised by the parser as an instance - of this argument """ - return getattr(self, '_parsed_name', None) + :returns: (str) of the form --smth or -s is recognised as a call to an + argument instance + """ + return getattr(self, '_parsed_name', []) @parsed_name.setter def parsed_name(self, newname): + assert newname, 'No parsed name for argument %s' % self self._parsed_name = getattr(self, '_parsed_name', []) if isinstance(newname, list) or isinstance(newname, tuple): self._parsed_name += list(newname) @@ -87,36 +85,6 @@ class Argument(object): self._parsed_name.append('%s' % newname) @property - def help(self): - """a user friendly help message""" - return getattr(self, '_help', None) - - @help.setter - def help(self, newhelp): - self._help = '%s' % newhelp - - @property - def arity(self): - """negative for repeating, 0 for flag, 1 or more for values""" - return getattr(self, '_arity', None) - - @arity.setter - def arity(self, newarity): - newarity = int(newarity) - self._arity = newarity - - @property - def default(self): - """the value of this argument when not set""" - if not hasattr(self, '_default'): - self._default = False if self.arity == 0 else None - return self._default - - @default.setter - def default(self, newdefault): - self._default = newdefault - - @property def value(self): """the value of the argument""" return getattr(self, '_value', self.default) diff --git a/kamaki/cli/argument/test.py b/kamaki/cli/argument/test.py new file mode 100644 index 0000000..ae83108 --- /dev/null +++ b/kamaki/cli/argument/test.py @@ -0,0 +1,67 @@ +# Copyright 2013 GRNET S.A. All rights reserved. +# +# Redistribution and use in source and binary forms, with or +# without modification, are permitted provided that the following +# conditions are met: +# +# 1. Redistributions of source code must retain the above +# copyright notice, this list of conditions and the following +# disclaimer. +# +# 2. Redistributions in binary form must reproduce the above +# copyright notice, this list of conditions and the following +# disclaimer in the documentation and/or other materials +# provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS +# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# +# The views and conclusions contained in the software and +# documentation are those of the authors and should not be +# interpreted as representing official policies, either expressed +# or implied, of GRNET S.A. + +#from mock import patch, call +from unittest import TestCase +#from itertools import product + +from kamaki.cli import argument + + +class Argument(TestCase): + + def test___init__(self): + self.assertRaises(ValueError, argument.Argument, 'non-integer') + self.assertRaises(AssertionError, argument.Argument, 1) + for arity, help, parsed_name, default in ( + (0, 'help 0', '--zero', None), + (1, 'help 1', ['--one', '-o'], 'lala'), + (-1, 'help -1', ['--help', '--or', '--more'], 0), + (0, 'help 0 again', ['--again', ], True)): + a = argument.Argument(arity, help, parsed_name, default) + if arity: + self.assertEqual(arity, a.arity) + self.assertEqual(help, a.help) + + exp_name = parsed_name if ( + isinstance(parsed_name, list)) else [parsed_name, ] + self.assertEqual(exp_name, a.parsed_name) + + exp_default = default or (None if arity else False) + self.assertEqual(exp_default, a.default) + + +if __name__ == '__main__': + from sys import argv + from kamaki.cli.test import runTestCase + runTestCase(Argument, 'Argument', argv[1:]) diff --git a/kamaki/cli/test.py b/kamaki/cli/test.py index 852eb35..388e408 100644 --- a/kamaki/cli/test.py +++ b/kamaki/cli/test.py @@ -31,14 +31,11 @@ # interpreted as representing official policies, either expressed # or implied, of GRNET S.A. -from mock import patch, call from unittest import makeSuite, TestSuite, TextTestRunner, TestCase -from time import sleep from inspect import getmembers, isclass -from itertools import product -from random import randint from kamaki.cli.command_tree.test import Command, CommandTree +from kamaki.cli.argument.test import Argument # TestCase auxiliary methods