Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (16.2 kB)

1 2fde8651 Stavros Sachtouris
# Copyright 2013 GRNET S.A. All rights reserved.
2 2fde8651 Stavros Sachtouris
#
3 2fde8651 Stavros Sachtouris
# Redistribution and use in source and binary forms, with or
4 2fde8651 Stavros Sachtouris
# without modification, are permitted provided that the following
5 2fde8651 Stavros Sachtouris
# conditions are met:
6 2fde8651 Stavros Sachtouris
#
7 2fde8651 Stavros Sachtouris
#   1. Redistributions of source code must retain the above
8 2fde8651 Stavros Sachtouris
#      copyright notice, this list of conditions and the following
9 2fde8651 Stavros Sachtouris
#      disclaimer.
10 2fde8651 Stavros Sachtouris
#
11 2fde8651 Stavros Sachtouris
#   2. Redistributions in binary form must reproduce the above
12 2fde8651 Stavros Sachtouris
#      copyright notice, this list of conditions and the following
13 2fde8651 Stavros Sachtouris
#      disclaimer in the documentation and/or other materials
14 2fde8651 Stavros Sachtouris
#      provided with the distribution.
15 2fde8651 Stavros Sachtouris
#
16 2fde8651 Stavros Sachtouris
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 2fde8651 Stavros Sachtouris
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 2fde8651 Stavros Sachtouris
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 2fde8651 Stavros Sachtouris
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 2fde8651 Stavros Sachtouris
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 2fde8651 Stavros Sachtouris
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 2fde8651 Stavros Sachtouris
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 2fde8651 Stavros Sachtouris
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 2fde8651 Stavros Sachtouris
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 2fde8651 Stavros Sachtouris
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 2fde8651 Stavros Sachtouris
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 2fde8651 Stavros Sachtouris
# POSSIBILITY OF SUCH DAMAGE.
28 2fde8651 Stavros Sachtouris
#
29 2fde8651 Stavros Sachtouris
# The views and conclusions contained in the software and
30 2fde8651 Stavros Sachtouris
# documentation are those of the authors and should not be
31 2fde8651 Stavros Sachtouris
# interpreted as representing official policies, either expressed
32 2fde8651 Stavros Sachtouris
# or implied, of GRNET S.A.
33 2fde8651 Stavros Sachtouris
34 2fde8651 Stavros Sachtouris
#from mock import patch, call
35 2fde8651 Stavros Sachtouris
from unittest import TestCase
36 2fde8651 Stavros Sachtouris
from itertools import product
37 2fde8651 Stavros Sachtouris
38 2fde8651 Stavros Sachtouris
from kamaki.cli import command_tree
39 2fde8651 Stavros Sachtouris
40 2fde8651 Stavros Sachtouris
41 2fde8651 Stavros Sachtouris
class Command(TestCase):
42 2fde8651 Stavros Sachtouris
43 2fde8651 Stavros Sachtouris
    def test___init__(self):
44 2fde8651 Stavros Sachtouris
        for args in product(
45 2fde8651 Stavros Sachtouris
                (None, '', 'cmd'),
46 2fde8651 Stavros Sachtouris
                (None, '', 'Some help'),
47 2fde8651 Stavros Sachtouris
                (None, '', {}, dict(cmd0a=None, cmd0b=None)),
48 2fde8651 Stavros Sachtouris
                (None, command_tree.Command('cmd_cmd0'))):
49 2fde8651 Stavros Sachtouris
            path, help, subcommands, cmd_class = args
50 2fde8651 Stavros Sachtouris
            try:
51 2fde8651 Stavros Sachtouris
                cmd = command_tree.Command(*args)
52 2fde8651 Stavros Sachtouris
            except Exception as e:
53 2fde8651 Stavros Sachtouris
                if path:
54 2fde8651 Stavros Sachtouris
                    raise
55 2fde8651 Stavros Sachtouris
                self.assertTrue(isinstance(e, AssertionError))
56 2fde8651 Stavros Sachtouris
                continue
57 2fde8651 Stavros Sachtouris
            self.assertEqual(cmd.help, help or '')
58 2fde8651 Stavros Sachtouris
            self.assertEqual(cmd.subcommands, subcommands or {})
59 2fde8651 Stavros Sachtouris
            self.assertEqual(cmd.cmd_class, cmd_class or None)
60 2fde8651 Stavros Sachtouris
61 2fde8651 Stavros Sachtouris
    def test_name(self):
