From bfc43a01d049864423a29a69102b194f60d197ce Mon Sep 17 00:00:00 2001 From: mander76 Date: Wed, 10 May 2023 07:59:40 +0200 Subject: [PATCH] fix of custom command action --- scenariogeneration/xosc/actions.py | 45 ++++++++++-------------------- tests/test_actions.py | 41 +++++++++++++-------------- 2 files changed, 34 insertions(+), 52 deletions(-) diff --git a/scenariogeneration/xosc/actions.py b/scenariogeneration/xosc/actions.py index ddfcf34..a8076d1 100644 --- a/scenariogeneration/xosc/actions.py +++ b/scenariogeneration/xosc/actions.py @@ -5166,37 +5166,19 @@ class UserDefinedAction(_ActionType): Returns the full ElementTree of the class """ - def __init__(self): + def __init__(self, custom_command_action): """initalize the UserDefinedAction Parameters ---------- """ - self.custom_command_actions = [] - - def add_custom_command_action(self, custom_command_action): - """add a CustomCommandAction - - Parameters - ---------- - custom_command_action (CustomCommandAction): A CustomCommandAction element - - """ - self.custom_command_actions.append(custom_command_action) - return self + self.custom_command_action = custom_command_action def __eq__(self, other): if isinstance(other, UserDefinedAction): - if len(self.custom_command_actions) == len(other.custom_command_actions): - if all( - [ - self.custom_command_actions[i] - == other.custom_command_actions[i] - for i in range(len(self.custom_command_actions)) - ] - ): - return True + if self.custom_command_action == other.custom_command_action: + return True return False @staticmethod @@ -5212,17 +5194,16 @@ def parse(element): userDefinedAction (UserDefinedAction): a UserDefinedAction object """ - user_defined_action = UserDefinedAction() - for custom_command_element in element.findall("CustomCommandAction"): - custom_command_action = CustomCommandAction.parse(custom_command_element) - user_defined_action.add_custom_command_action(custom_command_action) + custom_command_action = CustomCommandAction.parse( + element.find("CustomCommandAction") + ) + user_defined_action = UserDefinedAction(custom_command_action) return user_defined_action def get_element(self): """returns the elementTree of the UserDefinedAction""" element = ET.Element("UserDefinedAction") - for custom_command_action in self.custom_command_actions: - element.append(custom_command_action.get_element()) + element.append(self.custom_command_action.get_element()) return element @@ -5245,15 +5226,17 @@ class CustomCommandAction(_ActionType): """ - def __init__(self, type): + def __init__(self, type, content): """initalize the CustomCommandAction Parameters ---------- type (str): type of the custom command + content (str): content of the custom command """ self.type = type + self.content = content def __eq__(self, other): if isinstance(other, CustomCommandAction): @@ -5283,9 +5266,11 @@ def parse(element): raise NotAValidElement( 'CustomCommandAction is missing required argument "type".' ) - return CustomCommandAction(action_type) + + return CustomCommandAction(action_type, element.text) def get_element(self): """returns the elementTree of the CustomCommandAction""" element = ET.Element("CustomCommandAction", attrib={"type": self.type}) + element.text = self.content return element diff --git a/tests/test_actions.py b/tests/test_actions.py index 0db9a29..8211f2f 100644 --- a/tests/test_actions.py +++ b/tests/test_actions.py @@ -933,39 +933,36 @@ def test_trafficstopaction(): def test_customcommandaction(): - cca = OSC.CustomCommandAction("custom_command") + cca = OSC.CustomCommandAction("custom_command", "content") prettyprint(cca) - cca2 = OSC.CustomCommandAction("custom_command") + cca2 = OSC.CustomCommandAction("custom_command", "content") assert cca == cca2 - cca3 = OSC.CustomCommandAction("another_custom_command") + cca3 = OSC.CustomCommandAction("another_custom_command", "content") assert cca != cca3 cca4 = OSC.CustomCommandAction.parse(cca.get_element()) prettyprint(cca4.get_element()) assert cca == cca4 + assert version_validation("CustomCommandAction", cca, 0) == ValidationResponse.OK + assert version_validation("CustomCommandAction", cca, 1) == ValidationResponse.OK + assert version_validation("CustomCommandAction", cca, 2) == ValidationResponse.OK + def test_userdefinedaction(): - uda = OSC.UserDefinedAction() - prettyprint(uda) - uda2 = OSC.UserDefinedAction() - assert uda == uda2 - cca = OSC.CustomCommandAction("custom_command") - uda.add_custom_command_action(cca) + cca = OSC.CustomCommandAction("custom_command", "content") + cca2 = OSC.CustomCommandAction("another_custom_command", "content") + uda = OSC.UserDefinedAction(cca) prettyprint(uda) - assert uda != uda2 - uda2.add_custom_command_action(cca) - assert uda == uda2 - cca2 = OSC.CustomCommandAction("another_custom_command") - uda.add_custom_command_action(cca2) - assert uda != uda2 - uda2.add_custom_command_action(cca2) + uda2 = OSC.UserDefinedAction(cca) assert uda == uda2 - uda3 = OSC.UserDefinedAction.parse(uda.get_element()) - prettyprint(uda3) - assert uda3 == uda - # assert version_validation("UserDefinedAction",uda,0) - # assert version_validation("UserDefinedAction",uda,1) - # assert version_validation("UserDefinedAction",uda,2) + uda3 = OSC.UserDefinedAction(cca2) + assert uda != uda3 + uda4 = OSC.UserDefinedAction.parse(uda.get_element()) + prettyprint(uda4) + assert uda4 == uda + assert version_validation("UserDefinedAction", uda, 0) == ValidationResponse.OK + assert version_validation("UserDefinedAction", uda, 1) == ValidationResponse.OK + assert version_validation("UserDefinedAction", uda, 2) == ValidationResponse.OK def test_lightstateaction():