diff options
Diffstat (limited to 'tests/test_cmd2.py')
-rw-r--r-- | tests/test_cmd2.py | 102 |
1 files changed, 78 insertions, 24 deletions
diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py index c8955497..b570ad3c 100644 --- a/tests/test_cmd2.py +++ b/tests/test_cmd2.py @@ -104,16 +104,53 @@ def test_base_show_readonly(base_app): Commands may be terminated with: {} Arguments at invocation allowed: {} Output redirection and pipes allowed: {} - Parsing of command arguments: - Shell lexer mode for command argument splitting: {} - Strip Quotes after splitting arguments: {} - -""".format(base_app.terminators, base_app.allow_cli_args, base_app.allow_redirection, - "POSIX" if cmd2.POSIX_SHLEX else "non-POSIX", - "True" if cmd2.STRIP_QUOTES_FOR_NON_POSIX and not cmd2.POSIX_SHLEX else "False")) +""".format(base_app.terminators, base_app.allow_cli_args, base_app.allow_redirection)) assert out == expected +def test_cast(): + cast = cmd2.cast + + # Boolean + assert cast(True, True) == True + assert cast(True, False) == False + assert cast(True, 0) == False + assert cast(True, 1) == True + assert cast(True, 'on') == True + assert cast(True, 'off') == False + assert cast(True, 'ON') == True + assert cast(True, 'OFF') == False + assert cast(True, 'y') == True + assert cast(True, 'n') == False + assert cast(True, 't') == True + assert cast(True, 'f') == False + + # Non-boolean same type + assert cast(1, 5) == 5 + assert cast(3.4, 2.7) == 2.7 + assert cast('foo', 'bar') == 'bar' + assert cast([1,2], [3,4]) == [3,4] + +def test_cast_problems(capsys): + cast = cmd2.cast + + expected = 'Problem setting parameter (now {}) to {}; incorrect type?\n' + + # Boolean current, with new value not convertible to bool + current = True + new = [True, True] + assert cast(current, new) == current + out, err = capsys.readouterr() + assert out == expected.format(current, new) + + # Non-boolean current, with new value not convertible to current type + current = 1 + new = 'octopus' + assert cast(current, new) == current + out, err = capsys.readouterr() + assert out == expected.format(current, new) + + def test_base_set(base_app): out = run_cmd(base_app, 'set quiet True') expected = normalize(""" @@ -129,7 +166,7 @@ def test_set_not_supported(base_app, capsys): run_cmd(base_app, 'set qqq True') out, err = capsys.readouterr() expected = normalize(""" -EXCEPTION of type 'LookupError' occurred with message: 'Parameter 'qqq' not supported (type 'show' for list of parameters).' +EXCEPTION of type 'LookupError' occurred with message: 'Parameter 'qqq' not supported (type 'set' for list of parameters).' To enable full traceback, run the following command: 'set debug true' """) assert normalize(str(err)) == expected @@ -217,6 +254,34 @@ def test_base_error(base_app): assert out == ["*** Unknown syntax: meow"] +@pytest.fixture +def hist(): + from cmd2.cmd2 import History, HistoryItem + h = History([HistoryItem('first'), HistoryItem('second'), HistoryItem('third'), HistoryItem('fourth')]) + return h + +def test_history_span(hist): + h = hist + assert h == ['first', 'second', 'third', 'fourth'] + assert h.span('-2..') == ['third', 'fourth'] + assert h.span('2..3') == ['second', 'third'] # Inclusive of end + assert h.span('3') == ['third'] + assert h.span(':') == h + assert h.span('2..') == ['second', 'third', 'fourth'] + assert h.span('-1') == ['fourth'] + assert h.span('-2..-3') == ['third', 'second'] + assert h.span('*') == h + +def test_history_get(hist): + h = hist + assert h == ['first', 'second', 'third', 'fourth'] + assert h.get('') == h + assert h.get('-2') == h[:-2] + assert h.get('5') == [] + assert h.get('2-3') == ['second'] # Exclusive of end + assert h.get('ir') == ['first', 'third'] # Normal string search for all elements containing "ir" + assert h.get('/i.*d/') == ['third'] # Regex string search "i", then anything, then "d" + def test_base_history(base_app): run_cmd(base_app, 'help') run_cmd(base_app, 'shortcuts') @@ -604,18 +669,6 @@ def test_allow_redirection(base_app): # Verify that no file got created assert not os.path.exists(filename) - -def test_input_redirection(base_app, request): - test_dir = os.path.dirname(request.module.__file__) - filename = os.path.join(test_dir, 'redirect.txt') - - # NOTE: File 'redirect.txt" contains 1 word "history" - - # Verify that redirecting input ffom a file works - out = run_cmd(base_app, 'help < {}'.format(filename)) - assert out == normalize(HELP_HISTORY) - - def test_pipe_to_shell(base_app, capsys): if sys.platform == "win32": # Windows @@ -910,6 +963,7 @@ class SayApp(cmd2.Cmd): @pytest.fixture def say_app(): app = SayApp() + app.allow_cli_args = False app.stdout = StdOut() return app @@ -965,7 +1019,7 @@ def test_default_to_shell_good(capsys): line = 'dir' else: line = 'ls' - statement = app.parser_manager.parsed(line) + statement = app.statement_parser.parse(line) retval = app.default(statement) assert not retval out, err = capsys.readouterr() @@ -975,7 +1029,7 @@ def test_default_to_shell_failure(capsys): app = cmd2.Cmd() app.default_to_shell = True line = 'ls does_not_exist.xyz' - statement = app.parser_manager.parsed(line) + statement = app.statement_parser.parse(line) retval = app.default(statement) assert not retval out, err = capsys.readouterr() @@ -1329,7 +1383,7 @@ def test_which_editor_bad(): class MultilineApp(cmd2.Cmd): def __init__(self, *args, **kwargs): - self.multilineCommands = ['orate'] + self.multiline_commands = ['orate'] super().__init__(*args, **kwargs) orate_parser = argparse.ArgumentParser() @@ -1362,7 +1416,7 @@ def test_multiline_complete_statement_without_terminator(multiline_app): line = '{} {}'.format(command, args) statement = multiline_app._complete_statement(line) assert statement == args - assert statement.parsed.command == command + assert statement.command == command def test_clipboard_failure(capsys): |