diff options
author | Aldo Cortesi <aldo@corte.si> | 2017-12-20 09:11:19 +1300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-20 09:11:19 +1300 |
commit | 4f32b835f7d81250adc339a9f715bb3851e1f3da (patch) | |
tree | 69d6a6ea93396115bd34c2abb85d561ba0de15e8 /test | |
parent | 04d83d767254d79dd64a5fd81afa8264412d6bc0 (diff) | |
parent | 3341edc9fa8e70306e91cf911e4b2b19ef60ba84 (diff) | |
download | mitmproxy-4f32b835f7d81250adc339a9f715bb3851e1f3da.tar.gz mitmproxy-4f32b835f7d81250adc339a9f715bb3851e1f3da.tar.bz2 mitmproxy-4f32b835f7d81250adc339a9f715bb3851e1f3da.zip |
Merge pull request #2714 from cortesi/commander1
commander: highlight commands
Diffstat (limited to 'test')
-rw-r--r-- | test/mitmproxy/test_command.py | 90 | ||||
-rw-r--r-- | test/mitmproxy/test_types.py | 14 | ||||
-rw-r--r-- | test/mitmproxy/tools/console/test_commander.py | 25 |
3 files changed, 60 insertions, 69 deletions
diff --git a/test/mitmproxy/test_command.py b/test/mitmproxy/test_command.py index 75b10098..25df2e61 100644 --- a/test/mitmproxy/test_command.py +++ b/test/mitmproxy/test_command.py @@ -78,39 +78,54 @@ class TestCommand: [ "foo bar", [ - command.ParseResult(value = "foo", type = mitmproxy.types.Cmd, valid = True), + command.ParseResult(value = "foo", type = mitmproxy.types.Cmd, valid = False), command.ParseResult(value = "bar", type = str, valid = True) ], + [], ], [ - "foo 'bar", + "cmd1 'bar", [ - command.ParseResult(value = "foo", type = mitmproxy.types.Cmd, valid = True), + command.ParseResult(value = "cmd1", type = mitmproxy.types.Cmd, valid = True), command.ParseResult(value = "'bar", type = str, valid = True) - ] + ], + [], + ], + [ + "a", + [command.ParseResult(value = "a", type = mitmproxy.types.Cmd, valid = False)], + [], + ], + [ + "", + [command.ParseResult(value = "", type = mitmproxy.types.Cmd, valid = False)], + [] ], - ["a", [command.ParseResult(value = "a", type = mitmproxy.types.Cmd, valid = True)]], - ["", [command.ParseResult(value = "", type = mitmproxy.types.Cmd, valid = True)]], [ "cmd3 1", [ command.ParseResult(value = "cmd3", type = mitmproxy.types.Cmd, valid = True), command.ParseResult(value = "1", type = int, valid = True), - ] + ], + [] ], [ "cmd3 ", [ command.ParseResult(value = "cmd3", type = mitmproxy.types.Cmd, valid = True), command.ParseResult(value = "", type = int, valid = False), - ] + ], + [] ], [ "subcommand ", [ - command.ParseResult(value = "subcommand", type = mitmproxy.types.Cmd, valid = True), - command.ParseResult(value = "", type = mitmproxy.types.Cmd, valid = True), - ] + command.ParseResult( + value = "subcommand", type = mitmproxy.types.Cmd, valid = True, + ), + command.ParseResult(value = "", type = mitmproxy.types.Cmd, valid = False), + ], + ["arg"], ], [ "subcommand cmd3 ", @@ -118,13 +133,16 @@ class TestCommand: command.ParseResult(value = "subcommand", type = mitmproxy.types.Cmd, valid = True), command.ParseResult(value = "cmd3", type = mitmproxy.types.Cmd, valid = True), command.ParseResult(value = "", type = int, valid = False), - ] + ], + [] ], ] with taddons.context() as tctx: tctx.master.addons.add(TAddon()) - for s, expected in tests: - assert tctx.master.commands.parse_partial(s) == expected + for s, expected, expectedremain in tests: + current, remain = tctx.master.commands.parse_partial(s) + assert current == expected + assert expectedremain == remain def test_simple(): @@ -179,51 +197,11 @@ def test_parsearg(): with taddons.context() as tctx: tctx.master.addons.add(DummyConsole()) assert command.parsearg(tctx.master.commands, "foo", str) == "foo" - - assert command.parsearg(tctx.master.commands, "1", int) == 1 + with pytest.raises(exceptions.CommandError, match="Unsupported"): + command.parsearg(tctx.master.commands, "foo", type) with pytest.raises(exceptions.CommandError): command.parsearg(tctx.master.commands, "foo", int) - assert command.parsearg(tctx.master.commands, "true", bool) is True - assert command.parsearg(tctx.master.commands, "false", bool) is False - with pytest.raises(exceptions.CommandError): - command.parsearg(tctx.master.commands, "flobble", bool) - - assert len(command.parsearg( - tctx.master.commands, "2", typing.Sequence[flow.Flow] - )) == 2 - assert command.parsearg(tctx.master.commands, "1", flow.Flow) - with pytest.raises(exceptions.CommandError): - command.parsearg(tctx.master.commands, "2", flow.Flow) - with pytest.raises(exceptions.CommandError): - command.parsearg(tctx.master.commands, "0", flow.Flow) - with pytest.raises(exceptions.CommandError): - command.parsearg(tctx.master.commands, "foo", Exception) - - assert command.parsearg( - tctx.master.commands, "foo", typing.Sequence[str] - ) == ["foo"] - assert command.parsearg( - tctx.master.commands, "foo, bar", typing.Sequence[str] - ) == ["foo", "bar"] - - a = TAddon() - tctx.master.commands.add("choices", a.choices) - assert command.parsearg( - tctx.master.commands, "one", mitmproxy.types.Choice("choices"), - ) == "one" - with pytest.raises(exceptions.CommandError): - assert command.parsearg( - tctx.master.commands, "invalid", mitmproxy.types.Choice("choices"), - ) - - assert command.parsearg( - tctx.master.commands, "foo", mitmproxy.types.Path - ) == "foo" - assert command.parsearg( - tctx.master.commands, "foo", mitmproxy.types.Cmd - ) == "foo" - class TDec: @command.command("cmd1") diff --git a/test/mitmproxy/test_types.py b/test/mitmproxy/test_types.py index 5b1dd3a2..29e86203 100644 --- a/test/mitmproxy/test_types.py +++ b/test/mitmproxy/test_types.py @@ -88,7 +88,9 @@ def test_cmd(): b = mitmproxy.types._CmdType() assert b.is_valid(tctx.master.commands, mitmproxy.types.Cmd, "foo") is False assert b.is_valid(tctx.master.commands, mitmproxy.types.Cmd, "cmd1") is True - assert b.parse(tctx.master.commands, mitmproxy.types.Cmd, "foo") == "foo" + assert b.parse(tctx.master.commands, mitmproxy.types.Cmd, "cmd1") == "cmd1" + with pytest.raises(mitmproxy.exceptions.TypeError): + assert b.parse(tctx.master.commands, mitmproxy.types.Cmd, "foo") assert len( b.completion(tctx.master.commands, mitmproxy.types.Cmd, "") ) == len(tctx.master.commands.commands.keys()) @@ -134,6 +136,8 @@ def test_strseq(): class DummyConsole: @command.command("view.resolve") def resolve(self, spec: str) -> typing.Sequence[flow.Flow]: + if spec == "err": + raise mitmproxy.exceptions.CommandError() n = int(spec) return [tflow.tflow(resp=True)] * n @@ -155,9 +159,11 @@ def test_flow(): assert b.is_valid(tctx.master.commands, flow.Flow, tflow.tflow()) is True assert b.is_valid(tctx.master.commands, flow.Flow, "xx") is False with pytest.raises(mitmproxy.exceptions.TypeError): - assert b.parse(tctx.master.commands, flow.Flow, "0") + b.parse(tctx.master.commands, flow.Flow, "0") + with pytest.raises(mitmproxy.exceptions.TypeError): + b.parse(tctx.master.commands, flow.Flow, "2") with pytest.raises(mitmproxy.exceptions.TypeError): - assert b.parse(tctx.master.commands, flow.Flow, "2") + b.parse(tctx.master.commands, flow.Flow, "err") def test_flows(): @@ -173,6 +179,8 @@ def test_flows(): assert len(b.parse(tctx.master.commands, typing.Sequence[flow.Flow], "0")) == 0 assert len(b.parse(tctx.master.commands, typing.Sequence[flow.Flow], "1")) == 1 assert len(b.parse(tctx.master.commands, typing.Sequence[flow.Flow], "2")) == 2 + with pytest.raises(mitmproxy.exceptions.TypeError): + b.parse(tctx.master.commands, typing.Sequence[flow.Flow], "err") def test_data(): diff --git a/test/mitmproxy/tools/console/test_commander.py b/test/mitmproxy/tools/console/test_commander.py index 34062dcb..2a96995d 100644 --- a/test/mitmproxy/tools/console/test_commander.py +++ b/test/mitmproxy/tools/console/test_commander.py @@ -42,16 +42,16 @@ class TestCommandBuffer: with taddons.context() as tctx: for start, output in tests: cb = commander.CommandBuffer(tctx.master) - cb.buf, cb.cursor = start[0], start[1] + cb.text, cb.cursor = start[0], start[1] cb.backspace() - assert cb.buf == output[0] + assert cb.text == output[0] assert cb.cursor == output[1] def test_left(self): cursors = [3, 2, 1, 0, 0] with taddons.context() as tctx: cb = commander.CommandBuffer(tctx.master) - cb.buf, cb.cursor = "abcd", 4 + cb.text, cb.cursor = "abcd", 4 for c in cursors: cb.left() assert cb.cursor == c @@ -60,7 +60,7 @@ class TestCommandBuffer: cursors = [1, 2, 3, 4, 4] with taddons.context() as tctx: cb = commander.CommandBuffer(tctx.master) - cb.buf, cb.cursor = "abcd", 0 + cb.text, cb.cursor = "abcd", 0 for c in cursors: cb.right() assert cb.cursor == c @@ -74,20 +74,25 @@ class TestCommandBuffer: with taddons.context() as tctx: for start, output in tests: cb = commander.CommandBuffer(tctx.master) - cb.buf, cb.cursor = start[0], start[1] + cb.text, cb.cursor = start[0], start[1] cb.insert("x") - assert cb.buf == output[0] + assert cb.text == 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.text = "foo bar" + cb.cursor = len(cb.text) cb.cycle_completion() def test_render(self): with taddons.context() as tctx: cb = commander.CommandBuffer(tctx.master) - cb.buf = "foo" - assert cb.render() == "foo" + cb.text = "foo" + assert cb.render() + + def test_flatten(self): + with taddons.context() as tctx: + cb = commander.CommandBuffer(tctx.master) + assert cb.flatten("foo bar") == "foo bar" |