Statistics
| Branch: | Tag: | Revision:

root / kamaki / cli / command_tree / test.py @ a624a072

History | View | Annotate | Download (9.1 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 test___init__(self):
44
        for args in product(
45
                (None, '', 'cmd'),
46
                (None, '', 'Some help'),
47
                (None, '', {}, dict(cmd0a=None, cmd0b=None)),
48
                (None, command_tree.Command('cmd_cmd0'))):
49
            path, help, subcommands, cmd_class = args
50
            try:
51
                cmd = command_tree.Command(*args)
52
            except Exception as e:
53
                if path:
54
                    raise
55
                self.assertTrue(isinstance(e, AssertionError))
56
                continue
57
            self.assertEqual(cmd.help, help or '')
58
            self.assertEqual(cmd.subcommands, subcommands or {})
59
            self.assertEqual(cmd.cmd_class, cmd_class or None)
60

    
61
    def test_name(self):
62
        for path in ('cmd', 'cmd_cmd0', 'cmd_cmd0_cmd1', '', None):
63
            if path:
64
                cmd = command_tree.Command(path)
65
                self.assertEqual(cmd.name, path.split('_')[-1])
66
            else:
67
                try:
68
                    command_tree.Command(path)
69
                except Exception as e:
70
                    self.assertTrue(isinstance(e, AssertionError))
71

    
72
    def test_add_subcmd(self):
73
        cmd = command_tree.Command('cmd')
74
        for subname in (None, 'cmd0', 'cmd_cmd0'):
75
            if subname:
76
                subcmd = command_tree.Command(subname)
77
                if subname.startswith(cmd.name + '_'):
78
                    self.assertTrue(cmd.add_subcmd(subcmd))
79
                    self.assertTrue(subcmd.name in cmd.subcommands)
80
                else:
81
                    self.assertFalse(cmd.add_subcmd(subcmd))
82
                    self.assertTrue(len(cmd.subcommands) == 0)
83
            else:
84
                self.assertRaises(cmd.add_subcmd, subname, AttributeError)
85

    
86
    def test_get_subcmd(self):
87
        cmd = command_tree.Command('cmd')
88
        cmd.subcommands = dict(
89
            cmd0a=command_tree.Command('cmd_cmd0a', subcommands=dict(
90
                cmd1=command_tree.Command('cmd_cmd0a_cmd1'))),
91
            cmd0b=command_tree.Command('cmd_cmd0b'))
92
        for subname in ('', None, 'cmd0a', 'cmd1', 'cmd0b'):
93
            try:
94
                expected = cmd.subcommands[subname] if subname else None
95
            except KeyError:
96
                expected = None
97
            self.assertEqual(cmd.get_subcmd(subname), expected)
98

    
99
    def test_contains(self):
100
        cmd = command_tree.Command('cmd')
101
        for subname in ('', 'cmd0'):
102
            self.assertFalse(cmd.contains(subname))
103
        cmd.subcommands = dict(
104
            cmd0a=command_tree.Command('cmd_cmd0a'),
105
            cmd0b=command_tree.Command('cmd_cmd0b'))
106
        for subname in ('cmd0a', 'cmd0b'):
107
            self.assertTrue(cmd.contains(subname))
108
        for subname in ('', 'cmd0c'):
109
            self.assertFalse(cmd.contains(subname))
110
        cmd.subcommands['cmd0a'].subcommands = dict(
111
            cmd1=command_tree.Command('cmd_cmd0a_cmd1'))
112
        for subname in ('cmd0a', 'cmd0b'):
113
            self.assertTrue(cmd.contains(subname))
114
        for subname in ('', 'cmd0c', 'cmd1', 'cmd0a_cmd1'):
115
            self.assertFalse(cmd.contains(subname))
116

    
117
    def test_is_command(self):
118
        cmd = command_tree.Command('cmd')
119
        cmd.subcommands = dict(
120
            itis=command_tree.Command('cmd_itis', cmd_class=Command),
121
            itsnot=command_tree.Command('cmd_itsnot'))
122
        self.assertFalse(cmd.is_command)
123
        self.assertTrue(cmd.subcommands['itis'].is_command)
124
        self.assertFalse(cmd.subcommands['itsnot'].is_command)
125

    
126
    def test_parent_path(self):
127
        cmd = command_tree.Command('cmd')
128
        cmd.subcommands = dict(
129
            cmd0a=command_tree.Command('cmd_cmd0a', subcommands=dict(
130
                cmd1=command_tree.Command('cmd_cmd0a_cmd1'))),
131
            cmd0b=command_tree.Command('cmd_cmd0b'))
132
        self.assertEqual(cmd.parent_path, '')
133
        self.assertEqual(cmd.subcommands['cmd0a'].parent_path, cmd.path)
134
        self.assertEqual(cmd.subcommands['cmd0b'].parent_path, cmd.path)
135
        cmd0a = cmd.subcommands['cmd0a']
136
        self.assertEqual(cmd0a.subcommands['cmd1'].parent_path, cmd0a.path)
137

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

    
165

    
166
class CommandTree(TestCase):
167

    
168
    def setUp(self):
169
        cmd = command_tree.Command('cmd', subcommands=dict(
170
            cmd0a=command_tree.Command('cmd_cmd0a', subcommands=dict(
171
                cmd1a=command_tree.Command(
172
                    'cmd_cmd0a_cmd1a', subcommands=dict(
173
                        cmd2=command_tree.Command('cmd_cmd0a_cmd1a_cmd2'),
174
                    )
175
                ),
176
                cmd1b=command_tree.Command(
177
                    'cmd_cmd0a_cmd1b', subcommands=dict(
178
                        cmd2=command_tree.Command('cmd_cmd0a_cmd1b_cmd2'),
179
                    )
180
                )
181
            )),
182
            cmd0b=command_tree.Command('cmd_cmd0b'),
183
            cmd0c=command_tree.Command('cmd_cmd0c', subcommands=dict(
184
                cmd1a=command_tree.Command('cmd_cmd0c_cmd1a'),
185
                cmd1b=command_tree.Command(
186
                    'cmd_cmd0c_cmd1b', subcommands=dict(
187
                        cmd2=command_tree.Command('cmd_cmd0c_cmd1b_cmd2'),
188
                    )
189
                )
190
            ))
191
        ))
192
        self.commands = [
193
            cmd,
194
            cmd.subcommands['cmd0a'],
195
            cmd.subcommands['cmd0a'].subcommands['cmd1a'],
196
            cmd.subcommands['cmd0a'].subcommands['cmd1a'].subcommands['cmd2'],
197
            cmd.subcommands['cmd0a'].subcommands['cmd1b'],
198
            cmd.subcommands['cmd0a'].subcommands['cmd1b'].subcommands['cmd2'],
199
            cmd.subcommands['cmd0b'],
200
            cmd.subcommands['cmd0c'],
201
            cmd.subcommands['cmd0c'].subcommands['cmd1a'],
202
            cmd.subcommands['cmd0c'].subcommands['cmd1b'],
203
            cmd.subcommands['cmd0c'].subcommands['cmd1b'].subcommands['cmd2'],
204
        ]
205

    
206
    def tearDown(self):
207
        for cmd in self.commands:
208
            del cmd
209
        del self.commands
210

    
211
    def test___init__(self):
212
        ctree = command_tree.CommandTree('sampleTree', 'a sample Tree')
213
        ctree.pretty_print()
214

    
215

    
216
if __name__ == '__main__':
217
    from sys import argv
218
    from kamaki.cli.test import runTestCase
219
    runTestCase(Command, 'Command', argv[1:])
220
    runTestCase(CommandTree, 'CommandTree', argv[1:])