62 2fde8651 Stavros Sachtouris
        for path in ('cmd', 'cmd_cmd0', 'cmd_cmd0_cmd1', '', None):
63 2fde8651 Stavros Sachtouris
            if path:
64 2fde8651 Stavros Sachtouris
                cmd = command_tree.Command(path)
65 2fde8651 Stavros Sachtouris
                self.assertEqual(cmd.name, path.split('_')[-1])
66 2fde8651 Stavros Sachtouris
            else:
67 2fde8651 Stavros Sachtouris
                try:
68 2fde8651 Stavros Sachtouris
                    command_tree.Command(path)
69 2fde8651 Stavros Sachtouris
                except Exception as e:
70 2fde8651 Stavros Sachtouris
                    self.assertTrue(isinstance(e, AssertionError))
71 2fde8651 Stavros Sachtouris
72 9a22e094 Stavros Sachtouris
    def test_add_subcmd(self):
73 9a22e094 Stavros Sachtouris
        cmd = command_tree.Command('cmd')
74 9a22e094 Stavros Sachtouris
        for subname in (None, 'cmd0', 'cmd_cmd0'):
75 9a22e094 Stavros Sachtouris
            if subname:
76 9a22e094 Stavros Sachtouris
                subcmd = command_tree.Command(subname)
77 9a22e094 Stavros Sachtouris
                if subname.startswith(cmd.name + '_'):
78 9a22e094 Stavros Sachtouris
                    self.assertTrue(cmd.add_subcmd(subcmd))
79 9a22e094 Stavros Sachtouris
                    self.assertTrue(subcmd.name in cmd.subcommands)
80 9a22e094 Stavros Sachtouris
                else:
81 9a22e094 Stavros Sachtouris
                    self.assertFalse(cmd.add_subcmd(subcmd))
82 9a22e094 Stavros Sachtouris
                    self.assertTrue(len(cmd.subcommands) == 0)
83 9a22e094 Stavros Sachtouris
            else:
84 9a22e094 Stavros Sachtouris
                self.assertRaises(cmd.add_subcmd, subname, AttributeError)
85 9a22e094 Stavros Sachtouris
86 9a22e094 Stavros Sachtouris
    def test_get_subcmd(self):
87 9a22e094 Stavros Sachtouris
        cmd = command_tree.Command('cmd')
88 9a22e094 Stavros Sachtouris
        cmd.subcommands = dict(
89 9a22e094 Stavros Sachtouris
            cmd0a=command_tree.Command('cmd_cmd0a', subcommands=dict(
90 9a22e094 Stavros Sachtouris
                cmd1=command_tree.Command('cmd_cmd0a_cmd1'))),
91 9a22e094 Stavros Sachtouris
            cmd0b=command_tree.Command('cmd_cmd0b'))
92 9a22e094 Stavros Sachtouris
        for subname in ('', None, 'cmd0a', 'cmd1', 'cmd0b'):
93 9a22e094 Stavros Sachtouris
            try:
94 9a22e094 Stavros Sachtouris
                expected = cmd.subcommands[subname] if subname else None
95 9a22e094 Stavros Sachtouris
            except KeyError:
96 9a22e094 Stavros Sachtouris
                expected = None
97 9a22e094 Stavros Sachtouris
            self.assertEqual(cmd.get_subcmd(subname), expected)
98 9a22e094 Stavros Sachtouris
99 9a22e094 Stavros Sachtouris
    def test_contains(self):
100 9a22e094 Stavros Sachtouris
        cmd = command_tree.Command('cmd')
101 9a22e094 Stavros Sachtouris
        for subname in ('', 'cmd0'):
102 9a22e094 Stavros Sachtouris
            self.assertFalse(cmd.contains(subname))
103 9a22e094 Stavros Sachtouris
        cmd.subcommands = dict(
104 9a22e094 Stavros Sachtouris
            cmd0a=command_tree.Command('cmd_cmd0a'),
105 9a22e094 Stavros Sachtouris
            cmd0b=command_tree.Command('cmd_cmd0b'))
106 9a22e094 Stavros Sachtouris
        for subname in ('cmd0a', 'cmd0b'):
107 9a22e094 Stavros Sachtouris
            self.assertTrue(cmd.contains(subname))
108 9a22e094 Stavros Sachtouris
        for subname in ('', 'cmd0c'):
109 9a22e094 Stavros Sachtouris
            self.assertFalse(cmd.contains(subname))
110 9a22e094 Stavros Sachtouris
        cmd.subcommands['cmd0a'].subcommands = dict(
111 9a22e094 Stavros Sachtouris
            cmd1=command_tree.Command('cmd_cmd0a_cmd1'))
