John Chandler avatar John Chandler committed 16db9b0

Added test and code to write labels

Comments (0)

Files changed (2)

 This software is distributed under the terms of the GNU General Public License version 3
 """
 
+def get_next_file_id():
+    """ Generator returning incrementing file IDs """
+    current_id = 0
+    while True:
+        current_id += 1
+        yield current_id
+        
+
 class DOTWriter( object ):
     """ DOT File Writer """
     def __init__( self, code_hierarchy ):
-        pass
+        self._code_hierarchy = code_hierarchy
 
     def write( self, file_object ):
         """ Write a DOT file to the file object
             Assumes we can start writing at the current location in the file (i.e. append)
         """
         file_object.write( "digraph code_hierarchy {\n" )
+
+        file_id = get_next_file_id()
+        for filename in self._code_hierarchy:
+            file_object.write( '  f%s [label="%s"]\n' % ( file_id.next(), filename ) )
+            
         file_object.write( "}\n" )
 

tests/test_dot_writer.py

 
 class TestDOTWriter( unittest.TestCase ):
     """ Tests for DOT writer """
+    def setUp( self ):
+        self.output_file = StringIO()
+
+    def tearDown( self ):
+        self.output_file.close()
+        
     def test_minimal_file( self ):
         """ Tests output when no hierarchy """
-        output_file = StringIO()
-        writer      = DOTWriter("")
-        
-        writer.write(output_file)
-        output_file.seek(0)  # rewind to the beginning
+        writer = DOTWriter({})
+        writer.write( self.output_file )
+        self.output_file.seek(0)  # rewind to the beginning
 
-        self.assertEquals( output_file.readline(), "digraph code_hierarchy {\n" )
-        self.assertEquals( output_file.readline(), "}\n" )
+        self.assertEquals( self.output_file.readline(), "digraph code_hierarchy {\n" )
+        self.assertEquals( self.output_file.readline(), "}\n" )
 
+    def test_labels( self ):
+        """ Tests output of labels i.e. filenames """
+        writer = DOTWriter({ "file1.php": [], "file2.php": [] })
+        writer.write( self.output_file )
+        self.output_file.seek(0)  # rewind to the beginning
+
+        self.assertEquals( self.output_file.readline(), "digraph code_hierarchy {\n" )
+        self.assertEquals( self.output_file.readline(), '  f1 [label="file1.php"]\n' )
+        self.assertEquals( self.output_file.readline(), '  f2 [label="file2.php"]\n' )
+        self.assertEquals( self.output_file.readline(), "}\n" )
+    
 
 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.