77
88import cmd2
99from cmd2 import (
10+ Completions ,
1011 Settable ,
1112)
1213from cmd2 .exceptions import (
1516
1617from .conftest import (
1718 WithCommandSets ,
18- complete_tester ,
1919 normalize ,
2020 run_cmd ,
2121)
@@ -497,8 +497,8 @@ def __init__(self, dummy) -> None:
497497 def do_arugula (self , _ : cmd2 .Statement ) -> None :
498498 self ._cmd .poutput ('Arugula' )
499499
500- def complete_style_arg (self , text : str , line : str , begidx : int , endidx : int ) -> list [ str ] :
501- return ['quartered' , 'diced' ]
500+ def complete_style_arg (self , text : str , line : str , begidx : int , endidx : int ) -> Completions :
501+ return Completions . from_values ( ['quartered' , 'diced' ])
502502
503503 bokchoy_parser = cmd2 .Cmd2ArgumentParser ()
504504 bokchoy_parser .add_argument ('style' , completer = complete_style_arg )
@@ -549,11 +549,10 @@ def test_subcommands(manual_command_sets_app) -> None:
549549 line = f'cut { text } '
550550 endidx = len (line )
551551 begidx = endidx
552- first_match = complete_tester (text , line , begidx , endidx , manual_command_sets_app )
552+ completions = manual_command_sets_app . complete (text , line , begidx , endidx )
553553
554- assert first_match is not None
555554 # check that the alias shows up correctly
556- assert manual_command_sets_app . completion_matches == ['banana' , 'bananer' , 'bokchoy' ]
555+ assert completions . to_strings () == Completions . from_values ( ['banana' , 'bananer' , 'bokchoy' ]). to_strings ()
557556
558557 cmd_result = manual_command_sets_app .app_cmd ('cut banana discs' )
559558 assert 'cutting banana: discs' in cmd_result .stdout
@@ -562,11 +561,10 @@ def test_subcommands(manual_command_sets_app) -> None:
562561 line = f'cut bokchoy { text } '
563562 endidx = len (line )
564563 begidx = endidx
565- first_match = complete_tester (text , line , begidx , endidx , manual_command_sets_app )
564+ completions = manual_command_sets_app . complete (text , line , begidx , endidx )
566565
567- assert first_match is not None
568566 # verify that argparse completer in commandset functions correctly
569- assert manual_command_sets_app . completion_matches == ['diced' , 'quartered' ]
567+ assert completions . to_strings () == Completions . from_values ( ['diced' , 'quartered' ]). to_strings ()
570568
571569 # verify that command set uninstalls without problems
572570 manual_command_sets_app .unregister_command_set (fruit_cmds )
@@ -594,21 +592,19 @@ def test_subcommands(manual_command_sets_app) -> None:
594592 line = f'cut { text } '
595593 endidx = len (line )
596594 begidx = endidx
597- first_match = complete_tester (text , line , begidx , endidx , manual_command_sets_app )
595+ completions = manual_command_sets_app . complete (text , line , begidx , endidx )
598596
599- assert first_match is not None
600597 # check that the alias shows up correctly
601- assert manual_command_sets_app . completion_matches == ['banana' , 'bananer' , 'bokchoy' ]
598+ assert completions . to_strings () == Completions . from_values ( ['banana' , 'bananer' , 'bokchoy' ]). to_strings ()
602599
603600 text = ''
604601 line = f'cut bokchoy { text } '
605602 endidx = len (line )
606603 begidx = endidx
607- first_match = complete_tester (text , line , begidx , endidx , manual_command_sets_app )
604+ completions = manual_command_sets_app . complete (text , line , begidx , endidx )
608605
609- assert first_match is not None
610606 # verify that argparse completer in commandset functions correctly
611- assert manual_command_sets_app . completion_matches == ['diced' , 'quartered' ]
607+ assert completions . to_strings () == Completions . from_values ( ['diced' , 'quartered' ]). to_strings ()
612608
613609 # disable again and verify can still uninstnall
614610 manual_command_sets_app .disable_command ('cut' , 'disabled for test' )
@@ -735,8 +731,8 @@ def cut_banana(self, ns: argparse.Namespace) -> None:
735731 """Cut banana"""
736732 self .poutput ('cutting banana: ' + ns .direction )
737733
738- def complete_style_arg (self , text : str , line : str , begidx : int , endidx : int ) -> list [ str ] :
739- return ['quartered' , 'diced' ]
734+ def complete_style_arg (self , text : str , line : str , begidx : int , endidx : int ) -> Completions :
735+ return Completions . from_values ( ['quartered' , 'diced' ])
740736
741737 bokchoy_parser = cmd2 .Cmd2ArgumentParser ()
742738 bokchoy_parser .add_argument ('style' , completer = complete_style_arg )
@@ -759,21 +755,19 @@ def test_static_subcommands(static_subcommands_app) -> None:
759755 line = f'cut { text } '
760756 endidx = len (line )
761757 begidx = endidx
762- first_match = complete_tester (text , line , begidx , endidx , static_subcommands_app )
758+ completions = static_subcommands_app . complete (text , line , begidx , endidx )
763759
764- assert first_match is not None
765760 # check that the alias shows up correctly
766- assert static_subcommands_app . completion_matches == ['banana' , 'bananer' , 'bokchoy' ]
761+ assert completions . to_strings () == Completions . from_values ( ['banana' , 'bananer' , 'bokchoy' ]). to_strings ()
767762
768763 text = ''
769764 line = f'cut bokchoy { text } '
770765 endidx = len (line )
771766 begidx = endidx
772- first_match = complete_tester (text , line , begidx , endidx , static_subcommands_app )
767+ completions = static_subcommands_app . complete (text , line , begidx , endidx )
773768
774- assert first_match is not None
775769 # verify that argparse completer in commandset functions correctly
776- assert static_subcommands_app . completion_matches == ['diced' , 'quartered' ]
770+ assert completions . to_strings () == Completions . from_values ( ['diced' , 'quartered' ]). to_strings ()
777771
778772
779773complete_states_expected_self = None
@@ -789,7 +783,7 @@ def __init__(self, dummy) -> None:
789783 """Dummy variable prevents this from being autoloaded in other tests"""
790784 super ().__init__ ()
791785
792- def complete_states (self , text : str , line : str , begidx : int , endidx : int ) -> list [ str ] :
786+ def complete_states (self , text : str , line : str , begidx : int , endidx : int ) -> Completions :
793787 assert self is complete_states_expected_self
794788 return self ._cmd .basic_complete (text , line , begidx , endidx , self .states )
795789
@@ -831,7 +825,7 @@ def do_user_unrelated(self, ns: argparse.Namespace) -> None:
831825 self ._cmd .poutput (f'something { ns .state } ' )
832826
833827
834- def test_cross_commandset_completer (manual_command_sets_app , capsys ) -> None :
828+ def test_cross_commandset_completer (manual_command_sets_app ) -> None :
835829 global complete_states_expected_self # noqa: PLW0603
836830 # This tests the different ways to locate the matching CommandSet when completing an argparse argument.
837831 # Exercises the 3 cases in cmd2.Cmd._resolve_func_self() which is called during argparse tab completion.
@@ -858,11 +852,10 @@ def test_cross_commandset_completer(manual_command_sets_app, capsys) -> None:
858852 endidx = len (line )
859853 begidx = endidx
860854 complete_states_expected_self = user_sub1
861- first_match = complete_tester (text , line , begidx , endidx , manual_command_sets_app )
855+ completions = manual_command_sets_app . complete (text , line , begidx , endidx )
862856 complete_states_expected_self = None
863857
864- assert first_match == 'alabama'
865- assert manual_command_sets_app .completion_matches == list (SupportFuncProvider .states )
858+ assert completions .to_strings () == Completions .from_values (SupportFuncProvider .states ).to_strings ()
866859
867860 assert (
868861 getattr (manual_command_sets_app .cmd_func ('user_sub1' ).__func__ , cmd2 .constants .CMD_ATTR_HELP_CATEGORY )
@@ -885,11 +878,10 @@ def test_cross_commandset_completer(manual_command_sets_app, capsys) -> None:
885878 endidx = len (line )
886879 begidx = endidx
887880 complete_states_expected_self = func_provider
888- first_match = complete_tester (text , line , begidx , endidx , manual_command_sets_app )
881+ completions = manual_command_sets_app . complete (text , line , begidx , endidx )
889882 complete_states_expected_self = None
890883
891- assert first_match == 'alabama'
892- assert manual_command_sets_app .completion_matches == list (SupportFuncProvider .states )
884+ assert completions .to_strings () == Completions .from_values (SupportFuncProvider .states ).to_strings ()
893885
894886 manual_command_sets_app .unregister_command_set (user_unrelated )
895887 manual_command_sets_app .unregister_command_set (func_provider )
@@ -908,11 +900,10 @@ def test_cross_commandset_completer(manual_command_sets_app, capsys) -> None:
908900 endidx = len (line )
909901 begidx = endidx
910902 complete_states_expected_self = user_sub1
911- first_match = complete_tester (text , line , begidx , endidx , manual_command_sets_app )
903+ completions = manual_command_sets_app . complete (text , line , begidx , endidx )
912904 complete_states_expected_self = None
913905
914- assert first_match == 'alabama'
915- assert manual_command_sets_app .completion_matches == list (SupportFuncProvider .states )
906+ assert completions .to_strings () == Completions .from_values (SupportFuncProvider .states ).to_strings ()
916907
917908 manual_command_sets_app .unregister_command_set (user_unrelated )
918909 manual_command_sets_app .unregister_command_set (user_sub1 )
@@ -929,12 +920,10 @@ def test_cross_commandset_completer(manual_command_sets_app, capsys) -> None:
929920 line = f'user_unrelated { text } '
930921 endidx = len (line )
931922 begidx = endidx
932- first_match = complete_tester (text , line , begidx , endidx , manual_command_sets_app )
933- out , _err = capsys .readouterr ()
923+ completions = manual_command_sets_app .complete (text , line , begidx , endidx )
934924
935- assert first_match is None
936- assert manual_command_sets_app .completion_matches == []
937- assert "Could not find CommandSet instance" in out
925+ assert not completions
926+ assert "Could not find CommandSet instance" in completions .completion_error
938927
939928 manual_command_sets_app .unregister_command_set (user_unrelated )
940929
@@ -952,12 +941,10 @@ def test_cross_commandset_completer(manual_command_sets_app, capsys) -> None:
952941 line = f'user_unrelated { text } '
953942 endidx = len (line )
954943 begidx = endidx
955- first_match = complete_tester (text , line , begidx , endidx , manual_command_sets_app )
956- out , _err = capsys .readouterr ()
944+ completions = manual_command_sets_app .complete (text , line , begidx , endidx )
957945
958- assert first_match is None
959- assert manual_command_sets_app .completion_matches == []
960- assert "Could not find CommandSet instance" in out
946+ assert not completions
947+ assert "Could not find CommandSet instance" in completions .completion_error
961948
962949 manual_command_sets_app .unregister_command_set (user_unrelated )
963950 manual_command_sets_app .unregister_command_set (user_sub2 )
@@ -986,9 +973,9 @@ def test_path_complete(manual_command_sets_app) -> None:
986973 line = f'path { text } '
987974 endidx = len (line )
988975 begidx = endidx
989- first_match = complete_tester (text , line , begidx , endidx , manual_command_sets_app )
976+ completions = manual_command_sets_app . complete (text , line , begidx , endidx )
990977
991- assert first_match is not None
978+ assert completions
992979
993980
994981def test_bad_subcommand () -> None :
0 commit comments