John Chandler avatar John Chandler committed 62acec4

Added test and code for adding dependencies

Comments (0)

Files changed (3)

qadpid/hierarchy.py

         file_object = open( filepath, "r" )
         file_text = file_object.read()
         file_object.close()
-        return file_object
+        return file_text
     
     except IOError:
         # It's Quick and Dirty, if we can't open a file we don't care at the moment
         """ Takes a (dirpath, dirs, files) tuple (e.g. as returned by os.walk)
             Adds .php files to the hierarchy
         """
-        dirpath, _, files = directory
-        for filepath in [ os.path.join( dirpath, x ) for x in files if x.endswith(".php") ]:    
-            self.hierarchy[ filepath ] = self.parser.parse( self.reader(filepath) )
+        dirpath, _, files = directory        
+        for filepath in [ os.path.join( dirpath, x ) for x in files if x.endswith(".php") ]:
+            dependencies = []
+            for dep_type, dep_filepath in self.parser.parse( self.reader(filepath) ):
+                dependencies.append( ( dep_type, os.path.join(dirpath, dep_filepath) ) )
+            self.hierarchy[ filepath ] = dependencies

tests/test_hierarchy.py

 import unittest
 
 from qadpid.hierarchy import PHPHierarchyCreator
-
-
-class FakeParser( object ):
-    def parse( self, _ ):
-        return None
+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 add a dir with PHP files """
         self.creator.add( (".", [], ["foo.php", "bar.php"]) )
         
-        self.assertEquals( self.creator.hierarchy, { "./foo.php": None, "./bar.php": None } )
+        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": None, "./wibble/bar.php": None } )
+        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": []
+                                                   } )
+        
 
 
 if __name__ == "__main__":

tests/test_parser.py

         '''
         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.