Source

qadpid / tests / test_hierarchy.py

Full commit
"""
File hierarchy tests

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

import unittest

from qadpid.hierarchy import PHPHierarchyCreator
from qadpid.parser    import PHPFileParser


class TestHierarchyCreator( unittest.TestCase ):
    """ Tests the HierarchyCreator """
    def setUp( self ):
        class FakeParser( object ):
            def parse( self, _ ):
                return []

        self.creator = PHPHierarchyCreator( parser=FakeParser() )
    
    def test_no_hierarchy( self ):
        """ Tests result when we have nothing to do """
        self.assertEquals( self.creator.hierarchy, {} )

    def test_no_php( self ):
        """ Tests result when we add a dir with no PHP files """
        self.creator.add( (".", [], ["foo.js", "bar.py"]) )
        
        self.assertEquals( self.creator.hierarchy, {} )

    def test_php( self ):
        """ Tests result when we add a dir with PHP files """
        self.creator.add( (".", [], ["foo.php", "bar.php"]) )
        
        self.assertEquals( self.creator.hierarchy, { "./foo.php": [], "./bar.php": [] } )

    def test_subdir( self ):
        """ Tests result when we add a sub dir with PHP files """
        self.creator.add( ("./wibble", [], ["foo.php", "bar.php"]) )
        
        self.assertEquals( self.creator.hierarchy, { "./wibble/foo.php": [], "./wibble/bar.php": [] } )


class TestHierarchyDependencies( unittest.TestCase ):
    """ Tests the HierarchyCreator with dependencies """
    def setUp( self ):
        self.creator = PHPHierarchyCreator( parser=PHPFileParser() )

    def test_hierarchy_dependencies( self ):
        """ Test setting of dependencies via parser """
        def fake_reader( filepath ):
            """ Simulates text in the foo.php file """
            if filepath == "./foo.php":
                return 'some php code\ninclude "bar.php"\nmore php\n'
            return ""
        
        self.creator.reader = fake_reader  # replace our reader for the test
        self.creator.add( (".", [], ["foo.php", "bar.php"]) )

        self.assertEquals( self.creator.hierarchy, { "./foo.php": [ ("include", "bar.php") ],
                                                     "./bar.php": []
                                                   } )
        
    def test_parent_dir( self ):
        """ Tests when we have a parent dir reference """
        def fake_reader( filepath ):
            """ Simulates text in the foo.php file """
            if filepath == "./wibble/foo.php":
                return 'some php code\ninclude "../bar.php"\nmore php\n'
            return ""
        
        self.creator.reader = fake_reader  # replace our reader for the test
        self.creator.add( (".",        [], ["bar.php"]) )
        self.creator.add( ("./wibble", [], ["foo.php"]) )

        self.assertEquals( self.creator.hierarchy.get("./wibble/foo.php"), [("include", "bar.php")] )


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