112 9a22e094 Stavros Sachtouris
        for subname in ('cmd0a', 'cmd0b'):
113 9a22e094 Stavros Sachtouris
            self.assertTrue(cmd.contains(subname))
114 9a22e094 Stavros Sachtouris
        for subname in ('', 'cmd0c', 'cmd1', 'cmd0a_cmd1'):
115 9a22e094 Stavros Sachtouris
            self.assertFalse(cmd.contains(subname))
116 9a22e094 Stavros Sachtouris
117 9a22e094 Stavros Sachtouris
    def test_is_command(self):
118 9a22e094 Stavros Sachtouris
        cmd = command_tree.Command('cmd')
119 9a22e094 Stavros Sachtouris
        cmd.subcommands = dict(
120 9a22e094 Stavros Sachtouris
            itis=command_tree.Command('cmd_itis', cmd_class=Command),
121 9a22e094 Stavros Sachtouris
            itsnot=command_tree.Command('cmd_itsnot'))
122 9a22e094 Stavros Sachtouris
        self.assertFalse(cmd.is_command)
123 9a22e094 Stavros Sachtouris
        self.assertTrue(cmd.subcommands['itis'].is_command)
124 9a22e094 Stavros Sachtouris
        self.assertFalse(cmd.subcommands['itsnot'].is_command)
125 9a22e094 Stavros Sachtouris
126 9a22e094 Stavros Sachtouris
    def test_parent_path(self):
127 9a22e094 Stavros Sachtouris
        cmd = command_tree.Command('cmd')
128 9a22e094 Stavros Sachtouris
        cmd.subcommands = dict(
129 9a22e094 Stavros Sachtouris
            cmd0a=command_tree.Command('cmd_cmd0a', subcommands=dict(
130 9a22e094 Stavros Sachtouris
                cmd1=command_tree.Command('cmd_cmd0a_cmd1'))),
131 9a22e094 Stavros Sachtouris
            cmd0b=command_tree.Command('cmd_cmd0b'))
132 9a22e094 Stavros Sachtouris
        self.assertEqual(cmd.parent_path, '')
133 9a22e094 Stavros Sachtouris
        self.assertEqual(cmd.subcommands['cmd0a'].parent_path, cmd.path)
134 9a22e094 Stavros Sachtouris
        self.assertEqual(cmd.subcommands['cmd0b'].parent_path, cmd.path)
135 9a22e094 Stavros Sachtouris
        cmd0a = cmd.subcommands['cmd0a']
136 9a22e094 Stavros Sachtouris
        self.assertEqual(cmd0a.subcommands['cmd1'].parent_path, cmd0a.path)
137 9a22e094 Stavros Sachtouris
138 9a22e094 Stavros Sachtouris
    def test_parse_out(self):
139 9a22e094 Stavros Sachtouris
        cmd = command_tree.Command('cmd')
140 9a22e094 Stavros Sachtouris
        cmd.subcommands = dict(
141 9a22e094 Stavros Sachtouris
            cmd0a=command_tree.Command('cmd_cmd0a', subcommands=dict(
142 9a22e094 Stavros Sachtouris
                cmd1=command_tree.Command('cmd_cmd0a_cmd1'))),
143 9a22e094 Stavros Sachtouris
            cmd0b=command_tree.Command('cmd_cmd0b'))
144 9a22e094 Stavros Sachtouris
        for invalids in (None, 42, 0.88):
145 9a22e094 Stavros Sachtouris
            self.assertRaises(TypeError, cmd.parse_out, invalids)
