Skip to content

Commit c2b1f61

Browse files
committed
Added unit test for onchange_hook
1 parent bd84b42 commit c2b1f61

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

tests/test_cmd2.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,30 @@ def test_set_quiet(base_app):
184184
assert out == ['quiet: True']
185185

186186

187+
class OnChangeHookApp(cmd2.Cmd):
188+
def __init__(self, *args, **kwargs):
189+
super().__init__(*args, **kwargs)
190+
191+
def _onchange_quiet(self, old, new) -> None:
192+
"""Runs when quiet is changed via set command"""
193+
self.poutput("You changed quiet")
194+
195+
@pytest.fixture
196+
def onchange_app():
197+
app = OnChangeHookApp()
198+
app.stdout = utils.StdSim(app.stdout)
199+
return app
200+
201+
def test_set_onchange_hook(onchange_app):
202+
out = run_cmd(onchange_app, 'set quiet True')
203+
expected = normalize("""
204+
quiet - was: False
205+
now: True
206+
You changed quiet
207+
""")
208+
assert out == expected
209+
210+
187211
def test_base_shell(base_app, monkeypatch):
188212
m = mock.Mock()
189213
monkeypatch.setattr("{}.Popen".format('subprocess'), m)

0 commit comments

Comments
 (0)