MCPcopy Index your code
hub / github.com/python-cmd2/cmd2 / test_command_synonyms

Function test_command_synonyms

tests/test_commandset.py:152–195  ·  view source on GitHub ↗

Test the use of command synonyms in CommandSets

()

Source from the content-addressed store, hash-verified

150
151
152def test_command_synonyms() -> None:
153 """Test the use of command synonyms in CommandSets"""
154
155 class SynonymCommandSet(cmd2.CommandSet):
156 def __init__(self, arg1) -> None:
157 super().__init__()
158 self._arg1 = arg1
159
160 @cmd2.with_argparser(cmd2.Cmd2ArgumentParser(description="Native Command"))
161 def do_builtin(self, _) -> None:
162 """Builtin Command"""
163
164 # Create a synonym to a command inside of this CommandSet
165 do_builtin_synonym = do_builtin
166
167 # Create a synonym to a command outside of this CommandSet with subcommands.
168 # This will best test the synonym check in cmd2.Cmd._check_uninstallable() when
169 # we unregister this CommandSet.
170 do_alias_synonym = cmd2.Cmd.do_alias
171
172 cs = SynonymCommandSet("foo")
173 app = WithCommandSets(command_sets=[cs])
174
175 # Make sure the synonyms have the same parser as what they alias
176 builtin_parser = app.command_parsers.get(app.do_builtin)
177 builtin_synonym_parser = app.command_parsers.get(app.do_builtin_synonym)
178 assert builtin_parser is not None
179 assert builtin_parser is builtin_synonym_parser
180
181 alias_parser = app.command_parsers.get(cmd2.Cmd.do_alias)
182 alias_synonym_parser = app.command_parsers.get(app.do_alias_synonym)
183 assert alias_parser is not None
184 assert alias_parser is alias_synonym_parser
185
186 # Unregister the CommandSet and make sure built-in command and synonyms are gone
187 app.unregister_command_set(cs)
188 assert not hasattr(app, "do_builtin")
189 assert not hasattr(app, "do_builtin_synonym")
190 assert not hasattr(app, "do_alias_synonym")
191
192 # Make sure the alias command still exists, has the same parser, and works.
193 assert alias_parser is app.command_parsers.get(cmd2.Cmd.do_alias)
194 out, _err = run_cmd(app, "alias --help")
195 assert normalize(alias_parser.format_help())[0] in out
196
197
198def test_custom_construct_commandsets() -> None:

Callers

nothing calls this directly

Calls 7

SynonymCommandSetClass · 0.85
run_cmdFunction · 0.85
normalizeFunction · 0.85
format_helpMethod · 0.80
WithCommandSetsClass · 0.70
getMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…