146 9a22e094 Stavros Sachtouris
        for c, l, expc, expl in (
147 9a22e094 Stavros Sachtouris
                (cmd, ['cmd'], cmd, ['cmd']),
148 9a22e094 Stavros Sachtouris
                (cmd, ['XXX'], cmd, ['XXX']),
149 9a22e094 Stavros Sachtouris
                (cmd, ['cmd0a'], cmd.subcommands['cmd0a'], []),
150 9a22e094 Stavros Sachtouris
                (cmd, ['XXX', 'cmd0a'], cmd, ['XXX', 'cmd0a']),
151 9a22e094 Stavros Sachtouris
                (cmd, ['cmd0a', 'XXX'], cmd.subcommands['cmd0a'], ['XXX']),
152 9a22e094 Stavros Sachtouris
                (cmd, ['cmd0a', 'cmd0b'], cmd.subcommands['cmd0a'], ['cmd0b']),
153 9a22e094 Stavros Sachtouris
                (cmd, ['cmd0b', 'XXX'], cmd.subcommands['cmd0b'], ['XXX']),
154 9a22e094 Stavros Sachtouris
                (
155 9a22e094 Stavros Sachtouris
                    cmd, ['cmd0a', 'cmd1'],
156 9a22e094 Stavros Sachtouris
                    cmd.subcommands['cmd0a'].subcommands['cmd1'], []),
157 9a22e094 Stavros Sachtouris
                (
158 9a22e094 Stavros Sachtouris
                    cmd, ['cmd0a', 'cmd1', 'XXX'],
159 9a22e094 Stavros Sachtouris
                    cmd.subcommands['cmd0a'].subcommands['cmd1'], ['XXX']),
160 9a22e094 Stavros Sachtouris
                (
161 9a22e094 Stavros Sachtouris
                    cmd, ['cmd0a', 'XXX', 'cmd1'],
162 9a22e094 Stavros Sachtouris
                    cmd.subcommands['cmd0a'], ['XXX', 'cmd1'])):
163 9a22e094 Stavros Sachtouris
            self.assertEqual((expc, expl), c.parse_out(l))
164 9a22e094 Stavros Sachtouris
165 2fde8651 Stavros Sachtouris
166 0962adb7 Stavros Sachtouris
class CommandTree(TestCase):
167 0962adb7 Stavros Sachtouris
168 d252a7a8 Stavros Sachtouris
    def _add_commands(self, ctree):
169 d252a7a8 Stavros Sachtouris
        for cmd in self.commands:
170 d252a7a8 Stavros Sachtouris
            ctree.add_command(cmd.path, cmd.help, cmd.cmd_class)
171 d252a7a8 Stavros Sachtouris
172 d252a7a8 Stavros Sachtouris
    def _commands_are_equal(self, c1, c2):
173 d252a7a8 Stavros Sachtouris
        self.assertEqual(c1.path, c2.path)
174 d252a7a8 Stavros Sachtouris
        self.assertEqual(c1.name, c2.name)
175 d252a7a8 Stavros Sachtouris
        self.assertEqual(c1.cmd_class, c2.cmd_class)
176 d252a7a8 Stavros Sachtouris
        self.assertEqual(c1.help, c2.help)
177 d252a7a8 Stavros Sachtouris
178 0962adb7 Stavros Sachtouris
    def setUp(self):
179 0962adb7 Stavros Sachtouris
        cmd = command_tree.Command('cmd', subcommands=dict(
180 0962adb7 Stavros Sachtouris
            cmd0a=command_tree.Command('cmd_cmd0a', subcommands=dict(
181 0962adb7 Stavros Sachtouris
                cmd1a=command_tree.Command(
182 0962adb7 Stavros Sachtouris
                    'cmd_cmd0a_cmd1a', subcommands=dict(
183 d252a7a8 Stavros Sachtouris
                        cmd2=command_tree.Command(
184 d252a7a8 Stavros Sachtouris
                            'cmd_cmd0a_cmd1a_cmd2', cmd_class=Command)
185 d252a7a8 Stavros Sachtouris
                        ),
186 0962adb7 Stavros Sachtouris
                ),
187 0962adb7 Stavros Sachtouris
                cmd1b=command_tree.Command(
188 0962adb7 Stavros Sachtouris
                    'cmd_cmd0a_cmd1b', subcommands=dict(
189 d252a7a8 Stavros Sachtouris
                        cmd2=command_tree.Command(
190 d252a7a8 Stavros Sachtouris
                            'cmd_cmd0a_cmd1b_cmd2', cmd_class=Command)
191 d252a7a8 Stavros Sachtouris
                        ),
192 0962adb7 Stavros Sachtouris
                )
193 0962adb7 Stavros Sachtouris
            )),
194 0962adb7 Stavros Sachtouris
            cmd0b=command_tree.Command('cmd_cmd0b'),
195 0962adb7 Stavros Sachtouris
            cmd0c=command_tree.Command('cmd_cmd0c', subcommands=dict(
196 0962adb7 Stavros Sachtouris
                cmd1a=command_tree.Command('cmd_cmd0c_cmd1a'),
197 0962adb7 Stavros Sachtouris
                cmd1b=command_tree.Command(
198 0962adb7 Stavros Sachtouris
                    'cmd_cmd0c_cmd1b', subcommands=dict(
199 d252a7a8 Stavros Sachtouris
                        cmd2=command_tree.Command(
200 d252a7a8 Stavros Sachtouris
                            'cmd_cmd0c_cmd1b_cmd2', cmd_class=Command)
201 d252a7a8 Stavros Sachtouris
                        ),
202 0962adb7 Stavros Sachtouris
                )
203 0962adb7 Stavros Sachtouris
            ))
204 0962adb7 Stavros Sachtouris
        ))
