summaryrefslogtreecommitdiff
path: root/tests/test_parsing.py
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2018-09-01 11:50:37 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2018-09-01 11:50:37 -0400
commit610aad33eb8fe1772480e98af8b255bd56dfe78c (patch)
treeb9dfb60fa0a60b438c8b8e31679f6df768f24665 /tests/test_parsing.py
parent20f4a52399c5c1ee87ae57b9f082113663b20060 (diff)
parent93d40a4a486ae6121858f9fb7369ed272a768672 (diff)
downloadcmd2-git-610aad33eb8fe1772480e98af8b255bd56dfe78c.tar.gz
Merge branch 'quoted_args' of github.com:python-cmd2/cmd2 into quoted_args
Diffstat (limited to 'tests/test_parsing.py')
-rw-r--r--tests/test_parsing.py181
1 files changed, 83 insertions, 98 deletions
diff --git a/tests/test_parsing.py b/tests/test_parsing.py
index de4c637e..e1dfa982 100644
--- a/tests/test_parsing.py
+++ b/tests/test_parsing.py
@@ -33,8 +33,7 @@ def default_parser():
def test_parse_empty_string_default(default_parser):
statement = default_parser.parse('')
- assert not statement.command
- assert not statement.args
+ assert statement.command is None
assert statement == ''
assert statement.raw == ''
@@ -54,8 +53,7 @@ def test_tokenize_default(default_parser, line, tokens):
def test_parse_empty_string(parser):
statement = parser.parse('')
- assert not statement.command
- assert not statement.args
+ assert statement.command is None
assert statement == ''
assert statement.raw == ''
@@ -98,9 +96,9 @@ def test_command_and_args(parser, tokens, command, args):
def test_parse_single_word(parser, line):
statement = parser.parse(line)
assert statement.command == line
- assert statement.args is None
assert statement == ''
assert statement.argv == [utils.strip_quotes(line)]
+ assert not statement.arg_list
@pytest.mark.parametrize('line,terminator', [
('termbare;', ';'),
@@ -111,9 +109,9 @@ def test_parse_single_word(parser, line):
def test_parse_word_plus_terminator(parser, line, terminator):
statement = parser.parse(line)
assert statement.command == 'termbare'
- assert statement.args is None
assert statement == ''
assert statement.argv == ['termbare']
+ assert not statement.arg_list
assert statement.terminator == terminator
@pytest.mark.parametrize('line,terminator', [
@@ -125,9 +123,9 @@ def test_parse_word_plus_terminator(parser, line, terminator):
def test_parse_suffix_after_terminator(parser, line, terminator):
statement = parser.parse(line)
assert statement.command == 'termbare'
- assert statement.args is None
assert statement == ''
assert statement.argv == ['termbare']
+ assert not statement.arg_list
assert statement.terminator == terminator
assert statement.suffix == 'suffx'
@@ -135,72 +133,74 @@ def test_parse_command_with_args(parser):
line = 'command with args'
statement = parser.parse(line)
assert statement.command == 'command'
- assert statement.args == 'with args'
- assert statement == statement.args
+ assert statement == 'with args'
assert statement.argv == ['command', 'with', 'args']
+ assert statement.arg_list == statement.argv[1:]
def test_parse_command_with_quoted_args(parser):
line = 'command with "quoted args" and "some not"'
statement = parser.parse(line)
assert statement.command == 'command'
- assert statement.args == 'with "quoted args" and "some not"'
- assert statement == statement.args
+ assert statement == 'with "quoted args" and "some not"'
assert statement.argv == ['command', 'with', 'quoted args', 'and', 'some not']
+ assert statement.arg_list == ['with', '"quoted args"', 'and', '"some not"']
def test_parse_command_with_args_terminator_and_suffix(parser):
line = 'command with args and terminator; and suffix'
statement = parser.parse(line)
assert statement.command == 'command'
- assert statement.args == "with args and terminator"
+ assert statement == "with args and terminator"
assert statement.argv == ['command', 'with', 'args', 'and', 'terminator']
- assert statement == statement.args
+ assert statement.arg_list == statement.argv[1:]
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.args is None
assert 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.args is None
assert statement.argv == ['hi']
+ assert not statement.arg_list
assert statement == ''
- assert not statement.pipe_to
+ assert statement.pipe_to is None
def test_parse_c_comment_empty(parser):
statement = parser.parse('/* this is | all a comment */')
- assert not statement.command
- assert not statement.args
- assert not statement.pipe_to
+ assert statement.command is None
+ assert statement.pipe_to is None
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.args == '/tmp/*.txt'
- assert not statement.pipe_to
+ assert statement == '/tmp/*.txt'
+ assert statement.pipe_to is None
assert statement.argv == ['cat', '/tmp/*.txt']
+ 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.args == '/tmp/*.txt /tmp/*.cfg'
- assert not statement.pipe_to
+ assert statement == '/tmp/*.txt /tmp/*.cfg'
+ assert statement.pipe_to is None
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.args == 'if "quoted strings /* seem to " start comments?'
+ assert statement == 'if "quoted strings /* seem to " start comments?'
assert statement.argv == ['what', 'if', 'quoted strings /* seem to ', 'start', 'comments?']
- assert statement == statement.args
- assert not statement.pipe_to
+ assert statement.arg_list == ['if', '"quoted strings /* seem to "', 'start', 'comments?']
+ assert statement.pipe_to is None
@pytest.mark.parametrize('line',[
'simple | piped',
@@ -209,27 +209,27 @@ def test_parse_what_if_quoted_strings_seem_to_start_comments(parser):
def test_parse_simple_pipe(parser, line):
statement = parser.parse(line)
assert statement.command == 'simple'
- assert statement.args is None
assert statement == ''
assert statement.argv == ['simple']
+ assert not statement.arg_list
assert statement.pipe_to == ['piped']
def test_parse_double_pipe_is_not_a_pipe(parser):
line = 'double-pipe || is not a pipe'
statement = parser.parse(line)
assert statement.command == 'double-pipe'
- assert statement.args == '|| is not a pipe'
- assert statement == statement.args
+ assert statement == '|| is not a pipe'
assert statement.argv == ['double-pipe', '||', 'is', 'not', 'a', 'pipe']
- assert not statement.pipe_to
+ assert statement.arg_list == statement.argv[1:]
+ assert statement.pipe_to is None
def test_parse_complex_pipe(parser):
line = 'command with args, terminator&sufx | piped'
statement = parser.parse(line)
assert statement.command == 'command'
- assert statement.args == "with args, terminator"
+ assert statement == "with args, terminator"
assert statement.argv == ['command', 'with', 'args,', 'terminator']
- assert statement == statement.args
+ assert statement.arg_list == statement.argv[1:]
assert statement.terminator == '&'
assert statement.suffix == 'sufx'
assert statement.pipe_to == ['piped']
@@ -243,7 +243,6 @@ def test_parse_complex_pipe(parser):
def test_parse_redirect(parser,line, output):
statement = parser.parse(line)
assert statement.command == 'help'
- assert statement.args is None
assert statement == ''
assert statement.output == output
assert statement.output_to == 'out.txt'
@@ -252,9 +251,9 @@ def test_parse_redirect_with_args(parser):
line = 'output into > afile.txt'
statement = parser.parse(line)
assert statement.command == 'output'
- assert statement.args == 'into'
- assert statement == statement.args
+ assert statement == 'into'
assert statement.argv == ['output', 'into']
+ assert statement.arg_list == statement.argv[1:]
assert statement.output == '>'
assert statement.output_to == 'afile.txt'
@@ -262,9 +261,9 @@ def test_parse_redirect_with_dash_in_path(parser):
line = 'output into > python-cmd2/afile.txt'
statement = parser.parse(line)
assert statement.command == 'output'
- assert statement.args == 'into'
- assert statement == statement.args
+ assert statement == 'into'
assert statement.argv == ['output', 'into']
+ assert statement.arg_list == statement.argv[1:]
assert statement.output == '>'
assert statement.output_to == 'python-cmd2/afile.txt'
@@ -272,9 +271,9 @@ def test_parse_redirect_append(parser):
line = 'output appended to >> /tmp/afile.txt'
statement = parser.parse(line)
assert statement.command == 'output'
- assert statement.args == 'appended to'
- assert statement == statement.args
+ assert statement == 'appended to'
assert statement.argv == ['output', 'appended', 'to']
+ assert statement.arg_list == statement.argv[1:]
assert statement.output == '>>'
assert statement.output_to == '/tmp/afile.txt'
@@ -282,22 +281,22 @@ def test_parse_pipe_and_redirect(parser):
line = 'output into;sufx | pipethrume plz > afile.txt'
statement = parser.parse(line)
assert statement.command == 'output'
- assert statement.args == 'into'
- assert statement == statement.args
+ assert statement == 'into'
assert statement.argv == ['output', 'into']
+ assert statement.arg_list == statement.argv[1:]
assert statement.terminator == ';'
assert statement.suffix == 'sufx'
assert statement.pipe_to == ['pipethrume', 'plz', '>', 'afile.txt']
- assert not statement.output
- assert not statement.output_to
+ assert statement.output is None
+ assert statement.output_to is None
def test_parse_output_to_paste_buffer(parser):
line = 'output to paste buffer >> '
statement = parser.parse(line)
assert statement.command == 'output'
- assert statement.args == 'to paste buffer'
- assert statement == statement.args
+ assert statement == 'to paste buffer'
assert statement.argv == ['output', 'to', 'paste', 'buffer']
+ assert statement.arg_list == statement.argv[1:]
assert statement.output == '>>'
def test_parse_redirect_inside_terminator(parser):
@@ -307,9 +306,9 @@ def test_parse_redirect_inside_terminator(parser):
line = 'has > inside;'
statement = parser.parse(line)
assert statement.command == 'has'
- assert statement.args == '> inside'
- assert statement == statement.args
+ assert statement == '> inside'
assert statement.argv == ['has', '>', 'inside']
+ assert statement.arg_list == statement.argv[1:]
assert statement.terminator == ';'
@pytest.mark.parametrize('line,terminator',[
@@ -325,9 +324,9 @@ def test_parse_redirect_inside_terminator(parser):
def test_parse_multiple_terminators(parser, line, terminator):
statement = parser.parse(line)
assert statement.multiline_command == 'multiline'
- assert statement.args == 'with | inside'
- assert statement == statement.args
+ assert statement == 'with | inside'
assert statement.argv == ['multiline', 'with', '|', 'inside']
+ assert statement.arg_list == statement.argv[1:]
assert statement.terminator == terminator
def test_parse_unfinished_multiliine_command(parser):
@@ -335,10 +334,10 @@ def test_parse_unfinished_multiliine_command(parser):
statement = parser.parse(line)
assert statement.multiline_command == 'multiline'
assert statement.command == 'multiline'
- assert statement.args == 'has > inside an unfinished command'
- assert statement == statement.args
+ assert statement == 'has > inside an unfinished command'
assert statement.argv == ['multiline', 'has', '>', 'inside', 'an', 'unfinished', 'command']
- assert not statement.terminator
+ assert statement.arg_list == statement.argv[1:]
+ assert statement.terminator is None
@pytest.mark.parametrize('line,terminator',[
('multiline has > inside;', ';'),
@@ -350,9 +349,9 @@ def test_parse_unfinished_multiliine_command(parser):
def test_parse_multiline_command_ignores_redirectors_within_it(parser, line, terminator):
statement = parser.parse(line)
assert statement.multiline_command == 'multiline'
- assert statement.args == 'has > inside'
- assert statement == statement.args
+ assert statement == 'has > inside'
assert statement.argv == ['multiline', 'has', '>', 'inside']
+ assert statement.arg_list == statement.argv[1:]
assert statement.terminator == terminator
def test_parse_multiline_with_incomplete_comment(parser):
@@ -362,8 +361,9 @@ def test_parse_multiline_with_incomplete_comment(parser):
statement = parser.parse(line)
assert statement.multiline_command == 'multiline'
assert statement.command == 'multiline'
- assert statement.args == 'command /* with unclosed comment'
+ assert statement == 'command /* with unclosed comment'
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):
@@ -371,9 +371,9 @@ def test_parse_multiline_with_complete_comment(parser):
statement = parser.parse(line)
assert statement.multiline_command == 'multiline'
assert statement.command == 'multiline'
- assert statement.args == 'command is done'
- assert statement == statement.args
+ assert statement == 'command is done'
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):
@@ -381,9 +381,9 @@ def test_parse_multiline_terminated_by_empty_line(parser):
statement = parser.parse(line)
assert statement.multiline_command == 'multiline'
assert statement.command == 'multiline'
- assert statement.args == 'command ends'
- assert statement == statement.args
+ assert statement == 'command ends'
assert statement.argv == ['multiline', 'command', 'ends']
+ assert statement.arg_list == statement.argv[1:]
assert statement.terminator == '\n'
@pytest.mark.parametrize('line,terminator',[
@@ -398,9 +398,9 @@ def test_parse_multiline_with_embedded_newline(parser, line, terminator):
statement = parser.parse(line)
assert statement.multiline_command == 'multiline'
assert statement.command == 'multiline'
- assert statement.args == 'command "with\nembedded newline"'
- assert statement == statement.args
+ assert statement == 'command "with\nembedded newline"'
assert statement.argv == ['multiline', 'command', 'with\nembedded newline']
+ assert statement.arg_list == ['command', '"with\nembedded newline"']
assert statement.terminator == terminator
def test_parse_multiline_ignores_terminators_in_comments(parser):
@@ -408,34 +408,34 @@ def test_parse_multiline_ignores_terminators_in_comments(parser):
statement = parser.parse(line)
assert statement.multiline_command == 'multiline'
assert statement.command == 'multiline'
- assert statement.args == 'command "with term; ends" now'
- assert statement == statement.args
+ assert statement == 'command "with term; ends" now'
assert statement.argv == ['multiline', 'command', 'with term; ends', 'now']
+ assert statement.arg_list == ['command', '"with term; ends"', 'now']
assert statement.terminator == '\n'
def test_parse_command_with_unicode_args(parser):
line = 'drink café'
statement = parser.parse(line)
assert statement.command == 'drink'
- assert statement.args == 'café'
- assert statement == statement.args
+ assert statement == 'café'
assert statement.argv == ['drink', 'café']
+ assert statement.arg_list == statement.argv[1:]
def test_parse_unicode_command(parser):
line = 'café au lait'
statement = parser.parse(line)
assert statement.command == 'café'
- assert statement.args == 'au lait'
- assert statement == statement.args
+ assert statement == 'au lait'
assert statement.argv == ['café', 'au', 'lait']
+ assert statement.arg_list == statement.argv[1:]
def test_parse_redirect_to_unicode_filename(parser):
line = 'dir home > café'
statement = parser.parse(line)
assert statement.command == 'dir'
- assert statement.args == 'home'
- assert statement == statement.args
+ assert statement == 'home'
assert statement.argv == ['dir', 'home']
+ assert statement.arg_list == statement.argv[1:]
assert statement.output == '>'
assert statement.output_to == 'café'
@@ -452,9 +452,9 @@ def test_empty_statement_raises_exception():
app._complete_statement(' ')
@pytest.mark.parametrize('line,command,args', [
- ('helpalias', 'help', None),
+ ('helpalias', 'help', ''),
('helpalias mycommand', 'help', 'mycommand'),
- ('42', 'theanswer', None),
+ ('42', 'theanswer', ''),
('42 arg1 arg2', 'theanswer', 'arg1 arg2'),
('!ls', 'shell', 'ls'),
('!ls -al /tmp', 'shell', 'ls -al /tmp'),
@@ -463,20 +463,15 @@ def test_empty_statement_raises_exception():
def test_parse_alias_and_shortcut_expansion(parser, line, command, args):
statement = parser.parse(line)
assert statement.command == command
- assert statement.args == args
- if statement.args is None:
- assert statement == ''
- else:
- assert statement == statement.args
+ assert statement == args
def test_parse_alias_on_multiline_command(parser):
line = 'anothermultiline has > inside an unfinished command'
statement = parser.parse(line)
assert statement.multiline_command == 'multiline'
assert statement.command == 'multiline'
- assert statement.args == 'has > inside an unfinished command'
- assert statement == statement.args
- assert not statement.terminator
+ assert statement == 'has > inside an unfinished command'
+ assert statement.terminator is None
@pytest.mark.parametrize('line,output', [
('helpalias > out.txt', '>'),
@@ -487,7 +482,6 @@ def test_parse_alias_on_multiline_command(parser):
def test_parse_alias_redirection(parser, line, output):
statement = parser.parse(line)
assert statement.command == 'help'
- assert statement.args is None
assert statement == ''
assert statement.output == output
assert statement.output_to == 'out.txt'
@@ -499,7 +493,6 @@ def test_parse_alias_redirection(parser, line, output):
def test_parse_alias_pipe(parser, line):
statement = parser.parse(line)
assert statement.command == 'help'
- assert statement.args is None
assert statement == ''
assert statement.pipe_to == ['less']
@@ -514,7 +507,6 @@ def test_parse_alias_pipe(parser, line):
def test_parse_alias_terminator_no_whitespace(parser, line):
statement = parser.parse(line)
assert statement.command == 'help'
- assert statement.args is None
assert statement == ''
assert statement.terminator == ';'
@@ -522,8 +514,7 @@ def test_parse_command_only_command_and_args(parser):
line = 'help history'
statement = parser.parse_command_only(line)
assert statement.command == 'help'
- assert statement.args == 'history'
- assert statement == statement.args
+ assert statement == 'history'
assert statement.command_and_args == line
def test_parse_command_only_emptyline(parser):
@@ -533,40 +524,36 @@ def test_parse_command_only_emptyline(parser):
# should be '', to retain backwards compatibility with
# the cmd in the standard library
assert statement.command is None
- assert statement.args is None
assert statement == ''
assert not statement.argv
- assert statement.command_and_args == None
+ assert not statement.arg_list
+ assert statement.command_and_args is None
def test_parse_command_only_strips_line(parser):
line = ' help history '
statement = parser.parse_command_only(line)
assert statement.command == 'help'
- assert statement.args == 'history'
- assert statement == statement.args
+ assert statement == 'history'
assert statement.command_and_args == line.strip()
def test_parse_command_only_expands_alias(parser):
line = 'fake foobar.py'
statement = parser.parse_command_only(line)
assert statement.command == 'pyscript'
- assert statement.args == 'foobar.py'
- assert statement == statement.args
+ assert statement == 'foobar.py'
def test_parse_command_only_expands_shortcuts(parser):
line = '!cat foobar.txt'
statement = parser.parse_command_only(line)
assert statement.command == 'shell'
- assert statement.args == 'cat foobar.txt'
- assert statement == statement.args
+ assert statement == 'cat foobar.txt'
assert statement.command_and_args == 'shell cat foobar.txt'
def test_parse_command_only_quoted_args(parser):
line = 'l "/tmp/directory with spaces/doit.sh"'
statement = parser.parse_command_only(line)
assert statement.command == 'shell'
- assert statement.args == 'ls -al "/tmp/directory with spaces/doit.sh"'
- assert statement == statement.args
+ assert statement == 'ls -al "/tmp/directory with spaces/doit.sh"'
assert statement.command_and_args == line.replace('l', 'shell ls -al')
@pytest.mark.parametrize('line', [
@@ -598,7 +585,6 @@ def test_parse_command_only_specialchars(parser, line):
def test_parse_command_only_none(parser, line):
statement = parser.parse_command_only(line)
assert statement.command is None
- assert statement.args is None
assert statement == ''
def test_parse_command_only_multiline(parser):
@@ -606,8 +592,7 @@ def test_parse_command_only_multiline(parser):
statement = parser.parse_command_only(line)
assert statement.command == 'multiline'
assert statement.multiline_command == 'multiline'
- assert statement.args == 'with partially "open quotes and no terminator'
- assert statement == statement.args
+ assert statement == 'with partially "open quotes and no terminator'
assert statement.command_and_args == line
@@ -617,7 +602,7 @@ def test_statement_initialization(parser):
assert string == statement
assert statement.raw is None
assert statement.command is None
- assert statement.args is None
+ assert isinstance(statement.arg_list, list)
assert isinstance(statement.argv, list)
assert not statement.argv
assert statement.multiline_command is None