Skip to content

Commit

Permalink
Correct parameters parsing and test
Browse files Browse the repository at this point in the history
Signed-off-by: ivanpauno <ivanpauno@ekumenlabs.com>
  • Loading branch information
ivanpauno committed Jul 8, 2019
1 parent c1c1d6a commit 3a95e38
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
13 changes: 11 additions & 2 deletions launch_ros/launch_ros/actions/node.py
Expand Up @@ -32,6 +32,7 @@
import launch.logging
from launch.some_substitutions_type import SomeSubstitutionsType
from launch.substitutions import LocalSubstitution
from launch.substitutions import TextSubstitution
from launch.utilities import ensure_argument_type
from launch.utilities import normalize_to_list_of_substitutions
from launch.utilities import perform_substitutions
Expand Down Expand Up @@ -190,8 +191,16 @@ def get_nested_dictionary_from_nested_key_value_pairs(params):
if value is not None and nested_params:
raise RuntimeError('param and value attributes are mutually exclusive')
elif value is not None:
if isinstance(value, str):
value = parser.parse_substitution(value)
def normalize_scalar_value(value):
if isinstance(value, str):
value = parser.parse_substitution(value)
if len(value) == 1 and isinstance(value[0], TextSubstitution):
value = value[0].text # python `str` are not converted like yaml
return value
if isinstance(value, list):
value = [normalize_scalar_value(x) for x in value]
else:
value = normalize_scalar_value(value)
param_dict[name] = value
elif nested_params:
param_dict.update({
Expand Down
28 changes: 13 additions & 15 deletions test_launch_ros/test/test_launch_ros/frontend/test_node_frontend.py
Expand Up @@ -25,13 +25,11 @@

import pytest

# Scaping the quote is needed in 'a_string' launch configuration,
# becuase of how the substitution grammar works.
yaml_params = str(pathlib.Path(__file__).parent / 'params.yaml')
xml_file = \
"""\
r"""
<launch>
<let name="a_string" value="\\'[2, 5, 8]\\'"/>
<let name="a_string" value="\'[2, 5, 8]\'"/>
<let name="a_list" value="[2, 5, 8]"/>
<node package="demo_nodes_py" executable="talker_qos" output="screen" name="my_node" namespace="my_ns" args="--number_of_cycles 1">
<param name="param1" value="ads"/>
Expand All @@ -45,9 +43,9 @@
<param name="param6" value="2., 5., 8." value-sep=", "/>
<param name="param7" value="'2', '5', '8'" value-sep=", "/>
<param name="param8" value="''2'', ''5'', ''8''" value-sep=", "/>
<param name="param9" value="\\'2\\', \\'5\\', \\'8\\'" value-sep=", "/>
<param name="param9" value="\'2\', \'5\', \'8\'" value-sep=", "/>
<param name="param10" value="''asd'', ''bsd'', ''csd''" value-sep=", "/>
<param name="param11" value="'\\asd', '\\bsd', '\\csd'" value-sep=", "/>
<param name="param11" value="'\asd', '\bsd', '\csd'" value-sep=", "/>
</param>
<param from="{}"/>
<env name="var" value="1"/>
Expand All @@ -56,11 +54,11 @@
""".format(yaml_params) # noqa: E501
xml_file = textwrap.dedent(xml_file)
yaml_file = \
"""\
r"""
launch:
- let:
name: 'a_string'
value: '\\"[2, 5, 8]\\"'
value: "'[2, 5, 8]'"
- let:
name: 'a_list'
value: '[2, 5, 8]'
Expand Down Expand Up @@ -97,7 +95,7 @@
- name: param10
value: ["'asd'", "'bsd'", "'csd'"]
- name: param11
value: ["\\asd", "\\bsd", "\\csd"]
value: ['\asd', '\bsd', '\csd']
- from: {}
env:
- name: var
Expand Down Expand Up @@ -137,9 +135,9 @@ def test_node_frontend(file):
assert param_dict['param_group1.param3'] == (2, 5, 8)
assert param_dict['param_group1.param4'] == (2, 5, 8)
assert param_dict['param_group1.param5'] == '[2, 5, 8]'
assert param_dict['param_group1.param6'] == [2., 5., 8.]
assert param_dict['param_group1.param7'] == ['2', '5', '8']
assert param_dict['param_group1.param8'] == ["'2'", "'5'", "'8'"]
assert param_dict['param_group1.param9'] == ["'2'", "'5'", "'8'"]
assert param_dict['param_group1.param10'] == ["'asd'", "'bsd'", "'csd'"]
assert param_dict['param_group1.param11'] == ['asd', 'bsd', 'csd']
assert param_dict['param_group1.param6'] == (2., 5., 8.)
assert param_dict['param_group1.param7'] == ('2', '5', '8')
assert param_dict['param_group1.param8'] == ("'2'", "'5'", "'8'")
assert param_dict['param_group1.param9'] == ("'2'", "'5'", "'8'")
assert param_dict['param_group1.param10'] == ("'asd'", "'bsd'", "'csd'")
assert param_dict['param_group1.param11'] == ('asd', 'bsd', 'csd')

0 comments on commit 3a95e38

Please sign in to comment.