1414
1515"""Tests for the PushRosNamespace Action."""
1616
17- from launch import LaunchContext
18-
1917from launch_ros .actions import Node
2018from launch_ros .actions import PushRosNamespace
19+ from launch_ros .actions .load_composable_nodes import get_composable_node_load_request
20+ from launch_ros .descriptions import ComposableNode
2121
2222import pytest
2323
2424
25+ class MockContext :
26+
27+ def __init__ (self ):
28+ self .launch_configurations = {}
29+
30+ def perform_substitution (self , sub ):
31+ return sub .perform (None )
32+
33+
2534class Config :
2635
2736 def __init__ (
@@ -39,51 +48,62 @@ def __init__(
3948 self .expected_ns = expected_ns
4049 self .second_push_ns = second_push_ns
4150
51+ def __repr__ (self ):
52+ return (
53+ f'TestConfig(node_name={ self .node_name } , node_ns={ self .node_ns } , '
54+ f'push_ns={ self .push_ns } , expected_ns={ self .expected_ns } , '
55+ f'second_push_ns={ self .second_push_ns } )'
56+ )
4257
43- @pytest .mark .parametrize ('config' , (
44- Config (
45- push_ns = 'relative_ns' ,
46- node_ns = 'node_ns' ,
47- expected_ns = '/relative_ns/node_ns' ),
48- Config (
49- push_ns = 'relative_ns' ,
50- node_ns = '/node_ns' ,
51- expected_ns = '/node_ns' ),
52- Config (
53- push_ns = 'relative_ns' ,
54- node_ns = '/' ,
55- expected_ns = '/' ),
56- Config (
57- push_ns = 'relative_ns' ,
58- node_ns = '' ,
59- expected_ns = '/relative_ns' ),
60- Config (
61- push_ns = 'relative_ns' ,
62- second_push_ns = 'another_relative_ns' ,
63- node_ns = 'node_ns' ,
64- expected_ns = '/relative_ns/another_relative_ns/node_ns' ),
65- Config (
66- push_ns = 'relative_ns' ,
67- second_push_ns = '/absolute_ns' ,
68- node_ns = 'node_ns' ,
69- expected_ns = '/absolute_ns/node_ns' ),
70- Config (
71- node_name = 'my_node' ,
72- push_ns = 'relative_ns' ,
73- second_push_ns = '/absolute_ns' ,
74- node_ns = 'node_ns' ,
75- expected_ns = '/absolute_ns/node_ns' ),
76- Config (
77- node_name = 'my_node' ,
78- node_ns = 'node_ns' ,
79- expected_ns = '/node_ns' ),
80- Config (),
81- Config (
82- push_ns = '' ,
83- expected_ns = '/' ),
84- ))
58+
59+ def get_test_cases ():
60+ return (
61+ Config (
62+ push_ns = 'relative_ns' ,
63+ node_ns = 'node_ns' ,
64+ expected_ns = '/relative_ns/node_ns' ),
65+ Config (
66+ push_ns = 'relative_ns' ,
67+ node_ns = '/node_ns' ,
68+ expected_ns = '/node_ns' ),
69+ Config (
70+ push_ns = 'relative_ns' ,
71+ node_ns = '/' ,
72+ expected_ns = '/' ),
73+ Config (
74+ push_ns = 'relative_ns' ,
75+ node_ns = '' ,
76+ expected_ns = '/relative_ns' ),
77+ Config (
78+ push_ns = 'relative_ns' ,
79+ second_push_ns = 'another_relative_ns' ,
80+ node_ns = 'node_ns' ,
81+ expected_ns = '/relative_ns/another_relative_ns/node_ns' ),
82+ Config (
83+ push_ns = 'relative_ns' ,
84+ second_push_ns = '/absolute_ns' ,
85+ node_ns = 'node_ns' ,
86+ expected_ns = '/absolute_ns/node_ns' ),
87+ Config (
88+ node_name = 'my_node' ,
89+ push_ns = 'relative_ns' ,
90+ second_push_ns = '/absolute_ns' ,
91+ node_ns = 'node_ns' ,
92+ expected_ns = '/absolute_ns/node_ns' ),
93+ Config (
94+ node_name = 'my_node' ,
95+ node_ns = 'node_ns' ,
96+ expected_ns = '/node_ns' ),
97+ Config (),
98+ Config (
99+ push_ns = '' ,
100+ expected_ns = '/' ),
101+ )
102+
103+
104+ @pytest .mark .parametrize ('config' , get_test_cases ())
85105def test_push_ros_namespace (config ):
86- lc = LaunchContext ()
106+ lc = MockContext ()
87107 if config .push_ns is not None :
88108 pns1 = PushRosNamespace (config .push_ns )
89109 pns1 .execute (lc )
@@ -106,3 +126,23 @@ def test_push_ros_namespace(config):
106126 expected_fqn = expected_ns .rstrip ('/' ) + '/' + expected_name
107127 assert expected_ns == node .expanded_node_namespace
108128 assert expected_fqn == node .node_name
129+
130+
131+ @pytest .mark .parametrize ('config' , get_test_cases ())
132+ def test_push_ros_namespace_with_composable_node (config ):
133+ lc = MockContext ()
134+ if config .push_ns is not None :
135+ pns1 = PushRosNamespace (config .push_ns )
136+ pns1 .execute (lc )
137+ if config .second_push_ns is not None :
138+ pns2 = PushRosNamespace (config .second_push_ns )
139+ pns2 .execute (lc )
140+ node_description = ComposableNode (
141+ package = 'asd' ,
142+ plugin = 'my_plugin' ,
143+ namespace = config .node_ns ,
144+ name = config .node_name ,
145+ )
146+ request = get_composable_node_load_request (node_description , lc )
147+ expected_ns = config .expected_ns if config .expected_ns is not None else ''
148+ assert expected_ns == request .node_namespace
0 commit comments