Skip to content

Commit 39c1e71

Browse files
committed
Updated CommandSet unit tests
1 parent e328b67 commit 39c1e71

File tree

1 file changed

+66
-48
lines changed

1 file changed

+66
-48
lines changed

tests_isolated/test_commandset/test_commandset.py

Lines changed: 66 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -695,165 +695,183 @@ def test_static_subcommands(static_subcommands_app):
695695

696696

697697
@cmd2.with_default_category('With Completer')
698-
class WithCompleterCommandSet(cmd2.CommandSet):
698+
class SupportFuncProvider(cmd2.CommandSet):
699+
"""CommandSet which provides a support function (complete_states) to other CommandSets"""
700+
699701
states = ['alabama', 'alaska', 'arizona', 'arkansas', 'california', 'colorado', 'connecticut', 'delaware']
700702

701703
def __init__(self, dummy):
702704
"""dummy variable prevents this from being autoloaded in other tests"""
703-
super(WithCompleterCommandSet, self).__init__()
705+
super(SupportFuncProvider, self).__init__()
704706

705707
def complete_states(self, text: str, line: str, begidx: int, endidx: int) -> List[str]:
706708
assert self is complete_states_expected_self
707709
return self._cmd.basic_complete(text, line, begidx, endidx, self.states)
708710

709711

710-
class SubclassCommandSetCase1(WithCompleterCommandSet):
712+
class SupportFuncUserSubclass1(SupportFuncProvider):
713+
"""A sub-class of SupportFuncProvider which uses its support function"""
714+
711715
parser = cmd2.Cmd2ArgumentParser()
712-
parser.add_argument('state', type=str, completer=WithCompleterCommandSet.complete_states)
716+
parser.add_argument('state', type=str, completer=SupportFuncProvider.complete_states)
713717

714718
@cmd2.with_argparser(parser)
715-
def do_case1(self, ns: argparse.Namespace):
719+
def do_user_sub1(self, ns: argparse.Namespace):
716720
self._cmd.poutput('something {}'.format(ns.state))
717721

718722

719-
class SubclassCommandSetErrorCase2(WithCompleterCommandSet):
723+
class SupportFuncUserSubclass2(SupportFuncProvider):
724+
"""A second sub-class of SupportFuncProvider which uses its support function"""
725+
720726
parser = cmd2.Cmd2ArgumentParser()
721-
parser.add_argument('state', type=str, completer=WithCompleterCommandSet.complete_states)
727+
parser.add_argument('state', type=str, completer=SupportFuncProvider.complete_states)
722728

723729
@cmd2.with_argparser(parser)
724-
def do_error2(self, ns: argparse.Namespace):
730+
def do_user_sub2(self, ns: argparse.Namespace):
725731
self._cmd.poutput('something {}'.format(ns.state))
726732

727733

728-
class SubclassCommandSetCase2(cmd2.CommandSet):
734+
class SupportFuncUserUnrelated(cmd2.CommandSet):
735+
"""A CommandSet that isn't related to SupportFuncProvider which uses its support function"""
736+
729737
def __init__(self, dummy):
730738
"""dummy variable prevents this from being autoloaded in other tests"""
731-
super(SubclassCommandSetCase2, self).__init__()
739+
super(SupportFuncUserUnrelated, self).__init__()
732740

733741
parser = cmd2.Cmd2ArgumentParser()
734-
parser.add_argument('state', type=str, completer=WithCompleterCommandSet.complete_states)
742+
parser.add_argument('state', type=str, completer=SupportFuncProvider.complete_states)
735743

736744
@cmd2.with_argparser(parser)
737-
def do_case2(self, ns: argparse.Namespace):
745+
def do_user_unrelated(self, ns: argparse.Namespace):
738746
self._cmd.poutput('something {}'.format(ns.state))
739747

740748

