|
| 1 | +# Copyright 2025 Open Source Robotics Foundation, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Test parsing a StringJoinSubstitution in XML launch file.""" |
| 16 | + |
| 17 | +import io |
| 18 | +import textwrap |
| 19 | + |
| 20 | +from launch.actions import DeclareLaunchArgument, SetLaunchConfiguration |
| 21 | +from launch.frontend import Parser |
| 22 | +from launch.launch_context import LaunchContext |
| 23 | +from launch.substitutions import StringJoinSubstitution |
| 24 | + |
| 25 | + |
| 26 | +def test_nested(): |
| 27 | + xml_file = textwrap.dedent( |
| 28 | + """ |
| 29 | + <launch> |
| 30 | + <arg name="subdomain" default="wiki"/> |
| 31 | + <let name="url" value="$(string-join . https://$(var subdomain) ros org)"/> |
| 32 | + </launch> |
| 33 | + """ |
| 34 | + ) |
| 35 | + root_entity, parser = Parser.load(io.StringIO(xml_file)) |
| 36 | + ld = parser.parse_description(root_entity) |
| 37 | + |
| 38 | + assert len(ld.entities) == 2 |
| 39 | + assert isinstance(ld.describe_sub_entities()[0], DeclareLaunchArgument) |
| 40 | + assert isinstance(ld.describe_sub_entities()[1], SetLaunchConfiguration) |
| 41 | + |
| 42 | + lc = LaunchContext() |
| 43 | + ld.entities[0].visit(lc) |
| 44 | + |
| 45 | + let = ld.describe_sub_entities()[1] |
| 46 | + assert isinstance(let.value[0], StringJoinSubstitution) |
| 47 | + assert let.value[0].perform(lc) == 'https://wiki.ros.org' |
| 48 | + |
| 49 | + |
| 50 | +def test_delimiter(): |
| 51 | + yaml_file = textwrap.dedent( |
| 52 | + """ |
| 53 | + <launch> |
| 54 | + <arg name="delimiter" default="^_^"/> |
| 55 | + <let name="text" value="$(string-join '($(var delimiter))' a b c)"/> |
| 56 | + </launch> |
| 57 | + """ |
| 58 | + ) |
| 59 | + root_entity, parser = Parser.load(io.StringIO(yaml_file)) |
| 60 | + ld = parser.parse_description(root_entity) |
| 61 | + |
| 62 | + assert len(ld.entities) == 2 |
| 63 | + assert isinstance(ld.entities[0], DeclareLaunchArgument) |
| 64 | + assert isinstance(ld.entities[1], SetLaunchConfiguration) |
| 65 | + |
| 66 | + lc = LaunchContext() |
| 67 | + ld.entities[0].visit(lc) |
| 68 | + |
| 69 | + let = ld.describe_sub_entities()[1] |
| 70 | + assert isinstance(let.value[0], StringJoinSubstitution) |
| 71 | + assert let.value[0].perform(lc) == 'a(^_^)b(^_^)c' |
0 commit comments