205 0962adb7 Stavros Sachtouris
        self.commands = [
206 0962adb7 Stavros Sachtouris
            cmd,
207 0962adb7 Stavros Sachtouris
            cmd.subcommands['cmd0a'],
208 0962adb7 Stavros Sachtouris
            cmd.subcommands['cmd0a'].subcommands['cmd1a'],
209 0962adb7 Stavros Sachtouris
            cmd.subcommands['cmd0a'].subcommands['cmd1a'].subcommands['cmd2'],
210 0962adb7 Stavros Sachtouris
            cmd.subcommands['cmd0a'].subcommands['cmd1b'],
211 0962adb7 Stavros Sachtouris
            cmd.subcommands['cmd0a'].subcommands['cmd1b'].subcommands['cmd2'],
212 0962adb7 Stavros Sachtouris
            cmd.subcommands['cmd0b'],
213 0962adb7 Stavros Sachtouris
            cmd.subcommands['cmd0c'],
214 0962adb7 Stavros Sachtouris
            cmd.subcommands['cmd0c'].subcommands['cmd1a'],
215 0962adb7 Stavros Sachtouris
            cmd.subcommands['cmd0c'].subcommands['cmd1b'],
216 0962adb7 Stavros Sachtouris
            cmd.subcommands['cmd0c'].subcommands['cmd1b'].subcommands['cmd2'],
217 d252a7a8 Stavros Sachtouris
            command_tree.Command('othercmd')
218 0962adb7 Stavros Sachtouris
        ]
219 0962adb7 Stavros Sachtouris
220 0962adb7 Stavros Sachtouris
    def tearDown(self):
221 0962adb7 Stavros Sachtouris
        for cmd in self.commands:
222 0962adb7 Stavros Sachtouris
            del cmd
223 0962adb7 Stavros Sachtouris
        del self.commands
224 0962adb7 Stavros Sachtouris
225 0962adb7 Stavros Sachtouris
    def test___init__(self):
226 d252a7a8 Stavros Sachtouris
        name, description = 'sampleTree', 'a sample Tree'
227 d252a7a8 Stavros Sachtouris
        ctree = command_tree.CommandTree(name)
228 d252a7a8 Stavros Sachtouris
        for attr, exp in (
229 d252a7a8 Stavros Sachtouris
                ('groups', {}), ('_all_commands', {}),
230 d252a7a8 Stavros Sachtouris
                ('name', name), ('description', '')):
231 d252a7a8 Stavros Sachtouris
            self.assertEqual(getattr(ctree, attr), exp)
232 d252a7a8 Stavros Sachtouris
        ctree = command_tree.CommandTree(name, description)
233 d252a7a8 Stavros Sachtouris
        for attr, exp in (
234 d252a7a8 Stavros Sachtouris
                ('groups', {}), ('_all_commands', {}),
235 d252a7a8 Stavros Sachtouris
                ('name', name), ('description', description)):
236 d252a7a8 Stavros Sachtouris
            self.assertEqual(getattr(ctree, attr), exp)
237 d252a7a8 Stavros Sachtouris
238 d252a7a8 Stavros Sachtouris
    def test_exclude(self):
239 d252a7a8 Stavros Sachtouris
        ctree = command_tree.CommandTree('excludeTree', 'test exclude group')
240 d252a7a8 Stavros Sachtouris
        exp = dict()
241 d252a7a8 Stavros Sachtouris
        for cmd in self.commands[0:6]:
242 d252a7a8 Stavros Sachtouris
            ctree.groups[cmd.name] = cmd
243 d252a7a8 Stavros Sachtouris
            exp[cmd.name] = cmd
244 d252a7a8 Stavros Sachtouris
        self.assertEqual(exp, ctree.groups)
245 d252a7a8 Stavros Sachtouris
        ctree.exclude(exp.keys()[1::2])
246 d252a7a8 Stavros Sachtouris
        for key in exp.keys()[1::2]:
247 d252a7a8 Stavros Sachtouris
            exp.pop(key)
248 d252a7a8 Stavros Sachtouris
        self.assertEqual(exp, ctree.groups)
