refactor tests

This commit is contained in:
2025-12-05 16:30:56 +03:00
parent 20b638c421
commit 913e7f16ca
4 changed files with 174 additions and 27 deletions
+1 -1
View File
@@ -30,7 +30,7 @@ App
system_router_title: str | None = "System points:", system_router_title: str | None = "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 = True, repeat_command_groups_printing: bool = False,
override_system_messages: bool = False, override_system_messages: bool = False,
autocompleter: AutoCompleter = DEFAULT_AUTOCOMPLETER, autocompleter: AutoCompleter = DEFAULT_AUTOCOMPLETER,
print_func: Printer = DEFAULT_PRINT_FUNC) -> None print_func: Printer = DEFAULT_PRINT_FUNC) -> None
+1 -1
View File
@@ -363,7 +363,7 @@ class App(BaseApp):
system_router_title: str | None = "System points:", system_router_title: str | None = "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 = True, repeat_command_groups_printing: bool = False,
override_system_messages: bool = False, override_system_messages: bool = False,
autocompleter: AutoCompleter = DEFAULT_AUTOCOMPLETER, autocompleter: AutoCompleter = DEFAULT_AUTOCOMPLETER,
print_func: Printer = DEFAULT_PRINT_FUNC, print_func: Printer = DEFAULT_PRINT_FUNC,
+3 -3
View File
@@ -58,11 +58,11 @@ class Router:
self._validate_command(redefined_command) self._validate_command(redefined_command)
if overlapping := (self.aliases | self.triggers) & command.aliases: if overlapping := (self.aliases | self.triggers) & redefined_command.aliases:
Console().print(f"\n[b red]WARNING:[/b red] Overlapping trigger or alias: [b blue]{overlapping}[/b blue]") Console().print(f"\n[b red]WARNING:[/b red] Overlapping trigger or alias: [b blue]{overlapping}[/b blue]")
self.aliases.update(command.aliases) self.aliases.update(redefined_command.aliases)
self.triggers.add(command.trigger) self.triggers.add(redefined_command.trigger)
def decorator(func: HandlerFunc) -> HandlerFunc: def decorator(func: HandlerFunc) -> HandlerFunc:
_validate_func_args(func) _validate_func_args(func)
+169 -22
View File
@@ -1,14 +1,17 @@
from argenta.app.dividing_line import DynamicDividingLine, StaticDividingLine
from argenta.app.protocols import DescriptionMessageGenerator, NonStandardBehaviorHandler
from pytest import CaptureFixture from pytest import CaptureFixture
from argenta.app import App from argenta.app import App
from argenta.response import Response from argenta.response import Response
from argenta.command.models import Command, InputCommand from argenta.command.models import Command, InputCommand
from argenta.router import Router from argenta.router import Router
import pytest
def test_is_exit_command1(): def test_is_exit_command1():
app = App() app = App()
assert app._is_exit_command(InputCommand('q')) is True assert app._is_exit_command(InputCommand('q')) is True
def test_is_exit_command5(): def test_is_exit_command5():
@@ -18,80 +21,224 @@ def test_is_exit_command5():
def test_is_exit_command2(): def test_is_exit_command2():
app = App(ignore_command_register=False) app = App(ignore_command_register=False)
assert app._is_exit_command(InputCommand('q')) is False assert app._is_exit_command(InputCommand('q')) is False
def test_is_exit_command3(): def test_is_exit_command3():
app = App(exit_command=Command('quit')) app = App(exit_command=Command('quit'))
assert app._is_exit_command(InputCommand('quit')) is True assert app._is_exit_command(InputCommand('quit')) is True
def test_is_exit_command4(): def test_is_exit_command4():
app = App(exit_command=Command('quit')) app = App(exit_command=Command('quit'))
assert app._is_exit_command(InputCommand('qUIt')) is True assert app._is_exit_command(InputCommand('qUIt')) is True
def test_is_exit_command6(): def test_is_exit_command6():
app = App(ignore_command_register=False, app = App(ignore_command_register=False,
exit_command=Command('quit')) exit_command=Command('quit'))
assert app._is_exit_command(InputCommand('qUIt')) is False assert app._is_exit_command(InputCommand('qUIt')) is False
def test_is_unknown_command1(): def test_is_unknown_command1():
app = App() app = App()
app.set_unknown_command_handler(lambda command: None) app.set_unknown_command_handler(lambda command: None)
app._current_matching_triggers_with_routers = {'fr': Router(), 'tr': Router(), 'de': Router()} app._current_matching_triggers_with_routers = {'fr': Router(), 'tr': Router(), 'de': Router()}
assert app._is_unknown_command(InputCommand('fr')) is False assert app._is_unknown_command(InputCommand('fr')) is False
def test_is_unknown_command2(): def test_is_unknown_command2():
app = App() app = App()
app.set_unknown_command_handler(lambda command: None) app.set_unknown_command_handler(lambda command: None)
app._current_matching_triggers_with_routers = {'fr': Router(), 'tr': Router(), 'de': Router()} app._current_matching_triggers_with_routers = {'fr': Router(), 'tr': Router(), 'de': Router()}
assert app._is_unknown_command(InputCommand('cr')) is True assert app._is_unknown_command(InputCommand('cr')) is True
def test_is_unknown_command3(): def test_is_unknown_command3():
app = App(ignore_command_register=False) app = App(ignore_command_register=False)
app.set_unknown_command_handler(lambda command: None) app.set_unknown_command_handler(lambda command: None)
app._current_matching_triggers_with_routers = {'Pr': Router(), 'tW': Router(), 'deQW': Router()} app._current_matching_triggers_with_routers = {'Pr': Router(), 'tW': Router(), 'deQW': Router()}
assert app._is_unknown_command(InputCommand('pr')) is True assert app._is_unknown_command(InputCommand('pr')) is True
def test_is_unknown_command4(): def test_is_unknown_command4():
app = App(ignore_command_register=False) app = App(ignore_command_register=False)
app.set_unknown_command_handler(lambda command: None) app.set_unknown_command_handler(lambda command: None)
app._current_matching_triggers_with_routers = {'Pr': Router(), 'tW': Router(), 'deQW': Router()} app._current_matching_triggers_with_routers = {'Pr': Router(), 'tW': Router(), 'deQW': Router()}
assert app._is_unknown_command(InputCommand('tW')) is False assert app._is_unknown_command(InputCommand('tW')) is False
def test_add_messages_on_startup(): def test_add_messages_on_startup():
app = App() app = App()
app.add_message_on_startup('Some message') app.add_message_on_startup('Some message')
assert app._messages_on_startup == ['Some message'] assert app._messages_on_startup == ['Some message']
def test_include_routers(): def test_include_routers():
app = App() app = App()
router = Router() router = Router()
router2 = Router() router2 = Router()
app.include_routers(router, router2) app.include_routers(router, router2)
assert app.registered_routers.registered_routers == [router, router2] assert app.registered_routers.registered_routers == [router, router2]
def test_overlapping_aliases(capsys: CaptureFixture[str]): def test_overlapping_aliases(capsys: CaptureFixture[str]):
app = App(override_system_messages=True) app = App(override_system_messages=True)
router = Router() router = Router()
@router.command(Command('test', aliases=['alias'])) @router.command(Command('test', aliases={'alias'}))
def handler(res: Response): def handler(res: Response):
pass pass
@router.command(Command('test2', aliases={'alias'}))
def handler2(res: Response):
pass
app.include_routers(router)
app._pre_cycle_setup()
captured = capsys.readouterr()
assert "Overlapping" in captured.out
def test_print_framed_text(capsys: CaptureFixture[str]):
app = App(
override_system_messages=True,
dividing_line=StaticDividingLine(length=5)
)
app._print_framed_text('test')
captured = capsys.readouterr()
assert '\n-----\n\ntest\n\n-----\n' in captured.out
def test_print_framed_text2(capsys: CaptureFixture[str]):
app = App(
override_system_messages=True,
dividing_line=DynamicDividingLine()
)
app._print_framed_text('test as test as test')
captured = capsys.readouterr()
assert '\n' + '-'*20 + '\n\ntest as test as test\n\n' + '-'*20 + '\n' in captured.out
def test_print_framed_text3(capsys: CaptureFixture[str]):
app = App(
override_system_messages=True,
dividing_line=DynamicDividingLine()
)
app._print_framed_text('some long test')
captured = capsys.readouterr()
assert '\n--------------\n\nsome long test\n\n--------------\n' in captured.out
def test_print_framed_text4(capsys: CaptureFixture[str]):
class OtherDividingLine:
pass
@router.command(Command('test2', aliases=['alias'])) app = App(
override_system_messages=True,
dividing_line=OtherDividingLine() # pyright: ignore[reportArgumentType]
)
with pytest.raises(NotImplementedError):
app._print_framed_text('some long test')
def test_most_similiar_command():
app = App(override_system_messages=True)
router = Router()
@router.command(Command('port', aliases={'p'}))
def handler(res: Response):
pass
@router.command(Command('host', aliases={'h'}))
def handler2(res: Response): def handler2(res: Response):
pass pass
app.include_routers(router) app.include_routers(router)
app._pre_cycle_setup() app._pre_cycle_setup()
captured = capsys.readouterr() assert app._most_similar_command('por') == 'port'
assert "Overlapping" in captured.out def test_most_similiar_command2():
app = App(override_system_messages=True)
router = Router()
@router.command(Command('command'))
def handler(res: Response):
pass
@router.command(Command('command_other'))
def handler2(res: Response):
pass
app.include_routers(router)
app._pre_cycle_setup()
assert app._most_similar_command('com') == 'command'
def test_most_similiar_command3():
app = App(override_system_messages=True)
router = Router()
@router.command(Command('command'))
def handler(res: Response):
pass
@router.command(Command('command_other'))
def handler2(res: Response):
pass
app.include_routers(router)
app._pre_cycle_setup()
assert app._most_similar_command('command_') == 'command_other'
def test_most_similiar_command4():
app = App(override_system_messages=True)
router = Router()
@router.command(Command('command'))
def handler(res: Response):
pass
@router.command(Command('command_other'))
def handler2(res: Response):
pass
app.include_routers(router)
app._pre_cycle_setup()
assert app._most_similar_command('nonexists') is None
def test_most_similiar_command3():
app = App(override_system_messages=True)
router = Router()
@router.command(Command('command', aliases={'other_name'}))
def handler(res: Response):
pass
@router.command(Command('command_other', aliases={'more_name'}))
def handler2(res: Response):
pass
app.include_routers(router)
app._pre_cycle_setup()
assert app._most_similar_command('othe') == 'other_name'
def test_app_set_description_message_gen():
app = App()
descr_gen: DescriptionMessageGenerator = lambda command, description: print(command + '-+-' + description)
app.set_description_message_pattern(descr_gen)
assert app._description_message_gen is descr_gen
def test_app_set_exit_handler():
app = App()
handler: NonStandardBehaviorHandler[Response] = lambda response: print('goodbye')
app.set_exit_command_handler(handler)
assert app._exit_command_handler is handler