| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- import os
- import sys
- import io
- import pprint
- import tempfile
- import unittest
- import xml.etree.ElementTree as ET
- from drawio2py import parser, abstract_syntax, generator, shapelib, util
- DATADIR = os.path.join(os.path.dirname(__file__), "data")
- class DummyOutput:
- def write(self, text: str):
- pass
- # The bare minimum that we can consider 'a test':
- # We parse XML, write it out again, then parse it again and write it out again.
- # Finally, we check if both serializations (the first and second one) are bitwise equal (they should).
- # To verify if the generated drawio file is really the "same" as the original, we currently manually open the file in drawio.
- def run_test(filename):
- # Parse (1st time):
- asyntax = parser.Parser.parse(os.path.join(DATADIR,filename))
- # Generate .drawio (1st time):
- csyntax = io.BytesIO()
- generator.generate(asyntax, csyntax)
- csyntax.seek(0)
- # Parse (2nd time):
- asyntax2 = parser.Parser.parse(csyntax)
- # Generate .drawio (2nd time):
- csyntax2 = io.BytesIO()
- generator.generate(asyntax2, csyntax2)
- csyntax2.seek(0)
- if (csyntax.getvalue() != csyntax2.getvalue()):
- # print("csyntax:", csyntax.getvalue())
- # print("csyntax2:", csyntax2.getvalue())
- raise Exception("Files differ after round-trip!")
- # Compares two XML trees.
- # From: https://stackoverflow.com/a/24349916
- def elements_equal(e1, e2, depth=0):
- print(" "*depth+e1.tag)
- if e1.tag != e2.tag:
- print("tags differ")
- return False
- if e1.tail != e2.tail:
- print("tail differs")
- return False
- if e1.attrib != e2.attrib:
- print("attributes differ")
- pprint.pprint(e1.attrib)
- pprint.pprint(e2.attrib)
- return False
- if len(e1) != len(e2):
- print("number of children differs")
- return False
- return all(elements_equal(c1, c2, depth+1) for c1, c2 in zip(e1, e2))
- # Currently unused.
- # Our generated XML always differs from the parsed XML.
- # That's because we skip certain attributes.
- def assert_roundtrip_equal(filename):
- expected_xml = ET.parse(os.path.join(DATADIR,filename)).getroot()
- asyntax = parser.Parser.parse_xml_root(expected_xml)
- actual_xml = generator.generate_mxfile(asyntax)
- if not elements_equal(expected_xml, actual_xml):
- raise Exception("Generated XML tree differs from parsed XML tree.")
- def parse_shapelib(filename):
- return shapelib.parse_library(os.path.join(DATADIR,filename))
- class Tests(unittest.TestCase):
- def test_1(self):
- run_test("test.drawio")
- def test_2(self):
- run_test("overview.drawio") # we eat our own dog food :)
- def test_3(self):
- run_test("TrivialPM.drawio")
- def test_label_offset(self):
- run_test("labelOffset.drawio")
- def test_edge_label(self):
- run_test("edgeLabel.drawio")
- # asyntax = parser.Parser.parse(os.path.join(DATADIR, "edgeLabel.drawio"))
- # with open(os.path.join(DATADIR, "edgeLabel-1.drawio"), 'wb') as f:
- # generator.generate(asyntax, f)
- def test_shapelib(self):
- common_lib = parse_shapelib("shapelibs/common.xml")
- pm_lib = parse_shapelib("shapelibs/pm.xml")
- id_gen = util.DrawioIDGenerator()
- page = util.generate_empty_page(id_gen, "MyFancyPage")
- cloner = shapelib.ShapeCloner(id_gen)
- initial = cloner.clone_vertex(pm_lib["(PM) Initial"], page.root, 100, 100)
- final = cloner.clone_vertex(pm_lib["(PM) Final"], page.root, 300, 300)
- cloner.clone_edge(common_lib["Control Flow"], page.root, initial, final)
- def parse_shapelib1(self):
- parse_shapelib("shapelibs/ftg.xml")
- def parse_shapelib2(self):
- parse_shapelib("shapelibs/pt.xml")
- def parse_shapelib3(self):
- parse_shapelib("shapelibs/ss.xml")
|