diff options
author | Aldo Cortesi <aldo@corte.si> | 2018-07-21 16:11:00 +1200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-07-21 16:11:00 +1200 |
commit | f20d78c3768deaef3a4b4a59273e0650248c8c9a (patch) | |
tree | 0b98fac9ed0c51d406999e24a5d85864938e76c6 /test | |
parent | 7f464b89296881f4d9ec032378c4418e832d17e3 (diff) | |
parent | dcb3de40b12bafd07979052647e71a172d55b360 (diff) | |
download | mitmproxy-f20d78c3768deaef3a4b4a59273e0650248c8c9a.tar.gz mitmproxy-f20d78c3768deaef3a4b4a59273e0650248c8c9a.tar.bz2 mitmproxy-f20d78c3768deaef3a4b4a59273e0650248c8c9a.zip |
Merge pull request #3251 from kajojify/command_history
Command history
Diffstat (limited to 'test')
-rw-r--r-- | test/mitmproxy/tools/console/test_commander.py | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/test/mitmproxy/tools/console/test_commander.py b/test/mitmproxy/tools/console/test_commander.py index 2a96995d..b5e226fe 100644 --- a/test/mitmproxy/tools/console/test_commander.py +++ b/test/mitmproxy/tools/console/test_commander.py @@ -28,6 +28,68 @@ class TestListCompleter: assert c.cycle() == expected +class TestCommandHistory: + def fill_history(self, commands): + with taddons.context() as tctx: + history = commander.CommandHistory(tctx.master, size=3) + for c in commands: + cbuf = commander.CommandBuffer(tctx.master, c) + history.add_command(cbuf) + return history, tctx.master + + def test_add_command(self): + commands = ["command1", "command2"] + history, tctx_master = self.fill_history(commands) + + saved_commands = [buf.text for buf in history.saved_commands] + assert saved_commands == [""] + commands + + # The history size is only 3. So, we forget the first + # one command, when adding fourth command + cbuf = commander.CommandBuffer(tctx_master, "command3") + history.add_command(cbuf) + saved_commands = [buf.text for buf in history.saved_commands] + assert saved_commands == commands + ["command3"] + + # Commands with the same text are not repeated in the history one by one + history.add_command(cbuf) + saved_commands = [buf.text for buf in history.saved_commands] + assert saved_commands == commands + ["command3"] + + # adding command in execution mode sets index at the beginning of the history + # and replace the last command buffer if it is empty or has the same text + cbuf = commander.CommandBuffer(tctx_master, "") + history.add_command(cbuf) + history.index = 0 + cbuf = commander.CommandBuffer(tctx_master, "command4") + history.add_command(cbuf, True) + assert history.index == history.last_index + saved_commands = [buf.text for buf in history.saved_commands] + assert saved_commands == ["command2", "command3", "command4"] + + def test_get_next(self): + commands = ["command1", "command2"] + history, tctx_master = self.fill_history(commands) + + history.index = -1 + expected_items = ["", "command1", "command2"] + for i in range(3): + assert history.get_next().text == expected_items[i] + # We are at the last item of the history + assert history.get_next() is None + + def test_get_prev(self): + commands = ["command1", "command2"] + history, tctx_master = self.fill_history(commands) + + expected_items = ["command2", "command1", ""] + history.index = history.last_index + 1 + for i in range(3): + assert history.get_prev().text == expected_items[i] + # We are at the first item of the history + assert history.get_prev() is None + + class TestCommandBuffer: def test_backspace(self): |