def test_parse_out(self): cmd = cmdtree.Command('cmd') cmd.subcommands = dict(cmd0a=cmdtree.Command( 'cmd_cmd0a', subcommands=dict(cmd1=cmdtree.Command('cmd_cmd0a_cmd1'))), cmd0b=cmdtree.Command('cmd_cmd0b')) for invalids in (None, 42, 0.88): self.assertRaises(TypeError, cmd.parse_out, invalids) for c, l, expc, expl in ((cmd, ['cmd'], cmd, ['cmd']), (cmd, ['XXX'], cmd, ['XXX']), (cmd, ['cmd0a'], cmd.subcommands['cmd0a'], []), (cmd, ['XXX', 'cmd0a'], cmd, ['XXX', 'cmd0a']), (cmd, ['cmd0a', 'XXX'], cmd.subcommands['cmd0a'], ['XXX']), (cmd, ['cmd0a', 'cmd0b' ], cmd.subcommands['cmd0a'], ['cmd0b']), (cmd, ['cmd0b', 'XXX'], cmd.subcommands['cmd0b'], ['XXX']), (cmd, ['cmd0a', 'cmd1'], cmd.subcommands['cmd0a'].subcommands['cmd1'], []), (cmd, ['cmd0a', 'cmd1', 'XXX'], cmd.subcommands['cmd0a'].subcommands['cmd1'], ['XXX']), (cmd, ['cmd0a', 'XXX', 'cmd1' ], cmd.subcommands['cmd0a'], ['XXX', 'cmd1'])): self.assertEqual((expc, expl), c.parse_out(l))
def test_is_command(self): cmd = cmdtree.Command('cmd') cmd.subcommands = dict(itis=cmdtree.Command('cmd_itis', cmd_class=Command), itsnot=cmdtree.Command('cmd_itsnot')) self.assertFalse(cmd.is_command) self.assertTrue(cmd.subcommands['itis'].is_command) self.assertFalse(cmd.subcommands['itsnot'].is_command)
def test_name(self): for path in ('cmd', 'cmd_cmd0', 'cmd_cmd0_cmd1', '', None): if path: cmd = cmdtree.Command(path) self.assertEqual(cmd.name, path.split('_')[-1]) else: try: cmdtree.Command(path) except Exception as e: self.assertTrue(isinstance(e, AssertionError))
def test_parent_path(self): cmd = cmdtree.Command('cmd') cmd.subcommands = dict(cmd0a=cmdtree.Command( 'cmd_cmd0a', subcommands=dict(cmd1=cmdtree.Command('cmd_cmd0a_cmd1'))), cmd0b=cmdtree.Command('cmd_cmd0b')) self.assertEqual(cmd.parent_path, '') self.assertEqual(cmd.subcommands['cmd0a'].parent_path, cmd.path) self.assertEqual(cmd.subcommands['cmd0b'].parent_path, cmd.path) cmd0a = cmd.subcommands['cmd0a'] self.assertEqual(cmd0a.subcommands['cmd1'].parent_path, cmd0a.path)
def test_get_subcmd(self): cmd = cmdtree.Command('cmd') cmd.subcommands = dict(cmd0a=cmdtree.Command( 'cmd_cmd0a', subcommands=dict(cmd1=cmdtree.Command('cmd_cmd0a_cmd1'))), cmd0b=cmdtree.Command('cmd_cmd0b')) for subname in ('', None, 'cmd0a', 'cmd1', 'cmd0b'): try: expected = cmd.subcommands[subname] if subname else None except KeyError: expected = None self.assertEqual(cmd.get_subcmd(subname), expected)
def test_add_subcmd(self): cmd = cmdtree.Command('cmd') for subname in (None, 'cmd0', 'cmd_cmd0'): if subname: subcmd = cmdtree.Command(subname) if subname.startswith(cmd.name + '_'): self.assertTrue(cmd.add_subcmd(subcmd)) self.assertTrue(subcmd.name in cmd.subcommands) else: self.assertFalse(cmd.add_subcmd(subcmd)) self.assertTrue(len(cmd.subcommands) == 0) else: self.assertRaises(AttributeError, cmd.add_subcmd, subname)
def test_contains(self): cmd = cmdtree.Command('cmd') for subname in ('', 'cmd0'): self.assertFalse(cmd.contains(subname)) cmd.subcommands = dict(cmd0a=cmdtree.Command('cmd_cmd0a'), cmd0b=cmdtree.Command('cmd_cmd0b')) for subname in ('cmd0a', 'cmd0b'): self.assertTrue(cmd.contains(subname)) for subname in ('', 'cmd0c'): self.assertFalse(cmd.contains(subname)) cmd.subcommands['cmd0a'].subcommands = dict( cmd1=cmdtree.Command('cmd_cmd0a_cmd1')) for subname in ('cmd0a', 'cmd0b'): self.assertTrue(cmd.contains(subname)) for subname in ('', 'cmd0c', 'cmd1', 'cmd0a_cmd1'): self.assertFalse(cmd.contains(subname))
def test___init__(self): for args in product((None, '', 'cmd'), (None, '', 'Some help'), (None, '', {}, dict(cmd0a=None, cmd0b=None)), (None, cmdtree.Command('cmd_cmd0')), (None, 'long description')): path, help, subcommands, cmd_class, long_help = args try: cmd = cmdtree.Command(*args) except Exception as e: if path: raise self.assertTrue(isinstance(e, AssertionError)) continue self.assertEqual(cmd.help, help or '') self.assertEqual(cmd.subcommands, subcommands or {}) self.assertEqual(cmd.cmd_class, cmd_class or None) self.assertEqual(cmd.long_help, long_help or '')
def setUp(self): cmd = cmdtree.Command( 'cmd', subcommands=dict( cmd0a=cmdtree.Command( 'cmd_cmd0a', subcommands=dict( cmd1a=cmdtree.Command( 'cmd_cmd0a_cmd1a', subcommands=dict(cmd2=cmdtree.Command( 'cmd_cmd0a_cmd1a_cmd2', cmd_class=Command)), ), cmd1b=cmdtree.Command( 'cmd_cmd0a_cmd1b', subcommands=dict(cmd2=cmdtree.Command( 'cmd_cmd0a_cmd1b_cmd2', cmd_class=Command)), ))), cmd0b=cmdtree.Command('cmd_cmd0b'), cmd0c=cmdtree.Command( 'cmd_cmd0c', subcommands=dict( cmd1a=cmdtree.Command('cmd_cmd0c_cmd1a'), cmd1b=cmdtree.Command( 'cmd_cmd0c_cmd1b', subcommands=dict(cmd2=cmdtree.Command( 'cmd_cmd0c_cmd1b_cmd2', cmd_class=Command)), ))))) self.commands = [ cmd, cmd.subcommands['cmd0a'], cmd.subcommands['cmd0a'].subcommands['cmd1a'], cmd.subcommands['cmd0a'].subcommands['cmd1a'].subcommands['cmd2'], cmd.subcommands['cmd0a'].subcommands['cmd1b'], cmd.subcommands['cmd0a'].subcommands['cmd1b'].subcommands['cmd2'], cmd.subcommands['cmd0b'], cmd.subcommands['cmd0c'], cmd.subcommands['cmd0c'].subcommands['cmd1a'], cmd.subcommands['cmd0c'].subcommands['cmd1b'], cmd.subcommands['cmd0c'].subcommands['cmd1b'].subcommands['cmd2'], cmdtree.Command('othercmd') ]