def test_launch_description_source_constructors():
    """Test the constructors for LaunchDescriptionSource class."""
    LaunchDescriptionSource()
    LaunchDescriptionSource(LaunchDescription())
    LaunchDescriptionSource(LaunchDescription(), 'location')
    LaunchDescriptionSource(LaunchDescription(), 'location',
                            'method description')
def test_include_launch_description_launch_file_location():
    """Test the ability of the IncludeLaunchDescription class to set the launch file location."""
    ld = LaunchDescription()
    action = IncludeLaunchDescription(LaunchDescriptionSource(ld, '<script>'))
    assert 'IncludeLaunchDescription' in action.describe()
    assert isinstance(action.describe_sub_entities(), list)
    assert isinstance(action.describe_conditional_sub_entities(), list)
    lc1 = LaunchContext()
    # Result should only contain the launch description as there are no launch arguments.
    assert action.visit(lc1) == [ld]
    assert lc1.locals.current_launch_file_directory == '<script>'
    assert action.get_asyncio_future() is None

    this_file = os.path.abspath(__file__)
    ld2 = LaunchDescription()
    action2 = IncludeLaunchDescription(LaunchDescriptionSource(ld2, this_file))
    assert 'IncludeLaunchDescription' in action2.describe()
    assert isinstance(action2.describe_sub_entities(), list)
    assert isinstance(action2.describe_conditional_sub_entities(), list)
    lc2 = LaunchContext()
    # Result should only contain the launch description as there are no launch arguments.
    assert action2.visit(lc2) == [ld2]
    assert lc2.locals.current_launch_file_directory == os.path.dirname(
        this_file)
    assert action2.get_asyncio_future() is None
Exemple #3
0
def test_include_launch_description_methods():
    """Test the methods of the IncludeLaunchDescription class."""
    ld = LaunchDescription()
    action = IncludeLaunchDescription(LaunchDescriptionSource(ld))
    assert 'IncludeLaunchDescription' in action.describe()
    assert isinstance(action.describe_sub_entities(), list)
    assert isinstance(action.describe_conditional_sub_entities(), list)
    assert action.visit(LaunchContext()) == [ld]
    assert action.get_asyncio_future() is None

    ld2 = LaunchDescription([action])
    action2 = IncludeLaunchDescription(LaunchDescriptionSource(ld2))
    assert 'IncludeLaunchDescription' in action2.describe()
    assert isinstance(action2.describe_sub_entities(), list)
    assert isinstance(action2.describe_conditional_sub_entities(), list)
    assert action2.visit(LaunchContext()) == [ld2]
    assert action2.get_asyncio_future() is None
Exemple #4
0
def test_include_launch_description_launch_arguments():
    """Test the interactions between declared launch arguments and IncludeLaunchDescription."""
    # test that arguments are set when given, even if they are not declared
    ld1 = LaunchDescription([])
    action1 = IncludeLaunchDescription(
        LaunchDescriptionSource(ld1),
        launch_arguments={'foo': 'FOO'}.items(),
    )
    assert len(action1.launch_arguments) == 1
    lc1 = LaunchContext()
    result1 = action1.visit(lc1)
    assert len(result1) == 2
    assert isinstance(result1[0], SetLaunchConfiguration)
    assert perform_substitutions(lc1, result1[0].name) == 'foo'
    assert perform_substitutions(lc1, result1[0].value) == 'FOO'
    assert result1[1] == ld1

    # test that a declared argument that is not provided raises an error
    ld2 = LaunchDescription([DeclareLaunchArgument('foo')])
    action2 = IncludeLaunchDescription(LaunchDescriptionSource(ld2))
    lc2 = LaunchContext()
    with pytest.raises(RuntimeError) as excinfo2:
        action2.visit(lc2)
    assert 'Included launch description missing required argument' in str(
        excinfo2)

    # test that a declared argument that is not provided raises an error, but with other args set
    ld2 = LaunchDescription([DeclareLaunchArgument('foo')])
    action2 = IncludeLaunchDescription(
        LaunchDescriptionSource(ld2),
        launch_arguments={'not_foo': 'NOT_FOO'}.items(),
    )
    lc2 = LaunchContext()
    with pytest.raises(RuntimeError) as excinfo2:
        action2.visit(lc2)
    assert 'Included launch description missing required argument' in str(
        excinfo2)
    assert 'not_foo' in str(excinfo2)

    # test that a declared argument with a default value that is not provided does not raise
    ld2 = LaunchDescription(
        [DeclareLaunchArgument('foo', default_value='FOO')])
    action2 = IncludeLaunchDescription(LaunchDescriptionSource(ld2))
    lc2 = LaunchContext()
    action2.visit(lc2)
