Skip to content

Commit

Permalink
Refactor Riposte.run() in order to make it easier to test.
Browse files Browse the repository at this point in the history
Factor out from the main loop logic regarding line processing.
  • Loading branch information
fwkz committed Jun 30, 2019
1 parent d473243 commit 095c041
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
18 changes: 13 additions & 5 deletions riposte/riposte.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,23 @@ def wrapper(func: Callable):

return wrapper

def _process(self) -> None:
""" Process input provided by the input generator.
Get provided input, parse it, pick appropriate command handling
function and execute it.
"""
user_input = input(self.prompt)
if not user_input:
return
command_name, *args = self._parse_line(user_input)
self._get_command(command_name).execute(*args)

def run(self) -> None:
self._printer_thread.start()
while True:
try:
user_input = input(self.prompt)
if not user_input:
continue
command_name, *args = self._parse_line(user_input)
self._get_command(command_name).execute(*args)
self._process()
except RiposteException as err:
self.error(err)
except EOFError:
Expand Down
28 changes: 27 additions & 1 deletion tests/test_guides.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import pytest

from riposte import guides
from riposte import Riposte, guides
from riposte.exceptions import GuideError


Expand Down Expand Up @@ -68,3 +68,29 @@ def test_extract_guides(mocked_get_guides):

mocked_get_guides.assert_called_once_with(type_hint)
assert extracted_guides == {"foo": mocked_get_guides.return_value}


@pytest.mark.parametrize(
("input", "guide", "expected"),
(
("foobar", str, "foobar"),
("'foobar'", str, "foobar"),
("'foo bar'", str, "foo bar"),
("foobar", bytes, b"foobar"),
("'foobar'", bytes, b"foobar"),
("'foo bar'", bytes, b"foo bar"),
("1", int, 1),
("'1'", int, 1),
("\"[1, 'foo']\"", list, [1, "foo"]),
("\"{'foo': 'bar'}\"", dict, {"foo": "bar"}),
),
)
@mock.patch("builtins.input")
def test_guides(mocked_input, input, guide, expected, repl: Riposte):
mocked_input.return_value = "foobar " + input

@repl.command("foobar")
def handler_function(x: guide):
assert x == expected

repl._process()

0 comments on commit 095c041

Please sign in to comment.