@@ -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
12251231def 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+
13851423def 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