serial-device/tests/test_command_parser.py

55 lines
1.8 KiB
Python
Raw Permalink Normal View History

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()