249 d252a7a8 Stavros Sachtouris
250 d252a7a8 Stavros Sachtouris
    def test_add_command(self):
251 d252a7a8 Stavros Sachtouris
        ctree = command_tree.CommandTree('addCommand', 'test add_command')
252 d252a7a8 Stavros Sachtouris
        self._add_commands(ctree)
253 d252a7a8 Stavros Sachtouris
        for cmd in self.commands:
254 d252a7a8 Stavros Sachtouris
            self.assertTrue(cmd, ctree._all_commands)
255 d252a7a8 Stavros Sachtouris
            if cmd.path.count('_'):
256 d252a7a8 Stavros Sachtouris
                self.assertFalse(cmd.name in ctree.groups)
257 d252a7a8 Stavros Sachtouris
            else:
258 d252a7a8 Stavros Sachtouris
                self.assertTrue(cmd.name in ctree.groups)
259 d252a7a8 Stavros Sachtouris
                self._commands_are_equal(cmd, ctree.groups[cmd.name])
260 d252a7a8 Stavros Sachtouris
261 d252a7a8 Stavros Sachtouris
    def test_find_best_match(self):
262 d252a7a8 Stavros Sachtouris
        ctree = command_tree.CommandTree('bestMatch', 'test find_best_match')
263 d252a7a8 Stavros Sachtouris
        for cmd in self.commands:
264 d252a7a8 Stavros Sachtouris
            terms = cmd.path.split('_')
265 d252a7a8 Stavros Sachtouris
            best_match, rest = ctree.find_best_match(terms)
266 d252a7a8 Stavros Sachtouris
            if len(terms) > 1:
267 d252a7a8 Stavros Sachtouris
                self.assertEqual(best_match.path, '_'.join(terms[:-1]))
268 d252a7a8 Stavros Sachtouris
            else:
269 d252a7a8 Stavros Sachtouris
                self.assertEqual(best_match, None)
270 d252a7a8 Stavros Sachtouris
            self.assertEqual(rest, terms[-1:])
271 d252a7a8 Stavros Sachtouris
            ctree.add_command(cmd.path, cmd.help, cmd.cmd_class)
272 d252a7a8 Stavros Sachtouris
            best_match, rest = ctree.find_best_match(terms)
273 d252a7a8 Stavros Sachtouris
            self._commands_are_equal(best_match, cmd)
274 d252a7a8 Stavros Sachtouris
            self.assertEqual(rest, [])
275 d252a7a8 Stavros Sachtouris
276 d252a7a8 Stavros Sachtouris
    def test_add_tree(self):
277 d252a7a8 Stavros Sachtouris
        ctree = command_tree.CommandTree('tree', 'the main tree')
278 d252a7a8 Stavros Sachtouris
        ctree1 = command_tree.CommandTree('tree1', 'the first tree')
279 d252a7a8 Stavros Sachtouris
        ctree2 = command_tree.CommandTree('tree2', 'the second tree')
280 d252a7a8 Stavros Sachtouris
281 d252a7a8 Stavros Sachtouris
        cmds = list(self.commands)
282 d252a7a8 Stavros Sachtouris
        del self.commands
283 d252a7a8 Stavros Sachtouris
        cmds1, cmds2 = cmds[:6], cmds[6:]
284 d252a7a8 Stavros Sachtouris
        self.commands = cmds1
285 d252a7a8 Stavros Sachtouris
        self._add_commands(ctree1)
286 d252a7a8 Stavros Sachtouris
        self.commands = cmds2
287 d252a7a8 Stavros Sachtouris
        self._add_commands(ctree2)
288 d252a7a8 Stavros Sachtouris
        self.commands = cmds
289 d252a7a8 Stavros Sachtouris
290 d252a7a8 Stavros Sachtouris
        def check_all(
291 d252a7a8 Stavros Sachtouris
                p1=False, p2=False, p3=False, p4=False, p5=False, p6=False):
292 d252a7a8 Stavros Sachtouris
            for cmd in cmds[:6]:
293 d252a7a8 Stavros Sachtouris
                self.assertEquals(cmd.path in ctree._all_commands, p1)
294 d252a7a8 Stavros Sachtouris
                self.assertEquals(cmd.path in ctree1._all_commands, p2)
295 d252a7a8 Stavros Sachtouris
                if cmd.path != 'cmd':
296 d252a7a8 Stavros Sachtouris
                    self.assertEquals(cmd.path in ctree2._all_commands, p3)
