3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-11-27 04:59:24 +01:00

Begin integrating ircdocs/parser-tests

This commit is contained in:
James Lu 2019-09-10 18:04:05 -07:00
parent c1859b64fa
commit 19f7ba38b3
3 changed files with 62 additions and 0 deletions

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "test/parser_tests"]
path = test/parser-tests
url = https://github.com/ircdocs/parser-tests

1
test/parser-tests Submodule

@ -0,0 +1 @@
Subproject commit 1eea408666145cd4ef08f255523350ebef8f063f

58
test/test_irc_parsers.py Normal file
View File

@ -0,0 +1,58 @@
"""
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)
'''
spec = importlib.util.spec_from_file_location("parser_tests", PARSER_DATA_PATH / 'data.py')
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
print(parser_tests)
'''
from pylinkirc.protocols.ircs2s_common import IRCCommonProtocol
class MessageParserTest(unittest.TestCase):
def testMessageSplit(self):
with open(PARSER_DATA_PATH / 'msg-split.yaml') as f:
splittest_data = yaml.safe_load(f)
for testdata in splittest_data['tests']:
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:
# HACK: in PyLink, message tags are processed in handle_events(), which is a dynamic
# method that relies on command handlers being present. So we can't reasonably test
# them here (plus handle_events() outputs params as a command-specific dict instead of)
# lists)
self.assertEqual(atoms['tags'], IRCCommonProtocol.parse_message_tags(inp.split(" ")))
_, inp = inp.split(" ", 1)
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)
if __name__ == '__main__':
unittest.main()