diff options
author | kotfu <kotfu@kotfu.net> | 2018-05-26 18:29:58 -0600 |
---|---|---|
committer | kotfu <kotfu@kotfu.net> | 2018-05-26 18:29:58 -0600 |
commit | edb52d649f6496632115b3350349d53a32c1a2bd (patch) | |
tree | 475bb181e85dd9460f193241c2e3718ccc968926 /tests/test_plugin.py | |
parent | 50bb3eaf9d1fa5f1d6d3eb75810e0446a7e0820e (diff) | |
download | cmd2-git-edb52d649f6496632115b3350349d53a32c1a2bd.tar.gz |
Add preloop and postloop hook capabilities
Diffstat (limited to 'tests/test_plugin.py')
-rw-r--r-- | tests/test_plugin.py | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/tests/test_plugin.py b/tests/test_plugin.py index c82e6bcd..a6fc8fc1 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -23,6 +23,12 @@ class Plugin: def reset_counters(self): self.called_pph = 0 + def prepost_hook_one(self): + self.poutput("one") + + def prepost_hook_two(self): + self.poutput("two") + def pph(self, statement: cmd2.Statement) -> Tuple[bool, cmd2.Statement]: self.called_pph += 1 return False, statement @@ -53,6 +59,48 @@ class PluggedApp(Plugin, cmd2.Cmd): # test hooks # ### +def test_preloop_hook(capsys): + app = PluggedApp() + app.register_preloop_hook(app.prepost_hook_one) + app.cmdqueue.append('say hello') + app.cmdqueue.append('quit') + app.cmdloop() + out, err = capsys.readouterr() + assert out == 'one\nhello\n' + assert not err + +def test_preloop_hooks(capsys): + app = PluggedApp() + app.register_preloop_hook(app.prepost_hook_one) + app.register_preloop_hook(app.prepost_hook_two) + app.cmdqueue.append('say hello') + app.cmdqueue.append('quit') + app.cmdloop() + out, err = capsys.readouterr() + assert out == 'one\ntwo\nhello\n' + assert not err + +def test_postloop_hook(capsys): + app = PluggedApp() + app.register_postloop_hook(app.prepost_hook_one) + app.cmdqueue.append('say hello') + app.cmdqueue.append('quit') + app.cmdloop() + out, err = capsys.readouterr() + assert out == 'hello\none\n' + assert not err + +def test_postloop_hooks(capsys): + app = PluggedApp() + app.register_postloop_hook(app.prepost_hook_one) + app.register_postloop_hook(app.prepost_hook_two) + app.cmdqueue.append('say hello') + app.cmdqueue.append('quit') + app.cmdloop() + out, err = capsys.readouterr() + assert out == 'hello\none\ntwo\n' + assert not err + def test_postparsing_hook(capsys): app = PluggedApp() app.onecmd_plus_hooks('say hello') |