Skip to content

Commit 84e7a49

Browse files
committed
Added unit test for Cmd.select() return values
1 parent 4fac490 commit 84e7a49

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
* Bug Fixes
33
* Fixed `AttributeError` in `rl_get_prompt()` when prompt is `None`.
44
* Fixed bug where using choices on a Settable didn't verify that a valid choice had been entered.
5+
* Fixed bug introduced in cmd2 2.0.0 in which `select()` converts return values to strings. It should never
6+
have converted return values.
57
* Enhancements
68
* Added settings to Column class which prevent a table from overriding existing styles in header
79
and/or data text. This allows for things like nesting an AlternatingTable in another AlternatingTable.

tests/test_cmd2.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,6 +1220,12 @@ def do_play(self, arg):
12201220
result = 'Charm us with the {}...\n'.format(instrument)
12211221
self.stdout.write(result)
12221222

1223+
def do_return_type(self, arg):
1224+
"""Test that return values can be non-strings"""
1225+
choice = self.select([(1, 'Integer'), ("test_str", 'String'), (self.do_play, 'Method')], 'Choice? ')
1226+
result = f'The return type is {type(choice)}\n'
1227+
self.stdout.write(result)
1228+
12231229

12241230
@pytest.fixture
12251231
def select_app():
@@ -1382,6 +1388,38 @@ def test_select_uneven_list_of_tuples(select_app, monkeypatch):
13821388
assert out == expected
13831389

13841390

1391+
@pytest.mark.parametrize(
1392+
'selection, type_str',
1393+
[
1394+
('1', "<class 'int'>"),
1395+
('2', "<class 'str'>"),
1396+
('3', "<class 'method'>"),
1397+
],
1398+
)
1399+
def test_select_return_type(select_app, monkeypatch, selection, type_str):
1400+
# Mock out the input call so we don't actually wait for a user's response on stdin
1401+
read_input_mock = mock.MagicMock(name='read_input', return_value=selection)
1402+
monkeypatch.setattr("cmd2.Cmd.read_input", read_input_mock)
1403+
1404+
out, err = run_cmd(select_app, "return_type")
1405+
expected = normalize(
1406+
"""
1407+
1. Integer
1408+
2. String
1409+
3. Method
1410+
The return type is {}
1411+
""".format(
1412+
type_str
1413+
)
1414+
)
1415+
1416+
# Make sure our mock was called with the expected arguments
1417+
read_input_mock.assert_called_once_with('Choice? ')
1418+
1419+
# And verify the expected output to stdout
1420+
assert out == expected
1421+
1422+
13851423
def test_select_eof(select_app, monkeypatch):
13861424
# Ctrl-D during select causes an EOFError that just reprompts the user
13871425
read_input_mock = mock.MagicMock(name='read_input', side_effect=[EOFError, 2])

0 commit comments

Comments
 (0)