From 0b3e72109c0b858a9a65c64c9ed6424a76e105e3 Mon Sep 17 00:00:00 2001 From: Denis Fedoseev Date: Mon, 9 Oct 2023 01:10:37 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D1=84=D0=B0=D0=BA=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=B8=D0=BD=D0=B3=20=D0=BF=D0=B0=D1=80=D1=81=D0=B5=D1=80?= =?UTF-8?q?=D0=B0=20=D0=BA=D0=BE=D0=BC=D0=B0=D0=BD=D0=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- command_parser/__init__.py | 28 +++++++++++++------ tests/test_command_parser.py | 54 ++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 8 deletions(-) create mode 100644 tests/test_command_parser.py diff --git a/command_parser/__init__.py b/command_parser/__init__.py index e10f3b0..c1ef871 100644 --- a/command_parser/__init__.py +++ b/command_parser/__init__.py @@ -1,13 +1,25 @@ +import re + + class CommandParser: @staticmethod def parse(command_string): - parsed_command = command_string.split('_') - command = parsed_command[0] - value = parsed_command[1] + if not command_string: + raise ValueError("Empty command received!") - match command: - case 'GET': - return {'command': command, 'value': value} - case _: - raise ValueError("Unknown command: "+command) + if not isinstance(command_string, str): + raise ValueError("Command must be a sting!") + + if re.search('[^a-zA-Z0-9_\-]', command_string): + raise ValueError('Only [a-zA-Z0-9_\-] allowed') + + if re.search('^GET_.*', command_string): + parsed_command = command_string.split('_', 1) + + command = parsed_command[0] + value = parsed_command[1] + + return {'command': command, 'value': value} + else: + raise ValueError("Unknown command: " + command_string) diff --git a/tests/test_command_parser.py b/tests/test_command_parser.py new file mode 100644 index 0000000..ef4e09f --- /dev/null +++ b/tests/test_command_parser.py @@ -0,0 +1,54 @@ +import unittest +from command_parser import CommandParser + + +class TestCommandParser(unittest.TestCase): + def test_empty_line(self): + with self.assertRaises(ValueError) as context: + CommandParser.parse('') + + self.assertEqual('Empty command received!', str(context.exception)) + + def test_unknown_command(self): + with self.assertRaises(ValueError) as context: + CommandParser.parse('REALLY_UNKNOWN_COMMAND') + + self.assertEqual('Unknown command: REALLY_UNKNOWN_COMMAND', str(context.exception)) + + def test_parse_get_command(self): + result = CommandParser.parse('GET_A') + + self.assertEqual(result, {'command': 'GET', 'value': 'A'}) + + def test_parse_get_command_w_dual_underlines(self): + result = CommandParser.parse('GET_A_B') + + self.assertEqual(result, {'command': 'GET', 'value': 'A_B'}) + + def test_invalid_command(self): + with self.assertRaises(ValueError) as context: + CommandParser.parse('NOT_GET_A') + + self.assertEqual('Unknown command: NOT_GET_A', str(context.exception)) + + def test_invalid_command_string_type(self): + with self.assertRaises(ValueError) as context: + CommandParser.parse(b'some_cmd') + + self.assertEqual('Command must be a sting!', str(context.exception)) + + def test_not_allowed_symbols_in_command_string(self): + with self.assertRaises(ValueError) as context: + CommandParser.parse('GET_!') + + self.assertEqual('Only [a-zA-Z0-9_\-] allowed', str(context.exception)) + + def test_not_allowed_newline_in_command_string(self): + with self.assertRaises(ValueError) as context: + CommandParser.parse("GET_A\nGET_B") + + self.assertEqual('Only [a-zA-Z0-9_\-] allowed', str(context.exception)) + + +if __name__ == '__main__': + unittest.main()