Skip to content

Commit

Permalink
Merge pull request pyoscx#162 from pyoscx/custom_command_action_fix
Browse files Browse the repository at this point in the history
fix of custom command action
  • Loading branch information
mander76 authored May 10, 2023
2 parents 425b9ad + bfc43a0 commit e639b4b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 52 deletions.
45 changes: 15 additions & 30 deletions scenariogeneration/xosc/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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


Expand All @@ -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):
Expand Down Expand Up @@ -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
41 changes: 19 additions & 22 deletions tests/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down

0 comments on commit e639b4b

Please sign in to comment.