def test_get_all_commands(self): class TestImpl(Cmd): def do_test(self): pass expected_commands = { 'exit': Command('exit', Cmd.do_exit), 'EOF': Command('EOF', Cmd.do_EOF), 'get_error': Command('get_error', Cmd.do_get_error), 'help': Command('help', Cmd.do_help), 'test': Command('test', TestImpl.do_test) } self.assertEqual(expected_commands, TestImpl()._get_all_commands())
def test_complete_simple(self): def do_test(self, arg_first: str, arg_second: int, third: str): pass cmds = CommandsDict() cmds['test'] = Command('test', do_test) completer = Completer(cmds) self.assertEqual(list(completer.get_completions(Document(text='t', cursor_position=1))), [Completion('test', start_position=-1)]) self.assertEqual(list(completer.get_completions(Document(text='test ', cursor_position=5))), [Completion('arg_first', start_position=0, display_meta=str.__name__), Completion('arg_second', start_position=0, display_meta=int.__name__), Completion('third', start_position=0, display_meta=str.__name__)]) self.assertEqual(list(completer.get_completions(Document(text='test arg', cursor_position=8))), [Completion('arg_first', start_position=-3, display_meta=str.__name__), Completion('arg_second', start_position=-3, display_meta=int.__name__)]) self.assertEqual(list(completer.get_completions(Document(text='test arg_f', cursor_position=10))), [Completion('arg_first', start_position=-5, display_meta=str.__name__)])
def test_complete_custom_completer_legacy(self): class TestType(object): @test_utils.static_mock @staticmethod def powercmd_complete(text): # test completer returning a list of strings instead of List[Completion] return ['complete'] if 'complete'.startswith(text) else [] def __init__(self, s): self.s = s def do_test(self, arg: TestType): pass cmds = CommandsDict() cmds['test'] = Command('test', do_test) completer = Completer(cmds) with TestType.powercmd_complete.expect_call('arg'): self.assertEqual(list(completer.get_completions(Document(text='test arg', cursor_position=8))), [Completion('arg', start_position=-3, display_meta=TestType.__name__)]) with TestType.powercmd_complete.expect_call('c'): self.assertEqual(list(completer.get_completions(Document(text='test arg=c', cursor_position=9))), [Completion('complete', start_position=-1)])
def test_parameters(self): def func(a: int, b: str = 'x'): pass cmd = Command(name='func', handler=func) self.assertEqual( cmd.parameters, { 'a': Parameter(name='a', type=int, default=inspect._empty), 'b': Parameter(name='b', type=str, default='x') })
def test_short_description(self): def func(): """ Test function. Detailed description follows. """ cmd = Command(name='func', handler=func) self.assertEqual(cmd.short_description, 'Test function.')
def test_construct_tuple(self): @test_utils.mock def do_test(self, arg: Tuple[float, str]): pass cmds = CommandsDict() cmds['test'] = Command('test', do_test) invoker = CommandInvoker(cmds) with do_test.expect_call(arg=(float(3), 'foo')): invoker.invoke(self, cmdline=CommandLine('test arg=(3,foo)'))
def test_invoke_primitive_str(self): @test_utils.mock def do_test(self, str_var: str): pass cmds = CommandsDict() cmds['test'] = Command('test', do_test) invoker = CommandInvoker(cmds) with do_test.expect_call(str_var='str'): invoker.invoke(self, cmdline=CommandLine('test str'))
def test_invoke_primitive_int(self): @test_utils.mock def do_test(self, int_var: int): pass cmds = CommandsDict() cmds['test'] = Command('test', do_test) invoker = CommandInvoker(cmds) with do_test.expect_call(int_var=42): invoker.invoke(self, cmdline=CommandLine('test 42'))
def test_invoke_list(self): @test_utils.mock def do_test(self, arg: List[int]): pass cmds = CommandsDict() cmds['test'] = Command('test', do_test) invoker = CommandInvoker(cmds) with do_test.expect_call(arg=[3, 42]): invoker.invoke(self, cmdline=CommandLine('test arg=[3,42]'))
def test_invoke_mixed_free_named(self): @test_utils.mock def do_test(self, first: str, second: int): pass cmds = CommandsDict() cmds['test'] = Command('test', do_test) invoker = CommandInvoker(cmds) with do_test.expect_call(first='first', second=2): invoker.invoke(self, cmdline=CommandLine('test first second=2'))
def test_construct_union(self): @test_utils.mock def do_test(self, arg: Union[float, str]): pass cmds = CommandsDict() cmds['test'] = Command('test', do_test) invoker = CommandInvoker(cmds) with do_test.expect_call(arg=3.14): invoker.invoke(self, cmdline=CommandLine('test arg=3.14')) with do_test.expect_call(arg='test_arg'): invoker.invoke(self, cmdline=CommandLine('test arg=test_arg'))
def test_invoke_class(self): class ClassConstructor(str): pass @test_utils.mock def do_test(self, cls_var: ClassConstructor): pass cmds = CommandsDict() cmds['test'] = Command('test', do_test) invoker = CommandInvoker(cmds) with do_test.expect_call(cls_var=ClassConstructor('cls')): invoker.invoke(self, cmdline=CommandLine('test cls'))
def test_complete_list(self): def do_test(self, arg: List[TestEnum]): pass cmds = CommandsDict() cmds['test'] = Command('test', do_test) completer = Completer(cmds) self.assertEqual(list(completer.get_completions(Document(text='test arg=', cursor_position=9))), [Completion('First', start_position=0, display_meta='1'), Completion('Second', start_position=0, display_meta='2')]) self.assertEqual(list(completer.get_completions(Document(text='test arg=First,', cursor_position=14))), [Completion('First', start_position=0, display_meta='1'), Completion('Second', start_position=0, display_meta='2')])
def test_get_current_arg(self): def do_foo(self, bar: str = '', baz: str = ''): pass cmd = Command('foo', do_foo) self.assertEqual(CommandLine('').get_current_arg(cmd), None) self.assertEqual(CommandLine('foo').get_current_arg(cmd), None) self.assertEqual(CommandLine('foo ').get_current_arg(cmd), IncompleteArg(Parameter('bar', str, ''), '')) self.assertEqual(CommandLine('foo arg').get_current_arg(cmd), IncompleteArg(Parameter('bar', str, ''), 'arg')) self.assertEqual(CommandLine('foo arg ').get_current_arg(cmd), IncompleteArg(Parameter('baz', str, ''), '')) self.assertEqual(CommandLine('foo arg arg').get_current_arg(cmd), IncompleteArg(Parameter('baz', str, ''), 'arg')) self.assertEqual(CommandLine('foo baz=arg bar=').get_current_arg(cmd), IncompleteArg(Parameter('bar', str, ''), ''))
def test_complete_tuple(self): class TestEnum2(enum.Enum): A = 1 B = 2 def do_test(self, arg: Tuple[TestEnum, TestEnum2]): pass cmds = CommandsDict() cmds['test'] = Command('test', do_test) completer = Completer(cmds) self.assertEqual(list(completer.get_completions(Document(text='test arg=', cursor_position=9))), [Completion('First', start_position=0, display_meta='1'), Completion('Second', start_position=0, display_meta='2')]) self.assertEqual(list(completer.get_completions(Document(text='test arg=First,', cursor_position=14))), [Completion('A', start_position=0, display_meta='1'), Completion('B', start_position=0, display_meta='2')]) self.assertEqual(list(completer.get_completions(Document(text='test arg=First,A,', cursor_position=16))), [])
def _get_all_commands(self) -> CommandsDict: """Returns all defined commands.""" import types def unbind(f): """ Returns the base function if the argument is a bound one. https://bugs.python.org/msg166144 """ if not callable(f): raise TypeError('%s is not callable' % (repr(f), )) self = getattr(f, '__self__', None) if (self is not None and not isinstance(self, types.ModuleType) and not isinstance(self, type)): if hasattr(f, '__func__'): return f.__func__ return getattr(type(f.__self__), f.__name__) return f members = inspect.getmembers(self) prefixes = self.get_command_prefixes() commands = CommandsDict() for name, handler in members: if not callable(handler): continue for prefix, substitution in prefixes.items(): if name.startswith(prefix): assert substitution + name not in commands cmd_name = substitution + name[len(prefix):] commands[cmd_name] = Command(name=cmd_name, handler=unbind(handler)) return commands
def test_complete_union(self): class TestEnum1(enum.Enum): A1 = 1 B1 = 2 class TestEnum2(enum.Enum): A2 = 3 C2 = 4 def do_test(self, arg: Union[TestEnum1, TestEnum2]): pass cmds = CommandsDict() cmds['test'] = Command('test', do_test) completer = Completer(cmds) self.assertEqual(list(completer.get_completions(Document(text='test ', cursor_position=5))), [Completion('arg', start_position=0, display_meta=str(Union[TestEnum1, TestEnum2])), Completion('A1', start_position=0, display_meta='1'), Completion('B1', start_position=0, display_meta='2'), Completion('A2', start_position=0, display_meta='3'), Completion('C2', start_position=0, display_meta='4')]) self.assertEqual(list(completer.get_completions(Document(text='test arg=', cursor_position=9))), [Completion('A1', start_position=0, display_meta='1'), Completion('B1', start_position=0, display_meta='2'), Completion('A2', start_position=0, display_meta='3'), Completion('C2', start_position=0, display_meta='4')]) self.assertEqual(list(completer.get_completions(Document(text='test arg=A', cursor_position=10))), [Completion('A1', start_position=-1, display_meta='1'), Completion('A2', start_position=-1, display_meta='3')]) self.assertEqual(list(completer.get_completions(Document(text='test arg=C', cursor_position=10))), [Completion('C2', start_position=-1, display_meta='4')])