run_tests.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import os
  2. import sys
  3. import io
  4. from drawio2py import parser, abstract_syntax, generator
  5. import pprint
  6. import tempfile
  7. import unittest
  8. DATADIR = os.path.join(os.path.dirname(__file__), "data")
  9. class DummyOutput:
  10. def write(self, text: str):
  11. # sys.stdout.write(text)
  12. pass
  13. def run_test(filename):
  14. # Parse (1st time):
  15. asyntax = parser.Parser.parse(os.path.join(DATADIR,filename))
  16. # Print abstract syntax:
  17. # pprint.pprint(asyntax, indent=2, compact=True)
  18. # Generate .drawio (1st time):
  19. csyntax = io.BytesIO()
  20. generator.generate(asyntax, csyntax)
  21. csyntax.seek(0)
  22. # Print generated .drawio
  23. # print(csyntax.getvalue())
  24. # Parse (2nd time):
  25. asyntax2 = parser.Parser.parse(csyntax)
  26. # Generate .drawio (2nd time):
  27. csyntax2 = io.BytesIO()
  28. generator.generate(asyntax2, csyntax2)
  29. csyntax2.seek(0)
  30. if (csyntax.getvalue() != csyntax2.getvalue()):
  31. # print(csyntax.getvalue())
  32. # print(csyntax2.getvalue())
  33. raise Exception("Files differ after round-trip!")
  34. print(filename, "OK")
  35. class Tests(unittest.TestCase):
  36. def test_1(self):
  37. run_test("test.drawio")
  38. def test_2(self):
  39. run_test("overview.drawio") # we eat our own dog food :)
  40. def test_3(self):
  41. run_test("TrivialPM.drawio")