From f08be5d666e7638b4f2a3d578ccb7af34e946a83 Mon Sep 17 00:00:00 2001 From: Eduard Braun Date: Tue, 16 Aug 2016 22:17:38 +0200 Subject: [PATCH] Adjust unittests for previous commit; some reordering --- scour/scour.py | 61 +++++++++++++++++++++++++------------------------- testscour.py | 32 +++++++++++++------------- 2 files changed, 47 insertions(+), 46 deletions(-) diff --git a/scour/scour.py b/scour/scour.py index cf511c0..0807bf6 100644 --- a/scour/scour.py +++ b/scour/scour.py @@ -3263,13 +3263,6 @@ _option_group_compatibility.add_option("--error-on-flowtext", _options_parser.add_option_group(_option_group_compatibility) -def maybe_gziped_file(filename, mode="r"): - if os.path.splitext(filename)[1].lower() in (".svgz", ".gz"): - import gzip - return gzip.GzipFile(filename, mode) - return open(filename, mode) - - def parse_args(args=None, ignore_additional_args=False): options, rargs = _options_parser.parse_args(args) @@ -3294,6 +3287,37 @@ def parse_args(args=None, ignore_additional_args=False): +def generateDefaultOptions(): + ## FIXME: clean up this mess/hack and refactor arg parsing to argparse + class Struct: + def __init__(self, **entries): + self.__dict__.update(entries) + + d = parse_args(args = [], ignore_additional_args = True)[0].__dict__.copy() + + return Struct(**d) + + + +# sanitizes options by updating attributes in a set of defaults options while discarding unknown attributes +def sanitizeOptions(options): + optionsDict = dict((key, getattr(options, key)) for key in dir(options) if not key.startswith('__')) + + sanitizedOptions = _options_parser.get_default_values() + sanitizedOptions._update_careful(optionsDict) + + return sanitizedOptions + + + +def maybe_gziped_file(filename, mode="r"): + if os.path.splitext(filename)[1].lower() in (".svgz", ".gz"): + import gzip + return gzip.GzipFile(filename, mode) + return open(filename, mode) + + + def getInOut(options): if options.infilename: infile = maybe_gziped_file(options.infilename, "rb") @@ -3340,29 +3364,6 @@ def getReport(): -def generateDefaultOptions(): - ## FIXME: clean up this mess/hack and refactor arg parsing to argparse - class Struct: - def __init__(self, **entries): - self.__dict__.update(entries) - - d = parse_args(args = [], ignore_additional_args = True)[0].__dict__.copy() - - return Struct(**d) - - - -# sanitizes options by updating attributes in a set of defaults options while discarding unknown attributes -def sanitizeOptions(options): - optionsDict = dict((key, getattr(options, key)) for key in dir(options) if not key.startswith('__')) - - sanitizedOptions = _options_parser.get_default_values() - sanitizedOptions._update_careful(optionsDict) - - return sanitizedOptions - - - def start(options, input, output): start = walltime() diff --git a/testscour.py b/testscour.py index 322b7f9..9c29e91 100755 --- a/testscour.py +++ b/testscour.py @@ -193,7 +193,7 @@ class RemoveUnreferencedDefs(unittest.TestCase): class KeepUnreferencedDefs(unittest.TestCase): def runTest(self): doc = scour.scourXmlFile('unittests/unreferenced-defs.svg', - scour.parse_args(['--keep-unreferenced-defs'])[0]) + scour.parse_args(['--keep-unreferenced-defs'])) self.assertEqual(len(doc.getElementsByTagNameNS(SVGNS, 'linearGradient')), 1, 'Referenced linearGradient removed from defs with `--keep-unreferenced-defs`' ) self.assertEqual(len(doc.getElementsByTagNameNS(SVGNS, 'radialGradient')), 1, @@ -233,7 +233,7 @@ class KeepUnreferencedIDsWhenEnabled(unittest.TestCase): class RemoveUnreferencedIDsWhenEnabled(unittest.TestCase): def runTest(self): doc = scour.scourXmlFile('unittests/ids-to-strip.svg', - scour.parse_args(['--enable-id-stripping'])[0]) + scour.parse_args(['--enable-id-stripping'])) self.assertEqual(doc.getElementsByTagNameNS(SVGNS, 'svg')[0].getAttribute('id'), '', ' ID not stripped' ) @@ -246,7 +246,7 @@ class RemoveUselessNestedGroups(unittest.TestCase): class DoNotRemoveUselessNestedGroups(unittest.TestCase): def runTest(self): doc = scour.scourXmlFile('unittests/nested-useless-groups.svg', - scour.parse_args(['--disable-group-collapsing'])[0]) + scour.parse_args(['--disable-group-collapsing'])) self.assertEqual(len(doc.getElementsByTagNameNS(SVGNS, 'g')), 2, 'Useless nested groups were removed despite --disable-group-collapsing' ) @@ -495,7 +495,7 @@ class RemoveFillOpacityWhenFillNone(unittest.TestCase): class ConvertFillPropertyToAttr(unittest.TestCase): def runTest(self): doc = scour.scourXmlFile('unittests/fill-none.svg', - scour.parse_args(['--disable-simplify-colors'])[0]) + scour.parse_args(['--disable-simplify-colors'])) self.assertEqual(doc.getElementsByTagNameNS(SVGNS, 'path')[1].getAttribute('fill'), 'black', 'fill property not converted to XML attribute' ) @@ -1144,7 +1144,7 @@ class PathImplicitLineWithMoveCommands(unittest.TestCase): class RemoveMetadataOption(unittest.TestCase): def runTest(self): doc = scour.scourXmlFile('unittests/full-metadata.svg', - scour.parse_args(['--remove-metadata'])[0]) + scour.parse_args(['--remove-metadata'])) self.assertEqual(doc.childNodes.length, 1, 'Did not remove tag with --remove-metadata') @@ -1153,7 +1153,7 @@ class EnableCommentStrippingOption(unittest.TestCase): with open('unittests/comment-beside-xml-decl.svg') as f: docStr = f.read() docStr = scour.scourString(docStr, - scour.parse_args(['--enable-comment-stripping'])[0]) + scour.parse_args(['--enable-comment-stripping'])) self.assertEqual(docStr.find('