741749
def test_cross_commandset_completer(command_sets_manual):
742750
global complete_states_expected_self
743751
# This tests the different ways to locate the matching CommandSet when completing an argparse argument.
744-
# Exercises the `_complete_arg` function of `ArgparseCompleter` in `argparse_completer.py`
752+
# Exercises the 3 cases in cmd2.Cmd._resolve_func_self().
753+
754+
# Create all the CommandSets for these tests
755+
func_provider = SupportFuncProvider(1)
756+
user_sub1 = SupportFuncUserSubclass1(2)
757+
user_sub2 = SupportFuncUserSubclass2(3)
758+
user_unrelated = SupportFuncUserUnrelated(4)
745759

746760
####################################################################################################################
747761
# This exercises Case 1
748762
# If the CommandSet holding a command is a sub-class of the class that defines the completer function, then use that
749763
# CommandSet instance as self when calling the completer
750-
case1_set = SubclassCommandSetCase1(1)
751764

752-
command_sets_manual.register_command_set(case1_set)
765+
# Create instances of two different sub-class types to ensure no one removes the case 1 check in Cmd._resolve_func_self().
766+
# If that check is removed, testing with only 1 sub-class type will still pass. Testing it with two sub-class types
767+
# will fail and show that the case 1 check cannot be removed.
768+
command_sets_manual.register_command_set(user_sub1)
769+
command_sets_manual.register_command_set(user_sub2)
753770

754771
text = ''
755-
line = 'case1 {}'.format(text)
772+
line = 'user_sub1 {}'.format(text)
756773
endidx = len(line)
757774
begidx = endidx
758-
complete_states_expected_self = case1_set
775+
complete_states_expected_self = user_sub1
759776
first_match = complete_tester(text, line, begidx, endidx, command_sets_manual)
760777
complete_states_expected_self = None
761778

762779
assert first_match == 'alabama'
763-
assert command_sets_manual.completion_matches == WithCompleterCommandSet.states
780+
assert command_sets_manual.completion_matches == SupportFuncProvider.states
764781

765-
assert getattr(command_sets_manual.cmd_func('case1').__func__, cmd2.constants.CMD_ATTR_HELP_CATEGORY) == 'With Completer'
782+
assert (
783+
getattr(command_sets_manual.cmd_func('user_sub1').__func__, cmd2.constants.CMD_ATTR_HELP_CATEGORY) == 'With Completer'
784+
)
766785

767-
command_sets_manual.unregister_command_set(case1_set)
786+
command_sets_manual.unregister_command_set(user_sub2)
787+
command_sets_manual.unregister_command_set(user_sub1)
768788

769789
####################################################################################################################
770790
# This exercises Case 2
771791
# If the CommandSet holding a command is unrelated to the CommandSet holding the completer function, then search
772792
# all installed CommandSet instances for one that is an exact type match
773793

774-
# First verify that, without the correct command set
775-
base_set = WithCompleterCommandSet(1)
776-
case2_set = SubclassCommandSetCase2(2)
777-
command_sets_manual.register_command_set(base_set)
778-
command_sets_manual.register_command_set(case2_set)
794+
command_sets_manual.register_command_set(func_provider)
795+
command_sets_manual.register_command_set(user_unrelated)
779796

780797
text = ''
781-
line = 'case2 {}'.format(text)
798+
line = 'user_unrelated {}'.format(text)
782799
endidx = len(line)
783800
begidx = endidx
784-
complete_states_expected_self = base_set
801+
complete_states_expected_self = func_provider
785802
first_match = complete_tester(text, line, begidx, endidx, command_sets_manual)
786803
complete_states_expected_self = None
787804

788805
assert first_match == 'alabama'
789-
assert command_sets_manual.completion_matches == WithCompleterCommandSet.states
806+
assert command_sets_manual.completion_matches == SupportFuncProvider.states
790807

791-
command_sets_manual.unregister_command_set(case2_set)
792-
command_sets_manual.unregister_command_set(base_set)
808+
command_sets_manual.unregister_command_set(user_unrelated)
809+
command_sets_manual.unregister_command_set(func_provider)
793810

