Add unit tests for reading from different sources in svg2paths

pull/176/head
FlyingSamson 2022-05-22 13:32:09 +02:00
parent ce43c75cd8
commit ccdd10212c
1 changed files with 31 additions and 1 deletions

View File

@ -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")
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)