@@ -2161,51 +2161,45 @@ def test_url_requests(self):
21612161
21622162
21632163class TestHelper (unittest .TestCase ):
2164+ def mock_interactive_session (self , inputs ):
2165+ """
2166+ Given a list of inputs, run an interactive help session. Returns a string
2167+ of what would be shown on screen.
2168+ """
2169+ input_iter = iter (inputs )
2170+
2171+ def mock_getline (prompt ):
2172+ output .write (prompt )
2173+ next_input = next (input_iter )
2174+ output .write (next_input + os .linesep )
2175+ return next_input
2176+
2177+ with captured_stdout () as output :
2178+ helper = pydoc .Helper (output = output )
2179+ with unittest .mock .patch .object (helper , "getline" , mock_getline ):
2180+ helper .interact ()
2181+
2182+ return output .getvalue ().replace (os .linesep , "\n " )
2183+
21642184 def test_keywords (self ):
21652185 self .assertEqual (sorted (pydoc .Helper .keywords ),
21662186 sorted (keyword .kwlist ))
21672187
21682188 def test_interact_empty_line_continues (self ):
21692189 # gh-138568: test pressing Enter without input should continue in help session
2170- with captured_stdout () as output :
2171- helper = pydoc .Helper (output = output )
2172- input_sequence = ['' , 'quit' ]
2173- call_count = [0 ]
2174- def mock_getline (prompt ):
2175- output .write (prompt )
2176- result = input_sequence [call_count [0 ]]
2177- call_count [0 ] += 1
2178- return result
2179-
2180- with unittest .mock .patch .object (helper , 'getline' , side_effect = mock_getline ):
2181- helper .interact ()
2182-
2183- output_text = output .getvalue ()
2184- prompt_count = output_text .count ('help> ' )
2185- self .assertEqual (prompt_count , 2 )
2190+ self .assertEqual (
2191+ self .mock_interactive_session (["" , " " , "quit" ]),
2192+ "\n help> \n help> \n help> quit\n " ,
2193+ )
21862194
21872195 def test_interact_quit_commands_exit (self ):
2188- quit_commands = ['quit' , 'q' , 'exit' ]
2189-
2196+ quit_commands = ["quit" , "q" , "exit" ]
21902197 for quit_cmd in quit_commands :
21912198 with self .subTest (quit_command = quit_cmd ):
2192- with captured_stdout () as output :
2193- helper = pydoc .Helper (output = output )
2194-
2195- call_count = [0 ]
2196-
2197- def mock_getline (prompt ):
2198- output .write (prompt )
2199- call_count [0 ] += 1
2200- return quit_cmd
2201-
2202- with unittest .mock .patch .object (helper , 'getline' , side_effect = mock_getline ):
2203- helper .interact ()
2204-
2205- self .assertEqual (call_count [0 ], 1 )
2206- output_text = output .getvalue ()
2207- prompt_count = output_text .count ('help> ' )
2208- self .assertEqual (prompt_count , 1 )
2199+ self .assertEqual (
2200+ self .mock_interactive_session ([quit_cmd ]),
2201+ f"\n help> { quit_cmd } \n " ,
2202+ )
22092203
22102204
22112205class PydocWithMetaClasses (unittest .TestCase ):
0 commit comments