qadpid / tests / test_parser.py

"""
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_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()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.