297 d252a7a8 Stavros Sachtouris
            for cmd in cmds[6:]:
298 d252a7a8 Stavros Sachtouris
                self.assertEquals(cmd.path in ctree._all_commands, p4)
299 d252a7a8 Stavros Sachtouris
                if cmd.path != 'cmd':
300 d252a7a8 Stavros Sachtouris
                    self.assertEquals(cmd.path in ctree1._all_commands, p5)
301 d252a7a8 Stavros Sachtouris
                self.assertEquals(cmd.path in ctree2._all_commands, p6)
302 d252a7a8 Stavros Sachtouris
303 d252a7a8 Stavros Sachtouris
        check_all(False, True, False, False, False, True)
304 d252a7a8 Stavros Sachtouris
        ctree.add_tree(ctree1)
305 d252a7a8 Stavros Sachtouris
        check_all(True, True, False, False, False, True)
306 d252a7a8 Stavros Sachtouris
        ctree.add_tree(ctree2)
307 d252a7a8 Stavros Sachtouris
        check_all(True, True, False, True, False, True)
308 d252a7a8 Stavros Sachtouris
        ctree2.add_tree(ctree1)
309 d252a7a8 Stavros Sachtouris
        check_all(True, True, True, True, False, True)
310 d252a7a8 Stavros Sachtouris
311 d252a7a8 Stavros Sachtouris
    def test_has_command(self):
312 d252a7a8 Stavros Sachtouris
        ctree = command_tree.CommandTree('treeHasCommand', 'test has_command')
313 d252a7a8 Stavros Sachtouris
        for cmd in self.commands:
314 d252a7a8 Stavros Sachtouris
            self.assertFalse(ctree.has_command(cmd.path))
315 d252a7a8 Stavros Sachtouris
        self._add_commands(ctree)
316 d252a7a8 Stavros Sachtouris
        for cmd in self.commands:
317 d252a7a8 Stavros Sachtouris
            self.assertTrue(ctree.has_command(cmd.path))
318 d252a7a8 Stavros Sachtouris
        self.assertFalse(ctree.has_command('NON_EXISTING_COMMAND'))
319 d252a7a8 Stavros Sachtouris
320 d252a7a8 Stavros Sachtouris
    def test_get_command(self):
321 d252a7a8 Stavros Sachtouris
        ctree = command_tree.CommandTree('treeGetCommand', 'test get_command')
322 d252a7a8 Stavros Sachtouris
        for cmd in self.commands:
323 d252a7a8 Stavros Sachtouris
            self.assertRaises(KeyError, ctree.get_command, cmd.path)
324 d252a7a8 Stavros Sachtouris
        self._add_commands(ctree)
325 d252a7a8 Stavros Sachtouris
        for cmd in self.commands:
326 d252a7a8 Stavros Sachtouris
            self._commands_are_equal(ctree.get_command(cmd.path), cmd)
327 d252a7a8 Stavros Sachtouris
        self.assertRaises(KeyError, ctree.get_command, 'NON_EXISTNG_COMMAND')
328 d252a7a8 Stavros Sachtouris
329 d252a7a8 Stavros Sachtouris
    def test_subnames(self):
330 d252a7a8 Stavros Sachtouris
        ctree = command_tree.CommandTree('treeSubnames', 'test subnames')
331 d252a7a8 Stavros Sachtouris
        self.assertEqual(ctree.subnames(), [])
332 d252a7a8 Stavros Sachtouris
        self.assertRaises(KeyError, ctree.subnames, 'cmd')
333 d252a7a8 Stavros Sachtouris
        self._add_commands(ctree)
334 d252a7a8 Stavros Sachtouris
        for l1, l2 in (
335 d252a7a8 Stavros Sachtouris
                (ctree.subnames(), ['cmd', 'othercmd']),
336 d252a7a8 Stavros Sachtouris
                (ctree.subnames('cmd'), ['cmd0a', 'cmd0b', 'cmd0c']),
337 d252a7a8 Stavros Sachtouris
                (ctree.subnames('cmd_cmd0a'), ['cmd1a', 'cmd1b']),
338 d252a7a8 Stavros Sachtouris
                (ctree.subnames('cmd_cmd0a_cmd1a'), ['cmd2', ]),
339 d252a7a8 Stavros Sachtouris
                (ctree.subnames('cmd_cmd0a_cmd1b'), ['cmd2', ]),
340 d252a7a8 Stavros Sachtouris
                (ctree.subnames('cmd_cmd0a_cmd1a_cmd2'), []),
341 d252a7a8 Stavros Sachtouris
                (ctree.subnames('cmd_cmd0a_cmd1b_cmd2'), []),
342 d252a7a8 Stavros Sachtouris
                (ctree.subnames('cmd_cmd0b'), []),
343 d252a7a8 Stavros Sachtouris
                (ctree.subnames('cmd_cmd0c'), ['cmd1a', 'cmd1b']),
344 d252a7a8 Stavros Sachtouris
                (ctree.subnames('cmd_cmd0c_cmd1a'), []),
345 d252a7a8 Stavros Sachtouris
                (ctree.subnames('cmd_cmd0c_cmd1b'), ['cmd2', ]),
346 d252a7a8 Stavros Sachtouris
                (ctree.subnames('cmd_cmd0c_cmd1b_cmd2'), []),
347 d252a7a8 Stavros Sachtouris
                (ctree.subnames('othercmd'), [])):
