summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2017-06-24 17:25:49 -0400
committerGitHub <noreply@github.com>2017-06-24 17:25:49 -0400
commit829973e473044cb4d3d8fb5dd55ff09aefb46d5e (patch)
tree7f5f4d4b7d9d62361538d10145dc8dbadd9af880
parentb72a8ba7eed899ee8c44da36aa2636496bff89c7 (diff)
parent97a4f49948db468a247f4dd9862440cddc5e3db2 (diff)
downloadcmd2-git-829973e473044cb4d3d8fb5dd55ff09aefb46d5e.tar.gz
Merge pull request #138 from python-cmd2/unit_tests
Add path completion unit tests
-rw-r--r--tests/test_cmd2.py12
-rw-r--r--tests/test_completion.py98
2 files changed, 109 insertions, 1 deletions
diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py
index 757ed418..c1d406f4 100644
--- a/tests/test_cmd2.py
+++ b/tests/test_cmd2.py
@@ -111,6 +111,18 @@ def test_base_run_python_script(base_app, capsys, request):
assert out == expected
+@pytest.mark.skipif(sys.platform == 'win32',
+ reason="Unit test doesn't work on win32, but feature does")
+def test_base_run_pyscript(base_app, capsys, request):
+ test_dir = os.path.dirname(request.module.__file__)
+ python_script = os.path.join(test_dir, 'script.py')
+ expected = 'This is a python script running ...\n'
+
+ run_cmd(base_app, "pyscript {}".format(python_script))
+ out, err = capsys.readouterr()
+ assert out == expected
+
+
def test_base_error(base_app):
out = run_cmd(base_app, 'meow')
assert out == ["*** Unknown syntax: meow"]
diff --git a/tests/test_completion.py b/tests/test_completion.py
index 98f4387a..74cc3d57 100644
--- a/tests/test_completion.py
+++ b/tests/test_completion.py
@@ -8,6 +8,7 @@ file system paths, and shell commands.
Copyright 2017 Todd Leonhardt <todd.leonhardt@gmail.com>
Released under MIT license, see LICENSE file
"""
+import os
import sys
import cmd2
@@ -44,6 +45,13 @@ def test_cmd2_command_completion_multiple(cmd2_app):
# It is not at end of line, so no extra space
assert cmd2_app.completenames(text, line, begidx, endidx) == ['help', 'history']
+def test_cmd2_command_completion_nomatch(cmd2_app):
+ text = 'z'
+ line = 'z'
+ begidx = 0
+ endidx = 1
+ assert cmd2_app.completenames(text, line, begidx, endidx) == []
+
def test_cmd2_help_completion_single_end(cmd2_app):
text = 'he'
line = 'help he'
@@ -66,6 +74,13 @@ def test_cmd2_help_completion_multiple(cmd2_app):
endidx = 6
assert cmd2_app.completenames(text, line, begidx, endidx) == ['help', 'history']
+def test_cmd2_help_completion_nomatch(cmd2_app):
+ text = 'z'
+ line = 'help z'
+ begidx = 5
+ endidx = 6
+ assert cmd2_app.completenames(text, line, begidx, endidx) == []
+
def test_shell_command_completion(cmd2_app):
if sys.platform == "win32":
text = 'calc'
@@ -94,4 +109,85 @@ def test_shell_command_completion_multiple(cmd2_app):
endidx = 2
assert 'ls' in cmd2_app.complete_shell(text, line, begidx, endidx)
-# TODO: Add tests for path completion
+def test_shell_command_completion_nomatch(cmd2_app):
+ text = 'zzzz'
+ line = 'shell zzzz'
+ begidx = 6
+ endidx = 10
+ assert cmd2_app.complete_shell(text, line, begidx, endidx) == []
+
+def test_path_completion_single_end(cmd2_app, request):
+ test_dir = os.path.dirname(request.module.__file__)
+
+ text = 'c'
+ path = os.path.join(test_dir, text)
+ line = '!cat {}'.format(path)
+
+ endidx = len(line)
+ begidx = endidx - len(text)
+
+ assert cmd2_app.path_complete(text, line, begidx, endidx) == ['conftest.py ']
+
+def test_path_completion_single_mid(cmd2_app, request):
+ test_dir = os.path.dirname(request.module.__file__)
+
+ text = 'tes'
+ path = os.path.join(test_dir, 'c')
+ line = '!cat {}'.format(path)
+
+ begidx = line.find(text)
+ endidx = begidx + len(text)
+
+ assert cmd2_app.path_complete(text, line, begidx, endidx) == ['tests' + os.path.sep]
+
+def test_path_completion_multiple(cmd2_app, request):
+ test_dir = os.path.dirname(request.module.__file__)
+
+ text = 's'
+ path = os.path.join(test_dir, text)
+ line = '!cat {}'.format(path)
+
+ endidx = len(line)
+ begidx = endidx - len(text)
+
+ assert cmd2_app.path_complete(text, line, begidx, endidx) == ['script.py', 'script.txt']
+
+def test_path_completion_nomatch(cmd2_app, request):
+ test_dir = os.path.dirname(request.module.__file__)
+
+ text = 'z'
+ path = os.path.join(test_dir, text)
+ line = '!cat {}'.format(path)
+
+ endidx = len(line)
+ begidx = endidx - len(text)
+
+ assert cmd2_app.path_complete(text, line, begidx, endidx) == []
+
+def test_parseline_command_and_args(cmd2_app):
+ line = 'help history'
+ command, args, out_line = cmd2_app.parseline(line)
+ assert command == 'help'
+ assert args == 'history'
+ assert line == out_line
+
+def test_parseline_emptyline(cmd2_app):
+ line = ''
+ command, args, out_line = cmd2_app.parseline(line)
+ assert command == None
+ assert args == None
+ assert line == out_line
+
+def test_parseline_strips_line(cmd2_app):
+ line = ' help history '
+ command, args, out_line = cmd2_app.parseline(line)
+ assert command == 'help'
+ assert args == 'history'
+ assert line.strip() == out_line
+
+def test_parseline_expands_shortcuts(cmd2_app):
+ line = '!cat foobar.txt'
+ command, args, out_line = cmd2_app.parseline(line)
+ assert command == 'shell'
+ assert args == 'cat foobar.txt'
+ assert line.replace('!', 'shell ') == out_line