~ubuntu-branches/ubuntu/raring/dot2tex/raring

« back to all changes in this revision

Viewing changes to tests/test_comparedotparsing.py

  • Committer: Bazaar Package Importer
  • Author(s): Peter Collingbourne, Sandro Tosi, Marco Rodrigues, Peter Collingbourne
  • Date: 2009-03-15 16:23:06 UTC
  • mfrom: (1.1.9 upstream) (4.1.3 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090315162306-cq7opa63u50dbqd4
Tags: 2.8.5-1
[ Sandro Tosi ]
* debian/control
  - switch Vcs-Browser field to viewsvn

[ Marco Rodrigues ]
* debian/control:
  + Add ${misc:Depends} to Depends to remove
    lintian warning.

[ Peter Collingbourne ]
* New upstream release
* debian/control: new Standards-Version
* debian/control: generated files now require pgf >= 2.00
* debian/copyright: changed year to 2009
* debian/control, debian/copyright, debian/dot2tex.1: changed
  maintainer email address (again)
* debian/copyright: refer specifically to GPL-2
* debian/copyright: use the word Copyright to correctly express
  copyright ownership

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
"""
2
 
Various test for checking that the dotparsing module is working properly.
3
 
The basic test is to compare PNG renderings of the original graph and the
4
 
parsed and saved version.
5
 
"""
6
 
 
7
 
import unittest
8
 
 
9
 
#import dot2tex.dotparsing as dotp
10
 
from dot2tex import dotparsing
11
 
import re, os,shutil, glob,sys,time
12
 
 
13
 
import ImageChops,Image
14
 
 
15
 
from os.path import join,basename,splitext,normpath
16
 
from dot2tex import dotparsing
17
 
import logging
18
 
 
19
 
# intitalize logging module
20
 
log = logging.getLogger("test_graphparser")
21
 
console = logging.StreamHandler()
22
 
console.setLevel(logging.WARNING)
23
 
# set a format which is simpler for console use
24
 
formatter = logging.Formatter('%(levelname)-8s %(message)s')
25
 
# tell the handler to use this format
26
 
console.setFormatter(formatter)
27
 
log.addHandler(console)
28
 
 
29
 
 
30
 
# Directory with test files
31
 
BASE_DIR = join(os.path.dirname(__file__),"testgraphs/")
32
 
TESTFILES_DIR = "parsetests"
33
 
TESTFILES_PATH = join(BASE_DIR,TESTFILES_DIR)
34
 
 
35
 
PNG_DIR = join(TESTFILES_PATH,'pngs')
36
 
DOT_DIR = join(TESTFILES_PATH,'dpdots')
37
 
 
38
 
graphparser = dotparsing.DotDataParser()
39
 
 
40
 
def equal(im1, im2):
41
 
    return ImageChops.difference(im1, im2).getbbox() is None
42
 
 
43
 
def compare_dot_file(dotfile):
44
 
    """Check that a dot file is correctly parsed
45
 
 
46
 
    """
47
 
    try:
48
 
        log.info('Processing %s', dotfile)
49
 
        basefilename = basename(dotfile)
50
 
        basefilenamenoext = os.path.splitext(basefilename)[0]
51
 
        f = open(dotfile)
52
 
        try:
53
 
            data = f.read()
54
 
        except:
55
 
            log.warning('Could not open %s',dotfile)
56
 
            raise
57
 
        f.close()
58
 
        try:
59
 
            graph = graphparser.parse_dot_data(data)
60
 
            # create a dotparsing version of the dot file
61
 
            newfilename = normpath(join(DOT_DIR,basefilename+'.new'))
62
 
            f = open(newfilename,'w')
63
 
            f.write(str(graph))
64
 
            f.close()
65
 
        except:
66
 
            log.exception('Failed to parse and save %s',basefilename)
67
 
            raise
68
 
        origpngfilename = join(PNG_DIR,basefilenamenoext+'.png')
69
 
        newpngfilename = join(PNG_DIR,basefilenamenoext+'.new.png')
70
 
        # create PNG from original dot file
71
 
        syscmd = 'dot -Tpng %s > %s' % (dotfile,origpngfilename)
72
 
        log.debug("Run %s",syscmd)
73
 
        err = os.system(syscmd)
74
 
        if err:
75
 
            log.warning("Failed to create original %s",origpngfilename)
76
 
            failedfiles.append(basefilename)
77
 
            raise
78
 
        # create PNG from dot file generated by dotparsing
79
 
        syscmd = 'dot -Tpng %s > %s' % (newfilename,newpngfilename)
80
 
        log.debug("Run %s",syscmd)
81
 
        err = os.system(syscmd)
82
 
        if err:
83
 
            log.warning("Failed to create new %s",origpngfilename)
84
 
            return -1
85
 
        # load and compare images
86
 
        try:
87
 
            imorig = Image.open(origpngfilename)
88
 
            imnew = Image.open(newpngfilename)
89
 
        except:
90
 
            log.exception('Could not load %s images',basefilename)
91
 
            raise
92
 
        if equal(imorig,imnew):
93
 
            log.info('%s matches.', basefilename)
94
 
            # remove files
95
 
            os.remove(origpngfilename)
96
 
            os.remove(newpngfilename)
97
 
            os.remove(newfilename)
98
 
            return 1
99
 
 
100
 
        else:
101
 
            log.warning('%s did not match!', basefilename)
102
 
            return 0
103
 
    except:
104
 
        log.exception('Failed to process %s', basefilename)
105
 
        raise
106
 
 
107
 
 
108
 
def test_dotfile(filename):
109
 
    return compare_dot_file(join(TESTFILES_PATH,filename))
110
 
 
111
 
class DotparsingTest(unittest.TestCase):
112
 
    def setUp(self):
113
 
        if not os.path.exists(PNG_DIR):
114
 
            os.mkdir(PNG_DIR)
115
 
        if not os.path.exists(DOT_DIR):
116
 
            os.mkdir(DOT_DIR)
117
 
 
118
 
    def test_ids(self):
119
 
        self.assertEqual(test_dotfile("ids.dot"),1)
120
 
    def test_multilines(self):
121
 
        self.assertEqual(test_dotfile("multilines.dot"),1)
122
 
    def test_subgraphs(self):
123
 
        self.assertEqual(test_dotfile("subgraphs.dot"),1)
124
 
    def test_quoting(self):
125
 
        self.assertEqual(test_dotfile("quoting.dot"),1)
126
 
 
127
 
 
128
 
if __name__ == "__main__":
129
 
    unittest.main()
130
 
 
 
1
"""
 
2
Various test for checking that the dotparsing module is working properly.
 
3
The basic test is to compare PNG renderings of the original graph and the
 
4
parsed and saved version.
 
5
"""
 
6
 
 
7
import unittest
 
8
 
 
9
#import dot2tex.dotparsing as dotp
 
10
from dot2tex import dotparsing
 
11
import re, os,shutil, glob,sys,time
 
12
 
 
13
import ImageChops,Image
 
14
 
 
15
from os.path import join,basename,splitext,normpath
 
16
from dot2tex import dotparsing
 
17
import logging
 
18
 
 
19
# intitalize logging module
 
20
log = logging.getLogger("test_graphparser")
 
21
console = logging.StreamHandler()
 
22
console.setLevel(logging.WARNING)
 
23
# set a format which is simpler for console use
 
24
formatter = logging.Formatter('%(levelname)-8s %(message)s')
 
25
# tell the handler to use this format
 
26
console.setFormatter(formatter)
 
27
log.addHandler(console)
 
28
 
 
29
 
 
30
# Directory with test files
 
31
BASE_DIR = join(os.path.dirname(__file__),"testgraphs/")
 
32
TESTFILES_DIR = "parsetests"
 
33
TESTFILES_PATH = join(BASE_DIR,TESTFILES_DIR)
 
34
 
 
35
PNG_DIR = join(TESTFILES_PATH,'pngs')
 
36
DOT_DIR = join(TESTFILES_PATH,'dpdots')
 
37
 
 
38
graphparser = dotparsing.DotDataParser()
 
39
 
 
40
def equal(im1, im2):
 
41
    return ImageChops.difference(im1, im2).getbbox() is None
 
42
 
 
43
def compare_dot_file(dotfile):
 
44
    """Check that a dot file is correctly parsed
 
45
 
 
46
    """
 
47
    try:
 
48
        log.info('Processing %s', dotfile)
 
49
        basefilename = basename(dotfile)
 
50
        basefilenamenoext = os.path.splitext(basefilename)[0]
 
51
        f = open(dotfile)
 
52
        try:
 
53
            data = f.read()
 
54
        except:
 
55
            log.warning('Could not open %s',dotfile)
 
56
            raise
 
57
        f.close()
 
58
        try:
 
59
            graph = graphparser.parse_dot_data(data)
 
60
            # create a dotparsing version of the dot file
 
61
            newfilename = normpath(join(DOT_DIR,basefilename+'.new'))
 
62
            f = open(newfilename,'w')
 
63
            f.write(str(graph))
 
64
            f.close()
 
65
        except:
 
66
            log.exception('Failed to parse and save %s',basefilename)
 
67
            raise
 
68
        origpngfilename = join(PNG_DIR,basefilenamenoext+'.png')
 
69
        newpngfilename = join(PNG_DIR,basefilenamenoext+'.new.png')
 
70
        # create PNG from original dot file
 
71
        syscmd = 'dot -Tpng %s > %s' % (dotfile,origpngfilename)
 
72
        log.debug("Run %s",syscmd)
 
73
        err = os.system(syscmd)
 
74
        if err:
 
75
            log.warning("Failed to create original %s",origpngfilename)
 
76
            #failedfiles.append(basefilename)
 
77
            raise
 
78
        # create PNG from dot file generated by dotparsing
 
79
        syscmd = 'dot -Tpng %s > %s' % (newfilename,newpngfilename)
 
80
        log.debug("Run %s",syscmd)
 
81
        err = os.system(syscmd)
 
82
        if err:
 
83
            log.warning("Failed to create new %s",origpngfilename)
 
84
            return -1
 
85
        # load and compare images
 
86
        try:
 
87
            imorig = Image.open(origpngfilename)
 
88
            imnew = Image.open(newpngfilename)
 
89
        except:
 
90
            log.exception('Could not load %s images',basefilename)
 
91
            raise
 
92
        if equal(imorig,imnew):
 
93
            log.info('%s matches.', basefilename)
 
94
            # remove files
 
95
            os.remove(origpngfilename)
 
96
            os.remove(newpngfilename)
 
97
            os.remove(newfilename)
 
98
            return 1
 
99
 
 
100
        else:
 
101
            log.warning('%s did not match!', basefilename)
 
102
            return 0
 
103
    except:
 
104
        log.exception('Failed to process %s', basefilename)
 
105
        raise
 
106
 
 
107
 
 
108
def test_dotfile(filename):
 
109
    return compare_dot_file(join(TESTFILES_PATH,filename))
 
110
 
 
111
class DotparsingTest(unittest.TestCase):
 
112
    def setUp(self):
 
113
        if not os.path.exists(PNG_DIR):
 
114
            os.mkdir(PNG_DIR)
 
115
        if not os.path.exists(DOT_DIR):
 
116
            os.mkdir(DOT_DIR)
 
117
 
 
118
    def test_ids(self):
 
119
        self.assertEqual(test_dotfile("ids.dot"),1)
 
120
    def test_multilines(self):
 
121
        self.assertEqual(test_dotfile("multilines.dot"),1)
 
122
    def test_subgraphs(self):
 
123
        self.assertEqual(test_dotfile("subgraphs.dot"),1)
 
124
    def test_quoting(self):
 
125
        self.assertEqual(test_dotfile("quoting.dot"),1)
 
126
    def test_unicode1(self):
 
127
        self.assertEqual(test_dotfile("unicode1.dot"),1)
 
128
    def test_current(self):
 
129
        self.assertEqual(test_dotfile("current.dot"),1)
 
130
    def test_ports(self):
 
131
        self.assertEqual(test_dotfile("ports.dot"),1)
 
132
        
 
133
    
 
134
 
 
135
 
 
136
if __name__ == "__main__":
 
137
    unittest.main()
 
138