def test_include_launch_description_methods():
    """Test the methods of the IncludeLaunchDescription class."""
    ld = LaunchDescription()
    action = IncludeLaunchDescription(LaunchDescriptionSource(ld))
    assert 'IncludeLaunchDescription' in action.describe()
    assert isinstance(action.describe_sub_entities(), list)
    assert isinstance(action.describe_conditional_sub_entities(), list)
    # Result should only contain the launch description as there are no launch arguments.
    assert action.visit(LaunchContext()) == [ld]
    assert action.get_asyncio_future() is None
    assert len(action.launch_arguments) == 0

    ld2 = LaunchDescription([action])
    action2 = IncludeLaunchDescription(LaunchDescriptionSource(ld2))
    assert 'IncludeLaunchDescription' in action2.describe()
    assert isinstance(action2.describe_sub_entities(), list)
    assert isinstance(action2.describe_conditional_sub_entities(), list)
    # Result should only contain the launch description as there are no launch arguments.
    assert action2.visit(LaunchContext()) == [ld2]
    assert action2.get_asyncio_future() is None
    assert len(action2.launch_arguments) == 0
Exemple #6
0
def test_launch_description_get_launch_arguments():
    """Test the get_launch_arguments() method of the LaunchDescription class."""
    ld = LaunchDescription([])
    assert len(ld.get_launch_arguments()) == 0

    ld = LaunchDescription([DeclareLaunchArgument('foo')])
    la = ld.get_launch_arguments()
    assert len(la) == 1
    assert la[0]._conditionally_included is False

    ld = LaunchDescription(
        [DeclareLaunchArgument('foo', condition=IfCondition('True'))])
    la = ld.get_launch_arguments()
    assert len(la) == 1
    assert la[0]._conditionally_included is True

    ld = LaunchDescription([
        IncludeLaunchDescription(
            LaunchDescriptionSource(
                LaunchDescription([
                    DeclareLaunchArgument('foo'),
                ]))),
    ])
    la = ld.get_launch_arguments()
    assert len(la) == 1
    assert la[0]._conditionally_included is False

    this_dir = os.path.dirname(os.path.abspath(__file__))
    ld = LaunchDescription([
        IncludeLaunchDescription(
            PythonLaunchDescriptionSource(
                os.path.join(this_dir,
                             'launch_file_with_argument.launch.py'))),
    ])
    la = ld.get_launch_arguments()
    assert len(la) == 1
    assert la[0]._conditionally_included is False

    ld = LaunchDescription([
        IncludeLaunchDescription(
            PythonLaunchDescriptionSource([
                # This will prevent loading of this launch file to find arguments in it.
                ThisLaunchFileDir(),
                'launch_file_with_argument.launch.py',
            ])),
    ])
    la = ld.get_launch_arguments()
    assert len(la) == 0
def test_launch_description_get_launch_arguments():
    """Test the get_launch_arguments() method of the LaunchDescription class."""
    ld = LaunchDescription([])
    assert len(ld.get_launch_arguments()) == 0

    ld = LaunchDescription([DeclareLaunchArgument('foo')])
    la = ld.get_launch_arguments()
    assert len(la) == 1
    assert la[0]._conditionally_included is False

    ld = LaunchDescription(
        [DeclareLaunchArgument('foo', condition=IfCondition('True'))])
    la = ld.get_launch_arguments()
    assert len(la) == 1
    assert la[0]._conditionally_included is True

    ld = LaunchDescription([
        IncludeLaunchDescription(
            LaunchDescriptionSource(
                LaunchDescription([
                    DeclareLaunchArgument('foo'),
                ]))),
    ])
    la = ld.get_launch_arguments()
    assert len(la) == 1

    this_dir = os.path.dirname(os.path.abspath(__file__))
    ld = LaunchDescription([
        IncludeLaunchDescription(
            PythonLaunchDescriptionSource(
                os.path.join(this_dir,
                             'launch_file_with_argument.launch.py'))),
    ])
    la = ld.get_launch_arguments()
    assert len(la) == 1

    # From issue #144: get_launch_arguments was broken when an entitity had conditional
    # sub entities
    class EntityWithConditional(LaunchDescriptionEntity):
        def describe_conditional_sub_entities(self):
            return [('String describing condition',
                     [DeclareLaunchArgument('foo')])]

    ld = LaunchDescription([EntityWithConditional()])
    la = ld.get_launch_arguments()
    assert len(la) == 1
