Skip to content

Commit

Permalink
Added unit test for Cmd.select() return values
Browse files Browse the repository at this point in the history
  • Loading branch information
kmvanbrunt committed Oct 25, 2021
1 parent 4fac490 commit 84e7a49
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* Bug Fixes
* Fixed `AttributeError` in `rl_get_prompt()` when prompt is `None`.
* Fixed bug where using choices on a Settable didn't verify that a valid choice had been entered.
* Fixed bug introduced in cmd2 2.0.0 in which `select()` converts return values to strings. It should never
have converted return values.
* Enhancements
* Added settings to Column class which prevent a table from overriding existing styles in header
and/or data text. This allows for things like nesting an AlternatingTable in another AlternatingTable.
Expand Down
38 changes: 38 additions & 0 deletions tests/test_cmd2.py
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,12 @@ def do_play(self, arg):
result = 'Charm us with the {}...\n'.format(instrument)
self.stdout.write(result)

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


@pytest.fixture
def select_app():
Expand Down Expand Up @@ -1382,6 +1388,38 @@ def test_select_uneven_list_of_tuples(select_app, monkeypatch):
assert out == expected


@pytest.mark.parametrize(
'selection, type_str',
[
('1', "<class 'int'>"),
('2', "<class 'str'>"),
('3', "<class 'method'>"),
],
)
def test_select_return_type(select_app, monkeypatch, selection, type_str):
# Mock out the input call so we don't actually wait for a user's response on stdin
read_input_mock = mock.MagicMock(name='read_input', return_value=selection)
monkeypatch.setattr("cmd2.Cmd.read_input", read_input_mock)

out, err = run_cmd(select_app, "return_type")
expected = normalize(
"""
1. Integer
2. String
3. Method
The return type is {}
""".format(
type_str
)
)

# Make sure our mock was called with the expected arguments
read_input_mock.assert_called_once_with('Choice? ')

# And verify the expected output to stdout
assert out == expected


def test_select_eof(select_app, monkeypatch):
# Ctrl-D during select causes an EOFError that just reprompts the user
read_input_mock = mock.MagicMock(name='read_input', side_effect=[EOFError, 2])
Expand Down

0 comments on commit 84e7a49

Please sign in to comment.