summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2019-05-27 15:45:34 -0400
committerTodd Leonhardt <todd.leonhardt@gmail.com>2019-05-27 15:45:34 -0400
commit9dd00461b22085d3f16b4cc178a222c30fe95d11 (patch)
treee1644047665caf1d15860be388510fe7b7b61d1a /tests
parent916060bde828d8911ec2bbb4db54396212514481 (diff)
downloadcmd2-git-9dd00461b22085d3f16b4cc178a222c30fe95d11.tar.gz
Add the -a/--all flag to the history command for showing all commands including those persisted from previous sessions
Also: - History class has been modified to keep track of the session start index - History class span(), str_search(), and regex_search() methods now take an optional 2nd boolean parameter `include_persisted` which determines whether or not commands persisted from previous sessions should be included by default - If a start index is manually specified, then it automatically includes the full search - Updates unit tests
Diffstat (limited to 'tests')
-rw-r--r--tests/conftest.py3
-rw-r--r--tests/test_history.py78
2 files changed, 77 insertions, 4 deletions
diff --git a/tests/conftest.py b/tests/conftest.py
index 9d55eb4d..769e5a8f 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -59,6 +59,7 @@ shortcuts List available shortcuts
# Help text for the history command
HELP_HISTORY = """Usage: history [-h] [-r | -e | -o FILE | -t TRANSCRIPT | -c] [-s] [-x] [-v]
+ [-a]
[arg]
View, run, edit, save, or clear previously entered commands
@@ -88,7 +89,7 @@ formatting:
macros expanded, instead of typed commands
-v, --verbose display history and include expanded commands if they
differ from the typed command
-
+ -a, --all display all commands, including ones persisted from previous sessions
"""
# Output from the shortcuts command with default built-in shortcuts
diff --git a/tests/test_history.py b/tests/test_history.py
index 1956a574..5e01688c 100644
--- a/tests/test_history.py
+++ b/tests/test_history.py
@@ -43,6 +43,19 @@ def hist():
HistoryItem(Statement('', raw='fourth'),4)])
return h
+@pytest.fixture
+def persisted_hist():
+ from cmd2.parsing import Statement
+ from cmd2.cmd2 import History, HistoryItem
+ h = History([HistoryItem(Statement('', raw='first'), 1),
+ HistoryItem(Statement('', raw='second'), 2),
+ HistoryItem(Statement('', raw='third'), 3),
+ HistoryItem(Statement('', raw='fourth'),4)])
+ h.start_session()
+ h.append(Statement('', raw='fifth'))
+ h.append(Statement('', raw='sixth'))
+ return h
+
def test_history_class_span(hist):
for tryit in ['*', ':', '-', 'all', 'ALL']:
assert hist.span(tryit) == hist
@@ -119,6 +132,62 @@ def test_history_class_span(hist):
with pytest.raises(ValueError):
hist.span(tryit)
+def test_persisted_history_span(persisted_hist):
+ for tryit in ['*', ':', '-', 'all', 'ALL']:
+ assert persisted_hist.span(tryit, include_persisted=True) == persisted_hist
+ assert persisted_hist.span(tryit, include_persisted=False) != persisted_hist
+
+ assert persisted_hist.span('3')[0].statement.raw == 'third'
+ assert persisted_hist.span('-1')[0].statement.raw == 'sixth'
+
+ span = persisted_hist.span('2..')
+ assert len(span) == 5
+ assert span[0].statement.raw == 'second'
+ assert span[1].statement.raw == 'third'
+ assert span[2].statement.raw == 'fourth'
+ assert span[3].statement.raw == 'fifth'
+ assert span[4].statement.raw == 'sixth'
+
+ span = persisted_hist.span('-2..')
+ assert len(span) == 2
+ assert span[0].statement.raw == 'fifth'
+ assert span[1].statement.raw == 'sixth'
+
+ span = persisted_hist.span('1..3')
+ assert len(span) == 3
+ assert span[0].statement.raw == 'first'
+ assert span[1].statement.raw == 'second'
+ assert span[2].statement.raw == 'third'
+
+ span = persisted_hist.span('2:-1')
+ assert len(span) == 5
+ assert span[0].statement.raw == 'second'
+ assert span[1].statement.raw == 'third'
+ assert span[2].statement.raw == 'fourth'
+ assert span[3].statement.raw == 'fifth'
+ assert span[4].statement.raw == 'sixth'
+
+ span = persisted_hist.span('-3:4')
+ assert len(span) == 1
+ assert span[0].statement.raw == 'fourth'
+
+ span = persisted_hist.span(':-2', include_persisted=True)
+ assert len(span) == 5
+ assert span[0].statement.raw == 'first'
+ assert span[1].statement.raw == 'second'
+ assert span[2].statement.raw == 'third'
+ assert span[3].statement.raw == 'fourth'
+ assert span[4].statement.raw == 'fifth'
+
+ span = persisted_hist.span(':-2', include_persisted=False)
+ assert len(span) == 1
+ assert span[0].statement.raw == 'fifth'
+
+ value_errors = ['fred', 'fred:joe', 'a..b', '2 ..', '1 : 3', '1:0', '0:3']
+ for tryit in value_errors:
+ with pytest.raises(ValueError):
+ persisted_hist.span(tryit)
+
def test_history_class_get(hist):
assert hist.get('1').statement.raw == 'first'
assert hist.get(3).statement.raw == 'third'
@@ -401,7 +470,8 @@ def test_history_verbose_with_other_options(base_app):
options_to_test = ['-r', '-e', '-o file', '-t file', '-c', '-x']
for opt in options_to_test:
out, err = run_cmd(base_app, 'history -v ' + opt)
- assert len(out) == 3
+ assert len(out) == 4
+ assert out[0] == '-v can not be used with any other options'
assert out[1].startswith('Usage:')
def test_history_verbose(base_app):
@@ -417,7 +487,8 @@ def test_history_script_with_invalid_options(base_app):
options_to_test = ['-r', '-e', '-o file', '-t file', '-c']
for opt in options_to_test:
out, err = run_cmd(base_app, 'history -s ' + opt)
- assert len(out) == 3
+ assert len(out) == 4
+ assert out[0] == '-s and -x can not be used with -c, -r, -e, -o, or -t'
assert out[1].startswith('Usage:')
def test_history_script(base_app):
@@ -432,7 +503,8 @@ def test_history_expanded_with_invalid_options(base_app):
options_to_test = ['-r', '-e', '-o file', '-t file', '-c']
for opt in options_to_test:
out, err = run_cmd(base_app, 'history -x ' + opt)
- assert len(out) == 3
+ assert len(out) == 4
+ assert out[0] == '-s and -x can not be used with -c, -r, -e, -o, or -t'
assert out[1].startswith('Usage:')
def test_history_expanded(base_app):