Skip to content

Commit b4fa4c9

Browse files
committed
Added unit tests for CompletionError
1 parent bf099c8 commit b4fa4c9

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

cmd2/argparse_custom.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def my_completer_function(text, line, begidx, endidx):
9494
as dynamic. Therefore it is up to the developer to validate if the user has typed an acceptable value for these
9595
arguments.
9696
97-
The following functions exist in cases where you may want to manually add choice providing function/methods to
97+
The following functions exist in cases where you may want to manually a add choice-providing function/method to
9898
an existing argparse action. For instance, in __init__() of a custom action class.
9999
100100
set_choices_function(action, func)
@@ -116,6 +116,13 @@ def my_completer_method(self, text, line, begidx, endidx, arg_tokens)
116116
their values. All tokens are stored in the dictionary as the raw strings provided on the command line. It is up to
117117
the developer to determine if the user entered the correct argument type (e.g. int) and validate their values.
118118
119+
CompletionError Class:
120+
Raised during tab-completion operations to report any sort of error you want printed by the AutoCompleter
121+
122+
Example use cases
123+
- Reading a database to retrieve a tab completion data set failed
124+
- A previous command line argument that determines the data set being completed is invalid
125+
119126
CompletionItem Class:
120127
This class was added to help in cases where uninformative data is being tab completed. For instance,
121128
tab completing ID numbers isn't very helpful to a user without context. Returning a list of CompletionItems
@@ -226,8 +233,8 @@ class CompletionError(Exception):
226233
Raised during tab-completion operations to report any sort of error you want printed by the AutoCompleter
227234
228235
Example use cases
229-
1. Reading a database to retrieve a tab completion data set failed
230-
2. A previous command line argument that determines the data set being completed is invalid
236+
- Reading a database to retrieve a tab completion data set failed
237+
- A previous command line argument that determines the data set being completed is invalid
231238
"""
232239
pass
233240

tests/test_argparse_completer.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import pytest
1010

1111
import cmd2
12-
from cmd2 import with_argparser, Cmd2ArgumentParser, CompletionItem
12+
from cmd2 import with_argparser, Cmd2ArgumentParser, CompletionError, CompletionItem
1313
from cmd2.utils import StdSim, basic_complete
1414
from .conftest import run_cmd, complete_tester
1515

@@ -210,6 +210,27 @@ def do_nargs(self, args: argparse.Namespace) -> None:
210210
def do_hint(self, args: argparse.Namespace) -> None:
211211
pass
212212

213+
############################################################################################################
214+
# Begin code related to CompletionError
215+
############################################################################################################
216+
def completer_raise_error(self, text: str, line: str, begidx: int, endidx: int) -> List[str]:
217+
"""Raises CompletionError"""
218+
raise CompletionError('completer broke something')
219+
220+
def choice_raise_error(self) -> List[str]:
221+
"""Raises CompletionError"""
222+
raise CompletionError('choice broke something')
223+
224+
comp_error_parser = Cmd2ArgumentParser()
225+
comp_error_parser.add_argument('completer', help='positional arg',
226+
completer_method=completer_raise_error)
227+
comp_error_parser.add_argument('--choice', help='flag arg',
228+
choices_method=choice_raise_error)
229+
230+
@with_argparser(comp_error_parser)
231+
def do_raise_completion_error(self, args: argparse.Namespace) -> None:
232+
pass
233+
213234
############################################################################################################
214235
# Begin code related to receiving arg_tokens
215236
############################################################################################################
@@ -723,6 +744,25 @@ def test_autocomp_hint_no_help_text(ac_app, capsys):
723744
'''
724745

725746

747+
@pytest.mark.parametrize('args, text', [
748+
# Exercise a flag arg and choices function that raises a CompletionError
749+
('--choice ', 'choice'),
750+
751+
# Exercise a positional arg and completer that raises a CompletionError
752+
('', 'completer')
753+
])
754+
def test_completion_error(ac_app, capsys, args, text):
755+
line = 'raise_completion_error {} {}'.format(args, text)
756+
endidx = len(line)
757+
begidx = endidx - len(text)
758+
759+
first_match = complete_tester(text, line, begidx, endidx, ac_app)
760+
out, err = capsys.readouterr()
761+
762+
assert first_match is None
763+
assert "{} broke something".format(text) in out
764+
765+
726766
@pytest.mark.parametrize('command_and_args, completions', [
727767
# Exercise a choices function that receives arg_tokens dictionary
728768
('arg_tokens choice subcmd', ['choice', 'subcmd']),

0 commit comments

Comments
 (0)