diff --git a/argenta/app/entity.py b/argenta/app/entity.py index d3e2dac..9bc4846 100644 --- a/argenta/app/entity.py +++ b/argenta/app/entity.py @@ -130,14 +130,13 @@ class App: def set_description_message_pattern(self, pattern: str) -> None: - first_check = re.match(r'.*{commmand}.*', pattern) + first_check = re.match(r'.*{command}.*', pattern) second_check = re.match(r'.*{description}.*', pattern) - print(first_check) - print(second_check) + if bool(first_check) and bool(second_check): - raise InvalidDescriptionMessagePatternException(pattern) - else: self._description_message_pattern: str = pattern + else: + raise InvalidDescriptionMessagePatternException(pattern) def set_invalid_input_flags_handler(self, handler: Callable[[str], None]) -> None: diff --git a/argenta/command/entity.py b/argenta/command/entity.py index 3217c22..782b017 100644 --- a/argenta/command/entity.py +++ b/argenta/command/entity.py @@ -15,10 +15,10 @@ T = TypeVar('T') class Command(Generic[T]): def __init__(self, command: str, - description: str | None = None, + description: str = None, flags: Flag | FlagsGroup | None = None): self._command = command - self._description = description + self._description = f'description for "{self._command}" command' if not description else description self._registered_flags: FlagsGroup | None = flags if isinstance(flags, FlagsGroup) else FlagsGroup([flags]) if isinstance(flags, Flag) else flags self._input_flags: FlagsGroup | None = None @@ -29,11 +29,7 @@ class Command(Generic[T]): def get_description(self): - if not self._description: - description = f'description for "{self._command}" command' - return description - else: - return self._description + return self._description def get_registered_flags(self): diff --git a/argenta/command/params/flag/entity.py b/argenta/command/params/flag/entity.py index c3cba34..e3ca0e6 100644 --- a/argenta/command/params/flag/entity.py +++ b/argenta/command/params/flag/entity.py @@ -48,5 +48,4 @@ class Flag: return True else: return False - else: - return True + return True diff --git a/mock/local_test.py b/mock/local_test.py new file mode 100644 index 0000000..ab458ee --- /dev/null +++ b/mock/local_test.py @@ -0,0 +1,9 @@ +import re + + +def set_description_message_pattern(pattern: str) -> None: + first_check = re.match(r'.*command.*', pattern) + second_check = re.match(r'.*{description}.*', pattern) + + +set_description_message_pattern('Invalid des{ommand}cription pattern') \ No newline at end of file diff --git a/mock/mock_default_app/__init__.py b/mock/mock_default_app/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/mock/mock_default_app/handlers/__init__.py b/mock/mock_default_app/handlers/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/mock/mock_default_app/handlers/routers.py b/mock/mock_default_app/handlers/routers.py deleted file mode 100644 index 804b74d..0000000 --- a/mock/mock_default_app/handlers/routers.py +++ /dev/null @@ -1,29 +0,0 @@ -from pprint import pprint -from rich.console import Console -from argenta.router import Router - - -work_router: Router = Router(name='work') -settings_router: Router = Router(name='settings') - -console = Console() - - -@work_router.command(command='a') -def command_help(): - console.print('[bold red]command help [/bold red]') - - -@work_router.command(command='B', description='tester') -def command_start_solving(): - console.print('[bold red]command start [/bold red]') - - -@settings_router.command(command='b') -def command_settings(): - console.print('[bold red]command settings [/bold red]') - - -@work_router.unknown_command -def command_unknown_command(command): - console.print(f'[bold red]Unknown command: [/bold red]{command}') diff --git a/mock/mock_default_app/main.py b/mock/mock_default_app/main.py deleted file mode 100644 index f61b843..0000000 --- a/mock/mock_default_app/main.py +++ /dev/null @@ -1,17 +0,0 @@ -from pprint import pprint -from tests.mock_default_app.handlers.routers import work_router, settings_router -from argenta.app.entity import App - - -app: App = App(ignore_command_register=False, - line_separate='\n-------------------------------\n') - - -def main(): - app.include_router(work_router, is_main=True) - app.include_router(settings_router) - - app.start_polling() - -if __name__ == "__main__": - main() diff --git a/mock/test.py b/mock/test.py deleted file mode 100644 index 47c04fe..0000000 --- a/mock/test.py +++ /dev/null @@ -1,11 +0,0 @@ -import re - - -def set_description_message_pattern(pattern: str) -> None: - first_check = re.fullmatch(r'.*{commmand}.*', pattern) - second_check = re.fullmatch(r'.*{description}.*', pattern) - print(bool(first_check)) - print(second_check) - - -set_description_message_pattern('Invalid des{command}cription pattern') \ No newline at end of file diff --git a/tests/test_app.py b/tests/test_app.py index 29c52fb..7256026 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -8,7 +8,11 @@ import unittest class TestApp(unittest.TestCase): def test_set_invalid_description_message_pattern(self): with self.assertRaises(InvalidDescriptionMessagePatternException): - App().set_description_message_pattern('Invalid des{}cription pattern') + App().set_description_message_pattern('Invalid description pattern') + + def test_set_invalid_description_message_pattern2(self): + with self.assertRaises(InvalidDescriptionMessagePatternException): + App().set_description_message_pattern('Invalid {desription} description {comand} pattern') def test_no_registered_router(self): with self.assertRaises(NoRegisteredRoutersException): diff --git a/tests/test_flag.py b/tests/test_flag.py new file mode 100644 index 0000000..f996fa7 --- /dev/null +++ b/tests/test_flag.py @@ -0,0 +1,75 @@ +from argenta.command.params.flag import Flag + +import unittest +import re + + +class TestFlag(unittest.TestCase): + def test_get_string_entity(self): + self.assertEqual(Flag(flag_name='test').get_string_entity(), + '-test') + + def test_get_string_entity2(self): + self.assertEqual(Flag(flag_name='test', + flag_prefix='---').get_string_entity(), + '---test') + + def test_get_flag_name(self): + self.assertEqual(Flag(flag_name='test').get_flag_name(), + 'test') + + def test_get_flag_prefix(self): + self.assertEqual(Flag(flag_name='test').get_flag_prefix(), + '-') + + def test_get_flag_prefix2(self): + self.assertEqual(Flag(flag_name='test', + flag_prefix='--').get_flag_prefix(), + '--') + + def test_get_flag_value_without_set(self): + self.assertEqual(Flag(flag_name='test').get_value(), + None) + + def test_get_flag_value_with_set(self): + flag = Flag(flag_name='test') + flag.set_value('example') + self.assertEqual(flag.get_value(), 'example') + + def test_validate_incorrect_flag_value_with_list_of_possible_flag_values(self): + flag = Flag(flag_name='test', possible_flag_values=['1', '2', '3']) + self.assertEqual(flag.validate_input_flag_value('bad value'), False) + + def test_validate_correct_flag_value_with_list_of_possible_flag_values(self): + flag = Flag(flag_name='test', possible_flag_values=['1', '2', '3']) + self.assertEqual(flag.validate_input_flag_value('1'), True) + + def test_validate_incorrect_flag_value_with_pattern_of_possible_flag_values(self): + flag = Flag(flag_name='test', possible_flag_values=re.compile(r'192.168.\d+.\d+')) + self.assertEqual(flag.validate_input_flag_value('152.123.9.8'), False) + + def test_validate_correct_flag_value_with_pattern_of_possible_flag_values(self): + flag = Flag(flag_name='test', possible_flag_values=re.compile(r'192.168.\d+.\d+')) + self.assertEqual(flag.validate_input_flag_value('192.168.9.8'), True) + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/test_flagsgroup.py b/tests/test_flagsgroup.py new file mode 100644 index 0000000..9b634cf --- /dev/null +++ b/tests/test_flagsgroup.py @@ -0,0 +1,26 @@ +from argenta.command.params.flag import Flag, FlagsGroup + +import unittest + + +class TestFlagsGroup(unittest.TestCase): + def test_get_flags(self): + flags = FlagsGroup() + list_of_flags = [ + Flag('test1'), + Flag('test2'), + Flag('test3'), + ] + flags.add_flags(list_of_flags) + self.assertEqual(flags.get_flags(), + list_of_flags) + + def test_add_flag(self): + flags = FlagsGroup() + flags.add_flag(Flag('test')) + self.assertEqual(len(flags.get_flags()), 1) + + def test_add_flags(self): + flags = FlagsGroup() + flags.add_flags([Flag('test'), Flag('test2')]) + self.assertEqual(len(flags.get_flags()), 2) diff --git a/tests/test_router.py b/tests/test_router.py new file mode 100644 index 0000000..671bffc --- /dev/null +++ b/tests/test_router.py @@ -0,0 +1,34 @@ +from argenta.command.params.flag import FlagsGroup, Flag +from argenta.router import Router +from argenta.command import Command + +import unittest + + +class TestRouter(unittest.TestCase): + def test_get_router_name(self): + self.assertEqual(Router(name='test name').get_name(), 'test name') + + 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(command='test')) + def test(): + return 'correct result' + + self.assertEqual(router.input_command_handler(Command(command='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(command='test')) + def test(): + return 'correct result' + + input_command = Command(command='test') + input_command.set_input_flags(FlagsGroup([Flag('host')])) + + self.assertEqual(router.input_command_handler(input_command), None)