From d17b009ffec3077bf8db6e6902a7456ec90e9c38 Mon Sep 17 00:00:00 2001 From: Matěj Cepl Date: Mon, 5 Jan 2015 09:03:05 +0100 Subject: First draft of the pure Python parser done, we should be noarch. Fixes #2 --- test/__init__.py | 13 -------- test/test_pyg.py | 13 +++----- test/test_wlp.py | 93 +++++++++++++++++++++++++++++++++++--------------------- 3 files changed, 63 insertions(+), 56 deletions(-) (limited to 'test') diff --git a/test/__init__.py b/test/__init__.py index 55f45b6..e69de29 100644 --- a/test/__init__.py +++ b/test/__init__.py @@ -1,13 +0,0 @@ -import os.path -import sys -import sysconfig - - -def distutils_dir_name(dname): - """Returns the name of a distutils build directory""" - f = "{dirname}.{platform}-{version[0]}.{version[1]}" - return f.format(dirname=dname, - platform=sysconfig.get_platform(), - version=sys.version_info) -wlp_lib_path = os.path.join('build', distutils_dir_name('lib')) -sys.path.insert(0, wlp_lib_path) diff --git a/test/test_pyg.py b/test/test_pyg.py index 240228b..4c1c1c0 100755 --- a/test/test_pyg.py +++ b/test/test_pyg.py @@ -1,15 +1,12 @@ #!/usr/bin/python # -*- coding: utf-8 -*- -import mail2news -import os -import os.path import re import subprocess -import sys -import test import unittest +import mail2news + class TestM2N(unittest.TestCase): expected_output = """Newsgroups: pyg.test @@ -38,6 +35,7 @@ one line test self.assertEqual(out, self.expected_output) +# @unittest.skip('not ready') class TestN2M(unittest.TestCase): expected_output = """Received: from GATEWAY by mitmanek.ceplovi.cz with pyg for ; Mon Dec 15 17:13:30 2014 (CEST) @@ -59,15 +57,12 @@ Resent-Sender: sender@example.com """ % (mail2news.VERSION, mail2news.DESC) def test_n2m(self): - env = os.environ - env['PYTHONPATH'] = ":".join(sys.path) - with open('examples/articletest.accepted') as in_mail: pid = subprocess.Popen(['./pygn2m', '-Tvt', 'test@example.com', '-s', 'sender@example.com', '-w', 'examples/whitelist.example'], stdin=subprocess.PIPE, - stdout=subprocess.PIPE, env=env) + stdout=subprocess.PIPE) in_message = in_mail.read().replace('pyg@pyg.server.tld', 'kame@inwind.it') out, err = pid.communicate(in_message) diff --git a/test/test_wlp.py b/test/test_wlp.py index 9825e44..430c235 100755 --- a/test/test_wlp.py +++ b/test/test_wlp.py @@ -1,13 +1,27 @@ #!/usr/bin/python # -*- coding: utf-8 -*- -import logging -logging.basicConfig(level=logging.DEBUG) import unittest -import wlp -import wlp_lex -import wlp_yacc +import wlp_parser from rply import Token +import whitelist +import wlp + + +# logging.basicConfig(level=logging.DEBUG) + +EXPECTED_WL = { + 'alfarano@students.cs.unibo.it': { + 'From:': 'Cosimo Alfarano', + 'X-Firstname:': 'Cosimo' + }, + 'kame@innocent.com': { + 'From:': 'kame@inwind.it', + 'Reply-to': 'me', + 'Reply-to:': 'KA', + 'Sender:': 'Kalfa' + } +} class TestWLP(unittest.TestCase): @@ -49,44 +63,55 @@ class TestWLP(unittest.TestCase): Token('VAR', 'Sender:'), Token('VAL', '"Kalfa"') ] - tokens = list(wlp_lex.lexer.lex(self.test_input)) + tokens = list(wlp_parser.lexer.lex(self.test_input)) self.assertEqual(tokens, expected_stream) def test_wlp_parser(self): expected_tree = [ - [[Token('OWNER', ''), - [[Token('VAR', 'From:'), - Token('VAL', "'ME'"), - [Token('VAR', 'Sender:'), Token('VAL', '"Cosimo"')], - [Token('VAR', 'Reply-to'), Token('VAL', '"me"')]]]], - [[Token('OWNER', ''), - [[Token('VAR', 'From:'), - Token('VAL', "'Cosimo Alfarano'"), - [Token('VAR', 'X-Firstname:'), Token('VAL', "'Cosimo'")]]]]]], - [[Token('OWNER', ''), - [[Token('VAR', 'From:'), - Token('VAL', "'kame@inwind.it'"), - [Token('VAR', 'Reply-to:'), Token('VAL', '"KA"')], - [Token('VAR', 'Sender:'), Token('VAL', '"Kalfa"')]]]]] + [ + Token('OWNER', ''), + [ + [Token('VAR', 'From:'), Token('VAL', "'ME'")], + [Token('VAR', 'Sender:'), Token('VAL', '"Cosimo"')], + [Token('VAR', 'Reply-to'), Token('VAL', '"me"')] + ] + ], + [ + Token('OWNER', ''), + [ + [Token('VAR', 'From:'), Token('VAL', "'Cosimo Alfarano'")], + [Token('VAR', 'X-Firstname:'), Token('VAL', "'Cosimo'")] + ] + ], + [ + Token('OWNER', ''), + [ + [Token('VAR', 'From:'), Token('VAL', "'kame@inwind.it'")], + [Token('VAR', 'Reply-to:'), Token('VAL', '"KA"')], + [Token('VAR', 'Sender:'), Token('VAL', '"Kalfa"')] + ] + ] ] - lex_stream = wlp_lex.lexer.lex(self.test_input) - tree = wlp_yacc.parser.parse(lex_stream) + lex_stream = wlp_parser.lexer.lex(self.test_input) + tree = wlp_parser.parser.parse(lex_stream) self.assertEqual(tree, expected_tree) - def test_wlp_C_parser(self): + def test_wlp(self): wlp.setfilebyname('examples/whitelist.example') wl_dict = wlp.mkdict() - expected_dict = {'alfarano@students.cs.unibo.it': { - 'From:': 'Cosimo Alfarano', - 'X-Firstname:': 'Cosimo' - }, - 'kame@innocent.com': { - 'From:': 'kame@inwind.it', - 'Reply-to': 'me', - 'Reply-to:': 'KA', - 'Sender:': 'Kalfa'} - } - self.assertEqual(wl_dict, expected_dict) + self.assertEqual(wl_dict, EXPECTED_WL) + + +class TestWhitelist(unittest.TestCase): + def setUp(self): + self.wl = whitelist.whitelist('examples/whitelist.example') + + def test_init(self): + self.assertEqual(self.wl.wl, EXPECTED_WL) + + def test_checkfrom(self): + self.assertEqual(self.wl.checkfrom('kame@inwind.it'), + 'kame@innocent.com') if __name__ == "__main__": unittest.main() -- cgit