diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2018-09-27 20:01:02 -0400 |
---|---|---|
committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2018-09-27 20:01:02 -0400 |
commit | 74129347da01bae550a56f3ecc09be7aec64d5c8 (patch) | |
tree | f469a7a3ea264c05effc3560a826275cc641c1a2 | |
parent | b79790d5e8259376cbf0201d1d69f9895492aeef (diff) | |
download | cmd2-git-74129347da01bae550a56f3ecc09be7aec64d5c8.tar.gz |
Made _func_named() more reliable
Added unit tests
-rw-r--r-- | cmd2/cmd2.py | 25 | ||||
-rw-r--r-- | tests/conftest.py | 4 | ||||
-rw-r--r-- | tests/test_completion.py | 19 |
3 files changed, 28 insertions, 20 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index 09fec115..62c188b4 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -2009,11 +2009,8 @@ class Cmd(cmd.Cmd): :param arg: command to look up method name which implements it :return: method name which implements the given command """ - result = None target = 'do_' + arg - if target in dir(self): - result = target - return result + return target if callable(getattr(self, target, None)) else '' def onecmd(self, statement: Union[Statement, str]) -> bool: """ This executes the actual do_* method for a command. @@ -2033,21 +2030,17 @@ class Cmd(cmd.Cmd): stop = self._run_macro(statement) else: funcname = self._func_named(statement.command) - if not funcname: - self.default(statement) - return False + if funcname: + func = getattr(self, funcname) + stop = func(statement) - # Since we have a valid command store it in the history - if statement.command not in self.exclude_from_history: - self.history.append(statement.raw) + # Since we have a valid command store it in the history + if statement.command not in self.exclude_from_history: + self.history.append(statement.raw) - try: - func = getattr(self, funcname) - except AttributeError: + else: self.default(statement) - return False - - stop = func(statement) + stop = False return stop diff --git a/tests/conftest.py b/tests/conftest.py index b86622ac..949d5419 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -160,11 +160,9 @@ def complete_tester(text: str, line: str, begidx: int, endidx: int, app) -> Opti def get_endidx(): return endidx - first_match = None with mock.patch.object(readline, 'get_line_buffer', get_line): with mock.patch.object(readline, 'get_begidx', get_begidx): with mock.patch.object(readline, 'get_endidx', get_endidx): # Run the readline tab-completion function with readline mocks in place first_match = app.complete(text, 0) - - return first_match + return first_match diff --git a/tests/test_completion.py b/tests/test_completion.py index 40299954..8741fbd5 100644 --- a/tests/test_completion.py +++ b/tests/test_completion.py @@ -15,7 +15,7 @@ import sys import pytest import cmd2 from cmd2 import utils -from .conftest import complete_tester +from .conftest import base_app, complete_tester, normalize, run_cmd from examples.subcommands import SubcommandsExample # List of strings used with completion functions @@ -113,6 +113,23 @@ def test_complete_bogus_command(cmd2_app): first_match = complete_tester(text, line, begidx, endidx, cmd2_app) assert first_match is None +def test_complete_macro(base_app, request): + # Create the macro + out = run_cmd(base_app, 'macro create fake pyscript {1}') + assert out == normalize("Macro 'fake' created") + + # Macros do path completion + test_dir = os.path.dirname(request.module.__file__) + + text = os.path.join(test_dir, 'script.py') + line = 'fake {}'.format(text) + + endidx = len(line) + begidx = endidx - len(text) + + first_match = complete_tester(text, line, begidx, endidx, base_app) + assert first_match == text + ' ' + def test_cmd2_command_completion_multiple(cmd2_app): text = 'h' |