summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--tests/test_cmd2.py43
-rw-r--r--tests/test_completion.py39
2 files changed, 80 insertions, 2 deletions
diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py
index 50b34949..d69bf343 100644
--- a/tests/test_cmd2.py
+++ b/tests/test_cmd2.py
@@ -1542,3 +1542,46 @@ def test_poutput_none(base_app):
out = base_app.stdout.buffer
expected = ''
assert out == expected
+
+
+def test_alias(base_app, capsys):
+ # Create the alias
+ out = run_cmd(base_app, 'alias fake pyscript')
+ assert out == normalize("Alias created")
+
+ # Use the alias
+ run_cmd(base_app, 'fake')
+ out, err = capsys.readouterr()
+ assert "pyscript command requires at least 1 argument" in err
+
+ # See a list of aliases
+ out = run_cmd(base_app, 'alias')
+ assert out == normalize('alias fake pyscript')
+
+def test_alias_with_cmd_name(base_app, capsys):
+ run_cmd(base_app, 'alias help eos')
+ out, err = capsys.readouterr()
+ assert "cannot match an existing command" in err
+
+def test_alias_with_invalid_name(base_app, capsys):
+ run_cmd(base_app, 'alias @ help')
+ out, err = capsys.readouterr()
+ assert "can only contain the following characters" in err
+
+
+def test_unalias(base_app):
+ # Create an alias
+ run_cmd(base_app, 'alias fake pyscript')
+
+ # Remove the alias
+ out = run_cmd(base_app, 'unalias fake')
+ assert out == normalize("Alias 'fake' cleared")
+
+def test_unalias_all(base_app):
+ out = run_cmd(base_app, 'unalias -a')
+ assert out == normalize("All aliases cleared")
+
+def test_unalias_non_existing(base_app, capsys):
+ run_cmd(base_app, 'unalias fake')
+ out, err = capsys.readouterr()
+ assert "does not exist" in err
diff --git a/tests/test_completion.py b/tests/test_completion.py
index 7eac5366..a9f75dce 100644
--- a/tests/test_completion.py
+++ b/tests/test_completion.py
@@ -17,7 +17,7 @@ import cmd2
import mock
import pytest
-from cmd2 import path_complete, flag_based_complete, index_based_complete
+from cmd2 import path_complete, basic_complete, flag_based_complete, index_based_complete
@pytest.fixture
def cmd2_app():
@@ -399,7 +399,7 @@ def test_path_completion_no_tokens():
assert path_complete(text, line, begidx, endidx) == []
-# List of strings used with flag and index based completion functions
+# List of strings used with basic, flag, and index based completion functions
food_item_strs = ['Pizza', 'Hamburger', 'Ham', 'Potato']
sport_item_strs = ['Bat', 'Basket', 'Basketball', 'Football']
@@ -414,6 +414,39 @@ flag_dict = \
'--other': path_complete, # Tab-complete using path_complete function after --other flag in command line
}
+def test_basic_completion_single_end():
+ text = 'Pi'
+ line = 'list_food -f Pi'
+ endidx = len(line)
+ begidx = endidx - len(text)
+
+ assert basic_complete(text, line, begidx, endidx, food_item_strs) == ['Pizza ']
+
+def test_basic_completion_single_mid():
+ text = 'Pi'
+ line = 'list_food -f Pi'
+ begidx = len(line) - len(text)
+ endidx = begidx + 1
+
+ assert basic_complete(text, line, begidx, endidx, food_item_strs) == ['Pizza']
+
+def test_basic_completion_multiple():
+ text = ''
+ line = 'list_food -f '
+ endidx = len(line)
+ begidx = endidx - len(text)
+
+ assert basic_complete(text, line, begidx, endidx, food_item_strs) == sorted(food_item_strs)
+
+def test_basic_completion_nomatch():
+ text = 'q'
+ line = 'list_food -f q'
+ endidx = len(line)
+ begidx = endidx - len(text)
+
+ assert basic_complete(text, line, begidx, endidx, food_item_strs) == []
+
+
def test_flag_based_completion_single_end():
text = 'Pi'
line = 'list_food -f Pi'
@@ -435,6 +468,7 @@ def test_flag_based_completion_multiple():
line = 'list_food -f '
endidx = len(line)
begidx = endidx - len(text)
+
assert flag_based_complete(text, line, begidx, endidx, flag_dict) == sorted(food_item_strs)
def test_flag_based_completion_nomatch():
@@ -442,6 +476,7 @@ def test_flag_based_completion_nomatch():
line = 'list_food -f q'
endidx = len(line)
begidx = endidx - len(text)
+
assert flag_based_complete(text, line, begidx, endidx, flag_dict) == []
def test_flag_based_default_completer(request):