Skip to content

Commit c061b95

Browse files
committed
Add unit test for uncovered code
1 parent de5f9a8 commit c061b95

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

tests/test_cmd2.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1801,6 +1801,27 @@ def test_select_uneven_tuples_labels(outsim_app, monkeypatch) -> None:
18011801
assert '3. v3' in out
18021802

18031803

1804+
def test_select_indexable_no_len(outsim_app, monkeypatch) -> None:
1805+
# Test that an object with __getitem__ but no __len__ works.
1806+
# This covers the except (IndexError, TypeError) block in select()
1807+
class IndexableNoLen:
1808+
def __getitem__(self, item: int) -> str:
1809+
if item == 0:
1810+
return 'value'
1811+
raise IndexError
1812+
1813+
# Mock read_input to return '1'
1814+
read_input_mock = mock.MagicMock(name='read_input', return_value='1')
1815+
monkeypatch.setattr("cmd2.Cmd.read_input", read_input_mock)
1816+
1817+
options = [IndexableNoLen()]
1818+
result = outsim_app.select(options, 'Choice? ')
1819+
assert result == 'value'
1820+
1821+
out = outsim_app.stdout.getvalue()
1822+
assert '1. value' in out
1823+
1824+
18041825
class HelpNoDocstringApp(cmd2.Cmd):
18051826
greet_parser = cmd2.Cmd2ArgumentParser()
18061827
greet_parser.add_argument('-s', '--shout', action="store_true", help="N00B EMULATION MODE")

0 commit comments

Comments
 (0)