From a743e0293c3ca04e7b5e4cdb7059085e2b4fdb6c Mon Sep 17 00:00:00 2001 From: FlyingSamson Date: Sun, 22 May 2022 15:46:56 +0200 Subject: [PATCH] Add tests for creating from file location, file, StringIO, and string --- test/test_document.py | 45 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 test/test_document.py diff --git a/test/test_document.py b/test/test_document.py new file mode 100644 index 0000000..f1004bb --- /dev/null +++ b/test/test_document.py @@ -0,0 +1,45 @@ +from __future__ import division, absolute_import, print_function +import unittest +from svgpathtools import * +from io import StringIO +from os.path import join, dirname + +class TestDocument(unittest.TestCase): + def test_from_file_path(self): + """ Test reading svg from file provided as path """ + doc = Document(join(dirname(__file__), 'polygons.svg')) + + self.assertEqual(len(doc.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: + doc = Document(file) + + self.assertEqual(len(doc.paths()), 2) + + def test_from_stringio(self): + """ Test reading svg object contained in a 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) + + doc = Document(file_as_stringio) + + self.assertEqual(len(doc.paths()), 2) + + def test_from_string_without_svg_attrs(self): + """ Test reading svg object contained in a string without svg attributes""" + with open(join(dirname(__file__), 'polygons.svg'), 'r') as file: + # read entire file into string + file_content: str = file.read() + + doc = Document.from_svg_string(file_content) + + self.assertEqual(len(doc.paths()), 2)