mirror of
https://github.com/koloideal/Argenta.git
synced 2026-06-10 18:15:28 +03:00
new tests, upgrade tests coverage
This commit is contained in:
+4
-14
@@ -1,15 +1,5 @@
|
|||||||
from argenta import App, Router, Command, Response
|
from argenta import App
|
||||||
|
|
||||||
|
app = App()
|
||||||
app = App(override_system_messages=True)
|
app._setup_default_view()
|
||||||
router = Router()
|
app._empty_input_command_handler()
|
||||||
|
|
||||||
@router.command(Command('test', aliases={'alias', 'primer'}))
|
|
||||||
def handler(res: Response):
|
|
||||||
pass
|
|
||||||
|
|
||||||
@router.command(Command('test2', aliases={'alias', 'primer'}))
|
|
||||||
def handler2(res: Response):
|
|
||||||
pass
|
|
||||||
|
|
||||||
print(router.aliases)
|
|
||||||
@@ -57,6 +57,10 @@ reportUnusedFunction = false
|
|||||||
|
|
||||||
[tool.coverage.run]
|
[tool.coverage.run]
|
||||||
branch = true
|
branch = true
|
||||||
|
omit = [
|
||||||
|
"src/argenta/app/protocols.py",
|
||||||
|
"src/argenta/command/exceptions.py"
|
||||||
|
]
|
||||||
|
|
||||||
[tool.mypy]
|
[tool.mypy]
|
||||||
disable_error_code = "import-untyped"
|
disable_error_code = "import-untyped"
|
||||||
|
|||||||
+27
-28
@@ -19,7 +19,6 @@ from argenta.app.protocols import (
|
|||||||
)
|
)
|
||||||
from argenta.app.registered_routers.entity import RegisteredRouters
|
from argenta.app.registered_routers.entity import RegisteredRouters
|
||||||
from argenta.command.exceptions import (
|
from argenta.command.exceptions import (
|
||||||
EmptyInputCommandException,
|
|
||||||
InputCommandException,
|
InputCommandException,
|
||||||
RepeatedInputFlagsException,
|
RepeatedInputFlagsException,
|
||||||
UnprocessedInputFlagException,
|
UnprocessedInputFlagException,
|
||||||
@@ -40,7 +39,7 @@ class BaseApp:
|
|||||||
initial_message: str,
|
initial_message: str,
|
||||||
farewell_message: str,
|
farewell_message: str,
|
||||||
exit_command: Command,
|
exit_command: Command,
|
||||||
system_router_title: str | None,
|
system_router_title: str,
|
||||||
ignore_command_register: bool,
|
ignore_command_register: bool,
|
||||||
dividing_line: StaticDividingLine | DynamicDividingLine,
|
dividing_line: StaticDividingLine | DynamicDividingLine,
|
||||||
repeat_command_groups_printing: bool,
|
repeat_command_groups_printing: bool,
|
||||||
@@ -51,7 +50,7 @@ class BaseApp:
|
|||||||
self._prompt: str = prompt
|
self._prompt: str = prompt
|
||||||
self._print_func: Printer = print_func
|
self._print_func: Printer = print_func
|
||||||
self._exit_command: Command = exit_command
|
self._exit_command: Command = exit_command
|
||||||
self._system_router_title: str | None = system_router_title
|
self._system_router_title: str = system_router_title
|
||||||
self._dividing_line: StaticDividingLine | DynamicDividingLine = dividing_line
|
self._dividing_line: StaticDividingLine | DynamicDividingLine = dividing_line
|
||||||
self._ignore_command_register: bool = ignore_command_register
|
self._ignore_command_register: bool = ignore_command_register
|
||||||
self._repeat_command_groups_printing_description: bool = repeat_command_groups_printing
|
self._repeat_command_groups_printing_description: bool = repeat_command_groups_printing
|
||||||
@@ -144,7 +143,6 @@ class BaseApp:
|
|||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
for registered_router in self.registered_routers:
|
for registered_router in self.registered_routers:
|
||||||
if registered_router.title:
|
|
||||||
self._print_func(registered_router.title)
|
self._print_func(registered_router.title)
|
||||||
for command_handler in registered_router.command_handlers:
|
for command_handler in registered_router.command_handlers:
|
||||||
handled_command = command_handler.handled_command
|
handled_command = command_handler.handled_command
|
||||||
@@ -239,7 +237,7 @@ class BaseApp:
|
|||||||
self._incorrect_input_syntax_handler(raw_command)
|
self._incorrect_input_syntax_handler(raw_command)
|
||||||
elif isinstance(error, RepeatedInputFlagsException):
|
elif isinstance(error, RepeatedInputFlagsException):
|
||||||
self._repeated_input_flags_handler(raw_command)
|
self._repeated_input_flags_handler(raw_command)
|
||||||
elif isinstance(error, EmptyInputCommandException):
|
else:
|
||||||
self._empty_input_command_handler()
|
self._empty_input_command_handler()
|
||||||
|
|
||||||
def _setup_system_router(self) -> None:
|
def _setup_system_router(self) -> None:
|
||||||
@@ -253,7 +251,6 @@ class BaseApp:
|
|||||||
def _(response: Response) -> None:
|
def _(response: Response) -> None:
|
||||||
self._exit_command_handler(response)
|
self._exit_command_handler(response)
|
||||||
|
|
||||||
if system_router not in self.registered_routers.registered_routers:
|
|
||||||
system_router.command_register_ignore = self._ignore_command_register
|
system_router.command_register_ignore = self._ignore_command_register
|
||||||
self.registered_routers.add_registered_router(system_router)
|
self.registered_routers.add_registered_router(system_router)
|
||||||
|
|
||||||
@@ -343,6 +340,28 @@ class BaseApp:
|
|||||||
if not self._repeat_command_groups_printing_description:
|
if not self._repeat_command_groups_printing_description:
|
||||||
self._print_command_group_description()
|
self._print_command_group_description()
|
||||||
|
|
||||||
|
def _process_exist_and_valid_command(self, input_command: InputCommand):
|
||||||
|
processing_router = self._current_matching_triggers_with_routers[input_command.trigger.lower()]
|
||||||
|
|
||||||
|
if processing_router.disable_redirect_stdout:
|
||||||
|
dividing_line_unit_part: str = self._dividing_line.get_unit_part()
|
||||||
|
self._print_func(
|
||||||
|
StaticDividingLine(dividing_line_unit_part).get_full_static_line(
|
||||||
|
is_override=self._override_system_messages
|
||||||
|
)
|
||||||
|
)
|
||||||
|
processing_router.finds_appropriate_handler(input_command)
|
||||||
|
self._print_func(
|
||||||
|
StaticDividingLine(dividing_line_unit_part).get_full_static_line(
|
||||||
|
is_override=self._override_system_messages
|
||||||
|
)
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
with redirect_stdout(io.StringIO()) as stdout:
|
||||||
|
processing_router.finds_appropriate_handler(input_command)
|
||||||
|
stdout_result: str = stdout.getvalue()
|
||||||
|
self._print_framed_text(stdout_result)
|
||||||
|
|
||||||
|
|
||||||
AVAILABLE_DIVIDING_LINES: TypeAlias = StaticDividingLine | DynamicDividingLine
|
AVAILABLE_DIVIDING_LINES: TypeAlias = StaticDividingLine | DynamicDividingLine
|
||||||
DEFAULT_DIVIDING_LINE: StaticDividingLine = StaticDividingLine()
|
DEFAULT_DIVIDING_LINE: StaticDividingLine = StaticDividingLine()
|
||||||
@@ -360,7 +379,7 @@ class App(BaseApp):
|
|||||||
initial_message: str = "Argenta\n",
|
initial_message: str = "Argenta\n",
|
||||||
farewell_message: str = "\nSee you\n",
|
farewell_message: str = "\nSee you\n",
|
||||||
exit_command: Command = DEFAULT_EXIT_COMMAND,
|
exit_command: Command = DEFAULT_EXIT_COMMAND,
|
||||||
system_router_title: str | None = "System points:",
|
system_router_title: str = "System points:",
|
||||||
ignore_command_register: bool = True,
|
ignore_command_register: bool = True,
|
||||||
dividing_line: AVAILABLE_DIVIDING_LINES = DEFAULT_DIVIDING_LINE,
|
dividing_line: AVAILABLE_DIVIDING_LINES = DEFAULT_DIVIDING_LINE,
|
||||||
repeat_command_groups_printing: bool = False,
|
repeat_command_groups_printing: bool = False,
|
||||||
@@ -433,27 +452,7 @@ class App(BaseApp):
|
|||||||
self._print_framed_text(stdout_res)
|
self._print_framed_text(stdout_res)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
processing_router = self._current_matching_triggers_with_routers[input_command.trigger.lower()]
|
self._process_exist_and_valid_command(input_command)
|
||||||
|
|
||||||
if processing_router.disable_redirect_stdout:
|
|
||||||
dividing_line_unit_part: str = self._dividing_line.get_unit_part()
|
|
||||||
self._print_func(
|
|
||||||
StaticDividingLine(dividing_line_unit_part).get_full_static_line(
|
|
||||||
is_override=self._override_system_messages
|
|
||||||
)
|
|
||||||
)
|
|
||||||
processing_router.finds_appropriate_handler(input_command)
|
|
||||||
self._print_func(
|
|
||||||
StaticDividingLine(dividing_line_unit_part).get_full_static_line(
|
|
||||||
is_override=self._override_system_messages
|
|
||||||
)
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
with redirect_stdout(io.StringIO()) as stdout:
|
|
||||||
processing_router.finds_appropriate_handler(input_command)
|
|
||||||
stdout_result: str = stdout.getvalue()
|
|
||||||
if stdout_result:
|
|
||||||
self._print_framed_text(stdout_result)
|
|
||||||
|
|
||||||
def include_router(self, router: Router) -> None:
|
def include_router(self, router: Router) -> None:
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -2,11 +2,12 @@ __all__ = ["NonStandardBehaviorHandler", "EmptyCommandHandler", "Printer", "Desc
|
|||||||
|
|
||||||
from typing import Protocol, TypeVar
|
from typing import Protocol, TypeVar
|
||||||
|
|
||||||
|
|
||||||
T = TypeVar("T", contravariant=True) # noqa: WPS111
|
T = TypeVar("T", contravariant=True) # noqa: WPS111
|
||||||
|
|
||||||
|
|
||||||
class NonStandardBehaviorHandler(Protocol[T]):
|
class NonStandardBehaviorHandler(Protocol[T]):
|
||||||
def __call__(self, __param: T) -> None:
|
def __call__(self, _param: T, /) -> None:
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
|
|
||||||
@@ -16,10 +17,10 @@ class EmptyCommandHandler(Protocol):
|
|||||||
|
|
||||||
|
|
||||||
class Printer(Protocol):
|
class Printer(Protocol):
|
||||||
def __call__(self, __text: str) -> None:
|
def __call__(self, _text: str, /) -> None:
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
|
|
||||||
class DescriptionMessageGenerator(Protocol):
|
class DescriptionMessageGenerator(Protocol):
|
||||||
def __call__(self, __first_param: str, __second_param: str) -> str:
|
def __call__(self, _command: str, _description: str, /) -> str:
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|||||||
@@ -24,6 +24,3 @@ class RegisteredRouters:
|
|||||||
|
|
||||||
def __iter__(self) -> Iterator[Router]:
|
def __iter__(self) -> Iterator[Router]:
|
||||||
return iter(self.registered_routers)
|
return iter(self.registered_routers)
|
||||||
|
|
||||||
def __next__(self) -> Router:
|
|
||||||
return next(iter(self.registered_routers))
|
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ HandlerFunc: TypeAlias = Callable[..., None]
|
|||||||
class Router:
|
class Router:
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
title: str | None = "Default title",
|
title: str = "Default title",
|
||||||
*,
|
*,
|
||||||
disable_redirect_stdout: bool = False,
|
disable_redirect_stdout: bool = False,
|
||||||
):
|
):
|
||||||
@@ -36,7 +36,7 @@ class Router:
|
|||||||
which is ambiguous behavior and can lead to unexpected work
|
which is ambiguous behavior and can lead to unexpected work
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
self.title: str | None = title
|
self.title: str = title
|
||||||
self.disable_redirect_stdout: bool = disable_redirect_stdout
|
self.disable_redirect_stdout: bool = disable_redirect_stdout
|
||||||
|
|
||||||
self.command_handlers: CommandHandlers = CommandHandlers()
|
self.command_handlers: CommandHandlers = CommandHandlers()
|
||||||
|
|||||||
@@ -99,8 +99,11 @@ class TestSystemHandlerNormalWork(PatchedArgvTestCase):
|
|||||||
if valid_flag and valid_flag.status == ValidationStatus.VALID:
|
if valid_flag and valid_flag.status == ValidationStatus.VALID:
|
||||||
print(f'flag value for {valid_flag.name} flag : {valid_flag.input_value}')
|
print(f'flag value for {valid_flag.name} flag : {valid_flag.input_value}')
|
||||||
|
|
||||||
app = App(override_system_messages=True,
|
app = App(
|
||||||
print_func=print)
|
override_system_messages=True,
|
||||||
|
repeat_command_groups_printing=True,
|
||||||
|
print_func=print
|
||||||
|
)
|
||||||
app.include_router(router)
|
app.include_router(router)
|
||||||
orchestrator.start_polling(app)
|
orchestrator.start_polling(app)
|
||||||
|
|
||||||
|
|||||||
@@ -131,7 +131,7 @@ def test_print_framed_text3(capsys: CaptureFixture[str]):
|
|||||||
|
|
||||||
assert '\n--------------\n\nsome long test\n\n--------------\n' in captured.out
|
assert '\n--------------\n\nsome long test\n\n--------------\n' in captured.out
|
||||||
|
|
||||||
def test_print_framed_text4(capsys: CaptureFixture[str]):
|
def test_print_framed_text4():
|
||||||
class OtherDividingLine:
|
class OtherDividingLine:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@@ -211,7 +211,7 @@ def test_most_similiar_command4():
|
|||||||
|
|
||||||
assert app._most_similar_command('nonexists') is None
|
assert app._most_similar_command('nonexists') is None
|
||||||
|
|
||||||
def test_most_similiar_command3():
|
def test_most_similiar_command5():
|
||||||
app = App(override_system_messages=True)
|
app = App(override_system_messages=True)
|
||||||
router = Router()
|
router = Router()
|
||||||
|
|
||||||
@@ -230,7 +230,7 @@ def test_most_similiar_command3():
|
|||||||
|
|
||||||
def test_app_set_description_message_gen():
|
def test_app_set_description_message_gen():
|
||||||
app = App()
|
app = App()
|
||||||
descr_gen: DescriptionMessageGenerator = lambda command, description: print(command + '-+-' + description)
|
descr_gen: DescriptionMessageGenerator = lambda command, description: command + '-+-' + description
|
||||||
app.set_description_message_pattern(descr_gen)
|
app.set_description_message_pattern(descr_gen)
|
||||||
|
|
||||||
assert app._description_message_gen is descr_gen
|
assert app._description_message_gen is descr_gen
|
||||||
@@ -242,3 +242,55 @@ def test_app_set_exit_handler():
|
|||||||
|
|
||||||
assert app._exit_command_handler is handler
|
assert app._exit_command_handler is handler
|
||||||
|
|
||||||
|
def test_app_is_exit_command():
|
||||||
|
app = App(exit_command=Command('q', aliases={'exit'}))
|
||||||
|
assert app._is_exit_command(InputCommand('exit')) is True
|
||||||
|
|
||||||
|
def test_app_is_exit_command2():
|
||||||
|
app = App(exit_command=Command('q', aliases={'exit'}), ignore_command_register=False)
|
||||||
|
assert app._is_exit_command(InputCommand('exit')) is True
|
||||||
|
|
||||||
|
def test_app_is_exit_command3():
|
||||||
|
app = App(exit_command=Command('q', aliases={'exit'}))
|
||||||
|
assert app._is_exit_command(InputCommand('quit')) is False
|
||||||
|
|
||||||
|
def test_app_is_exit_command4():
|
||||||
|
app = App(exit_command=Command('q', aliases={'exit'}), ignore_command_register=False)
|
||||||
|
assert app._is_exit_command(InputCommand('Exit')) is False
|
||||||
|
|
||||||
|
def test_app_setup_default_view():
|
||||||
|
app = App(prompt='>>')
|
||||||
|
app._setup_default_view()
|
||||||
|
|
||||||
|
assert app._prompt == '[italic dim bold]>>'
|
||||||
|
|
||||||
|
def test_app_setup_default_view2():
|
||||||
|
app = App()
|
||||||
|
app._setup_default_view()
|
||||||
|
assert app._unknown_command_handler(InputCommand('nonexists')) is None
|
||||||
|
|
||||||
|
def test_app_pre_cycle_setup(capsys: CaptureFixture[str]):
|
||||||
|
app = App()
|
||||||
|
app.add_message_on_startup('some message')
|
||||||
|
app._pre_cycle_setup()
|
||||||
|
stdout = capsys.readouterr()
|
||||||
|
|
||||||
|
assert 'some message' in stdout.out
|
||||||
|
|
||||||
|
def test_app_with_router_with_disable_redirect_stdout(capsys: CaptureFixture[str]):
|
||||||
|
app = App(repeat_command_groups_printing=True)
|
||||||
|
router = Router(disable_redirect_stdout=True)
|
||||||
|
|
||||||
|
@router.command('command')
|
||||||
|
def handler(res: Response):
|
||||||
|
print("Hello!")
|
||||||
|
|
||||||
|
app.include_router(router)
|
||||||
|
|
||||||
|
app._pre_cycle_setup()
|
||||||
|
app._process_exist_and_valid_command(InputCommand('command'))
|
||||||
|
|
||||||
|
stdout = capsys.readouterr()
|
||||||
|
|
||||||
|
assert 'Hello!' in stdout.out
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user