test_param.cpp
Go to the documentation of this file.
1 // Unit tests for <param> tags
2 // Author: Max Schwarz <max.schwarz@ais.uni-bonn.de>
3 
4 #include <catch_ros/catch.hpp>
5 
6 #include "../../src/launch/launch_config.h"
7 
8 #include "param_utils.h"
9 
10 using namespace rosmon::launch;
11 
12 TEST_CASE("global_param", "[param]")
13 {
14  LaunchConfig config;
15  config.parseString(R"EOF(
16  <launch>
17  <param name="global_param" value="hello_world" />
18  </launch>
19  )EOF");
20 
21  config.evaluateParameters();
22 
23  CAPTURE(config.parameters());
24 
25  auto it = config.parameters().find("/global_param");
26 
27  REQUIRE(it != config.parameters().end());
28 
29  XmlRpc::XmlRpcValue value = it->second;
30  REQUIRE(static_cast<std::string>(value) == "hello_world");
31 }
32 
33 TEST_CASE("param_types", "[param]")
34 {
35  LaunchConfig config;
36  config.parseString(R"EOF(
37  <launch>
38  <param name="int_param_auto" value="0" />
39  <param name="int_param_forced" value="0" type="int" />
40 
41  <param name="double_param_auto" value="0.0" />
42  <param name="double_param_forced" value="0" type="double" />
43 
44  <param name="str_param_auto" value="hello" />
45  <param name="str_param_forced" value="0" type="str" />
46 
47  <param name="bool_param_auto" value="true" />
48  <param name="bool_param_forced" value="true" type="boolean" />
49 
50  <param name="bool_param_auto_nonlowercase" value="True" />
51  <param name="bool_param_forced_nonlowercase" value="True" type="boolean" />
52 
53  <param name="yaml_param" type="yaml" value="test_param: true" />
54  <param name="yaml_param_scalar" type="yaml" value="true" />
55  </launch>
56  )EOF");
57 
58  config.evaluateParameters();
59 
60  auto& params = config.parameters();
61  checkTypedParam<int>(params, "/int_param_auto", XmlRpc::XmlRpcValue::TypeInt, 0);
62  checkTypedParam<int>(params, "/int_param_forced", XmlRpc::XmlRpcValue::TypeInt, 0);
63 
64  checkTypedParam<double>(params, "/double_param_auto", XmlRpc::XmlRpcValue::TypeDouble, 0.0);
65  checkTypedParam<double>(params, "/double_param_forced", XmlRpc::XmlRpcValue::TypeDouble, 0.0);
66 
67  checkTypedParam<std::string>(params, "/str_param_auto", XmlRpc::XmlRpcValue::TypeString, "hello");
68  checkTypedParam<std::string>(params, "/str_param_forced", XmlRpc::XmlRpcValue::TypeString, "0");
69 
70  checkTypedParam<bool>(params, "/bool_param_auto", XmlRpc::XmlRpcValue::TypeBoolean, true);
71  checkTypedParam<bool>(params, "/bool_param_forced", XmlRpc::XmlRpcValue::TypeBoolean, true);
72 
73  checkTypedParam<bool>(params, "/bool_param_auto_nonlowercase", XmlRpc::XmlRpcValue::TypeBoolean, true);
74  checkTypedParam<bool>(params, "/bool_param_forced_nonlowercase", XmlRpc::XmlRpcValue::TypeBoolean, true);
75 
76  checkTypedParam<bool>(params, "/yaml_param/test_param", XmlRpc::XmlRpcValue::TypeBoolean, true);
77  checkTypedParam<bool>(params, "/yaml_param_scalar", XmlRpc::XmlRpcValue::TypeBoolean, true);
78 }
79 
80 TEST_CASE("param command", "[param]")
81 {
82  LaunchConfig config;
83  config.parseString(R"EOF(
84  <launch>
85  <param name="test" command="echo -n hello_world" />
86 
87  <param name="multiline" command="/bin/echo -ne hello\\nworld" />
88 
89  <param name="yaml_param" type="yaml" command="echo test_param: true" />
90  </launch>
91  )EOF");
92 
93  config.evaluateParameters();
94 
95  auto& params = config.parameters();
96 
97  checkTypedParam<std::string>(params, "/test", XmlRpc::XmlRpcValue::TypeString, "hello_world");
98  checkTypedParam<std::string>(params, "/multiline", XmlRpc::XmlRpcValue::TypeString, "hello\nworld");
99 
100  checkTypedParam<bool>(params, "/yaml_param/test_param", XmlRpc::XmlRpcValue::TypeBoolean, true);
101 }
102 
103 TEST_CASE("param failing command", "[param]")
104 {
105  LaunchConfig config;
106  config.parseString(R"EOF(<launch><param name="test" command="false" /></launch>)EOF");
107  REQUIRE_THROWS_AS(
108  config.evaluateParameters(),
110  );
111 }
112 
113 TEST_CASE("param textfile", "[param]")
114 {
115  LaunchConfig config;
116  config.parseString(R"EOF(
117  <launch>
118  <param name="test" textfile="$(find rosmon_core)/test/textfile.txt" />
119  </launch>
120  )EOF");
121 
122  config.evaluateParameters();
123 
124  auto& params = config.parameters();
125 
126  checkTypedParam<std::string>(params, "/test", XmlRpc::XmlRpcValue::TypeString, "hello_world");
127 }
128 
129 TEST_CASE("param textfile does not exist", "[param]")
130 {
131  using Catch::Matchers::Contains;
132 
133  LaunchConfig config;
134  config.parseString(R"EOF(
135  <launch>
136  <param name="test" textfile="$(find rosmon_core)/test/textfile_does_not_exist.txt" />
137  </launch>
138  )EOF");
139 
140  REQUIRE_THROWS_WITH(
141  config.evaluateParameters(),
142  Contains("file")
143  );
144 }
145 
146 TEST_CASE("param binfile", "[param]")
147 {
148  LaunchConfig config;
149  config.parseString(R"EOF(
150  <launch>
151  <param name="test" binfile="$(find rosmon_core)/test/textfile.txt" />
152  </launch>
153  )EOF");
154 
155  config.evaluateParameters();
156 
157  auto& params = config.parameters();
158 
159  CAPTURE(params);
160 
161  auto it = params.find("/test");
162  REQUIRE(it != params.end());
163 
164  XmlRpc::XmlRpcValue value = it->second;
165 
166  REQUIRE(value.getType() == XmlRpc::XmlRpcValue::TypeBase64);
167 
168  std::string expectedData = "hello_world";
169  auto& data = static_cast<XmlRpc::XmlRpcValue::BinaryData&>(value);
170 
171  REQUIRE(expectedData.size() == data.size());
172  for(std::size_t i = 0; i < expectedData.size(); ++i)
173  REQUIRE(data[i] == expectedData[i]);
174 }
175 
176 TEST_CASE("scoped params", "[param]")
177 {
178  std::stringstream warnings;
179 
180  LaunchConfig config;
181  config.setWarningOutput(&warnings);
182  config.parseString(R"EOF(
183  <launch>
184  <param name="global/param" value="abc" />
185  <param name="/global/param2" value="def" />
186 
187  <group ns="namespace">
188  <param name="test" value="val1" />
189  <param name="/test2" value="val2" />
190  </group>
191 
192  <node name="test_node" pkg="rosmon_core" type="abort">
193  <param name="private" value="val3" />
194  <param name="~private2" value="val4" />
195  <param name="/leading_slash" value="val5" />
196  </node>
197  </launch>
198  )EOF");
199 
200  config.evaluateParameters();
201 
202  auto& params = config.parameters();
203 
204  checkTypedParam<std::string>(params, "/global/param", XmlRpc::XmlRpcValue::TypeString, "abc");
205  checkTypedParam<std::string>(params, "/global/param2", XmlRpc::XmlRpcValue::TypeString, "def");
206 
207  checkTypedParam<std::string>(params, "/namespace/test", XmlRpc::XmlRpcValue::TypeString, "val1");
208  checkTypedParam<std::string>(params, "/test2", XmlRpc::XmlRpcValue::TypeString, "val2");
209 
210  checkTypedParam<std::string>(params, "/test_node/private", XmlRpc::XmlRpcValue::TypeString, "val3");
211  checkTypedParam<std::string>(params, "/test_node/private2", XmlRpc::XmlRpcValue::TypeString, "val4");
212 
213  checkTypedParam<std::string>(params, "/test_node/leading_slash", XmlRpc::XmlRpcValue::TypeString, "val5");
214 
215  CAPTURE(warnings.str());
216  CHECK(warnings.str().find("leading slash") != std::string::npos);
217 }
218 
219 TEST_CASE("scoped params with double slash (#49)", "[param]")
220 {
221  LaunchConfig config;
222  config.parseString(R"EOF(
223  <launch>
224  <group ns="/">
225  <param name="param1" value="hello" />
226  </group>
227 
228  <node name="test_node" pkg="rosmon_core" type="abort" ns="/racecar">
229  <param name="private_param" value="hello again" />
230  </node>
231  </launch>
232  )EOF");
233 
234  config.evaluateParameters();
235 
236  auto& params = config.parameters();
237 
238  checkTypedParam<std::string>(params, "/param1", XmlRpc::XmlRpcValue::TypeString, "hello");
239  checkTypedParam<std::string>(params, "/racecar/test_node/private_param", XmlRpc::XmlRpcValue::TypeString, "hello again");
240 }
241 
242 TEST_CASE("wrong param types", "[param]")
243 {
244  REQUIRE_THROWS_AS(
245  LaunchConfig().parseString(R"EOF(<launch><param name="test" value="abc" type="int" /></launch>)EOF"),
247  );
248 
249  REQUIRE_THROWS_AS(
250  LaunchConfig().parseString(R"EOF(<launch><param name="test" value="0.5" type="int" /></launch>)EOF"),
252  );
253 
254  REQUIRE_THROWS_AS(
255  LaunchConfig().parseString(R"EOF(<launch><param name="test" value="0.5" type="bool" /></launch>)EOF"),
257  );
258 
259  REQUIRE_THROWS_AS(
260  LaunchConfig().parseString(R"EOF(<launch><param name="test" value="invalid: {{ yaml}} here" type="yaml" /></launch>)EOF"),
262  );
263 
264  {
265  LaunchConfig config;
266 
267  config.parseString(R"EOF(<launch><param name="test" command="echo -ne invalid: {{ yaml}} here" type="yaml" /></launch>)EOF");
268 
269  REQUIRE_THROWS_AS(
270  config.evaluateParameters(),
272  );
273  }
274 
275  REQUIRE_THROWS_AS(
276  LaunchConfig().parseString(R"EOF(<launch><param name="test" value="0.5" type="unknown_type" /></launch>)EOF"),
278  );
279 }
280 
281 TEST_CASE("invalid param input combinations", "[param]")
282 {
283  REQUIRE_THROWS_AS(
284  LaunchConfig().parseString(R"EOF(<launch><param name="test" value="abc" command="echo -ne test" /></launch>)EOF"),
286  );
287 
288  REQUIRE_THROWS_AS(
289  LaunchConfig().parseString(R"EOF(<launch><param name="test" textfile="$(find rosmon_core)/test/textfile.txt" command="echo -ne test" /></launch>)EOF"),
291  );
292 
293  REQUIRE_THROWS_AS(
294  LaunchConfig().parseString(R"EOF(<launch><param name="test" /></launch>)EOF"),
296  );
297 }
298 
299 TEST_CASE("invalid param names", "[param]")
300 {
301  using Catch::Matchers::Contains;
302 
303  REQUIRE_THROWS_WITH(
304  LaunchConfig().parseString(R"EOF(<launch><param name="$%*" value="abc" /></launch>)EOF"),
305  Contains("$%*")
306  );
307 
308  REQUIRE_THROWS_WITH(
309  LaunchConfig().parseString(R"EOF(<launch><param value="abc" /></launch>)EOF"),
310  Contains("name")
311  );
312 }
313 
314 TEST_CASE("param whitespace", "[param]")
315 {
316  LaunchConfig config;
317  config.parseString(R"EOF(
318 <launch>
319  <param name="string" type="string" value=" 0.5 "/>
320  <param name="double" type="double" value=" 0.5 "/>
321  <param name="auto_string" value=" Hallo " />
322  <param name="auto_double" value=" 0.6 " />
323  <param name="auto_int" value="6" />
324  <param name="auto_bool" value=" true " />
325 
326  <param name="command_param_string" type="string" command="echo ' Hallo '" />
327  <param name="command_param_double" type="double" command="echo -n ' 1.23 '" />
328  <param name="command_param_yaml" type="yaml" command="echo -n 1.23" />
329 
330  <param name="textfile_param_string" type="string" textfile="$(find rosmon_core)/test/textfile_double.txt" />
331  <param name="textfile_param_double" type="double" textfile="$(find rosmon_core)/test/textfile_double.txt" />
332  <param name="textfile_param_yaml" type="yaml" textfile="$(find rosmon_core)/test/analyzers.yaml" />
333 
334  <param name="multiple_lines1" value="first_line
335  second_line" />
336  <param name="multiple_lines2" command="echo -n first_line
337  second_line" />
338 </launch>
339  )EOF");
340 
341  config.evaluateParameters();
342 
343  auto& params = config.parameters();
344 
345  using V = XmlRpc::XmlRpcValue;
346 
347  CHECK(getTypedParam<std::string>(params, "/string", V::TypeString) == "0.5");
348  CHECK(getTypedParam<double>(params, "/double", V::TypeDouble) == Approx(0.5));
349  CHECK(getTypedParam<std::string>(params, "/auto_string", V::TypeString) == "Hallo");
350  CHECK(getTypedParam<double>(params, "/auto_double", V::TypeDouble) == Approx(0.6));
351  CHECK(getTypedParam<int>(params, "/auto_int", V::TypeInt) == 6);
352  CHECK(getTypedParam<bool>(params, "/auto_bool", V::TypeBoolean) == true);
353 
354  CHECK(getTypedParam<std::string>(params, "/command_param_string", V::TypeString) == " Hallo \n");
355  CHECK(getTypedParam<double>(params, "/command_param_double", V::TypeDouble) == Approx(1.23));
356 
357  CHECK(getTypedParam<std::string>(params, "/multiple_lines1", V::TypeString) == "first_line second_line");
358  CHECK(getTypedParam<std::string>(params, "/multiple_lines2", V::TypeString) == "first_line second_line");
359 }
360 
auto & params
Definition: test_param.cpp:226
const std::map< std::string, XmlRpc::XmlRpcValue > & parameters() const
void setWarningOutput(std::ostream *warningStream)
void parseString(const std::string &input, bool onlyArguments=false)
TEST_CASE("global_param","[param]")
Definition: test_param.cpp:12
config parseString(R"EOF( <launch> <group ns="/"> <param name="param1" value="hello" /> </group> <node name="test_node" pkg="rosmon_core" type="abort" ns="/racecar"> <param name="private_param" value="hello again" /> </node> </launch> )EOF")
std::vector< char > BinaryData


rosmon_core
Author(s): Max Schwarz
autogenerated on Sat Jan 9 2021 03:35:43