aboutsummaryrefslogblamecommitdiffstats
path: root/test/test_wlp.py
blob: 9825e446f3c7281e9386c44d13df5613902cca67 (plain) (tree)
1
2
3
4
5
6
7
8
9

                       

                                        

               

               
 
                      


                                 








































                                                               
                              















                                                                                

                                                       
                                             

                                













                                                           

                          
#!/usr/bin/python
# -*- coding: utf-8 -*-
import logging
logging.basicConfig(level=logging.DEBUG)
import unittest
import wlp
import wlp_lex
import wlp_yacc

from rply import Token


class TestWLP(unittest.TestCase):
    maxDiff = None
    test_input = '''<kame@innocent.com> {
                From: = 'ME' Sender: = "Cosimo" Reply-to = "me"
            }

            <alfarano@students.cs.unibo.it> {
                From: = 'Cosimo Alfarano'
                X-Firstname: = 'Cosimo'
            }

            <kame@innocent.com> {
                From: = 'kame@inwind.it'
                Reply-to: = "KA"
                Sender: = "Kalfa"
            }'''

    def test_wlp_lexer(self):
        expected_stream = [
            Token('OWNER', '<kame@innocent.com>'),
            Token('VAR', 'From:'),
            Token('VAL', "'ME'"),
            Token('VAR', 'Sender:'),
            Token('VAL', '"Cosimo"'),
            Token('VAR', 'Reply-to'),
            Token('VAL', '"me"'),
            Token('OWNER', '<alfarano@students.cs.unibo.it>'),
            Token('VAR', 'From:'),
            Token('VAL', "'Cosimo Alfarano'"),
            Token('VAR', 'X-Firstname:'),
            Token('VAL', "'Cosimo'"),
            Token('OWNER', '<kame@innocent.com>'),
            Token('VAR', 'From:'),
            Token('VAL', "'kame@inwind.it'"),
            Token('VAR', 'Reply-to:'),
            Token('VAL', '"KA"'),
            Token('VAR', 'Sender:'),
            Token('VAL', '"Kalfa"')
        ]
        tokens = list(wlp_lex.lexer.lex(self.test_input))
        self.assertEqual(tokens, expected_stream)

    def test_wlp_parser(self):
        expected_tree = [
           [[Token('OWNER', '<kame@innocent.com>'),
               [[Token('VAR', 'From:'),
                 Token('VAL', "'ME'"),
                 [Token('VAR', 'Sender:'), Token('VAL', '"Cosimo"')],
                 [Token('VAR', 'Reply-to'), Token('VAL', '"me"')]]]],
              [[Token('OWNER', '<alfarano@students.cs.unibo.it>'),
                [[Token('VAR', 'From:'),
                  Token('VAL', "'Cosimo Alfarano'"),
                  [Token('VAR', 'X-Firstname:'), Token('VAL', "'Cosimo'")]]]]]],
             [[Token('OWNER', '<kame@innocent.com>'),
               [[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)
        self.assertEqual(tree, expected_tree)

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

if __name__ == "__main__":
    unittest.main()