Skip to content

Commit 2885ada

Browse files
authored
Merge pull request #58 from python-cmd2/unicode_tests
Added unicode parsing tests
2 parents 233dc43 + 251ca40 commit 2885ada

File tree

2 files changed

+70
-5
lines changed

2 files changed

+70
-5
lines changed

README.rst

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ cmd2 provides the following features, in addition to those already existing in c
6161
- Bare ``>``, ``>>`` with no filename send output to paste buffer
6262
- Pipe output to shell commands with ``|``
6363
- Simple transcript-based application testing
64+
- Unicode character support (*Python 3 only*)
6465

6566
Instructions for implementing each feature follow.
6667

tests/test_parsing.py

+69-5
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
Unit/functional testing for helper functions/classes in the cmd2.py module.
44
55
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.
77
88
Copyright 2017 Todd Leonhardt <[email protected]>
99
Released under MIT license, see LICENSE file
1010
"""
11+
import sys
12+
1113
import cmd2
1214
import pyparsing
13-
from pytest import fixture
15+
import pytest
1416

1517
# NOTE: pyparsing's ParseResults.dump() function behaves differently in versions >= 2.1.10
1618
# In version 2.1.10, changed display of string values to show them in quotes
@@ -28,22 +30,22 @@
2830
new_pyparsing = False
2931

3032

31-
@fixture
33+
@pytest.fixture
3234
def hist():
3335
from cmd2 import HistoryItem
3436
h = cmd2.History([HistoryItem('first'), HistoryItem('second'), HistoryItem('third'), HistoryItem('fourth')])
3537
return h
3638

3739

38-
@fixture
40+
@pytest.fixture
3941
def parser():
4042
c = cmd2.Cmd()
4143
c.multilineCommands = ['multiline']
4244
c.case_insensitive = True
4345
c._init_parser()
4446
return c.parser
4547

46-
@fixture
48+
@pytest.fixture
4749
def input_parser():
4850
c = cmd2.Cmd()
4951
c._init_parser()
@@ -465,3 +467,65 @@ def test_parse_multiline_ignores_terminators_in_comments(parser):
465467
- terminator: {2}
466468
- terminator: {2}""".format(multiline, args, terminator)
467469
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

Comments
 (0)