diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_acargparse.py | 76 | ||||
-rw-r--r-- | tests/test_autocompletion.py | 22 |
2 files changed, 98 insertions, 0 deletions
diff --git a/tests/test_acargparse.py b/tests/test_acargparse.py new file mode 100644 index 00000000..01b4dce9 --- /dev/null +++ b/tests/test_acargparse.py @@ -0,0 +1,76 @@ +""" +Unit/functional testing for readline tab-completion functions in the cmd2.py module. + +These are primarily tests related to readline completer functions which handle tab-completion of cmd2/cmd commands, +file system paths, and shell commands. + +Copyright 2017 Todd Leonhardt <todd.leonhardt@gmail.com> +Released under MIT license, see LICENSE file +""" +import argparse +import os +import sys + +import cmd2 +from unittest import mock +import pytest +from AutoCompleter import ACArgumentParser + +# Prefer statically linked gnureadline if available (for macOS compatibility due to issues with libedit) +try: + import gnureadline as readline +except ImportError: + # Try to import readline, but allow failure for convenience in Windows unit testing + # Note: If this actually fails, you should install readline on Linux or Mac or pyreadline on Windows + try: + # noinspection PyUnresolvedReferences + import readline + except ImportError: + pass + + +def test_acarg_narg_empty_tuple(): + with pytest.raises(ValueError) as excinfo: + parser = ACArgumentParser(prog='test') + parser.add_argument('invalid_tuple', nargs=()) + assert 'Ranged values for nargs must be a tuple of 2 integers' in str(excinfo.value) + + +def test_acarg_narg_single_tuple(): + with pytest.raises(ValueError) as excinfo: + parser = ACArgumentParser(prog='test') + parser.add_argument('invalid_tuple', nargs=(1,)) + assert 'Ranged values for nargs must be a tuple of 2 integers' in str(excinfo.value) + + +def test_acarg_narg_tuple_triple(): + with pytest.raises(ValueError) as excinfo: + parser = ACArgumentParser(prog='test') + parser.add_argument('invalid_tuple', nargs=(1, 2, 3)) + assert 'Ranged values for nargs must be a tuple of 2 integers' in str(excinfo.value) + + +def test_acarg_narg_tuple_order(): + with pytest.raises(ValueError) as excinfo: + parser = ACArgumentParser(prog='test') + parser.add_argument('invalid_tuple', nargs=(2, 1)) + assert 'Invalid nargs range. The first value must be less than the second' in str(excinfo.value) + + +def test_acarg_narg_tuple_negative(): + with pytest.raises(ValueError) as excinfo: + parser = ACArgumentParser(prog='test') + parser.add_argument('invalid_tuple', nargs=(-1, 1)) + assert 'Negative numbers are invalid for nargs range' in str(excinfo.value) + + +def test_acarg_narg_tuple_zero_base(): + parser = ACArgumentParser(prog='test') + parser.add_argument('tuple', nargs=(0, 3)) + + +def test_acarg_narg_tuple_zero_to_one(): + parser = ACArgumentParser(prog='test') + parser.add_argument('tuple', nargs=(0, 1)) + + diff --git a/tests/test_autocompletion.py b/tests/test_autocompletion.py index aa82adad..7f61f997 100644 --- a/tests/test_autocompletion.py +++ b/tests/test_autocompletion.py @@ -298,6 +298,28 @@ def test_autcomp_pos_after_flag(cmd2_app): cmd2_app.completion_matches == ['John Boyega" '] +def test_autcomp_custom_func_list_arg(cmd2_app): + text = 'SW_' + line = 'library show add {}'.format(text) + endidx = len(line) + begidx = endidx - len(text) + + first_match = complete_tester(text, line, begidx, endidx, cmd2_app) + assert first_match is not None and \ + cmd2_app.completion_matches == ['SW_CW', 'SW_REB', 'SW_TCW'] + + +def test_autcomp_custom_func_list_and_dict_arg(cmd2_app): + text = '' + line = 'library show add SW_REB {}'.format(text) + endidx = len(line) + begidx = endidx - len(text) + + first_match = complete_tester(text, line, begidx, endidx, cmd2_app) + assert first_match is not None and \ + cmd2_app.completion_matches == ['S01E02', 'S01E03', 'S02E01', 'S02E03'] + + |