2022-05-22 13:46:56 +00:00
|
|
|
from __future__ import division, absolute_import, print_function
|
|
|
|
import unittest
|
2023-02-04 00:46:23 +00:00
|
|
|
from svgpathtools import Document
|
2022-05-22 13:46:56 +00:00
|
|
|
from io import StringIO
|
2022-06-06 04:00:07 +00:00
|
|
|
from io import open # overrides build-in open for compatibility with python2
|
2022-05-22 13:46:56 +00:00
|
|
|
from os.path import join, dirname
|
2022-06-06 04:17:28 +00:00
|
|
|
from sys import version_info
|
2022-06-06 04:00:07 +00:00
|
|
|
|
2022-05-22 13:46:56 +00:00
|
|
|
|
|
|
|
class TestDocument(unittest.TestCase):
|
2022-06-06 04:00:07 +00:00
|
|
|
def test_from_file_path_string(self):
|
|
|
|
"""Test reading svg from file provided as path"""
|
2022-05-22 13:46:56 +00:00
|
|
|
doc = Document(join(dirname(__file__), 'polygons.svg'))
|
|
|
|
|
|
|
|
self.assertEqual(len(doc.paths()), 2)
|
|
|
|
|
2022-06-06 04:00:07 +00:00
|
|
|
def test_from_file_path(self):
|
|
|
|
"""Test reading svg from file provided as path"""
|
2022-06-06 04:17:28 +00:00
|
|
|
if version_info >= (3, 6):
|
|
|
|
import pathlib
|
|
|
|
doc = Document(pathlib.Path(__file__).parent / 'polygons.svg')
|
2022-06-06 04:00:07 +00:00
|
|
|
|
2022-06-06 04:17:28 +00:00
|
|
|
self.assertEqual(len(doc.paths()), 2)
|
2022-06-06 04:00:07 +00:00
|
|
|
|
2022-05-22 13:46:56 +00:00
|
|
|
def test_from_file_object(self):
|
2022-06-06 04:00:07 +00:00
|
|
|
"""Test reading svg from file object that has already been opened"""
|
2022-05-22 13:46:56 +00:00
|
|
|
with open(join(dirname(__file__), 'polygons.svg'), 'r') as file:
|
|
|
|
doc = Document(file)
|
|
|
|
|
|
|
|
self.assertEqual(len(doc.paths()), 2)
|
|
|
|
|
|
|
|
def test_from_stringio(self):
|
2022-06-06 04:00:07 +00:00
|
|
|
"""Test reading svg object contained in a StringIO object"""
|
2022-05-22 14:17:43 +00:00
|
|
|
with open(join(dirname(__file__), 'polygons.svg'),
|
|
|
|
'r', encoding='utf-8') as file:
|
2022-05-22 13:46:56 +00:00
|
|
|
# read entire file into string
|
2022-05-22 13:51:03 +00:00
|
|
|
file_content = file.read()
|
2022-05-22 13:46:56 +00:00
|
|
|
# prepare stringio object
|
2022-05-24 16:15:52 +00:00
|
|
|
file_as_stringio = StringIO(file_content)
|
2022-05-22 13:46:56 +00:00
|
|
|
|
|
|
|
doc = Document(file_as_stringio)
|
|
|
|
|
|
|
|
self.assertEqual(len(doc.paths()), 2)
|
|
|
|
|
2022-05-22 14:17:43 +00:00
|
|
|
def test_from_string(self):
|
2022-06-06 04:00:07 +00:00
|
|
|
"""Test reading svg object contained in a string"""
|
2022-05-22 14:17:43 +00:00
|
|
|
with open(join(dirname(__file__), 'polygons.svg'),
|
|
|
|
'r', encoding='utf-8') as file:
|
2022-05-22 13:46:56 +00:00
|
|
|
# read entire file into string
|
2022-05-22 13:51:03 +00:00
|
|
|
file_content = file.read()
|
2022-05-22 13:46:56 +00:00
|
|
|
|
|
|
|
doc = Document.from_svg_string(file_content)
|
|
|
|
|
|
|
|
self.assertEqual(len(doc.paths()), 2)
|