run.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import argparse
  2. import unittest
  3. import functools
  4. from sccd.util.os_tools import *
  5. from sccd.util.debug import *
  6. # A TestCase loading and executing a statechart test file.
  7. class Test(unittest.TestCase):
  8. def __init__(self, src: str, enable_rust: bool):
  9. super().__init__()
  10. self.src = src
  11. self.enable_rust = enable_rust
  12. def __str__(self):
  13. return self.src
  14. def runTest(self):
  15. from sccd.statechart.parser.xml import statechart_parser_rules, parse_f
  16. from sccd.test.parser.xml import test_parser_rules
  17. from sccd.util import timer
  18. # assume external statechart files in same directory as test
  19. try:
  20. if self.enable_rust:
  21. from sccd.test.dynamic.test_rust import run_rust_test
  22. run_rust_test(self.src, self)
  23. else:
  24. path = os.path.dirname(self.src)
  25. sc_rules = functools.partial(statechart_parser_rules, path=path)
  26. test_rules = test_parser_rules(sc_rules)
  27. with timer.Context("parse test"):
  28. test = parse_f(self.src, {"test" :test_rules})
  29. from sccd.test.dynamic.test_interpreter import run_variant
  30. for v in test.variants:
  31. run_variant(v, self)
  32. except Exception as e:
  33. print_debug(e)
  34. raise e
  35. class FailingTest(Test):
  36. @unittest.expectedFailure
  37. def runTest(self):
  38. super().runTest()
  39. if __name__ == '__main__':
  40. argparser = argparse.ArgumentParser(
  41. description="Run SCCD tests.",
  42. epilog="Set environment variable SCCDDEBUG=1 to display debug information about the inner workings of the runtime.")
  43. argparser.add_argument('path', metavar='PATH', type=str, nargs='*', help="Tests to run. Can be a XML file or a directory. If a directory, it will be recursively scanned for XML files.")
  44. argparser.add_argument('--rust', action='store_true', help="Instead of testing the interpreter, generate Rust code from test and run it. Depends on the 'rustc' command in your environment's PATH. Does not depend on Cargo.")
  45. args = argparser.parse_args()
  46. src_files = get_files(args.path,
  47. filter=lambda file: (file.startswith("test_") or file.startswith("fail_")) and file.endswith(".xml"))
  48. if len(src_files) == 0:
  49. print("No input files specified.")
  50. print()
  51. argparser.print_usage()
  52. exit()
  53. suite = unittest.TestSuite()
  54. for src_file in src_files:
  55. should_fail = os.path.basename(src_file).startswith("fail_")
  56. if should_fail:
  57. suite.addTest(FailingTest(src_file, args.rust))
  58. else:
  59. suite.addTest(Test(src_file, args.rust))
  60. unittest.TextTestRunner(verbosity=2).run(suite)