MCPcopy Create free account
hub / github.com/python-cmd2/cmd2 / test_cross_commandset_completer

Function test_cross_commandset_completer

tests/test_commandset.py:844–965  ·  view source on GitHub ↗
(manual_command_sets_app)

Source from the content-addressed store, hash-verified

842
843
844def test_cross_commandset_completer(manual_command_sets_app) -> None:
845 global complete_states_expected_self # noqa: PLW0603
846 # This tests the different ways to locate the matching CommandSet when completing an argparse argument.
847 # Exercises the 3 cases in cmd2.Cmd._resolve_func_self() which is called during argparse tab completion.
848
849 # Create all the CommandSets for these tests
850 func_provider = SupportFuncProvider(1)
851 user_sub1 = SupportFuncUserSubclass1(2)
852 user_sub2 = SupportFuncUserSubclass2(3)
853 user_unrelated = SupportFuncUserUnrelated(4)
854
855 ####################################################################################################################
856 # This exercises Case 1
857 # If the CommandSet holding a command is a sub-class of the class that defines the completer function, then use that
858 # CommandSet instance as self when calling the completer
859
860 # Create instances of two different sub-class types to ensure no one removes the case 1 check in Cmd._resolve_func_self().
861 # If that check is removed, testing with only 1 sub-class type will still pass. Testing it with two sub-class types
862 # will fail and show that the case 1 check cannot be removed.
863 manual_command_sets_app.register_command_set(user_sub1)
864 manual_command_sets_app.register_command_set(user_sub2)
865
866 text = ""
867 line = f"user_sub1 {text}"
868 endidx = len(line)
869 begidx = endidx
870 complete_states_expected_self = user_sub1
871 completions = manual_command_sets_app.complete(text, line, begidx, endidx)
872 complete_states_expected_self = None
873
874 assert completions.to_strings() == Completions.from_values(SupportFuncProvider.states).to_strings()
875
876 cmds_cats, _help_topics = manual_command_sets_app._build_command_info()
877 assert "user_sub1" in cmds_cats["With Completer"]
878
879 manual_command_sets_app.unregister_command_set(user_sub2)
880 manual_command_sets_app.unregister_command_set(user_sub1)
881
882 ####################################################################################################################
883 # This exercises Case 2
884 # If the CommandSet holding a command is unrelated to the CommandSet holding the completer function, then search
885 # all installed CommandSet instances for one that is an exact type match
886
887 manual_command_sets_app.register_command_set(func_provider)
888 manual_command_sets_app.register_command_set(user_unrelated)
889
890 text = ""
891 line = f"user_unrelated {text}"
892 endidx = len(line)
893 begidx = endidx
894 complete_states_expected_self = func_provider
895 completions = manual_command_sets_app.complete(text, line, begidx, endidx)
896 complete_states_expected_self = None
897
898 assert completions.to_strings() == Completions.from_values(SupportFuncProvider.states).to_strings()
899
900 manual_command_sets_app.unregister_command_set(user_unrelated)
901 manual_command_sets_app.unregister_command_set(func_provider)

Callers

nothing calls this directly

Calls 10

SupportFuncProviderClass · 0.85
register_command_setMethod · 0.80
to_stringsMethod · 0.80
from_valuesMethod · 0.80
_build_command_infoMethod · 0.80
completeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…