Рефакторинг парсера команд
This commit is contained in:
parent
dc4c035a53
commit
0b3e72109c
2 changed files with 74 additions and 8 deletions
|
@ -1,13 +1,25 @@
|
|||
import re
|
||||
|
||||
|
||||
class CommandParser:
|
||||
@staticmethod
|
||||
def parse(command_string):
|
||||
parsed_command = command_string.split('_')
|
||||
|
||||
if not command_string:
|
||||
raise ValueError("Empty command received!")
|
||||
|
||||
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]
|
||||
|
||||
match command:
|
||||
case 'GET':
|
||||
return {'command': command, 'value': value}
|
||||
case _:
|
||||
raise ValueError("Unknown command: "+command)
|
||||
else:
|
||||
raise ValueError("Unknown command: " + command_string)
|
||||
|
|
54
tests/test_command_parser.py
Normal file
54
tests/test_command_parser.py
Normal file
|
@ -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()
|
Loading…
Reference in a new issue