Skip to content

Commit 1fe024f

Browse files
committed
Refactored implementation of HistoryItem.__str__ and added an explicit HistoryItem unit test
1 parent 2c96da0 commit 1fe024f

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

cmd2/history.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@ class HistoryItem():
2424

2525
def __str__(self):
2626
"""A convenient human readable representation of the history item"""
27-
if self.statement:
28-
return self.statement.raw
29-
else:
30-
return ''
27+
return self.statement.raw
3128

3229
@property
3330
def raw(self) -> str:

tests/test_history.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
"""
66
import tempfile
77
import os
8-
import pickle
9-
import sys
108

119
import pytest
1210

@@ -18,6 +16,8 @@
1816

1917
import cmd2
2018
from .conftest import run_cmd, normalize, HELP_HISTORY
19+
from cmd2.parsing import Statement
20+
from cmd2.history import HistoryItem
2121

2222

2323
def test_base_help_history(base_app):
@@ -50,17 +50,26 @@ def test_exclude_from_history(base_app, monkeypatch):
5050
expected = normalize(""" 1 help""")
5151
assert out == expected
5252

53-
5453
@pytest.fixture
5554
def hist():
56-
from cmd2.parsing import Statement
57-
from cmd2.cmd2 import History, HistoryItem
55+
from cmd2.history import History
5856
h = History([HistoryItem(Statement('', raw='first'), 1),
5957
HistoryItem(Statement('', raw='second'), 2),
6058
HistoryItem(Statement('', raw='third'), 3),
61-
HistoryItem(Statement('', raw='fourth'),4)])
59+
HistoryItem(Statement('', raw='fourth'), 4)])
6260
return h
6361

62+
def test_history_item():
63+
raw = 'help'
64+
stmt = Statement('', raw=raw)
65+
index = 1
66+
hi = HistoryItem(stmt, index)
67+
assert hi.statement == stmt
68+
assert hi.idx == index
69+
assert hi.statement.raw == raw
70+
assert str(hi) == raw
71+
72+
6473
def test_history_class_span(hist):
6574
for tryit in ['*', ':', '-', 'all', 'ALL']:
6675
assert hist.span(tryit) == hist

0 commit comments

Comments
 (0)