def test_include_launch_description_launch_arguments():
    """Test the interactions between declared launch arguments and IncludeLaunchDescription."""
    # test that arguments are set when given, even if they are not declared
    ld1 = LaunchDescription([])
    action1 = IncludeLaunchDescription(
        LaunchDescriptionSource(ld1),
        launch_arguments={'foo': 'FOO'}.items(),
    )
    assert len(action1.launch_arguments) == 1
    lc1 = LaunchContext()
    result1 = action1.visit(lc1)
    assert len(result1) == 2
    assert isinstance(result1[0], SetLaunchConfiguration)
    assert perform_substitutions(lc1, result1[0].name) == 'foo'
    assert perform_substitutions(lc1, result1[0].value) == 'FOO'
    assert result1[1] == ld1

    # test that a declared argument that is not provided raises an error
    ld2 = LaunchDescription([DeclareLaunchArgument('foo')])
    action2 = IncludeLaunchDescription(LaunchDescriptionSource(ld2))
    lc2 = LaunchContext()
    with pytest.raises(RuntimeError) as excinfo2:
        action2.visit(lc2)
    assert 'Included launch description missing required argument' in str(
        excinfo2.value)

    # test that a declared argument that is not provided raises an error, but with other args set
    ld2 = LaunchDescription([DeclareLaunchArgument('foo')])
    action2 = IncludeLaunchDescription(
        LaunchDescriptionSource(ld2),
        launch_arguments={'not_foo': 'NOT_FOO'}.items(),
    )
    lc2 = LaunchContext()
    with pytest.raises(RuntimeError) as excinfo2:
        action2.visit(lc2)
    assert 'Included launch description missing required argument' in str(
        excinfo2.value)
    assert 'not_foo' in str(excinfo2.value)

    # test that a declared argument with a default value that is not provided does not raise
    ld2 = LaunchDescription(
        [DeclareLaunchArgument('foo', default_value='FOO')])
    action2 = IncludeLaunchDescription(LaunchDescriptionSource(ld2))
    lc2 = LaunchContext()
    action2.visit(lc2)

    # Test that default arguments in nested IncludeLaunchDescription actions do not raise
    ld1 = LaunchDescription(
        [DeclareLaunchArgument('foo', default_value='FOO')])
    action1 = IncludeLaunchDescription(LaunchDescriptionSource(ld1), )
    ld2 = LaunchDescription([action1, DeclareLaunchArgument('foo2')])
    action2 = IncludeLaunchDescription(
        LaunchDescriptionSource(ld2),
        launch_arguments={'foo2': 'FOO2'}.items(),
    )
    lc2 = LaunchContext()
    action2.visit(lc2)

    # Test that provided launch arguments of nested IncludeLaunchDescription actions do not raise
    ld1 = LaunchDescription([DeclareLaunchArgument('foo')])
    action1 = IncludeLaunchDescription(
        LaunchDescriptionSource(ld1),
        launch_arguments={'foo': 'FOO'}.items(),
    )
    ld2 = LaunchDescription([action1, DeclareLaunchArgument('foo2')])
    action2 = IncludeLaunchDescription(
        LaunchDescriptionSource(ld2),
        launch_arguments={'foo2': 'FOO2'}.items(),
    )
    lc2 = LaunchContext()
    action2.visit(lc2)

    # Test that arguments can not be passed from the parent launch description
    ld1 = LaunchDescription([DeclareLaunchArgument('foo')])
    action1 = IncludeLaunchDescription(LaunchDescriptionSource(ld1))
    ld2 = LaunchDescription([action1, DeclareLaunchArgument('foo2')])
    action2 = IncludeLaunchDescription(
        LaunchDescriptionSource(ld2),
        launch_arguments={
            'foo': 'FOO',
            'foo2': 'FOO2'
        }.items(),
    )
    ld3 = LaunchDescription([action2])
    ls = LaunchService()
    ls.include_launch_description(ld3)
    assert 1 == ls.run()

    # Test that arguments can be redeclared in the parent launch description
    ld1 = LaunchDescription([DeclareLaunchArgument('foo')])
    action1 = IncludeLaunchDescription(LaunchDescriptionSource(ld1))
    ld2 = LaunchDescription(
        [action1,
         DeclareLaunchArgument('foo'),
         DeclareLaunchArgument('foo2')])
    action2 = IncludeLaunchDescription(
        LaunchDescriptionSource(ld2),
        launch_arguments={
            'foo': 'FOO',
            'foo2': 'FOO2'
        }.items(),
    )
    lc2 = LaunchContext()
    action2.visit(lc2)

    # Test that arguments after a ResetLaunchConfigurations action are not checked
    ld1 = LaunchDescription([DeclareLaunchArgument('foo')])
    action1 = IncludeLaunchDescription(LaunchDescriptionSource(ld1))
    ld2 = LaunchDescription([
        DeclareLaunchArgument('foo2'),
        ResetLaunchConfigurations(),
        SetLaunchConfiguration('foo', 'asd'), action1
    ])
    action2 = IncludeLaunchDescription(
        LaunchDescriptionSource(ld2),
        launch_arguments={'foo2': 'FOO2'}.items(),
    )
    lc2 = LaunchContext()
    action2.visit(lc2)
