Skip to content

Commit 6d711bc

Browse files
authored
Merge pull request #223 from python-cmd2/poutput_falsy
Improved poutput() so that it can print falsy things
2 parents 5d5675b + 25efdfb commit 6d711bc

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

cmd2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ def poutput(self, msg, end='\n'):
585585
:param msg: str - message to print to current stdout - anyting convertible to a str with '{}'.format() is OK
586586
:param end: str - string appended after the end of the message if not already present, default a newline
587587
"""
588-
if msg:
588+
if msg is not None and msg != '':
589589
try:
590590
msg_str = '{}'.format(msg)
591591
self.stdout.write(msg_str)

tests/test_cmd2.py

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,20 +1406,20 @@ def test_pseudo_raw_input_tty_rawinput_true():
14061406
# for the 'set' command, and once for the 'quit' command,
14071407
# that the rest of it worked
14081408
assert m_input.call_count == 2
1409-
1409+
14101410
def test_pseudo_raw_input_tty_rawinput_false():
14111411
# gin up some input like it's coming from a tty
14121412
fakein = io.StringIO(u'{}'.format('set\n'))
14131413
mtty = mock.MagicMock(name='isatty', return_value=True)
14141414
fakein.isatty = mtty
14151415
mreadline = mock.MagicMock(name='readline', wraps=fakein.readline)
14161416
fakein.readline = mreadline
1417-
1417+
14181418
# run the cmdloop, telling it where to get input from
14191419
app = cmd2.Cmd(stdin=fakein)
14201420
app.use_rawinput = False
14211421
app._cmdloop()
1422-
1422+
14231423
# because we mocked the readline() call, we won't get the prompt
14241424
# or the name of the command in the output, so we can't check
14251425
# if its there. We assume that if readline() got called twice, once
@@ -1523,3 +1523,32 @@ def test_empty_stdin_input():
15231523

15241524
line = app.pseudo_raw_input('(cmd2)')
15251525
assert line == 'eof'
1526+
1527+
1528+
def test_poutput_string(base_app):
1529+
msg = 'This is a test'
1530+
base_app.poutput(msg)
1531+
out = base_app.stdout.buffer
1532+
expected = msg + '\n'
1533+
assert out == expected
1534+
1535+
def test_poutput_zero(base_app):
1536+
msg = 0
1537+
base_app.poutput(msg)
1538+
out = base_app.stdout.buffer
1539+
expected = str(msg) + '\n'
1540+
assert out == expected
1541+
1542+
def test_poutput_empty_string(base_app):
1543+
msg = ''
1544+
base_app.poutput(msg)
1545+
out = base_app.stdout.buffer
1546+
expected = msg
1547+
assert out == expected
1548+
1549+
def test_poutput_none(base_app):
1550+
msg = None
1551+
base_app.poutput(msg)
1552+
out = base_app.stdout.buffer
1553+
expected = ''
1554+
assert out == expected

0 commit comments

Comments
 (0)