diff options
author | Aldo Cortesi <aldo@corte.si> | 2017-12-15 15:57:27 +1300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-15 15:57:27 +1300 |
commit | adad33595e204f905b571eede4a61f9a0789936c (patch) | |
tree | 3116083e3deb49a9e79a2b5c8579db72e858c133 /test | |
parent | 21324086c37dcf55d38562081ae9fc8f5e4852b1 (diff) | |
parent | 2cfe45428ae1df16d0f7a5cbb9a541a441b7413d (diff) | |
download | mitmproxy-adad33595e204f905b571eede4a61f9a0789936c.tar.gz mitmproxy-adad33595e204f905b571eede4a61f9a0789936c.tar.bz2 mitmproxy-adad33595e204f905b571eede4a61f9a0789936c.zip |
Merge pull request #2677 from cortesi/commander
Commander
Diffstat (limited to 'test')
-rw-r--r-- | test/mitmproxy/test_command.py | 48 | ||||
-rw-r--r-- | test/mitmproxy/tools/console/test_commander.py | 68 |
2 files changed, 116 insertions, 0 deletions
diff --git a/test/mitmproxy/test_command.py b/test/mitmproxy/test_command.py index e1879ba2..76ce2245 100644 --- a/test/mitmproxy/test_command.py +++ b/test/mitmproxy/test_command.py @@ -11,19 +11,24 @@ from mitmproxy.utils import typecheck class TAddon: + @command.command("cmd1") def cmd1(self, foo: str) -> str: """cmd1 help""" return "ret " + foo + @command.command("cmd2") def cmd2(self, foo: str) -> str: return 99 + @command.command("cmd3") def cmd3(self, foo: int) -> int: return foo + @command.command("empty") def empty(self) -> None: pass + @command.command("varargs") def varargs(self, one: str, *var: str) -> typing.Sequence[str]: return list(var) @@ -34,6 +39,7 @@ class TAddon: def choose(self, arg: str) -> typing.Sequence[str]: return ["one", "two", "three"] + @command.command("path") def path(self, arg: command.Path) -> None: pass @@ -64,6 +70,44 @@ class TestCommand: c = command.Command(cm, "cmd.three", a.cmd3) assert c.call(["1"]) == 1 + def test_parse_partial(self): + tests = [ + [ + "foo bar", + [ + command.ParseResult(value = "foo", type = command.Cmd), + command.ParseResult(value = "bar", type = str) + ], + ], + [ + "foo 'bar", + [ + command.ParseResult(value = "foo", type = command.Cmd), + command.ParseResult(value = "'bar", type = str) + ] + ], + ["a", [command.ParseResult(value = "a", type = command.Cmd)]], + ["", [command.ParseResult(value = "", type = command.Cmd)]], + [ + "cmd3 1", + [ + command.ParseResult(value = "cmd3", type = command.Cmd), + command.ParseResult(value = "1", type = int), + ] + ], + [ + "cmd3 ", + [ + command.ParseResult(value = "cmd3", type = command.Cmd), + command.ParseResult(value = "", type = int), + ] + ], + ] + with taddons.context() as tctx: + tctx.master.addons.add(TAddon()) + for s, expected in tests: + assert tctx.master.commands.parse_partial(s) == expected + def test_simple(): with taddons.context() as tctx: @@ -100,6 +144,7 @@ def test_typename(): assert command.typename(command.Choice("foo"), False) == "choice" assert command.typename(command.Path, False) == "path" + assert command.typename(command.Cmd, False) == "cmd" class DummyConsole: @@ -162,6 +207,9 @@ def test_parsearg(): assert command.parsearg( tctx.master.commands, "foo", command.Path ) == "foo" + assert command.parsearg( + tctx.master.commands, "foo", command.Cmd + ) == "foo" class TDec: diff --git a/test/mitmproxy/tools/console/test_commander.py b/test/mitmproxy/tools/console/test_commander.py new file mode 100644 index 00000000..1ac4c5c6 --- /dev/null +++ b/test/mitmproxy/tools/console/test_commander.py @@ -0,0 +1,68 @@ +from mitmproxy.tools.console.commander import commander +from mitmproxy.test import taddons + + +class TestListCompleter: + def test_cycle(self): + tests = [ + [ + "", + ["a", "b", "c"], + ["a", "b", "c", "a"] + ], + [ + "xxx", + ["a", "b", "c"], + ["xxx", "xxx", "xxx"] + ], + [ + "b", + ["a", "b", "ba", "bb", "c"], + ["b", "ba", "bb", "b"] + ], + ] + for start, options, cycle in tests: + c = commander.ListCompleter(start, options) + for expected in cycle: + assert c.cycle() == expected + + +class TestCommandBuffer: + + def test_backspace(self): + tests = [ + [("", 0), ("", 0)], + [("1", 0), ("1", 0)], + [("1", 1), ("", 0)], + [("123", 3), ("12", 2)], + [("123", 2), ("13", 1)], + [("123", 0), ("123", 0)], + ] + with taddons.context() as tctx: + for start, output in tests: + cb = commander.CommandBuffer(tctx.master) + cb.buf, cb.cursor = start[0], start[1] + cb.backspace() + assert cb.buf == output[0] + assert cb.cursor == output[1] + + def test_insert(self): + tests = [ + [("", 0), ("x", 1)], + [("a", 0), ("xa", 1)], + [("xa", 2), ("xax", 3)], + ] + with taddons.context() as tctx: + for start, output in tests: + cb = commander.CommandBuffer(tctx.master) + cb.buf, cb.cursor = start[0], start[1] + cb.insert("x") + assert cb.buf == output[0] + assert cb.cursor == output[1] + + def test_cycle_completion(self): + with taddons.context() as tctx: + cb = commander.CommandBuffer(tctx.master) + cb.buf = "foo bar" + cb.cursor = len(cb.buf) + cb.cycle_completion() |