Statistics
| Branch: | Tag: | Revision:

root / kamaki / cli / command_tree / test.py @ 9a22e094

History | View | Annotate | Download (7.2 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 itertools import product
37

    
38
from kamaki.cli import command_tree
39

    
40

    
41
class Command(TestCase):
42

    
43
    def setUp(self):
44
        pass
45

    
46
    def tearDown(self):
47
        pass
48

    
49
    def test___init__(self):
50
        for args in product(
51
                (None, '', 'cmd'),
52
                (None, '', 'Some help'),
53
                (None, '', {}, dict(cmd0a=None, cmd0b=None)),
54
                (None, command_tree.Command('cmd_cmd0'))):
55
            path, help, subcommands, cmd_class = args
56
            try:
57
                cmd = command_tree.Command(*args)
58
            except Exception as e:
59
                if path:
60
                    raise
61
                self.assertTrue(isinstance(e, AssertionError))
62
                continue
63
            self.assertEqual(cmd.help, help or '')
64
            self.assertEqual(cmd.subcommands, subcommands or {})
65
            self.assertEqual(cmd.cmd_class, cmd_class or None)
66

    
67
    def test_name(self):
68
        for path in ('cmd', 'cmd_cmd0', 'cmd_cmd0_cmd1', '', None):
69
            if path:
70
                cmd = command_tree.Command(path)
71
                self.assertEqual(cmd.name, path.split('_')[-1])
72
            else:
73
                try:
74
                    command_tree.Command(path)
75
                except Exception as e:
76
                    self.assertTrue(isinstance(e, AssertionError))
77

    
78
    def test_add_subcmd(self):
79
        cmd = command_tree.Command('cmd')
80
        for subname in (None, 'cmd0', 'cmd_cmd0'):
81
            if subname:
82
                subcmd = command_tree.Command(subname)
83
                if subname.startswith(cmd.name + '_'):
84
                    self.assertTrue(cmd.add_subcmd(subcmd))
85
                    self.assertTrue(subcmd.name in cmd.subcommands)
86
                else:
87
                    self.assertFalse(cmd.add_subcmd(subcmd))
88
                    self.assertTrue(len(cmd.subcommands) == 0)
89
            else:
90
                self.assertRaises(cmd.add_subcmd, subname, AttributeError)
91

    
92
    def test_get_subcmd(self):
93
        cmd = command_tree.Command('cmd')
94
        cmd.subcommands = dict(
95
            cmd0a=command_tree.Command('cmd_cmd0a', subcommands=dict(
96
                cmd1=command_tree.Command('cmd_cmd0a_cmd1'))),
97
            cmd0b=command_tree.Command('cmd_cmd0b'))
98
        for subname in ('', None, 'cmd0a', 'cmd1', 'cmd0b'):
99
            try:
100
                expected = cmd.subcommands[subname] if subname else None
101
            except KeyError:
102
                expected = None
103
            self.assertEqual(cmd.get_subcmd(subname), expected)
104

    
105
    def test_contains(self):
106
        cmd = command_tree.Command('cmd')
107
        for subname in ('', 'cmd0'):
108
            self.assertFalse(cmd.contains(subname))
109
        cmd.subcommands = dict(
110
            cmd0a=command_tree.Command('cmd_cmd0a'),
111
            cmd0b=command_tree.Command('cmd_cmd0b'))
112
        for subname in ('cmd0a', 'cmd0b'):
113
            self.assertTrue(cmd.contains(subname))
114
        for subname in ('', 'cmd0c'):
115
            self.assertFalse(cmd.contains(subname))
116
        cmd.subcommands['cmd0a'].subcommands = dict(
117
            cmd1=command_tree.Command('cmd_cmd0a_cmd1'))
118
        for subname in ('cmd0a', 'cmd0b'):
119
            self.assertTrue(cmd.contains(subname))
120
        for subname in ('', 'cmd0c', 'cmd1', 'cmd0a_cmd1'):
121
            self.assertFalse(cmd.contains(subname))
122

    
123
    def test_is_command(self):
124
        cmd = command_tree.Command('cmd')
125
        cmd.subcommands = dict(
126
            itis=command_tree.Command('cmd_itis', cmd_class=Command),
127
            itsnot=command_tree.Command('cmd_itsnot'))
128
        self.assertFalse(cmd.is_command)
129
        self.assertTrue(cmd.subcommands['itis'].is_command)
130
        self.assertFalse(cmd.subcommands['itsnot'].is_command)
131

    
132
    def test_parent_path(self):
133
        cmd = command_tree.Command('cmd')
134
        cmd.subcommands = dict(
135
            cmd0a=command_tree.Command('cmd_cmd0a', subcommands=dict(
136
                cmd1=command_tree.Command('cmd_cmd0a_cmd1'))),
137
            cmd0b=command_tree.Command('cmd_cmd0b'))
138
        self.assertEqual(cmd.parent_path, '')
139
        self.assertEqual(cmd.subcommands['cmd0a'].parent_path, cmd.path)
140
        self.assertEqual(cmd.subcommands['cmd0b'].parent_path, cmd.path)
141
        cmd0a = cmd.subcommands['cmd0a']
142
        self.assertEqual(cmd0a.subcommands['cmd1'].parent_path, cmd0a.path)
143

    
144
    def test_parse_out(self):
145
        cmd = command_tree.Command('cmd')
146
        cmd.subcommands = dict(
147
            cmd0a=command_tree.Command('cmd_cmd0a', subcommands=dict(
148
                cmd1=command_tree.Command('cmd_cmd0a_cmd1'))),
149
            cmd0b=command_tree.Command('cmd_cmd0b'))
150
        for invalids in (None, 42, 0.88):
151
            self.assertRaises(TypeError, cmd.parse_out, invalids)
152
        for c, l, expc, expl in (
153
                (cmd, ['cmd'], cmd, ['cmd']),
154
                (cmd, ['XXX'], cmd, ['XXX']),
155
                (cmd, ['cmd0a'], cmd.subcommands['cmd0a'], []),
156
                (cmd, ['XXX', 'cmd0a'], cmd, ['XXX', 'cmd0a']),
157
                (cmd, ['cmd0a', 'XXX'], cmd.subcommands['cmd0a'], ['XXX']),
158
                (cmd, ['cmd0a', 'cmd0b'], cmd.subcommands['cmd0a'], ['cmd0b']),
159
                (cmd, ['cmd0b', 'XXX'], cmd.subcommands['cmd0b'], ['XXX']),
160
                (
161
                    cmd, ['cmd0a', 'cmd1'],
162
                    cmd.subcommands['cmd0a'].subcommands['cmd1'], []),
163
                (
164
                    cmd, ['cmd0a', 'cmd1', 'XXX'],
165
                    cmd.subcommands['cmd0a'].subcommands['cmd1'], ['XXX']),
166
                (
167
                    cmd, ['cmd0a', 'XXX', 'cmd1'],
168
                    cmd.subcommands['cmd0a'], ['XXX', 'cmd1'])):
169
            self.assertEqual((expc, expl), c.parse_out(l))
170

    
171

    
172
if __name__ == '__main__':
173
    from sys import argv
174
    from kamaki.cli.test import runTestCase
175
    runTestCase(Command, 'Command', argv[1:])