run_tests.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import os
  2. import sys
  3. import io
  4. from drawio2py import parser, abstract_syntax, generator
  5. import pprint
  6. import tempfile
  7. if __name__ == "__main__":
  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. # Just see if these files parse without throwing an exception :)
  36. run_test("test.drawio")
  37. run_test("overview.drawio") # we eat our own dog food :)
  38. print("Tests passed.")