794811
####################################################################################################################
795812
# This exercises Case 3
796813
# If the CommandSet holding a command is unrelated to the CommandSet holding the completer function,
797814
# and no exact type match can be found, but sub-class matches can be found and there is only a single
798-
# subclass match, then use the lone subclass match as the parent CommandSet.
815+
# sub-class match, then use the lone sub-class match as the parent CommandSet.
799816

800-
command_sets_manual.register_command_set(case1_set)
801-
command_sets_manual.register_command_set(case2_set)
817+
command_sets_manual.register_command_set(user_sub1)
818+
command_sets_manual.register_command_set(user_unrelated)
802819

803820
text = ''
804-
line = 'case2 {}'.format(text)
821+
line = 'user_unrelated {}'.format(text)
805822
endidx = len(line)
806823
begidx = endidx
807-
complete_states_expected_self = case1_set
824+
complete_states_expected_self = user_sub1
808825
first_match = complete_tester(text, line, begidx, endidx, command_sets_manual)
809826
complete_states_expected_self = None
810827

811828
assert first_match == 'alabama'
812-
assert command_sets_manual.completion_matches == WithCompleterCommandSet.states
829+
assert command_sets_manual.completion_matches == SupportFuncProvider.states
813830

814-
command_sets_manual.unregister_command_set(case2_set)
815-
command_sets_manual.unregister_command_set(case1_set)
831+
command_sets_manual.unregister_command_set(user_unrelated)
832+
command_sets_manual.unregister_command_set(user_sub1)
816833

817834
####################################################################################################################
818835
# Error Case 1
819836
# If the CommandSet holding a command is unrelated to the CommandSet holding the completer function, then search
820837
# all installed CommandSet instances for one that is an exact type match, none are found
821838
# search for sub-class matches, also none are found.
822839

823-
command_sets_manual.register_command_set(case2_set)
840+
command_sets_manual.register_command_set(user_unrelated)
824841

825842
text = ''
826-
line = 'case2 {}'.format(text)
843+
line = 'user_unrelated {}'.format(text)
827844
endidx = len(line)
828845
begidx = endidx
829846
first_match = complete_tester(text, line, begidx, endidx, command_sets_manual)
830847

831848
assert first_match is None
832849
assert command_sets_manual.completion_matches == []
833850

834-
command_sets_manual.unregister_command_set(case2_set)
851+
command_sets_manual.unregister_command_set(user_unrelated)
835852

836853
####################################################################################################################
837854
# Error Case 2
838855
# If the CommandSet holding a command is unrelated to the CommandSet holding the completer function, then search
839856
# all installed CommandSet instances for one that is an exact type match, none are found
840-
# search for sub-class matches, more than 1 is found
857+
# search for sub-class matches, more than 1 is found.
841858

842-
error_case2_set = SubclassCommandSetErrorCase2(4)
843-
command_sets_manual.register_command_set(case1_set)
844-
command_sets_manual.register_command_set(case2_set)
845-
command_sets_manual.register_command_set(error_case2_set)
859+
command_sets_manual.register_command_set(user_sub1)
860+
command_sets_manual.register_command_set(user_sub2)
861+
command_sets_manual.register_command_set(user_unrelated)
846862

847863
text = ''
848-
line = 'case2 {}'.format(text)
864+
line = 'user_unrelated {}'.format(text)
849865
endidx = len(line)
850866
begidx = endidx
851867
first_match = complete_tester(text, line, begidx, endidx, command_sets_manual)
852868

853869
assert first_match is None
854870
assert command_sets_manual.completion_matches == []
855871

856-
command_sets_manual.unregister_command_set(case2_set)
872+
command_sets_manual.unregister_command_set(user_unrelated)
873+
command_sets_manual.unregister_command_set(user_sub2)
874+
command_sets_manual.unregister_command_set(user_sub1)
857875

858876

859877
class CommandSetWithPathComplete(cmd2.CommandSet):

0 commit comments

Comments
 (0)