348 d252a7a8 Stavros Sachtouris
            l1.sort(), l2.sort(), self.assertEqual(l1, l2)
349 d252a7a8 Stavros Sachtouris
        self.assertRaises(KeyError, ctree.subnames, 'NON_EXISTNG_CMD')
350 d252a7a8 Stavros Sachtouris
351 d252a7a8 Stavros Sachtouris
    def test_get_subcommands(self):
352 d252a7a8 Stavros Sachtouris
        ctree = command_tree.CommandTree('treeSub', 'test get_subcommands')
353 d252a7a8 Stavros Sachtouris
        self.assertEqual(ctree.get_subcommands(), [])
354 d252a7a8 Stavros Sachtouris
        self.assertRaises(KeyError, ctree.get_subcommands, 'cmd')
355 d252a7a8 Stavros Sachtouris
        self._add_commands(ctree)
356 d252a7a8 Stavros Sachtouris
        for s1, l2 in (
357 d252a7a8 Stavros Sachtouris
            ('', ['cmd', 'othercmd']),
358 d252a7a8 Stavros Sachtouris
            ('cmd', ['cmd0a', 'cmd0b', 'cmd0c']),
359 d252a7a8 Stavros Sachtouris
            ('cmd_cmd0a', ['cmd1a', 'cmd1b']),
360 d252a7a8 Stavros Sachtouris
            ('cmd_cmd0a_cmd1a', ['cmd2', ]),
361 d252a7a8 Stavros Sachtouris
            ('cmd_cmd0a_cmd1b', ['cmd2', ]),
362 d252a7a8 Stavros Sachtouris
            ('cmd_cmd0a_cmd1a_cmd2', []),
363 d252a7a8 Stavros Sachtouris
            ('cmd_cmd0a_cmd1b_cmd2', []),
364 d252a7a8 Stavros Sachtouris
            ('cmd_cmd0b', []),
365 d252a7a8 Stavros Sachtouris
            ('cmd_cmd0c', ['cmd1a', 'cmd1b']),
366 d252a7a8 Stavros Sachtouris
            ('cmd_cmd0c_cmd1a', []),
367 d252a7a8 Stavros Sachtouris
            ('cmd_cmd0c_cmd1b', ['cmd2', ]),
368 d252a7a8 Stavros Sachtouris
            ('cmd_cmd0c_cmd1b_cmd2', []),
369 d252a7a8 Stavros Sachtouris
            ('othercmd', [])
370 d252a7a8 Stavros Sachtouris
        ):
371 d252a7a8 Stavros Sachtouris
            l1 = [cmd.path for cmd in ctree.get_subcommands(s1)]
372 d252a7a8 Stavros Sachtouris
            l2 = ['_'.join([s1, i]) for i in l2] if s1 else l2
373 d252a7a8 Stavros Sachtouris
            l1.sort(), l2.sort(), self.assertEqual(l1, l2)
374 d252a7a8 Stavros Sachtouris
        self.assertRaises(KeyError, ctree.get_subcommands, 'NON_EXISTNG_CMD')
375 0962adb7 Stavros Sachtouris
376 0962adb7 Stavros Sachtouris
377 2fde8651 Stavros Sachtouris
if __name__ == '__main__':
378 2fde8651 Stavros Sachtouris
    from sys import argv
379 2fde8651 Stavros Sachtouris
    from kamaki.cli.test import runTestCase
380 2fde8651 Stavros Sachtouris
    runTestCase(Command, 'Command', argv[1:])
381 0962adb7 Stavros Sachtouris
    runTestCase(CommandTree, 'CommandTree', argv[1:])