瀏覽代碼

Expose function to parse <mxgraphmodel> + run test suite upon build.

Joeri Exelmans 2 年之前
父節點
當前提交
57534eb740
共有 3 個文件被更改,包括 49 次插入42 次删除
  1. 11 9
      drawio2py/parser.py
  2. 2 0
      setup.py
  3. 36 33
      test/run_tests.py

+ 11 - 9
drawio2py/parser.py

@@ -129,14 +129,22 @@ class Parser:
 
 	@staticmethod
 	def parse_page(page):
-		# found = page.find("mxGraphModel")
-		# page
 		if len(page) == 0:
 			nroot = Parser.decode_and_deflate(page.text)
 		else:
 			nroot = page[0]
 		mxgm = nroot
 
+		root = Parser.parse_mxgraphmodel(mxgm)
+
+		return Page(
+			id=page.attrib.get("id"),
+			name=page.attrib.get("name"),
+			attributes=mxgm.attrib,
+			root = root)
+
+	@staticmethod
+	def parse_mxgraphmodel(mxgm):
 		cell_dict = {} # mapping from ID to cell
 		Parser.parse_components(mxgm[0], cell_dict)
 
@@ -159,13 +167,7 @@ class Parser:
 
 		if root == None:
 			raise Exception("No root cell in page " + str(px) + " (every cell had a parent)")
-
-		pg = Page(
-			id=page.attrib.get("id"),
-			name=page.attrib.get("name"),
-			attributes=mxgm.attrib,
-			root = root)
-		return pg
+		return root
 
 	@staticmethod
 	def parse(filename):

+ 2 - 0
setup.py

@@ -13,4 +13,6 @@ setup(
 
     # include_package_data=True,
     # package_data={"drawio2py": ["template.oml"]},
+
+    test_suite="test.run_tests"
 )

+ 36 - 33
test/run_tests.py

@@ -4,48 +4,51 @@ import io
 from drawio2py import parser, abstract_syntax, generator
 import pprint
 import tempfile
+import unittest
 
-if __name__ == "__main__":
-    DATADIR = os.path.join(os.path.dirname(__file__), "data")
+DATADIR = os.path.join(os.path.dirname(__file__), "data")
 
-    class DummyOutput:
-        def write(self, text: str):
-            # sys.stdout.write(text)
-            pass
+class DummyOutput:
+    def write(self, text: str):
+        # sys.stdout.write(text)
+        pass
 
-    def run_test(filename):
-        # Parse (1st time):
-        asyntax = parser.Parser.parse(os.path.join(DATADIR,filename))
+def run_test(filename):
+    # Parse (1st time):
+    asyntax = parser.Parser.parse(os.path.join(DATADIR,filename))
 
-        # Print abstract syntax:
-        # pprint.pprint(asyntax, indent=2, compact=True)
+    # Print abstract syntax:
+    # pprint.pprint(asyntax, indent=2, compact=True)
 
-        # Generate .drawio (1st time):
-        csyntax = io.BytesIO()
-        generator.generate(asyntax, csyntax)
-        csyntax.seek(0)
+    # Generate .drawio (1st time):
+    csyntax = io.BytesIO()
+    generator.generate(asyntax, csyntax)
+    csyntax.seek(0)
 
-        # Print generated .drawio
-        # print(csyntax.getvalue())
+    # Print generated .drawio
+    # print(csyntax.getvalue())
+
+    # Parse (2nd time):
 
-        # Parse (2nd time):
+    asyntax2 = parser.Parser.parse(csyntax)
 
-        asyntax2 = parser.Parser.parse(csyntax)
+    # Generate .drawio (2nd time):
+    csyntax2 = io.BytesIO()
+    generator.generate(asyntax2, csyntax2)
+    csyntax2.seek(0)
 
-        # Generate .drawio (2nd time):
-        csyntax2 = io.BytesIO()
-        generator.generate(asyntax2, csyntax2)
-        csyntax2.seek(0)
+    if (csyntax.getvalue() != csyntax2.getvalue()):
+        # print(csyntax.getvalue())
+        # print(csyntax2.getvalue())
+        raise Exception("Files differ after round-trip!")
+    print(filename, "OK")
 
-        if (csyntax.getvalue() != csyntax2.getvalue()):
-            # print(csyntax.getvalue())
-            # print(csyntax2.getvalue())
-            raise Exception("Files differ after round-trip!")
-            
-        print(filename, "OK")
+class Tests(unittest.TestCase):
+    def test_1(self):
+        run_test("test.drawio")
 
-    # Just see if these files parse without throwing an exception :)
-    run_test("test.drawio")
-    run_test("overview.drawio") # we eat our own dog food :)
+    def test_2(self):
+        run_test("overview.drawio") # we eat our own dog food :)
 
-    print("Tests passed.")
+    def test_3(self):
+        run_test("TrivialPM.drawio")