Source

qadpid / tests / test_parser.py

Full commit
John Chandler 95dc222 

John Chandler 1649efb 


John Chandler 95dc222 

John Chandler 1649efb 
John Chandler 95dc222 
John Chandler fe57b9d 

John Chandler 95dc222 
John Chandler 1649efb 
John Chandler 95dc222 
John Chandler f80087d 


John Chandler fe57b9d 

John Chandler f80087d 
John Chandler fe57b9d 
John Chandler ebb2163 

John Chandler f80087d 
John Chandler 33194c9 
John Chandler ebb2163 

John Chandler f80087d 
John Chandler 33194c9 
John Chandler ebb2163 







John Chandler 33194c9 

John Chandler f80087d 
John Chandler 33194c9 


John Chandler f80087d 
John Chandler 33194c9 
John Chandler a78ab6d 







John Chandler 46c62d1 













































John Chandler 62acec4 



John Chandler 8f7f980 
John Chandler 95dc222 
John Chandler 1649efb 
John Chandler 95dc222 
"""
File parser tests

Copyright 2011 John Chandler
This software is distributed under the terms of the GNU General Public License version 3
"""

import unittest

from qadpid.parser import PHPFileParser


class TestPHPParser( unittest.TestCase ):
    """ Tests for the PHP file parser """
    def setUp( self ):
        self.parser = PHPFileParser()
    
    def test_parse_nothing( self ):
        """ Test for empty file contents """
        self.assertEquals( self.parser.parse(""), [] )

    def test_parse_one_include_double_quotes( self ):
        """ Test for presence of one include statement - double quotes """
        self.assertEquals( self.parser.parse('include "foo.php"'), [ ("include", "foo.php")] )

    def test_parse_one_require_double_quotes( self ):
        """ Test for presence of one require statement - double quotes """
        self.assertEquals( self.parser.parse('require "foo.php"'), [ ("require", "foo.php")] )

    def test_parse_one_include_single_quotes( self ):
        """ Test for presence of one include statement - single quotes """
        self.assertEquals( self.parser.parse("include 'foo.php'"), [ ("include", "foo.php")] )

    def test_parse_one_require_single_quotes( self ):
        """ Test for presence of one require statement - single quotes """
        self.assertEquals( self.parser.parse("require 'foo.php'"), [ ("require", "foo.php")] )

    def test_parse_one_include_once( self ):
        """ Test for presence of one include_once statement """
        self.assertEquals( self.parser.parse('include_once "foo.php"'), [ ("include", "foo.php")] )

    def test_parse_one_require_once( self ):
        """ Test for presence of one require_once statement """
        self.assertEquals( self.parser.parse('require_once "foo.php"'), [ ("require", "foo.php")] )

    def test_parse_include_brackets( self ):
        """ Test for presence of include with brackets """
        self.assertEquals( self.parser.parse("include_once('includes/foo.php');"), [ ("include", "includes/foo.php") ] )

    def test_parse_require_brackets( self ):
        """ Test for presence of require with brackets """
        self.assertEquals( self.parser.parse("require_once('includes/foo.php');"), [ ("require", "includes/foo.php") ] )

    def test_parse_multiple_includes( self ):
        """ Test for presence of multiple include statements """
        php_code = '''
        include "foo.php"
        include "bar.php"
        include "baz.php"
        '''
        self.assertEquals( self.parser.parse(php_code), [ ("include", "foo.php"), ("include", "bar.php"), ("include", "baz.php") ] )

    def test_parse_multiple_requires( self ):
        """ Test for presence of multiple require statements """
        php_code = '''
        require "foo.php"
        require "bar.php"
        require "baz.php"
        '''
        self.assertEquals( self.parser.parse(php_code), [ ("require", "foo.php"), ("require", "bar.php"), ("require", "baz.php") ] )

    def test_parse_multiple_include_onces( self ):
        """ Test for presence of multiple include_once statements """
        php_code = '''
        include_once "foo.php"
        include_once "bar.php"
        include_once "baz.php"
        '''
        self.assertEquals( self.parser.parse(php_code), [ ("include", "foo.php"), ("include", "bar.php"), ("include", "baz.php") ] )

    def test_parse_multiple_require_onces( self ):
        """ Test for presence of multiple require_once statements """
        php_code = '''
        require_once "foo.php"
        require_once "bar.php"
        require_once "baz.php"
        '''
        self.assertEquals( self.parser.parse(php_code), [ ("require", "foo.php"), ("require", "bar.php"), ("require", "baz.php") ] )

    def test_mixed_includes_and_requires( self ):
        """ Test for presence of multiple includes and requires """
        php_code = '''
        include_once "foo.php"
        require "bar.php"
        include "baz.php"
        require_once "qux.php"
        '''
        self.assertEquals( self.parser.parse(php_code), [ ("include", "foo.php"), ("require", "bar.php"), ("include", "baz.php"), ("require", "qux.php") ] )

    def test_all_in_one( self ):
        """ Test for all-in-one string - intended for debugging something, but useful to add more confidence """
        php_code = 'some php code\ninclude "bar.php"\nmore php\n'
        self.assertEquals( self.parser.parse(php_code), [ ("include", "bar.php") ] )


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