|
10 | 10 |
|
11 | 11 | # Python 3.5 had some regressions in the unitest.mock module, so use
|
12 | 12 | # 3rd party mock if available
|
| 13 | +from cmd2.parsing import StatementParser |
| 14 | + |
13 | 15 | try:
|
14 | 16 | import mock
|
15 | 17 | except ImportError:
|
@@ -262,6 +264,47 @@ def histitem():
|
262 | 264 | histitem = HistoryItem(statement, 1)
|
263 | 265 | return histitem
|
264 | 266 |
|
| 267 | +@pytest.fixture |
| 268 | +def parser(): |
| 269 | + from cmd2.parsing import StatementParser |
| 270 | + parser = StatementParser( |
| 271 | + allow_redirection=True, |
| 272 | + terminators=[';', '&'], |
| 273 | + multiline_commands=['multiline'], |
| 274 | + aliases={'helpalias': 'help', |
| 275 | + '42': 'theanswer', |
| 276 | + 'l': '!ls -al', |
| 277 | + 'anothermultiline': 'multiline', |
| 278 | + 'fake': 'pyscript'}, |
| 279 | + shortcuts=[('?', 'help'), ('!', 'shell')] |
| 280 | + ) |
| 281 | + return parser |
| 282 | + |
| 283 | +def test_multiline_histitem(parser): |
| 284 | + from cmd2.history import History |
| 285 | + line = 'multiline foo\nbar\n\n' |
| 286 | + statement = parser.parse(line) |
| 287 | + history = History() |
| 288 | + history.append(statement) |
| 289 | + assert len(history) == 1 |
| 290 | + hist_item = history[0] |
| 291 | + assert hist_item.raw == line |
| 292 | + pr_lines = hist_item.pr().splitlines() |
| 293 | + assert pr_lines[0].endswith('multiline foo bar') |
| 294 | + |
| 295 | +def test_multiline_histitem_verbose(parser): |
| 296 | + from cmd2.history import History |
| 297 | + line = 'multiline foo\nbar\n\n' |
| 298 | + statement = parser.parse(line) |
| 299 | + history = History() |
| 300 | + history.append(statement) |
| 301 | + assert len(history) == 1 |
| 302 | + hist_item = history[0] |
| 303 | + assert hist_item.raw == line |
| 304 | + pr_lines = hist_item.pr(verbose=True).splitlines() |
| 305 | + assert pr_lines[0].endswith('multiline foo') |
| 306 | + assert pr_lines[1] == 'bar' |
| 307 | + |
265 | 308 | def test_history_item_instantiate():
|
266 | 309 | from cmd2.parsing import Statement
|
267 | 310 | from cmd2.history import HistoryItem
|
|
0 commit comments