@@ -3431,3 +3431,64 @@ def test_no_console_screen_buffer_error_dummy():
34313431 # Check that it behaves like a normal exception
34323432 err = NoConsoleScreenBufferError ()
34333433 assert isinstance (err , Exception )
3434+
3435+
3436+ def test_read_input_dynamic_prompt (base_app , monkeypatch ):
3437+ """Test that read_input uses a dynamic prompt when provided prompt matches app.prompt"""
3438+ input_str = 'some input'
3439+ base_app .use_rawinput = True
3440+
3441+ # Mock PromptSession.prompt
3442+ # Also mock patch_stdout to prevent it from attempting to access the Windows console buffer in a Windows test environment
3443+ with (
3444+ mock .patch ('cmd2.cmd2.PromptSession.prompt' , return_value = input_str ) as mock_prompt ,
3445+ mock .patch ('cmd2.cmd2.patch_stdout' ),
3446+ mock .patch ('sys.stdin.isatty' , mock .MagicMock (name = 'isatty' , return_value = True )),
3447+ ):
3448+ # Call with exact app prompt
3449+ line = base_app .read_input (base_app .prompt )
3450+ assert line == input_str
3451+
3452+ # Check that mock_prompt was called with a callable for the prompt
3453+ # args[0] should be the prompt_to_use
3454+ args , _ = mock_prompt .call_args
3455+ prompt_arg = args [0 ]
3456+ assert callable (prompt_arg )
3457+
3458+ # Verify the callable returns the expected ANSI formatted prompt
3459+ from prompt_toolkit .formatted_text import ANSI
3460+
3461+ result = prompt_arg ()
3462+ assert isinstance (result , ANSI )
3463+ assert result .value == ANSI (base_app .prompt ).value
3464+
3465+
3466+ def test_read_input_dynamic_prompt_with_history (base_app , monkeypatch ):
3467+ """Test that read_input uses a dynamic prompt when provided prompt matches app.prompt and history is provided"""
3468+ input_str = 'some input'
3469+ base_app .use_rawinput = True
3470+ custom_history = ['cmd1' , 'cmd2' ]
3471+
3472+ # Mock PromptSession.prompt
3473+ # Also mock patch_stdout to prevent it from attempting to access the Windows console buffer in a Windows test environment
3474+ with (
3475+ mock .patch ('cmd2.cmd2.PromptSession.prompt' , return_value = input_str ) as mock_prompt ,
3476+ mock .patch ('cmd2.cmd2.patch_stdout' ),
3477+ mock .patch ('sys.stdin.isatty' , mock .MagicMock (name = 'isatty' , return_value = True )),
3478+ ):
3479+ # Call with exact app prompt and history
3480+ line = base_app .read_input (base_app .prompt , history = custom_history )
3481+ assert line == input_str
3482+
3483+ # Check that mock_prompt was called with a callable for the prompt
3484+ # args[0] should be the prompt_to_use
3485+ args , _ = mock_prompt .call_args
3486+ prompt_arg = args [0 ]
3487+ assert callable (prompt_arg )
3488+
3489+ # Verify the callable returns the expected ANSI formatted prompt
3490+ from prompt_toolkit .formatted_text import ANSI
3491+
3492+ result = prompt_arg ()
3493+ assert isinstance (result , ANSI )
3494+ assert result .value == ANSI (base_app .prompt ).value
0 commit comments