def test_include_launch_description_constructors():
    """Test the constructors for IncludeLaunchDescription class."""
    IncludeLaunchDescription(LaunchDescriptionSource(LaunchDescription()))
    IncludeLaunchDescription(LaunchDescriptionSource(LaunchDescription()),
                             launch_arguments={'foo': 'FOO'}.items())
def test_launch_description_source_methods():
    """Test the methods of the LaunchDescriptionSource class."""
    class MockLaunchContext:
        ...

    lds = LaunchDescriptionSource()
    with pytest.raises(RuntimeError):
        lds.get_launch_description(MockLaunchContext())

    ld = LaunchDescription()
    lds = LaunchDescriptionSource(ld)
    assert lds.get_launch_description(MockLaunchContext()) == ld

    ld = LaunchDescription()
    lds = LaunchDescriptionSource(ld, 'location')
    assert lds.get_launch_description(MockLaunchContext()) == ld
    assert lds.location == 'location'

    ld = LaunchDescription()
    lds = LaunchDescriptionSource(ld, 'location', 'method description')
    assert lds.get_launch_description(MockLaunchContext()) == ld
    assert lds.location == 'location'
    assert lds.method == 'method description'
def test_launch_description_source_methods():
    """Test the methods of the LaunchDescriptionSource class."""
    class MockLaunchContext:

        def perform_substitution(self, substitution):
            return substitution.perform(None)

    lds = LaunchDescriptionSource()
    with pytest.raises(RuntimeError):
        lds.get_launch_description(MockLaunchContext())

    ld = LaunchDescription()
    lds = LaunchDescriptionSource(ld)
    assert lds.get_launch_description(MockLaunchContext()) == ld

    ld = LaunchDescription()
    lds = LaunchDescriptionSource(ld, 'location')
    assert lds.get_launch_description(MockLaunchContext()) == ld
    assert lds.location == 'location'

    ld = LaunchDescription()
    lds = LaunchDescriptionSource(ld, 'location', 'method description')
    assert lds.get_launch_description(MockLaunchContext()) == ld
    assert lds.location == 'location'
    assert lds.method == 'method description'
Exemple #12
0
def test_include_launch_description_constructors():
    """Test the constructors for IncludeLaunchDescription class."""
    IncludeLaunchDescription(LaunchDescriptionSource(LaunchDescription()))