3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-11-01 17:29:21 +01:00
PyLink/test/test_irc_parsers.py

78 lines
2.7 KiB
Python
Raw Normal View History

2019-09-11 03:04:05 +02:00
"""
Tests for IRC parsers.
"""
from pathlib import Path
import unittest
import yaml
PARSER_DATA_PATH = Path(__file__).parent.resolve() / 'parser-tests' / 'tests'
print(PARSER_DATA_PATH)
2019-09-11 03:42:40 +02:00
from pylinkirc import utils
2019-09-11 03:04:05 +02:00
from pylinkirc.protocols.ircs2s_common import IRCCommonProtocol
class MessageParserTest(unittest.TestCase):
2019-09-11 03:42:40 +02:00
@classmethod
def setUpClass(cls):
2019-09-11 03:04:05 +02:00
with open(PARSER_DATA_PATH / 'msg-split.yaml') as f:
2019-09-11 03:42:40 +02:00
cls.MESSAGE_SPLIT_TEST_DATA = yaml.safe_load(f)
with open(PARSER_DATA_PATH / 'userhost-split.yaml') as f:
cls.USER_HOST_SPLIT_TEST_DATA = yaml.safe_load(f)
2019-09-11 03:04:05 +02:00
2019-09-11 03:42:40 +02:00
def testMessageSplit(self):
for testdata in self.MESSAGE_SPLIT_TEST_DATA['tests']:
2019-09-11 03:04:05 +02:00
inp = testdata['input']
atoms = testdata['atoms']
with self.subTest():
expected = []
has_source = False
if 'source' in atoms:
has_source = True
expected.append(atoms['source'])
if 'verb' in atoms:
expected.append(atoms['verb'])
if 'params' in atoms:
expected.extend(atoms['params'])
if 'tags' in atoms:
2019-09-11 03:42:40 +02:00
# Remove message tags before parsing
2019-09-11 03:04:05 +02:00
_, inp = inp.split(" ", 1)
2019-09-11 03:42:40 +02:00
2019-09-11 03:04:05 +02:00
if has_source:
parts = IRCCommonProtocol.parse_prefixed_args(inp)
else:
parts = IRCCommonProtocol.parse_args(inp)
self.assertEqual(expected, parts, "Parse test failed for string: %r" % inp)
2019-09-11 03:42:40 +02:00
@unittest.skip("Not quite working yet")
def testMessageTags(self):
for testdata in self.MESSAGE_SPLIT_TEST_DATA['tests']:
inp = testdata['input']
atoms = testdata['atoms']
with self.subTest():
if 'tags' in atoms:
self.assertEqual(atoms['tags'], IRCCommonProtocol.parse_message_tags(inp.split(" ")),
"Parse test failed for message tags: %r" % inp)
def testUserHostSplit(self):
for test in self.USER_HOST_SPLIT_TEST_DATA['tests']:
inp = test['source']
atoms = test['atoms']
with self.subTest():
if 'nick' not in atoms or 'user' not in atoms or 'host' not in atoms:
# Trying to parse a hostmask with missing atoms is an error in split_hostmask()
with self.assertRaises(ValueError):
utils.split_hostmask(inp)
else:
expected = [atoms['nick'], atoms['user'], atoms['host']]
self.assertEqual(expected, utils.split_hostmask(inp))
2019-09-11 03:42:40 +02:00
2019-09-11 03:04:05 +02:00
if __name__ == '__main__':
unittest.main()