Skip to content

Commit 7412934

Browse files
committed
Made _func_named() more reliable
Added unit tests
1 parent b79790d commit 7412934

File tree

3 files changed

+28
-20
lines changed

3 files changed

+28
-20
lines changed

cmd2/cmd2.py

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2009,11 +2009,8 @@ def _func_named(self, arg: str) -> str:
20092009
:param arg: command to look up method name which implements it
20102010
:return: method name which implements the given command
20112011
"""
2012-
result = None
20132012
target = 'do_' + arg
2014-
if target in dir(self):
2015-
result = target
2016-
return result
2013+
return target if callable(getattr(self, target, None)) else ''
20172014

20182015
def onecmd(self, statement: Union[Statement, str]) -> bool:
20192016
""" This executes the actual do_* method for a command.
@@ -2033,21 +2030,17 @@ def onecmd(self, statement: Union[Statement, str]) -> bool:
20332030
stop = self._run_macro(statement)
20342031
else:
20352032
funcname = self._func_named(statement.command)
2036-
if not funcname:
2037-
self.default(statement)
2038-
return False
2033+
if funcname:
2034+
func = getattr(self, funcname)
2035+
stop = func(statement)
20392036

2040-
# Since we have a valid command store it in the history
2041-
if statement.command not in self.exclude_from_history:
2042-
self.history.append(statement.raw)
2037+
# Since we have a valid command store it in the history
2038+
if statement.command not in self.exclude_from_history:
2039+
self.history.append(statement.raw)
20432040

2044-
try:
2045-
func = getattr(self, funcname)
2046-
except AttributeError:
2041+
else:
20472042
self.default(statement)
2048-
return False
2049-
2050-
stop = func(statement)
2043+
stop = False
20512044

20522045
return stop
20532046

tests/conftest.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,9 @@ def get_begidx():
160160
def get_endidx():
161161
return endidx
162162

163-
first_match = None
164163
with mock.patch.object(readline, 'get_line_buffer', get_line):
165164
with mock.patch.object(readline, 'get_begidx', get_begidx):
166165
with mock.patch.object(readline, 'get_endidx', get_endidx):
167166
# Run the readline tab-completion function with readline mocks in place
168167
first_match = app.complete(text, 0)
169-
170-
return first_match
168+
return first_match

tests/test_completion.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import pytest
1616
import cmd2
1717
from cmd2 import utils
18-
from .conftest import complete_tester
18+
from .conftest import base_app, complete_tester, normalize, run_cmd
1919
from examples.subcommands import SubcommandsExample
2020

2121
# List of strings used with completion functions
@@ -113,6 +113,23 @@ def test_complete_bogus_command(cmd2_app):
113113
first_match = complete_tester(text, line, begidx, endidx, cmd2_app)
114114
assert first_match is None
115115

116+
def test_complete_macro(base_app, request):
117+
# Create the macro
118+
out = run_cmd(base_app, 'macro create fake pyscript {1}')
119+
assert out == normalize("Macro 'fake' created")
120+
121+
# Macros do path completion
122+
test_dir = os.path.dirname(request.module.__file__)
123+
124+
text = os.path.join(test_dir, 'script.py')
125+
line = 'fake {}'.format(text)
126+
127+
endidx = len(line)
128+
begidx = endidx - len(text)
129+
130+
first_match = complete_tester(text, line, begidx, endidx, base_app)
131+
assert first_match == text + ' '
132+
116133

117134
def test_cmd2_command_completion_multiple(cmd2_app):
118135
text = 'h'

0 commit comments

Comments
 (0)