From ccdd10212c64d06970975467a9eadf1b388c3f24 Mon Sep 17 00:00:00 2001 From: FlyingSamson Date: Sun, 22 May 2022 13:32:09 +0200 Subject: [PATCH] Add unit tests for reading from different sources in svg2paths --- test/test_svg2paths.py | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/test/test_svg2paths.py b/test/test_svg2paths.py index 5a0dab0..b5cf5b4 100644 --- a/test/test_svg2paths.py +++ b/test/test_svg2paths.py @@ -1,6 +1,7 @@ from __future__ import division, absolute_import, print_function import unittest from svgpathtools import * +from io import StringIO from os.path import join, dirname from svgpathtools.svg_to_paths import rect2pathd @@ -57,4 +58,33 @@ class TestSVG2Paths(unittest.TestCase): non_rounded = {"x":"10", "y":"10", "width":"100","height":"100"} self.assertEqual(rect2pathd(non_rounded), 'M10.0 10.0 L 110.0 10.0 L 110.0 110.0 L 10.0 110.0 z') rounded = {"x":"10", "y":"10", "width":"100","height":"100", "rx":"15", "ry": "12"} - self.assertEqual(rect2pathd(rounded), "M 25.0 10.0 L 95.0 10.0 A 15.0 12.0 0 0 1 110.0 22.0 L 110.0 98.0 A 15.0 12.0 0 0 1 95.0 110.0 L 25.0 110.0 A 15.0 12.0 0 0 1 10.0 98.0 L 10.0 22.0 A 15.0 12.0 0 0 1 25.0 10.0 z") \ No newline at end of file + self.assertEqual(rect2pathd(rounded), "M 25.0 10.0 L 95.0 10.0 A 15.0 12.0 0 0 1 110.0 22.0 L 110.0 98.0 A 15.0 12.0 0 0 1 95.0 110.0 L 25.0 110.0 A 15.0 12.0 0 0 1 10.0 98.0 L 10.0 22.0 A 15.0 12.0 0 0 1 25.0 10.0 z") + + def test_from_file_path(self): + """ Test reading svg from file provided as path """ + paths, _ = svg2paths(join(dirname(__file__), 'polygons.svg')) + + self.assertEqual(len(paths), 2) + + def test_from_file_object(self): + """ Test reading svg from file object that has already been opened """ + with open(join(dirname(__file__), 'polygons.svg'), 'r') as file: + paths, _ = svg2paths(file) + + self.assertEqual(len(paths), 2) + + def test_from_stringio(self): + """ Test reading svg object contained in an StringIO object """ + with open(join(dirname(__file__), 'polygons.svg'), 'r') as file: + # read entire file into string + file_content: str = file.read() + # prepare stringio object + file_as_stringio = StringIO() + # paste file content into it + file_as_stringio.write(file_content) + # reset curser to its beginning + file_as_stringio.seek(0) + + paths, _ = svg2paths(file_as_stringio) + + self.assertEqual(len(paths), 2)