Commits

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()