aboutsummaryrefslogtreecommitdiff
path: root/svg-flatten/src
diff options
context:
space:
mode:
authorjaseg <git@jaseg.de>2021-06-03 23:45:11 +0200
committerjaseg <git@jaseg.de>2021-06-03 23:45:11 +0200
commit564ab243cc3fa7c0239d71a6dacb4a6c7765e9f5 (patch)
tree1aafc29c1fddfacd85a6b72cfe223817afdbbf4a /svg-flatten/src
parentde0f8516458578dbd9a38a965e21a1013cdcade2 (diff)
downloadgerbolyze-564ab243cc3fa7c0239d71a6dacb4a6c7765e9f5.tar.gz
gerbolyze-564ab243cc3fa7c0239d71a6dacb4a6c7765e9f5.tar.bz2
gerbolyze-564ab243cc3fa7c0239d71a6dacb4a6c7765e9f5.zip
Add svg-flatten SVG feature tests
Diffstat (limited to 'svg-flatten/src')
-rw-r--r--svg-flatten/src/test/nopencv_test.cpp (renamed from svg-flatten/src/nopencv_test.cpp)2
-rw-r--r--svg-flatten/src/test/svg_tests.py75
2 files changed, 76 insertions, 1 deletions
diff --git a/svg-flatten/src/nopencv_test.cpp b/svg-flatten/src/test/nopencv_test.cpp
index 17ba71e..2f358b2 100644
--- a/svg-flatten/src/nopencv_test.cpp
+++ b/svg-flatten/src/test/nopencv_test.cpp
@@ -15,7 +15,7 @@
using namespace gerbolyze;
using namespace gerbolyze::nopencv;
-char msg[1024];
+char msg[512];
class TempfileHack {
public:
diff --git a/svg-flatten/src/test/svg_tests.py b/svg-flatten/src/test/svg_tests.py
new file mode 100644
index 0000000..a5139ec
--- /dev/null
+++ b/svg-flatten/src/test/svg_tests.py
@@ -0,0 +1,75 @@
+#!/usr/bin/env python3
+
+import tempfile
+import unittest
+from pathlib import Path
+import subprocess
+import os
+
+from PIL import Image
+import numpy as np
+
+def run_svg_flatten(input_file, output_file, **kwargs):
+ if 'SVG_FLATTEN' in os.environ:
+ svg_flatten = os.environ.get('SVG_FLATTEN')
+ elif (Path(__file__) / '../../build/svg-flatten').is_file():
+ svg_flatten = '../../build/svg-flatten'
+ elif Path('./build/svg-flatten').is_file():
+ svg_flatten = './build/svg-flatten'
+ else:
+ svg_flatten = 'svg-flatten'
+
+ args = [ svg_flatten,
+ *(arg for (key, value) in kwargs.items() for arg in (f'--{key.replace("_", "-")}', value)),
+ str(input_file), str(output_file) ]
+
+ try:
+ proc = subprocess.run(args, capture_output=True, check=True)
+ except:
+ print('Subprocess stdout:')
+ print(proc.stdout)
+ print('Subprocess stderr:')
+ print(proc.stderr)
+ raise
+
+class SVGRoundTripTests(unittest.TestCase):
+
+ def compare_images(self, reference, output, test_name, mean=0.01):
+ ref = np.array(Image.open(reference))
+ out = np.array(Image.open(output))
+ delta = np.abs(out - ref).astype(float) / 255
+
+ #print(f'{test_name}: mean={delta.mean():.5g}')
+
+ self.assertTrue(delta.mean() < mean,
+ f'Expected mean pixel difference between images to be <{mean}, was {delta.mean():.5g}')
+
+ def run_svg_round_trip_test(self, test_in_svg):
+ with tempfile.NamedTemporaryFile(suffix='.svg') as tmp_out_svg,\
+ tempfile.NamedTemporaryFile(suffix='.png') as tmp_out_png,\
+ tempfile.NamedTemporaryFile(suffix='.png') as tmp_in_png:
+
+ run_svg_flatten(test_in_svg, tmp_out_svg.name, format='svg')
+
+ subprocess.run(['resvg', tmp_out_svg.name, tmp_out_png.name], check=True, stdout=subprocess.DEVNULL)
+ subprocess.run(['resvg', test_in_svg, tmp_in_png.name], check=True, stdout=subprocess.DEVNULL)
+
+ try:
+ self.compare_images(tmp_in_png, tmp_out_png, test_in_svg.stem)
+ except AssertionError as e:
+ import shutil
+ shutil.copyfile(tmp_in_png.name, f'/tmp/gerbolyze-fail-{test_in_svg.stem}-in.png')
+ shutil.copyfile(tmp_out_png.name, f'/tmp/gerbolyze-fail-{test_in_svg.stem}-out.png')
+ foo = list(e.args)
+ foo[0] += '\nFailing test renderings copied to:\n'
+ foo[0] += f' /tmp/gerbolyze-fail-{test_in_svg.stem}-{{in|out}}.png\n'
+ e.args = tuple(foo)
+ raise e
+
+for test_in_svg in Path('testdata/svg').glob('*.svg'):
+ # We need to make sure we capture the loop variable's current value here.
+ gen = lambda testcase: lambda self: self.run_svg_round_trip_test(testcase)
+ setattr(SVGRoundTripTests, f'test_{test_in_svg.stem}', gen(test_in_svg))
+
+if __name__ == '__main__':
+ unittest.main()