mirror of
https://github.com/koloideal/Argenta.git
synced 2026-06-10 10:05:28 +03:00
refactor, new model e.t.c.
This commit is contained in:
@@ -56,7 +56,7 @@ if __name__ == '__main__':
|
||||
import re
|
||||
from argenta.router import Router
|
||||
from argenta.command import Command
|
||||
from argenta.command.flag import FlagsGroup, Flag
|
||||
from argenta.command.flag.registered_flag import FlagsGroup, Flag
|
||||
|
||||
router = Router()
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ from argenta.app.exceptions import (InvalidRouterInstanceException,
|
||||
NoRegisteredRoutersException,
|
||||
NoRegisteredHandlersException,
|
||||
IncorrectNumberOfHandlerArgsException)
|
||||
from argenta.app.models import RegisteredRouters
|
||||
from argenta.app.registered_routers.entity import RegisteredRouters
|
||||
|
||||
|
||||
class App:
|
||||
@@ -219,7 +219,6 @@ class App:
|
||||
@system_router.command(Command(self.exit_command, self.exit_command_description))
|
||||
def exit_command():
|
||||
self._exit_command_handler()
|
||||
|
||||
if system_router not in self._registered_routers.get_registered_routers():
|
||||
self.include_router(system_router)
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
from argenta.command.flag.entity import Flag
|
||||
from argenta.command.flag.registered_flag.entity import Flag
|
||||
from argenta.command.flag.input_flag.entity import InputFlag
|
||||
from argenta.command.flag.flags_group import FlagsGroup
|
||||
from .exceptions import (UnprocessedInputFlagException,
|
||||
RepeatedInputFlagsException,
|
||||
@@ -32,7 +33,7 @@ class Command(Generic[CommandType]):
|
||||
return self._registered_flags
|
||||
|
||||
|
||||
def validate_input_flag(self, flag: Flag):
|
||||
def validate_input_flag(self, flag: InputFlag):
|
||||
registered_flags: FlagsGroup | None = self.get_registered_flags()
|
||||
if registered_flags:
|
||||
if isinstance(registered_flags, Flag):
|
||||
@@ -85,8 +86,8 @@ class Command(Generic[CommandType]):
|
||||
flag_prefix_last_symbol_index = current_flag_name.rfind('-')
|
||||
flag_prefix = current_flag_name[:flag_prefix_last_symbol_index+1]
|
||||
flag_name = current_flag_name[flag_prefix_last_symbol_index+1:]
|
||||
input_flag = Flag(flag_name=flag_name,
|
||||
flag_prefix=cast(Literal['-', '--', '---'], flag_prefix))
|
||||
input_flag = InputFlag(flag_name=flag_name,
|
||||
flag_prefix=cast(Literal['-', '--', '---'], flag_prefix))
|
||||
input_flag.set_value(current_flag_value)
|
||||
|
||||
all_flags = [x.get_string_entity() for x in flags.get_flags()]
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
from argenta.command.flag.entity import Flag
|
||||
from argenta.command.flag.input_flag.entity import InputFlag
|
||||
from argenta.command.flag.registered_flag.entity import Flag
|
||||
|
||||
|
||||
class UnprocessedInputFlagException(Exception):
|
||||
@@ -7,7 +8,7 @@ class UnprocessedInputFlagException(Exception):
|
||||
|
||||
|
||||
class RepeatedInputFlagsException(Exception):
|
||||
def __init__(self, flag: Flag):
|
||||
def __init__(self, flag: Flag | InputFlag):
|
||||
self.flag = flag
|
||||
def __str__(self):
|
||||
return ("Repeated Input Flags\n"
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
__all__ = ["Flag", "FlagsGroup"]
|
||||
|
||||
|
||||
from .entity import Flag
|
||||
from .flags_group.entity import FlagsGroup
|
||||
@@ -1,7 +1,7 @@
|
||||
from typing import Literal, Pattern
|
||||
|
||||
|
||||
class Flag:
|
||||
class BaseFlag:
|
||||
def __init__(self, flag_name: str,
|
||||
flag_prefix: Literal['-', '--', '---'] = '--',
|
||||
possible_flag_values: list[str] | Pattern[str] | False = True):
|
||||
@@ -9,8 +9,6 @@ class Flag:
|
||||
self._flag_prefix = flag_prefix
|
||||
self.possible_flag_values = possible_flag_values
|
||||
|
||||
self._flag_value = None
|
||||
|
||||
def get_string_entity(self):
|
||||
string_entity: str = self._flag_prefix + self._flag_name
|
||||
return string_entity
|
||||
@@ -21,12 +19,6 @@ class Flag:
|
||||
def get_flag_prefix(self):
|
||||
return self._flag_prefix
|
||||
|
||||
def get_value(self):
|
||||
return self._flag_value
|
||||
|
||||
def set_value(self, value):
|
||||
self._flag_value = value
|
||||
|
||||
def validate_input_flag_value(self, input_flag_value: str | None):
|
||||
if self.possible_flag_values is False:
|
||||
if input_flag_value is None:
|
||||
@@ -1,22 +1,23 @@
|
||||
from argenta.command.flag import Flag
|
||||
from argenta.command.flag.input_flag.entity import InputFlag
|
||||
from argenta.command.flag.registered_flag import Flag
|
||||
|
||||
|
||||
class FlagsGroup:
|
||||
def __init__(self, *flags: Flag):
|
||||
self._flags: list[Flag] = [] if not flags else flags
|
||||
def __init__(self, *flags: Flag | InputFlag):
|
||||
self._flags: list[Flag | InputFlag] = [] if not flags else flags
|
||||
|
||||
def get_flags(self) -> list[Flag]:
|
||||
def get_flags(self) -> list[Flag | InputFlag]:
|
||||
return self._flags
|
||||
|
||||
def add_flag(self, flag: Flag):
|
||||
def add_flag(self, flag: Flag | InputFlag):
|
||||
self._flags.append(flag)
|
||||
|
||||
def add_flags(self, flags: list[Flag]):
|
||||
def add_flags(self, flags: list[Flag | InputFlag]):
|
||||
self._flags.extend(flags)
|
||||
|
||||
def unparse_to_dict(self):
|
||||
result_dict: dict[str, dict] = {}
|
||||
for flag in self._flags:
|
||||
for flag in self.get_flags():
|
||||
result_dict[flag.get_flag_name()] = {
|
||||
'name': flag.get_flag_name(),
|
||||
'string_entity': flag.get_string_entity(),
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
from re import Pattern
|
||||
from typing import Literal
|
||||
|
||||
from argenta.command.flag.base_flag.entity import BaseFlag
|
||||
|
||||
|
||||
class InputFlag(BaseFlag):
|
||||
def __init__(self, flag_name: str,
|
||||
flag_prefix: Literal['-', '--', '---'] = '--',
|
||||
possible_flag_values: list[str] | Pattern[str] | False = True):
|
||||
super().__init__(flag_name, flag_prefix, possible_flag_values)
|
||||
self._flag_value = None
|
||||
|
||||
def get_value(self):
|
||||
return self._flag_value
|
||||
|
||||
def set_value(self, value):
|
||||
self._flag_value = value
|
||||
@@ -0,0 +1,5 @@
|
||||
__all__ = ["Flag", "FlagsGroup"]
|
||||
|
||||
|
||||
from .entity import Flag
|
||||
from argenta.command.flag.flags_group import FlagsGroup
|
||||
@@ -1,5 +1,5 @@
|
||||
from dataclasses import dataclass
|
||||
from argenta.command.flag import Flag
|
||||
from argenta.command.flag.registered_flag import Flag
|
||||
import re
|
||||
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
from argenta.command.flag.base_flag.entity import BaseFlag
|
||||
|
||||
|
||||
class Flag(BaseFlag):
|
||||
pass
|
||||
@@ -0,0 +1,20 @@
|
||||
from typing import Callable
|
||||
from argenta.command import Command
|
||||
|
||||
|
||||
class CommandHandler:
|
||||
def __init__(self, handler: Callable[[], None] | Callable[[dict], None], handled_command: Command):
|
||||
self.handler = handler
|
||||
self.handled_command = handled_command
|
||||
|
||||
def handling(self, input_flags: dict = None):
|
||||
if input_flags is not None:
|
||||
self.handler(input_flags)
|
||||
else:
|
||||
self.handler()
|
||||
|
||||
def get_handler(self):
|
||||
return self.handler
|
||||
|
||||
def get_handled_command(self):
|
||||
return self.handled_command
|
||||
@@ -0,0 +1,21 @@
|
||||
from argenta.router.command_handler.entity import CommandHandler
|
||||
|
||||
|
||||
class CommandHandlers:
|
||||
def __init__(self, command_handlers: list[CommandHandler] = None):
|
||||
self.command_handlers = command_handlers if command_handlers else []
|
||||
|
||||
def get_command_handlers(self) -> list[CommandHandler]:
|
||||
return self.command_handlers
|
||||
|
||||
def add_command_handler(self, command_handler: CommandHandler):
|
||||
self.command_handlers.append(command_handler)
|
||||
|
||||
def add_command_handlers(self, *command_handlers: CommandHandler):
|
||||
self.command_handlers.extend(command_handlers)
|
||||
|
||||
def __iter__(self):
|
||||
return iter(self.command_handlers)
|
||||
|
||||
def __next__(self):
|
||||
return next(iter(self.command_handlers))
|
||||
+12
-13
@@ -2,10 +2,10 @@ from typing import Callable, Any
|
||||
from inspect import getfullargspec
|
||||
|
||||
from argenta.command import Command
|
||||
from argenta.router.models import CommandHandler, CommandHandlers
|
||||
from argenta.command.flag import Flag, FlagsGroup
|
||||
from argenta.router.exceptions import (RepeatedCommandException,
|
||||
RepeatedFlagNameException,
|
||||
from argenta.router.command_handlers.entity import CommandHandlers
|
||||
from argenta.router.command_handler.entity import CommandHandler
|
||||
from argenta.command.flag.registered_flag import Flag, FlagsGroup
|
||||
from argenta.router.exceptions import (RepeatedFlagNameException,
|
||||
TooManyTransferredArgsException,
|
||||
RequiredArgumentNotPassedException,
|
||||
IncorrectNumberOfHandlerArgsException,
|
||||
@@ -61,26 +61,25 @@ class Router:
|
||||
if not is_valid:
|
||||
self._not_valid_flag_handler(flag)
|
||||
return
|
||||
return command_handler.handling(input_command_flags.unparse_to_dict())
|
||||
command_handler.handling(input_command_flags.unparse_to_dict())
|
||||
return
|
||||
else:
|
||||
return command_handler.handling({})
|
||||
command_handler.handling({})
|
||||
return
|
||||
else:
|
||||
if input_command_flags:
|
||||
self._not_valid_flag_handler(input_command_flags[0])
|
||||
return
|
||||
else:
|
||||
return command_handler.handling()
|
||||
command_handler.handling()
|
||||
return
|
||||
|
||||
|
||||
def _validate_command(self, command: Command):
|
||||
@staticmethod
|
||||
def _validate_command(command: Command):
|
||||
command_name: str = command.get_trigger()
|
||||
if command_name.find(' ') != -1:
|
||||
raise TriggerCannotContainSpacesException()
|
||||
if command_name in self.get_all_commands():
|
||||
raise RepeatedCommandException()
|
||||
if self._ignore_command_register:
|
||||
if command_name.lower() in [x.lower() for x in self.get_all_commands()]:
|
||||
raise RepeatedCommandException()
|
||||
|
||||
flags: FlagsGroup = command.get_registered_flags()
|
||||
if flags:
|
||||
|
||||
@@ -1,11 +1,6 @@
|
||||
class RepeatedCommandException(Exception):
|
||||
def __str__(self):
|
||||
return "Commands in handler cannot be repeated"
|
||||
|
||||
|
||||
class RepeatedFlagNameException(Exception):
|
||||
def __str__(self):
|
||||
return "Repeated flag name in register command"
|
||||
return "Repeated registered_flag name in register command"
|
||||
|
||||
|
||||
class TooManyTransferredArgsException(Exception):
|
||||
|
||||
@@ -1,49 +0,0 @@
|
||||
from typing import Callable
|
||||
from argenta.command import Command
|
||||
|
||||
|
||||
class CommandHandler:
|
||||
def __init__(self, handler: Callable[[], None] | Callable[[dict], None], handled_command: Command):
|
||||
self.handler = handler
|
||||
self.handled_command = handled_command
|
||||
|
||||
def handling(self, input_flags: dict = None):
|
||||
if input_flags is not None:
|
||||
self.handler(input_flags)
|
||||
else:
|
||||
self.handler()
|
||||
|
||||
def get_handler(self):
|
||||
return self.handler
|
||||
|
||||
def get_handled_command(self):
|
||||
return self.handled_command
|
||||
|
||||
|
||||
class CommandHandlers:
|
||||
def __init__(self, command_handlers: list[CommandHandler] = None):
|
||||
self.command_handlers = command_handlers if command_handlers else []
|
||||
|
||||
def get_command_handlers(self) -> list[CommandHandler]:
|
||||
return self.command_handlers
|
||||
|
||||
def add_command_handler(self, command_handler: CommandHandler):
|
||||
self.command_handlers.append(command_handler)
|
||||
|
||||
def add_command_handlers(self, *command_handlers: CommandHandler):
|
||||
self.command_handlers.extend(command_handlers)
|
||||
|
||||
def __iter__(self):
|
||||
return iter(self.command_handlers)
|
||||
|
||||
def __next__(self):
|
||||
return next(iter(self.command_handlers))
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
+15
-4
@@ -1,7 +1,18 @@
|
||||
import re
|
||||
from argenta.app import App
|
||||
from argenta.command import Command
|
||||
from argenta.router import Router
|
||||
|
||||
|
||||
def test(string):
|
||||
return bool(re.match(r'\ntest command\n(.|\s)*\nsome command\n', string))
|
||||
router = Router()
|
||||
|
||||
print(test('test command tpgm4tigm4tigmt\n i0hhmi6h some command'))
|
||||
@router.command(Command('test'))
|
||||
def test():
|
||||
print(f'test command')
|
||||
|
||||
@router.command(Command('some'))
|
||||
def test2():
|
||||
print(f'some command')
|
||||
|
||||
app = App()
|
||||
app.include_router(router)
|
||||
app.start_polling()
|
||||
@@ -2,10 +2,9 @@ from pprint import pprint
|
||||
from rich.console import Console
|
||||
|
||||
from argenta.command import Command
|
||||
from argenta.command.flag import FlagsGroup
|
||||
from argenta.command.flag.defaults import DefaultFlags
|
||||
from argenta.command.flag.registered_flag import FlagsGroup
|
||||
from argenta.command.flag.registered_flag.defaults import DefaultFlags
|
||||
from argenta.router import Router
|
||||
from argenta.router.defaults import system_router
|
||||
from .handlers_implementation.help_command import help_command
|
||||
|
||||
|
||||
|
||||
@@ -7,8 +7,8 @@ import re
|
||||
from argenta.app import App
|
||||
from argenta.command import Command
|
||||
from argenta.router import Router
|
||||
from argenta.command.flag import FlagsGroup
|
||||
from argenta.command.flag.defaults import DefaultFlags
|
||||
from argenta.command.flag.registered_flag import FlagsGroup
|
||||
from argenta.command.flag.registered_flag.defaults import DefaultFlags
|
||||
|
||||
|
||||
|
||||
@@ -64,7 +64,7 @@ class TestSystemHandlerNormalWork(unittest.TestCase):
|
||||
|
||||
output = mock_stdout.getvalue()
|
||||
|
||||
self.assertIn('\nUndefined or incorrect input flag: --help\n', output)
|
||||
self.assertIn('\nUndefined or incorrect input registered_flag: --help\n', output)
|
||||
|
||||
|
||||
@patch("builtins.input", side_effect=["test --port 22", "q"])
|
||||
@@ -82,14 +82,14 @@ class TestSystemHandlerNormalWork(unittest.TestCase):
|
||||
|
||||
output = mock_stdout.getvalue()
|
||||
|
||||
self.assertIn('\nUndefined or incorrect input flag: --port 22\n', output)
|
||||
self.assertIn('\nUndefined or incorrect input registered_flag: --port 22\n', output)
|
||||
|
||||
|
||||
@patch("builtins.input", side_effect=["test --host 192.168.32.1 --port 132", "q"])
|
||||
@patch("sys.stdout", new_callable=io.StringIO)
|
||||
def test_input_correct_command_with_one_correct_flag_an_one_incorrect_flag(self, mock_stdout: _io.StringIO, magick_mock: MagicMock):
|
||||
router = Router()
|
||||
flags = FlagsGroup(DefaultFlags.host_flag)
|
||||
flags = FlagsGroup(DefaultFlags.HOST)
|
||||
|
||||
@router.command(Command('test', flags=flags))
|
||||
def test(args: dict):
|
||||
@@ -101,7 +101,7 @@ class TestSystemHandlerNormalWork(unittest.TestCase):
|
||||
|
||||
output = mock_stdout.getvalue()
|
||||
|
||||
self.assertIn('\nUndefined or incorrect input flag: --port 132\n', output)
|
||||
self.assertIn('\nUndefined or incorrect input registered_flag: --port 132\n', output)
|
||||
|
||||
|
||||
@patch("builtins.input", side_effect=["test", "some", "q"])
|
||||
@@ -159,7 +159,7 @@ class TestSystemHandlerNormalWork(unittest.TestCase):
|
||||
|
||||
output = mock_stdout.getvalue()
|
||||
|
||||
self.assertIn("\nIncorrect flag syntax: \"test 535 --port\"\n", output)
|
||||
self.assertIn("\nIncorrect registered_flag syntax: \"test 535 --port\"\n", output)
|
||||
|
||||
|
||||
@patch("builtins.input", side_effect=["", "q"])
|
||||
@@ -185,7 +185,7 @@ class TestSystemHandlerNormalWork(unittest.TestCase):
|
||||
def test_input_correct_command_with_repeated_flags(self, mock_stdout: _io.StringIO, magick_mock: MagicMock):
|
||||
router = Router()
|
||||
|
||||
@router.command(Command('test', flags=DefaultFlags.port_flag))
|
||||
@router.command(Command('test', flags=DefaultFlags.PORT))
|
||||
def test(args):
|
||||
print('test command')
|
||||
|
||||
|
||||
@@ -7,8 +7,8 @@ import re
|
||||
from argenta.app import App
|
||||
from argenta.command import Command
|
||||
from argenta.router import Router
|
||||
from argenta.command.flag import Flag, FlagsGroup
|
||||
from argenta.command.flag.defaults import DefaultFlags
|
||||
from argenta.command.flag.registered_flag import Flag, FlagsGroup
|
||||
from argenta.command.flag.registered_flag.defaults import DefaultFlags
|
||||
|
||||
|
||||
|
||||
@@ -57,7 +57,7 @@ class TestSystemHandlerNormalWork(unittest.TestCase):
|
||||
|
||||
@router.command(Command('test', flags=flag))
|
||||
def test(args: dict):
|
||||
print(f'\nhelp for {args['help']['name']} flag\n')
|
||||
print(f'\nhelp for {args['help']['name']} registered_flag\n')
|
||||
|
||||
app = App()
|
||||
app.include_router(router)
|
||||
@@ -65,7 +65,7 @@ class TestSystemHandlerNormalWork(unittest.TestCase):
|
||||
|
||||
output = mock_stdout.getvalue()
|
||||
|
||||
self.assertIn('\nhelp for help flag\n', output)
|
||||
self.assertIn('\nhelp for help registered_flag\n', output)
|
||||
|
||||
@patch("builtins.input", side_effect=["test --port 22", "q"])
|
||||
@patch("sys.stdout", new_callable=io.StringIO)
|
||||
@@ -75,7 +75,7 @@ class TestSystemHandlerNormalWork(unittest.TestCase):
|
||||
|
||||
@router.command(Command('test', flags=flag))
|
||||
def test(args: dict):
|
||||
print(f'flag value for {args['port']['name']} flag : {args["port"]["value"]}')
|
||||
print(f'registered_flag value for {args['port']['name']} registered_flag : {args["port"]["value"]}')
|
||||
|
||||
app = App()
|
||||
app.include_router(router)
|
||||
@@ -83,18 +83,18 @@ class TestSystemHandlerNormalWork(unittest.TestCase):
|
||||
|
||||
output = mock_stdout.getvalue()
|
||||
|
||||
self.assertIn('\nflag value for port flag : 22\n', output)
|
||||
self.assertIn('\nregistered_flag value for port registered_flag : 22\n', output)
|
||||
|
||||
|
||||
@patch("builtins.input", side_effect=["test -h", "q"])
|
||||
@patch("sys.stdout", new_callable=io.StringIO)
|
||||
def test_input_correct_command_with_default_flag(self, mock_stdout: _io.StringIO, magick_mock: MagicMock):
|
||||
router = Router()
|
||||
flag = DefaultFlags.short_help_flag
|
||||
flag = DefaultFlags.SHORT_HELP
|
||||
|
||||
@router.command(Command('test', flags=flag))
|
||||
def test(args: dict):
|
||||
print(f'help for {args['h']['name']} flag')
|
||||
print(f'help for {args['h']['name']} registered_flag')
|
||||
|
||||
app = App()
|
||||
app.include_router(router)
|
||||
@@ -102,14 +102,14 @@ class TestSystemHandlerNormalWork(unittest.TestCase):
|
||||
|
||||
output = mock_stdout.getvalue()
|
||||
|
||||
self.assertIn('\nhelp for h flag\n', output)
|
||||
self.assertIn('\nhelp for h registered_flag\n', output)
|
||||
|
||||
|
||||
@patch("builtins.input", side_effect=["test --info", "q"])
|
||||
@patch("sys.stdout", new_callable=io.StringIO)
|
||||
def test_input_correct_command_with_default_flag2(self, mock_stdout: _io.StringIO, magick_mock: MagicMock):
|
||||
router = Router()
|
||||
flag = DefaultFlags.info_flag
|
||||
flag = DefaultFlags.INFO
|
||||
|
||||
@router.command(Command('test', flags=flag))
|
||||
def test(args: dict):
|
||||
@@ -129,7 +129,7 @@ class TestSystemHandlerNormalWork(unittest.TestCase):
|
||||
@patch("sys.stdout", new_callable=io.StringIO)
|
||||
def test_input_correct_command_with_default_flag3(self, mock_stdout: _io.StringIO, magick_mock: MagicMock):
|
||||
router = Router()
|
||||
flag = DefaultFlags.host_flag
|
||||
flag = DefaultFlags.HOST
|
||||
|
||||
@router.command(Command('test', flags=flag))
|
||||
def test(args: dict):
|
||||
@@ -148,7 +148,7 @@ class TestSystemHandlerNormalWork(unittest.TestCase):
|
||||
@patch("sys.stdout", new_callable=io.StringIO)
|
||||
def test_input_correct_command_with_two_flags(self, mock_stdout: _io.StringIO, magick_mock: MagicMock):
|
||||
router = Router()
|
||||
flags = FlagsGroup(DefaultFlags.host_flag, DefaultFlags.port_flag)
|
||||
flags = FlagsGroup(DefaultFlags.HOST, DefaultFlags.PORT)
|
||||
|
||||
@router.command(Command('test', flags=flags))
|
||||
def test(args: dict):
|
||||
@@ -173,7 +173,7 @@ class TestSystemHandlerNormalWork(unittest.TestCase):
|
||||
print(f'test command')
|
||||
|
||||
@router.command(Command('some'))
|
||||
def test():
|
||||
def test2():
|
||||
print(f'some command')
|
||||
|
||||
app = App()
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
from argenta.app import App
|
||||
from argenta.app.exceptions import (InvalidDescriptionMessagePatternException,
|
||||
NoRegisteredRoutersException)
|
||||
from argenta.app.exceptions import InvalidDescriptionMessagePatternException
|
||||
|
||||
import unittest
|
||||
|
||||
@@ -14,7 +13,3 @@ class TestApp(unittest.TestCase):
|
||||
with self.assertRaises(InvalidDescriptionMessagePatternException):
|
||||
App().set_description_message_pattern('Invalid {desription} description {comand} pattern')
|
||||
|
||||
def test_no_registered_router(self):
|
||||
with self.assertRaises(NoRegisteredRoutersException):
|
||||
App()._validate_number_of_routers()
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from argenta.command.flag import Flag
|
||||
from argenta.command.flag.registered_flag import Flag
|
||||
|
||||
import unittest
|
||||
import re
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from argenta.command.flag import Flag, FlagsGroup
|
||||
from argenta.command.flag.registered_flag import Flag, FlagsGroup
|
||||
|
||||
import unittest
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
from argenta.command.flag import FlagsGroup, Flag
|
||||
from argenta.router import Router
|
||||
from argenta.command import Command
|
||||
from argenta.router.exceptions import RepeatedCommandException, TriggerCannotContainSpacesException
|
||||
from argenta.router.exceptions import TriggerCannotContainSpacesException
|
||||
|
||||
import unittest
|
||||
|
||||
@@ -13,102 +12,6 @@ class TestRouter(unittest.TestCase):
|
||||
def test_get_router_title(self):
|
||||
self.assertEqual(Router(title='test title').get_title(), 'test title')
|
||||
|
||||
def test_input_correct_command(self):
|
||||
router = Router()
|
||||
@router.command(Command(trigger='test'))
|
||||
def test():
|
||||
return 'correct result'
|
||||
|
||||
self.assertEqual(router.input_command_handler(Command(trigger='test')), 'correct result')
|
||||
|
||||
def test_input_command_with_invalid_flag(self):
|
||||
router = Router()
|
||||
router.set_invalid_input_flag_handler(lambda x: x)
|
||||
|
||||
@router.command(Command(trigger='test'))
|
||||
def test():
|
||||
return 'correct result'
|
||||
|
||||
input_command = Command(trigger='test')
|
||||
input_command._set_input_flags(FlagsGroup([Flag('host')]))
|
||||
|
||||
self.assertEqual(router.input_command_handler(input_command), None)
|
||||
|
||||
def test_input_correct_command_with_one_register_and_ignore_command_register(self):
|
||||
router = Router()
|
||||
router.set_ignore_command_register(True)
|
||||
@router.command(Command(trigger='test'))
|
||||
def test():
|
||||
return 'correct result'
|
||||
|
||||
self.assertEqual(router.input_command_handler(Command(trigger='test')), 'correct result')
|
||||
|
||||
def test_input_correct_command_with_different_register_and_ignore_command_register(self):
|
||||
router = Router()
|
||||
router.set_ignore_command_register(True)
|
||||
@router.command(Command(trigger='test'))
|
||||
def test():
|
||||
return 'correct result'
|
||||
|
||||
self.assertEqual(router.input_command_handler(Command(trigger='TeSt')), 'correct result')
|
||||
|
||||
def test_input_incorrect_command_with_ignore_command_register(self):
|
||||
router = Router()
|
||||
router.set_ignore_command_register(True)
|
||||
@router.command(Command(trigger='test'))
|
||||
def test():
|
||||
return 'correct result'
|
||||
|
||||
self.assertEqual(router.input_command_handler(Command(trigger='Test2')), None)
|
||||
|
||||
def test_register_repeated_commands_with_one_register(self):
|
||||
router = Router()
|
||||
@router.command(Command(trigger='test'))
|
||||
def test():
|
||||
return 'correct result'
|
||||
|
||||
with self.assertRaises(RepeatedCommandException):
|
||||
@router.command(Command(trigger='test'))
|
||||
def test():
|
||||
return 'correct result'
|
||||
|
||||
def test_register_commands_with_different_register(self):
|
||||
router = Router()
|
||||
@router.command(Command(trigger='test'))
|
||||
def test():
|
||||
return 'correct result'
|
||||
|
||||
try:
|
||||
@router.command(Command(trigger='Test'))
|
||||
def test():
|
||||
return 'correct result'
|
||||
except RepeatedCommandException:
|
||||
self.fail('RepeatedCommandException should not have been thrown')
|
||||
|
||||
def test_register_repeated_commands_with_one_register_and_set_ignore_command_register(self):
|
||||
router = Router()
|
||||
router.set_ignore_command_register(True)
|
||||
@router.command(Command(trigger='test'))
|
||||
def test():
|
||||
return 'correct result'
|
||||
|
||||
with self.assertRaises(RepeatedCommandException):
|
||||
@router.command(Command(trigger='test'))
|
||||
def test():
|
||||
return 'correct result'
|
||||
|
||||
def test_register_repeated_commands_with_different_register_and_set_ignore_command_register(self):
|
||||
router = Router()
|
||||
router.set_ignore_command_register(True)
|
||||
@router.command(Command(trigger='test'))
|
||||
def test():
|
||||
return 'correct result'
|
||||
|
||||
with self.assertRaises(RepeatedCommandException):
|
||||
@router.command(Command(trigger='Test'))
|
||||
def test():
|
||||
return 'correct result'
|
||||
|
||||
def test_register_command_with_spaces_in_trigger(self):
|
||||
router = Router()
|
||||
with self.assertRaises(TriggerCannotContainSpacesException):
|
||||
|
||||
Reference in New Issue
Block a user