summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2020-08-18 12:06:47 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2020-08-18 12:06:47 -0400
commitb5e08b06b4032d8656fa768814069b17ea613b62 (patch)
tree0f942f22d57d2686ed44dde2aec3583767d7d54f /tests
parente8aa84b39872956cf881e845b8464a3807b58a6e (diff)
downloadcmd2-git-b5e08b06b4032d8656fa768814069b17ea613b62.tar.gz
Documented support for standalone functions being used as completers and choices_providers.
Added unit tests for this case.
Diffstat (limited to 'tests')
-rw-r--r--tests/test_argparse_completer.py39
1 files changed, 39 insertions, 0 deletions
diff --git a/tests/test_argparse_completer.py b/tests/test_argparse_completer.py
index b896a9bd..a964aa46 100644
--- a/tests/test_argparse_completer.py
+++ b/tests/test_argparse_completer.py
@@ -13,6 +13,19 @@ from cmd2 import Cmd2ArgumentParser, CompletionError, CompletionItem, with_argpa
from cmd2.utils import StdSim
from .conftest import complete_tester, run_cmd
+# Data and functions for testing standalone choice_provider and completer
+standalone_choices = ['standalone', 'provider']
+standalone_completions = ['standalone', 'completer']
+
+
+# noinspection PyUnusedLocal
+def standalone_choice_provider(cli: cmd2.Cmd) -> List[str]:
+ return standalone_choices
+
+
+def standalone_completer(cli: cmd2.Cmd, text: str, line: str, begidx: int, endidx: int) -> List[str]:
+ return cli.basic_complete(text, line, begidx, endidx, standalone_completions)
+
# noinspection PyMethodMayBeStatic,PyUnusedLocal,PyProtectedMember
class AutoCompleteTester(cmd2.Cmd):
@@ -263,6 +276,17 @@ class AutoCompleteTester(cmd2.Cmd):
def do_mutex(self, args: argparse.Namespace) -> None:
pass
+ ############################################################################################################
+ # Begin code related to standalone functions
+ ############################################################################################################
+ standalone_parser = Cmd2ArgumentParser()
+ standalone_parser.add_argument('--provider', help='standalone provider', choices_provider=standalone_choice_provider)
+ standalone_parser.add_argument('--completer', help='standalone completer', completer=standalone_completer)
+
+ @with_argparser(standalone_parser)
+ def do_standalone(self, args: argparse.Namespace) -> None:
+ pass
+
@pytest.fixture
def ac_app():
@@ -935,3 +959,18 @@ def test_complete_command_help_no_tokens(ac_app):
completions = ac.complete_subcommand_help(tokens=[], text='', line='', begidx=0, endidx=0)
assert not completions
+
+
+@pytest.mark.parametrize('flag, completions', [
+ ('--provider', standalone_choices),
+ ('--completer', standalone_completions)
+])
+def test_complete_standalone(ac_app, flag, completions):
+ text = ''
+ line = 'standalone {} {}'.format(flag, text)
+ endidx = len(line)
+ begidx = endidx - len(text)
+
+ first_match = complete_tester(text, line, begidx, endidx, ac_app)
+ assert first_match is not None
+ assert ac_app.completion_matches == sorted(completions, key=ac_app.default_sort_key)