Source

qadpid / tests / test_dot_writer.py

Full commit
"""
DOT writer tests

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

import unittest
from StringIO import StringIO

from qadpid.writer import DOTWriter


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 """
        writer = DOTWriter({})
        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(), "}\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" )

    def test_relationships( self ):
        """ Tests output of relationships """
        writer = DOTWriter({ "file1.php": [ ("require", "file2.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(), "  f2 -> f1;\n" )
        self.assertEquals( self.output_file.readline(), "}\n" )
        
    

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