refactor tests

This commit is contained in:
2025-12-05 15:54:03 +03:00
parent b0e6127c1e
commit 20b638c421
9 changed files with 235 additions and 194 deletions
+25 -39
View File
@@ -41,6 +41,9 @@ class Router:
self.command_handlers: CommandHandlers = CommandHandlers()
self.command_register_ignore: bool = False
self.aliases: set[str] = set()
self.triggers: set[str] = set()
def command(self, command: Command | str) -> Callable[[HandlerFunc], HandlerFunc]:
"""
@@ -53,7 +56,13 @@ class Router:
else:
redefined_command = command
_validate_command(redefined_command)
self._validate_command(redefined_command)
if overlapping := (self.aliases | self.triggers) & command.aliases:
Console().print(f"\n[b red]WARNING:[/b red] Overlapping trigger or alias: [b blue]{overlapping}[/b blue]")
self.aliases.update(command.aliases)
self.triggers.add(command.trigger)
def decorator(func: HandlerFunc) -> HandlerFunc:
_validate_func_args(func)
@@ -61,6 +70,20 @@ class Router:
return func
return decorator
def _validate_command(self, command: Command) -> None:
"""
Private. Validates the command registered in handler
:param command: validated command
:return: None if command is valid else raise exception
"""
command_name: str = command.trigger
if command_name.find(" ") != -1:
raise TriggerContainSpacesException()
flags: Flags = command.registered_flags
flags_name: list[str] = [flag.string_entity.lower() for flag in flags]
if len(set(flags_name)) < len(flags_name):
raise RepeatedFlagNameException()
def finds_appropriate_handler(self, input_command: InputCommand) -> None:
"""
@@ -105,29 +128,6 @@ class Router:
response = Response(ResponseStatus.ALL_FLAGS_VALID)
command_handler.handling(response)
@property
def triggers(self) -> list[str]:
"""
Public. Gets registered triggers
:return: registered in router triggers as list[str]
"""
all_triggers: list[str] = []
for command_handler in self.command_handlers:
all_triggers.append(command_handler.handled_command.trigger)
return all_triggers
@property
def aliases(self) -> list[str]:
"""
Public. Gets registered aliases
:return: registered in router aliases as list[str]
"""
all_aliases: list[str] = []
for command_handler in self.command_handlers:
if command_handler.handled_command.aliases:
all_aliases.extend(command_handler.handled_command.aliases)
return all_aliases
class CommandDecorator:
def __init__(self, router_instance: Router, command: Command):
@@ -188,18 +188,4 @@ def _validate_func_args(func: Callable[..., None]) -> None:
+ f" [i]but[/i] [bold blue]{response_arg_annotation}[/bold blue] [i]is specified[/i]",
highlight=False,
)
def _validate_command(command: Command) -> None:
"""
Private. Validates the command registered in handler
:param command: validated command
:return: None if command is valid else raise exception
"""
command_name: str = command.trigger
if command_name.find(" ") != -1:
raise TriggerContainSpacesException()
flags: Flags = command.registered_flags
flags_name: list[str] = [flag.string_entity.lower() for flag in flags]
if len(set(flags_name)) < len(flags_name):
raise RepeatedFlagNameException()