Skip to content

Commit a33da04

Browse files
committed
Added unit tests for CommandSet callbacks
1 parent 77a36b1 commit a33da04

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

tests/test_cmd2.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2247,15 +2247,15 @@ def test_disable_and_enable_category(disable_commands_app):
22472247

22482248
# Make sure neither function completes
22492249
text = ''
2250-
line = 'has_helper_funcs'
2250+
line = 'has_helper_funcs {}'.format(text)
22512251
endidx = len(line)
22522252
begidx = endidx - len(text)
22532253

22542254
first_match = complete_tester(text, line, begidx, endidx, disable_commands_app)
22552255
assert first_match is None
22562256

22572257
text = ''
2258-
line = 'has_no_helper_funcs'
2258+
line = 'has_no_helper_funcs {}'.format(text)
22592259
endidx = len(line)
22602260
begidx = endidx - len(text)
22612261

@@ -2291,7 +2291,7 @@ def test_disable_and_enable_category(disable_commands_app):
22912291

22922292
# has_helper_funcs should complete now
22932293
text = ''
2294-
line = 'has_helper_funcs'
2294+
line = 'has_helper_funcs {}'.format(text)
22952295
endidx = len(line)
22962296
begidx = endidx - len(text)
22972297

@@ -2300,7 +2300,7 @@ def test_disable_and_enable_category(disable_commands_app):
23002300

23012301
# has_no_helper_funcs had no completer originally, so there should be no results
23022302
text = ''
2303-
line = 'has_no_helper_funcs'
2303+
line = 'has_no_helper_funcs {}'.format(text)
23042304
endidx = len(line)
23052305
begidx = endidx - len(text)
23062306

tests_isolated/test_commandset/test_commandset.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ class CommandSetBase(cmd2.CommandSet):
2121

2222
@cmd2.with_default_category('Fruits')
2323
class CommandSetA(CommandSetBase):
24+
def on_register(self, cmd) -> None:
25+
super().on_register(cmd)
26+
print("in on_register now")
27+
28+
def on_registered(self) -> None:
29+
super().on_registered()
30+
print("in on_registered now")
31+
32+
def on_unregister(self) -> None:
33+
print("in on_unregister now")
34+
super().on_unregister()
35+
2436
def do_apple(self, statement: cmd2.Statement):
2537
self._cmd.poutput('Apple!')
2638

@@ -158,7 +170,7 @@ def test_custom_construct_commandsets():
158170
assert command_set_2 not in matches
159171

160172

161-
def test_load_commands(command_sets_manual):
173+
def test_load_commands(command_sets_manual, capsys):
162174

163175
# now install a command set and verify the commands are now present
164176
cmd_set = CommandSetA()
@@ -171,6 +183,11 @@ def test_load_commands(command_sets_manual):
171183
assert command_sets_manual.find_commandsets(CommandSetA)[0] is cmd_set
172184
assert command_sets_manual.find_commandset_for_command('elderberry') is cmd_set
173185

186+
# Make sure registration callbacks ran
187+
out, err = capsys.readouterr()
188+
assert "in on_register now" in out
189+
assert "in on_registered now" in out
190+
174191
cmds_cats, cmds_doc, cmds_undoc, help_topics = command_sets_manual._build_command_info()
175192

176193
assert 'Alone' in cmds_cats
@@ -192,6 +209,10 @@ def test_load_commands(command_sets_manual):
192209
assert 'Alone' not in cmds_cats
193210
assert 'Fruits' not in cmds_cats
194211

212+
# Make sure unregistration callback ran
213+
out, err = capsys.readouterr()
214+
assert "in on_unregister now" in out
215+
195216
# uninstall a second time and verify no errors happen
196217
command_sets_manual.unregister_command_set(cmd_set)
197218

0 commit comments

Comments
 (0)