def test___init__(self): name, description = 'sampleTree', 'a sample Tree' ctree = cmdtree.CommandTree(name) for attr, exp in (('groups', {}), ('_all_commands', {}), ('name', name), ('description', '')): self.assertEqual(getattr(ctree, attr), exp) ctree = cmdtree.CommandTree(name, description) for attr, exp in (('groups', {}), ('_all_commands', {}), ('name', name), ('description', description)): self.assertEqual(getattr(ctree, attr), exp)
def test_get_command(self): ctree = cmdtree.CommandTree('treeGetCommand', 'test get_command') for cmd in self.commands: self.assertRaises(KeyError, ctree.get_command, cmd.path) self._add_commands(ctree) for cmd in self.commands: self._commands_are_equal(ctree.get_command(cmd.path), cmd) self.assertRaises(KeyError, ctree.get_command, 'NON_EXISTNG_COMMAND')
def test_has_command(self): ctree = cmdtree.CommandTree('treeHasCommand', 'test has_command') for cmd in self.commands: self.assertFalse(ctree.has_command(cmd.path)) self._add_commands(ctree) for cmd in self.commands: self.assertTrue(ctree.has_command(cmd.path)) self.assertFalse(ctree.has_command('NON_EXISTING_COMMAND'))
def test_add_command(self): ctree = cmdtree.CommandTree('addCommand', 'test add_command') self._add_commands(ctree) for cmd in self.commands: self.assertTrue(cmd, ctree._all_commands) if cmd.path.count('_'): self.assertFalse(cmd.name in ctree.groups) else: self.assertTrue(cmd.name in ctree.groups) self._commands_are_equal(cmd, ctree.groups[cmd.name])
def test_exclude(self): ctree = cmdtree.CommandTree('excludeTree', 'test exclude group') exp = dict() for cmd in self.commands[0:6]: ctree.groups[cmd.name] = cmd exp[cmd.name] = cmd self.assertEqual(exp, ctree.groups) ctree.exclude(exp.keys()[1::2]) for key in exp.keys()[1::2]: exp.pop(key) self.assertEqual(exp, ctree.groups)
def test_add_tree(self): ctree = cmdtree.CommandTree('tree', 'the main tree') ctree1 = cmdtree.CommandTree('tree1', 'the first tree') ctree2 = cmdtree.CommandTree('tree2', 'the second tree') cmds = list(self.commands) del self.commands cmds1, cmds2 = cmds[:6], cmds[6:] self.commands = cmds1 self._add_commands(ctree1) self.commands = cmds2 self._add_commands(ctree2) self.commands = cmds def check_all(p1=False, p2=False, p3=False, p4=False, p5=False, p6=False): for cmd in cmds[:6]: self.assertEquals(cmd.path in ctree._all_commands, p1) self.assertEquals(cmd.path in ctree1._all_commands, p2) if cmd.path != 'cmd': self.assertEquals(cmd.path in ctree2._all_commands, p3) for cmd in cmds[6:]: self.assertEquals(cmd.path in ctree._all_commands, p4) if cmd.path != 'cmd': self.assertEquals(cmd.path in ctree1._all_commands, p5) self.assertEquals(cmd.path in ctree2._all_commands, p6) check_all(False, True, False, False, False, True) ctree.add_tree(ctree1) check_all(True, True, False, False, False, True) ctree.add_tree(ctree2) check_all(True, True, False, True, False, True) ctree2.add_tree(ctree1) check_all(True, True, True, True, False, True)
def test_find_best_match(self): ctree = cmdtree.CommandTree('bestMatch', 'test find_best_match') for cmd in self.commands: terms = cmd.path.split('_') best_match, rest = ctree.find_best_match(terms) if len(terms) > 1: self.assertEqual(best_match.path, '_'.join(terms[:-1])) else: self.assertEqual(best_match, None) self.assertEqual(rest, terms[-1:]) ctree.add_command(cmd.path, cmd.help, cmd.cmd_class) best_match, rest = ctree.find_best_match(terms) self._commands_are_equal(best_match, cmd) self.assertEqual(rest, [])
def test_get_subcommands(self): ctree = cmdtree.CommandTree('treeSub', 'test get_subcommands') self.assertEqual(ctree.get_subcommands(), []) self.assertRaises(KeyError, ctree.get_subcommands, 'cmd') self._add_commands(ctree) for s1, l2 in (('', ['cmd', 'othercmd']), ('cmd', [ 'cmd0a', 'cmd0b', 'cmd0c' ]), ('cmd_cmd0a', ['cmd1a', 'cmd1b']), ('cmd_cmd0a_cmd1a', [ 'cmd2', ]), ('cmd_cmd0a_cmd1b', [ 'cmd2', ]), ('cmd_cmd0a_cmd1a_cmd2', []), ('cmd_cmd0a_cmd1b_cmd2', []), ('cmd_cmd0b', []), ('cmd_cmd0c', ['cmd1a', 'cmd1b']), ('cmd_cmd0c_cmd1a', []), ('cmd_cmd0c_cmd1b', [ 'cmd2', ]), ('cmd_cmd0c_cmd1b_cmd2', []), ('othercmd', [])): l1 = [cmd.path for cmd in ctree.get_subcommands(s1)] l2 = ['_'.join([s1, i]) for i in l2] if s1 else l2 l1.sort(), l2.sort(), self.assertEqual(l1, l2) self.assertRaises(KeyError, ctree.get_subcommands, 'NON_EXISTNG_CMD')
def test_subnames(self): ctree = cmdtree.CommandTree('treeSubnames', 'test subnames') self.assertEqual(ctree.subnames(), []) self.assertRaises(KeyError, ctree.subnames, 'cmd') self._add_commands(ctree) for l1, l2 in ((ctree.subnames(), ['cmd', 'othercmd']), (ctree.subnames('cmd'), ['cmd0a', 'cmd0b', 'cmd0c']), (ctree.subnames('cmd_cmd0a'), ['cmd1a', 'cmd1b']), (ctree.subnames('cmd_cmd0a_cmd1a'), [ 'cmd2', ]), (ctree.subnames('cmd_cmd0a_cmd1b'), [ 'cmd2', ]), (ctree.subnames('cmd_cmd0a_cmd1a_cmd2'), []), (ctree.subnames('cmd_cmd0a_cmd1b_cmd2'), []), (ctree.subnames('cmd_cmd0b'), []), (ctree.subnames('cmd_cmd0c'), ['cmd1a', 'cmd1b']), (ctree.subnames('cmd_cmd0c_cmd1a'), []), (ctree.subnames('cmd_cmd0c_cmd1b'), [ 'cmd2', ]), (ctree.subnames('cmd_cmd0c_cmd1b_cmd2'), []), (ctree.subnames('othercmd'), [])): l1.sort(), l2.sort(), self.assertEqual(l1, l2) self.assertRaises(KeyError, ctree.subnames, 'NON_EXISTNG_CMD')