test_parser.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import os
  2. from sccd.parser.statechart_parser import *
  3. from sccd.parser.expression_parser import *
  4. from lib.test import *
  5. from copy import deepcopy
  6. @dataclass
  7. class TestVariant:
  8. name: str
  9. model: Any
  10. input: list
  11. output: list
  12. # Parses <test> element and all its children (including <statechart>)
  13. class TestParser(StatechartParser):
  14. def __init__(self):
  15. super().__init__()
  16. self.tests = XmlParser.Context("tests")
  17. self.globals = XmlParser.Context("globals")
  18. self.test_input = XmlParser.Context("test_input")
  19. self.test_output = XmlParser.Context("test_output")
  20. self.big_step = XmlParser.Context("big_step")
  21. def end_event(self, el):
  22. big_step = self.big_step.require()
  23. name = el.get("name")
  24. port = el.get("port")
  25. if name is None:
  26. raise Exception("missing attribute 'name'")
  27. if port is None:
  28. raise Exception("missing attribute 'port'")
  29. big_step.append(Event(id=0, name=name, port=port, parameters=[]))
  30. def start_big_step(self, el):
  31. self.test_output.require()
  32. self.big_step.push([])
  33. def end_big_step(self, el):
  34. output = self.test_output.require()
  35. big_step = self.big_step.pop()
  36. output.append(big_step)
  37. def end_input_event(self, el):
  38. input = self.test_input.require()
  39. globals = self.globals.require()
  40. name = el.get("name")
  41. port = el.get("port")
  42. time = el.get("time")
  43. if name is None:
  44. raise Exception("missing attribute 'name'")
  45. if port is None:
  46. raise Exception("missing attribute 'port'")
  47. if time is None:
  48. raise Exception("missing attribute 'time'")
  49. duration = parse_duration(globals, time)
  50. input.append(InputEvent(name=name, port=port, parameters=[], time_offset=duration))
  51. def start_input(self, el):
  52. self.test_input.require()
  53. def end_input(self, el):
  54. pass
  55. def start_output(self, el):
  56. self.test_output.require()
  57. def end_output(self, el):
  58. pass
  59. def start_test(self, el):
  60. self.globals.push(Globals(fixed_delta = None))
  61. self.test_input.push([])
  62. self.test_output.push([])
  63. self.statecharts.push([])
  64. def end_test(self, el):
  65. tests = self.tests.require()
  66. src_file = self.src_file.require()
  67. statecharts = self.statecharts.pop()
  68. input = self.test_input.pop()
  69. output = self.test_output.pop()
  70. globals = self.globals.pop()
  71. if len(statecharts) != 1:
  72. raise Exception("Expected exactly 1 <statechart> node, got %d." % len(statecharts))
  73. statechart = statecharts[0]
  74. globals.process_durations()
  75. variants = statechart.semantics.generate_variants()
  76. def variant_description(i, variant) -> str:
  77. if not variant:
  78. return ""
  79. text = "Semantic variant %d of %d:" % (i+1, len(variants))
  80. for f in fields(variant):
  81. text += "\n %s: %s" % (f.name, getattr(variant, f.name))
  82. return text
  83. # Generate test variants for all semantic wildcards filled in
  84. tests.extend(
  85. TestVariant(
  86. name=variant_description(i, variant),
  87. model=SingleInstanceModel(
  88. globals,
  89. Statechart(tree=statechart.tree, scope=statechart.scope, semantics=variant)),
  90. input=input,
  91. output=output)
  92. for i, variant in enumerate(variants)
  93. )