|
3 | 3 | Unit/functional testing for helper functions/classes in the cmd2.py module.
|
4 | 4 |
|
5 | 5 | These are primarily tests related to parsing. Moreover, they are mostly a port of the old doctest tests which were
|
6 |
| -problematic because they worked properly in Python 2, but not in Python 3. |
| 6 | +problematic because they worked properly for some versions of pyparsing but not for others. |
7 | 7 |
|
8 | 8 | Copyright 2017 Todd Leonhardt <[email protected]>
|
9 | 9 | Released under MIT license, see LICENSE file
|
10 | 10 | """
|
| 11 | +import sys |
| 12 | + |
11 | 13 | import cmd2
|
12 | 14 | import pyparsing
|
13 |
| -from pytest import fixture |
| 15 | +import pytest |
14 | 16 |
|
15 | 17 | # NOTE: pyparsing's ParseResults.dump() function behaves differently in versions >= 2.1.10
|
16 | 18 | # In version 2.1.10, changed display of string values to show them in quotes
|
|
28 | 30 | new_pyparsing = False
|
29 | 31 |
|
30 | 32 |
|
31 |
| -@fixture |
| 33 | +@pytest.fixture |
32 | 34 | def hist():
|
33 | 35 | from cmd2 import HistoryItem
|
34 | 36 | h = cmd2.History([HistoryItem('first'), HistoryItem('second'), HistoryItem('third'), HistoryItem('fourth')])
|
35 | 37 | return h
|
36 | 38 |
|
37 | 39 |
|
38 |
| -@fixture |
| 40 | +@pytest.fixture |
39 | 41 | def parser():
|
40 | 42 | c = cmd2.Cmd()
|
41 | 43 | c.multilineCommands = ['multiline']
|
42 | 44 | c.case_insensitive = True
|
43 | 45 | c._init_parser()
|
44 | 46 | return c.parser
|
45 | 47 |
|
46 |
| -@fixture |
| 48 | +@pytest.fixture |
47 | 49 | def input_parser():
|
48 | 50 | c = cmd2.Cmd()
|
49 | 51 | c._init_parser()
|
@@ -465,3 +467,65 @@ def test_parse_multiline_ignores_terminators_in_comments(parser):
|
465 | 467 | - terminator: {2}
|
466 | 468 | - terminator: {2}""".format(multiline, args, terminator)
|
467 | 469 | assert parser.parseString('multiline command "with term; ends" now\n\n').dump() == expected
|
| 470 | + |
| 471 | + |
| 472 | +# Unicode support is only present in cmd2 for Python 3 |
| 473 | +@pytest.mark.skipif(sys.version_info < (3,0), reason="cmd2 unicode support requires python3") |
| 474 | +def test_parse_command_with_unicode_args(parser): |
| 475 | + command = "drink" |
| 476 | + args = "café" |
| 477 | + if new_pyparsing: |
| 478 | + command = repr(command) |
| 479 | + args = repr(args) |
| 480 | + expected = """['drink', 'café'] |
| 481 | +- args: {1} |
| 482 | +- command: {0} |
| 483 | +- statement: ['drink', 'café'] |
| 484 | + - args: {1} |
| 485 | + - command: {0}""".format(command, args) |
| 486 | + assert parser.parseString('drink café').dump() == expected |
| 487 | + |
| 488 | +@pytest.mark.skipif(sys.version_info < (3, 0), reason="cmd2 unicode support requires python3") |
| 489 | +def test_parse_unicode_command(parser): |
| 490 | + command = "café" |
| 491 | + args = "au lait" |
| 492 | + if new_pyparsing: |
| 493 | + command = repr(command) |
| 494 | + args = repr(args) |
| 495 | + expected = """['café', 'au lait'] |
| 496 | +- args: {1} |
| 497 | +- command: {0} |
| 498 | +- statement: ['café', 'au lait'] |
| 499 | + - args: {1} |
| 500 | + - command: {0}""".format(command, args) |
| 501 | + assert parser.parseString('café au lait').dump() == expected |
| 502 | + |
| 503 | +@pytest.mark.skipif(sys.version_info < (3,0), reason="cmd2 unicode support requires python3") |
| 504 | +def test_parse_redirect_to_unicode_filename(parser): |
| 505 | + command = "dir" |
| 506 | + args = "home" |
| 507 | + redirect = ">" |
| 508 | + output = "café" |
| 509 | + if new_pyparsing: |
| 510 | + command = repr(command) |
| 511 | + args = repr(args) |
| 512 | + redirect = repr(redirect) |
| 513 | + output = repr(output) |
| 514 | + expected = """['dir', 'home', '>', 'café'] |
| 515 | +- args: {1} |
| 516 | +- command: {0} |
| 517 | +- output: {2} |
| 518 | +- outputTo: {3} |
| 519 | +- statement: ['dir', 'home'] |
| 520 | + - args: {1} |
| 521 | + - command: {0}""".format(command, args, redirect, output) |
| 522 | + assert parser.parseString('dir home > café').dump() == expected |
| 523 | + |
| 524 | +@pytest.mark.skipif(sys.version_info < (3,0), reason="cmd2 unicode support requires python3") |
| 525 | +def test_parse_input_redirect_from_unicode_filename(input_parser): |
| 526 | + input_from = "< café" |
| 527 | + if new_pyparsing: |
| 528 | + input_from = repr(input_from) |
| 529 | + expected = """['', '< café'] |
| 530 | +- inputFrom: {0}""".format(input_from) |
| 531 | + assert input_parser.parseString('< café').dump() == expected |
0 commit comments