From 19f7ba38b30f8a6bce4f20a00a6ec028fa055a9e Mon Sep 17 00:00:00 2001 From: James Lu Date: Tue, 10 Sep 2019 18:04:05 -0700 Subject: [PATCH] Begin integrating ircdocs/parser-tests --- .gitmodules | 3 +++ test/parser-tests | 1 + test/test_irc_parsers.py | 58 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 .gitmodules create mode 160000 test/parser-tests create mode 100644 test/test_irc_parsers.py diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..4c88690 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "test/parser_tests"] + path = test/parser-tests + url = https://github.com/ircdocs/parser-tests diff --git a/test/parser-tests b/test/parser-tests new file mode 160000 index 0000000..1eea408 --- /dev/null +++ b/test/parser-tests @@ -0,0 +1 @@ +Subproject commit 1eea408666145cd4ef08f255523350ebef8f063f diff --git a/test/test_irc_parsers.py b/test/test_irc_parsers.py new file mode 100644 index 0000000..04d71fd --- /dev/null +++ b/test/test_irc_parsers.py @@ -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()