From 8d598b20f212549b65054e02324d4ca2d7d211fa Mon Sep 17 00:00:00 2001 From: JSCHILL1 Date: Mon, 13 Apr 2009 11:31:50 -0500 Subject: [PATCH] Modified scour and added first unit test --- scour.py | 14 +++++++++++--- testscour.py | 19 +++++++++++++++++++ unittests/inkscape.svg | 5 +++++ 3 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 unittests/inkscape.svg diff --git a/scour.py b/scour.py index 058ad40..814418c 100755 --- a/scour.py +++ b/scour.py @@ -550,7 +550,7 @@ def properlySizeDoc(docElement): # this is the main method # input is a string representation of the input XML # returns a string representation of the output XML -def scour(in_string): +def scourString(in_string): global numAttrsRemoved global numStylePropsFixed global numElemsRemoved @@ -614,7 +614,15 @@ def scour(in_string): # output the document out_string = doc.documentElement.toxml() return out_string - + +# used mostly by unit tests +# input is a filename +# returns the minidom doc representation of the SVG +def scourXmlFile(filename): + in_string = open(filename).read() + out_string = scourString(in_string) + return xml.dom.minidom.parseString(out_string) + if __name__ == '__main__': # parse command-line arguments @@ -656,7 +664,7 @@ if __name__ == '__main__': # do the work in_string = input.read() - out_string = scour(in_string) + out_string = scourString(in_string) output.write(out_string) # Close input and output files diff --git a/testscour.py b/testscour.py index ad77f07..ffd4ef3 100755 --- a/testscour.py +++ b/testscour.py @@ -19,5 +19,24 @@ import unittest import scour +import xml.dom.minidom + +# performs a test on a given node +# func must return either True or False +def walkTree(elem, func): + if func(elem) == False: return False + for child in elem.childNodes: + if walkTree(child, func) == False: return False + return True + +class NoInkscapeElements(unittest.TestCase): + def runTest(self): + doc = scour.scourXmlFile('unittests/inkscape.svg') + self.assertNotEquals( walkTree( doc.documentElement, + lambda e: e.namespaceURI != "http://www.inkscape.org/namespaces/inkscape" ), False, + 'Found Inkscape elements' ) + +if __name__ == '__main__': + unittest.main() print "done" \ No newline at end of file diff --git a/unittests/inkscape.svg b/unittests/inkscape.svg new file mode 100644 index 0000000..e41b756 --- /dev/null +++ b/unittests/inkscape.svg @@ -0,0 +1,5 @@ + + + + \ No newline at end of file