@@ -1745,6 +1745,62 @@ def test_select_ctrl_c(outsim_app, monkeypatch) -> None:
17451745 assert out .rstrip ().endswith ('^C' )
17461746
17471747
1748+ def test_select_choice_tty (outsim_app , monkeypatch ) -> None :
1749+ # Mock choice to return the first option
1750+ choice_mock = mock .MagicMock (name = 'choice' , return_value = 'sweet' )
1751+ monkeypatch .setattr ("cmd2.cmd2.choice" , choice_mock )
1752+
1753+ # Mock isatty to be True for both stdin and stdout
1754+ monkeypatch .setattr (outsim_app .stdin , "isatty" , lambda : True )
1755+ monkeypatch .setattr (outsim_app .stdout , "isatty" , lambda : True )
1756+
1757+ prompt = 'Sauce? '
1758+ options = ['sweet' , 'salty' ]
1759+ result = outsim_app .select (options , prompt )
1760+
1761+ assert result == 'sweet'
1762+ choice_mock .assert_called_once_with (message = prompt , options = [('sweet' , 'sweet' ), ('salty' , 'salty' )])
1763+
1764+
1765+ def test_select_choice_tty_ctrl_c (outsim_app , monkeypatch ) -> None :
1766+ # Mock choice to raise KeyboardInterrupt
1767+ choice_mock = mock .MagicMock (name = 'choice' , side_effect = KeyboardInterrupt )
1768+ monkeypatch .setattr ("cmd2.cmd2.choice" , choice_mock )
1769+
1770+ # Mock isatty to be True for both stdin and stdout
1771+ monkeypatch .setattr (outsim_app .stdin , "isatty" , lambda : True )
1772+ monkeypatch .setattr (outsim_app .stdout , "isatty" , lambda : True )
1773+
1774+ prompt = 'Sauce? '
1775+ options = ['sweet' , 'salty' ]
1776+
1777+ with pytest .raises (KeyboardInterrupt ):
1778+ outsim_app .select (options , prompt )
1779+
1780+ out = outsim_app .stdout .getvalue ()
1781+ assert out .rstrip ().endswith ('^C' )
1782+
1783+
1784+ def test_select_uneven_tuples_labels (outsim_app , monkeypatch ) -> None :
1785+ # Test that uneven tuples still work and labels are handled correctly
1786+ # Case 1: (value, label) - normal
1787+ # Case 2: (value,) - label should be value
1788+ # Case 3: (value, None) - label should be value
1789+ options = [('v1' , 'l1' ), ('v2' ,), ('v3' , None )]
1790+
1791+ # Mock read_input to return '1'
1792+ read_input_mock = mock .MagicMock (name = 'read_input' , return_value = '1' )
1793+ monkeypatch .setattr ("cmd2.Cmd.read_input" , read_input_mock )
1794+
1795+ result = outsim_app .select (options , 'Choice? ' )
1796+ assert result == 'v1'
1797+
1798+ out = outsim_app .stdout .getvalue ()
1799+ assert '1. l1' in out
1800+ assert '2. v2' in out
1801+ assert '3. v3' in out
1802+
1803+
17481804class HelpNoDocstringApp (cmd2 .Cmd ):
17491805 greet_parser = cmd2 .Cmd2ArgumentParser ()
17501806 greet_parser .add_argument ('-s' , '--shout' , action = "store_true" , help = "N00B EMULATION MODE" )
0 commit comments