Statistics
| Branch: | Tag: | Revision:

root / kamaki / cli / argument / test.py @ f17d6cb5

History | View | Annotate | Download (3.7 kB)

1
# Copyright 2013 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

    
34
#from mock import patch, call
35
from unittest import TestCase
36
from StringIO import StringIO
37
#from itertools import product
38

    
39
from kamaki.cli import argument
40

    
41

    
42
class Argument(TestCase):
43

    
44
    def test___init__(self):
45
        self.assertRaises(ValueError, argument.Argument, 'non-integer')
46
        self.assertRaises(AssertionError, argument.Argument, 1)
47
        self.assertRaises(AssertionError, argument.Argument, 0, 'noname')
48
        self.assertRaises(AssertionError, argument.Argument, 0, '--no name')
49
        self.assertRaises(AssertionError, argument.Argument, 0, ['-n', 'n m'])
50
        for arity, help, parsed_name, default in (
51
                (0, 'help 0', '--zero', None),
52
                (1, 'help 1', ['--one', '-o'], 'lala'),
53
                (-1, 'help -1', ['--help', '--or', '--more'], 0),
54
                (0, 'help 0 again', ['--again', ], True)):
55
            a = argument.Argument(arity, help, parsed_name, default)
56
            if arity:
57
                self.assertEqual(arity, a.arity)
58
            self.assertEqual(help, a.help)
59

    
60
            exp_name = parsed_name if (
61
                isinstance(parsed_name, list)) else [parsed_name, ]
62
            self.assertEqual(exp_name, a.parsed_name)
63

    
64
            exp_default = default or (None if arity else False)
65
            self.assertEqual(exp_default, a.default)
66

    
67
    def test_value(self):
68
        a = argument.Argument(1, parsed_name='--value')
69
        for value in (None, '', 0, 0.1, -12, [1, 'a', 2.8], (3, 'lala'), 'pi'):
70
            a.value = value
71
            self.assertEqual(value, a.value)
72

    
73
    def test_update_parser(self):
74
        for i, arity in enumerate((-1, 0, 1)):
75
            arp = argument.ArgumentParser()
76
            pname, aname = '--pname%s' % i, 'a_name_%s' % i
77
            a = argument.Argument(arity, 'args', pname, 42)
78
            a.update_parser(arp, aname)
79

    
80
            f = StringIO()
81
            arp.print_usage(file=f), f.seek(0)
82
            usage, exp = f.readline(), '[%s%s]\n' % (
83
                pname, (' %s' % aname.upper()) if arity else '')
84
            self.assertEqual(usage[-len(exp):], exp)
85
            del arp
86

    
87

    
88
if __name__ == '__main__':
89
    from sys import argv
90
    from kamaki.cli.test import runTestCase
91
    runTestCase(Argument, 'Argument', argv[1:])