run_tests.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import sys
  2. import os
  3. import importlib
  4. import unittest
  5. from sccd.runtime.statecharts_core import *
  6. class PyTestCase(unittest.TestCase):
  7. def __init__(self, file_name):
  8. unittest.TestCase.__init__(self)
  9. self.file_name = file_name
  10. self.name = os.path.splitext(self.file_name)[0]
  11. self.module = importlib.import_module(self.name.replace(os.path.sep, "."))
  12. def __str__(self):
  13. return self.file_name
  14. def runTest(self):
  15. inputs = self.module.Test.input_events
  16. expected = self.module.Test.expected_events
  17. controller = self.module.Controller(False)
  18. if inputs:
  19. for i in inputs:
  20. controller.addInput(Event(i.name, i.port, i.parameters), int(i.time_offset * 1000))
  21. if not expected:
  22. controller.start()
  23. return
  24. output_ports = set()
  25. expected_result = []
  26. for s in expected:
  27. slot = []
  28. for event in s:
  29. slot.append(event)
  30. output_ports.add(event.port)
  31. if slot:
  32. expected_result.append(slot)
  33. output_listener = controller.addOutputListener(list(output_ports))
  34. def check_output():
  35. # check output
  36. for (slot_index, slot) in enumerate(expected_result, start=1) :
  37. for entry in slot:
  38. output_event = output_listener.fetch(0)
  39. self.assertNotEqual(output_event, None, "Not enough output events on selected ports while checking for event %s" % entry)
  40. matches = True
  41. if output_event.name != entry.name :
  42. matches = False
  43. if output_event.port != entry.port :
  44. matches = False
  45. compare_parameters = output_event.getParameters()
  46. if len(entry.parameters) != len(compare_parameters) :
  47. matches = False
  48. for index in range(len(entry.parameters)) :
  49. if entry.parameters[index] != compare_parameters[index]:
  50. matches = False
  51. self.assertTrue(matches, self.name + ", expected results slot " + str(slot_index) + " mismatch. Expected " + str(entry) + ", but got " + str(output_event) + " instead.") # no match found in the options
  52. # check if there are no extra events
  53. next_event = output_listener.fetch(0)
  54. self.assertEqual(next_event, None, "More output events than expected on selected ports: " + str(next_event))
  55. controller.start()
  56. check_output()
  57. if __name__ == '__main__':
  58. suite = unittest.TestSuite()
  59. for d in os.listdir("target_py"):
  60. subdir = os.path.join("target_py", d)
  61. if not os.path.isdir(subdir):
  62. continue
  63. for f in os.listdir(subdir):
  64. if f.endswith(".py") and not f.startswith("_"):
  65. suite.addTest(PyTestCase(os.path.join(subdir, f)))
  66. unittest.TextTestRunner(verbosity=2).run(suite)