summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/test_argparse.py4
-rw-r--r--tests/test_cmd2.py26
-rw-r--r--tests/test_parsing.py95
3 files changed, 43 insertions, 82 deletions
diff --git a/tests/test_argparse.py b/tests/test_argparse.py
index 7db35c71..6b810b44 100644
--- a/tests/test_argparse.py
+++ b/tests/test_argparse.py
@@ -141,10 +141,6 @@ def test_argparse_with_list_and_empty_doc(argparse_app):
out = run_cmd(argparse_app, 'speak -s hello world!')
assert out == ['HELLO WORLD!']
-def test_argparse_comment_stripping(argparse_app):
- out = run_cmd(argparse_app, 'speak it was /* not */ delicious! # Yuck!')
- assert out == ['it was delicious!']
-
def test_argparser_correct_args_with_quotes_and_midline_options(argparse_app):
out = run_cmd(argparse_app, "speak 'This is a' -s test of the emergency broadcast system!")
assert out == ['THIS IS A TEST OF THE EMERGENCY BROADCAST SYSTEM!']
diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py
index 8d0d56c6..d43f7786 100644
--- a/tests/test_cmd2.py
+++ b/tests/test_cmd2.py
@@ -24,8 +24,7 @@ except ImportError:
from unittest import mock
import cmd2
-from cmd2 import clipboard
-from cmd2 import utils
+from cmd2 import clipboard, constants, utils
from .conftest import run_cmd, normalize, BASE_HELP, BASE_HELP_VERBOSE, \
HELP_HISTORY, SHORTCUTS_TXT, SHOW_TXT, SHOW_LONG
@@ -1596,6 +1595,7 @@ def test_poutput_color_never(base_app):
# These are invalid names for aliases and macros
invalid_command_name = [
'""', # Blank name
+ constants.COMMENT_CHAR,
'!no_shortcut',
'">"',
'"no>pe"',
@@ -1668,6 +1668,17 @@ def test_alias_create_with_macro_name(base_app, capsys):
out, err = capsys.readouterr()
assert "Alias cannot have the same name as a macro" in err
+def test_alias_that_resolves_into_comment(base_app, capsys):
+ # Create the alias
+ out = run_cmd(base_app, 'alias create fake ' + constants.COMMENT_CHAR + ' blah blah')
+ assert out == normalize("Alias 'fake' created")
+
+ # Use the alias
+ run_cmd(base_app, 'fake')
+ out, err = capsys.readouterr()
+ assert not out
+ assert not err
+
def test_alias_list_invalid_alias(base_app, capsys):
# Look up invalid alias
out = run_cmd(base_app, 'alias list invalid')
@@ -1824,6 +1835,17 @@ def test_macro_create_with_missing_unicode_arg_nums(base_app, capsys):
out, err = capsys.readouterr()
assert "Not all numbers between 1 and 3" in err
+def test_macro_that_resolves_into_comment(base_app, capsys):
+ # Create the macro
+ out = run_cmd(base_app, 'macro create fake {1} blah blah')
+ assert out == normalize("Macro 'fake' created")
+
+ # Use the macro
+ run_cmd(base_app, 'fake ' + constants.COMMENT_CHAR)
+ out, err = capsys.readouterr()
+ assert not out
+ assert not err
+
def test_macro_list_invalid_macro(base_app, capsys):
# Look up invalid macro
run_cmd(base_app, 'macro list invalid')
diff --git a/tests/test_parsing.py b/tests/test_parsing.py
index c341f9e3..85ee0765 100644
--- a/tests/test_parsing.py
+++ b/tests/test_parsing.py
@@ -10,11 +10,10 @@ import attr
import pytest
import cmd2
-from cmd2 import utils
+from cmd2 import constants, utils
from cmd2.constants import MULTILINE_TERMINATOR
from cmd2.parsing import StatementParser
-
@pytest.fixture
def parser():
parser = StatementParser(
@@ -72,8 +71,8 @@ def test_parse_empty_string_default(default_parser):
@pytest.mark.parametrize('line,tokens', [
('command', ['command']),
- ('command /* with some comment */ arg', ['command', 'arg']),
- ('command arg1 arg2 # comment at the end', ['command', 'arg1', 'arg2']),
+ (constants.COMMENT_CHAR + 'comment', []),
+ ('not ' + constants.COMMENT_CHAR + ' a comment', ['not', constants.COMMENT_CHAR, 'a', 'comment']),
('termbare ; > /tmp/output', ['termbare', ';', '>', '/tmp/output']),
('termbare; > /tmp/output', ['termbare', ';', '>', '/tmp/output']),
('termbare & > /tmp/output', ['termbare', '&', '>', '/tmp/output']),
@@ -86,8 +85,8 @@ def test_tokenize_default(default_parser, line, tokens):
@pytest.mark.parametrize('line,tokens', [
('command', ['command']),
- ('command /* with some comment */ arg', ['command', 'arg']),
- ('command arg1 arg2 # comment at the end', ['command', 'arg1', 'arg2']),
+ ('# comment', []),
+ ('not ' + constants.COMMENT_CHAR + ' a comment', ['not', constants.COMMENT_CHAR, 'a', 'comment']),
('42 arg1 arg2', ['theanswer', 'arg1', 'arg2']),
('l', ['shell', 'ls', '-al']),
('termbare ; > /tmp/output', ['termbare', ';', '>', '/tmp/output']),
@@ -197,59 +196,23 @@ def test_parse_command_with_args_terminator_and_suffix(parser):
assert statement.terminator == ';'
assert statement.suffix == 'and suffix'
-def test_parse_hashcomment(parser):
- statement = parser.parse('hi # this is all a comment')
- assert statement.command == 'hi'
- assert statement == ''
- assert statement.args == statement
- assert statement.argv == ['hi']
- assert not statement.arg_list
-
-def test_parse_c_comment(parser):
- statement = parser.parse('hi /* this is | all a comment */')
- assert statement.command == 'hi'
- assert statement == ''
- assert statement.args == statement
- assert statement.argv == ['hi']
- assert not statement.arg_list
- assert not statement.pipe_to
-
-def test_parse_c_comment_empty(parser):
- statement = parser.parse('/* this is | all a comment */')
+def test_parse_comment(parser):
+ statement = parser.parse(constants.COMMENT_CHAR + ' this is all a comment')
assert statement.command == ''
+ assert statement == ''
assert statement.args == statement
- assert not statement.pipe_to
assert not statement.argv
assert not statement.arg_list
- assert statement == ''
-def test_parse_c_comment_no_closing(parser):
- statement = parser.parse('cat /tmp/*.txt')
- assert statement.command == 'cat'
- assert statement == '/tmp/*.txt'
+def test_parse_embedded_comment_char(parser):
+ command_str = 'hi ' + constants.COMMENT_CHAR + ' not a comment'
+ statement = parser.parse(command_str)
+ assert statement.command == 'hi'
+ assert statement == constants.COMMENT_CHAR + ' not a comment'
assert statement.args == statement
- assert not statement.pipe_to
- assert statement.argv == ['cat', '/tmp/*.txt']
+ assert statement.argv == command_str.split()
assert statement.arg_list == statement.argv[1:]
-def test_parse_c_comment_multiple_opening(parser):
- statement = parser.parse('cat /tmp/*.txt /tmp/*.cfg')
- assert statement.command == 'cat'
- assert statement == '/tmp/*.txt /tmp/*.cfg'
- assert statement.args == statement
- assert not statement.pipe_to
- assert statement.argv == ['cat', '/tmp/*.txt', '/tmp/*.cfg']
- assert statement.arg_list == statement.argv[1:]
-
-def test_parse_what_if_quoted_strings_seem_to_start_comments(parser):
- statement = parser.parse('what if "quoted strings /* seem to " start comments?')
- assert statement.command == 'what'
- assert statement == 'if "quoted strings /* seem to " start comments?'
- assert statement.args == statement
- assert statement.argv == ['what', 'if', 'quoted strings /* seem to ', 'start', 'comments?']
- assert statement.arg_list == ['if', '"quoted strings /* seem to "', 'start', 'comments?']
- assert not statement.pipe_to
-
@pytest.mark.parametrize('line',[
'simple | piped',
'simple|piped',
@@ -417,30 +380,6 @@ def test_parse_multiline_command_ignores_redirectors_within_it(parser, line, ter
assert statement.arg_list == statement.argv[1:]
assert statement.terminator == terminator
-def test_parse_multiline_with_incomplete_comment(parser):
- """A terminator within a comment will be ignored and won't terminate a multiline command.
- Un-closed comments effectively comment out everything after the start."""
- line = 'multiline command /* with unclosed comment;'
- statement = parser.parse(line)
- assert statement.multiline_command == 'multiline'
- assert statement.command == 'multiline'
- assert statement == 'command /* with unclosed comment'
- assert statement.args == statement
- assert statement.argv == ['multiline', 'command', '/*', 'with', 'unclosed', 'comment']
- assert statement.arg_list == statement.argv[1:]
- assert statement.terminator == ';'
-
-def test_parse_multiline_with_complete_comment(parser):
- line = 'multiline command /* with comment complete */ is done;'
- statement = parser.parse(line)
- assert statement.multiline_command == 'multiline'
- assert statement.command == 'multiline'
- assert statement == 'command is done'
- assert statement.args == statement
- assert statement.argv == ['multiline', 'command', 'is', 'done']
- assert statement.arg_list == statement.argv[1:]
- assert statement.terminator == ';'
-
def test_parse_multiline_terminated_by_empty_line(parser):
line = 'multiline command ends\n\n'
statement = parser.parse(line)
@@ -470,7 +409,7 @@ def test_parse_multiline_with_embedded_newline(parser, line, terminator):
assert statement.arg_list == ['command', '"with\nembedded newline"']
assert statement.terminator == terminator
-def test_parse_multiline_ignores_terminators_in_comments(parser):
+def test_parse_multiline_ignores_terminators_in_quotes(parser):
line = 'multiline command "with term; ends" now\n\n'
statement = parser.parse(line)
assert statement.multiline_command == 'multiline'
@@ -769,6 +708,10 @@ def test_is_valid_command_invalid(parser):
valid, errmsg = parser.is_valid_command('')
assert not valid and 'cannot be an empty string' in errmsg
+ # Start with the comment character
+ valid, errmsg = parser.is_valid_command(constants.COMMENT_CHAR)
+ assert not valid and 'cannot start with the comment character' in errmsg
+
# Starts with shortcut
valid, errmsg = parser.is_valid_command('!ls')
assert not valid and 'cannot start with a shortcut' in errmsg