@@ -353,3 +353,46 @@ def test_completion_items_as_choices(capsys) -> None:
353353 # Confirm error text contains correct value type of int
354354 _out , err = capsys .readouterr ()
355355 assert 'invalid choice: 3 (choose from 1, 2)' in err
356+
357+
358+ def test_formatter_coverage (mocker ) -> None :
359+ import sys
360+
361+ from cmd2 .argparse_custom import (
362+ Cmd2HelpFormatter ,
363+ Cmd2RichArgparseConsole ,
364+ )
365+
366+ # Line 1031: self._console = console (inside console.setter)
367+ formatter = Cmd2HelpFormatter (prog = 'test' )
368+ new_console = Cmd2RichArgparseConsole ()
369+ formatter .console = new_console
370+ assert formatter ._console is new_console
371+
372+ # Line 1041: return (inside _set_color if sys.version_info < (3, 14))
373+ mocker .patch ('cmd2.argparse_custom.sys.version_info' , (3 , 13 , 0 ))
374+ # This should return early without calling super()._set_color
375+ mock_set_color = mocker .patch ('rich_argparse.RichHelpFormatter._set_color' )
376+ formatter ._set_color (True )
377+ mock_set_color .assert_not_called ()
378+
379+ # Line 1045 and 1047: except TypeError and super()._set_color(color)
380+ mocker .patch ('cmd2.argparse_custom.sys.version_info' , (3 , 15 , 0 ))
381+
382+ # Reset mock and make it raise TypeError when called with kwargs
383+ mock_set_color .reset_mock ()
384+
385+ def side_effect (color , ** kwargs ):
386+ if kwargs :
387+ raise TypeError ("unexpected keyword argument 'file'" )
388+ return
389+
390+ mock_set_color .side_effect = side_effect
391+
392+ # This call should trigger the TypeError and then the fallback call
393+ formatter ._set_color (True , file = sys .stdout )
394+
395+ # It should have been called twice: once with kwargs (failed) and once without (fallback)
396+ assert mock_set_color .call_count == 2
397+ mock_set_color .assert_any_call (True , file = sys .stdout )
398+ mock_set_color .assert_any_call (True )
0 commit comments