From 2421d990ab8f522ce7c9759acf7228041c900903 Mon Sep 17 00:00:00 2001 From: Dominik Authaler Date: Sun, 3 Sep 2023 16:46:17 +0000 Subject: [PATCH] unittests for partial message conversion Signed-off-by: Dominik Authaler --- test_dynmsg/test/test_conversion.cpp | 59 ++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/test_dynmsg/test/test_conversion.cpp b/test_dynmsg/test/test_conversion.cpp index ea42866..ff9206f 100644 --- a/test_dynmsg/test/test_conversion.cpp +++ b/test_dynmsg/test/test_conversion.cpp @@ -932,3 +932,62 @@ TEST(TestConversion, rcl_interfaces__ParameterEvent_cpp) EXPECT_EQ(msg_from_yaml.new_parameters[0].value.type, 1u); EXPECT_EQ(msg_from_yaml.new_parameters[0].value.bool_value, true); } + +TEST(TestConversion, PartialConversion_std_msgs__Header_cpp) +{ + // Start with a ROS message, like a std_msgs/Header + std_msgs::msg::Header msg; + msg.frame_id = "my_frame"; + builtin_interfaces::msg::Time stamp; + stamp.sec = 4; + stamp.nanosec = 20u; + msg.stamp = stamp; + + // Convert solely the 'sec' member of the timestamp to a YAML representation + InterfaceTypeName interface{"std_msgs", "Header"}; + RosMessage_Cpp ros_msg; + ros_msg.type_info = dynmsg::cpp::get_type_info(interface); + ros_msg.data = reinterpret_cast(&msg); + YAML::Node yaml_msg = dynmsg::cpp::selected_member_to_yaml(ros_msg, "stamp/sec"); + std::cout << "'sec' as YAML:" << std::endl; + std::cout << yaml_msg << std::endl << std::endl; + + EXPECT_EQ(yaml_msg.as(), 4); +} + +TEST(TestConversion, PartialConversion_rcl_interfaces__ParameterEvent_cpp) +{ + // Start with a ROS message, like a rcl_interfaces/ParameterEvent + rcl_interfaces::msg::ParameterEvent msg; + + builtin_interfaces::msg::Time stamp; + stamp.sec = 4; + stamp.nanosec = 20u; + + rcl_interfaces::msg::ParameterValue param_value; + param_value.type = 1u; + param_value.bool_value = true; + + rcl_interfaces::msg::Parameter param; + param.name = "the_param_name"; + param.value = param_value; + + msg.stamp = stamp; + msg.node = "/my/node"; + msg.new_parameters.push_back(param); + + // Convert solely the parameter array to a YAML representation + InterfaceTypeName interface{"rcl_interfaces", "ParameterEvent"}; + RosMessage_Cpp ros_msg; + ros_msg.type_info = dynmsg::cpp::get_type_info(interface); + ros_msg.data = reinterpret_cast(&msg); + YAML::Node yaml_msg = dynmsg::cpp::selected_member_to_yaml(ros_msg, "new_parameters"); + std::cout << "partial message to YAML:" << std::endl; + std::cout << yaml_msg << std::endl << std::endl; + + EXPECT_TRUE(yaml_msg.IsSequence()); + EXPECT_EQ(yaml_msg.size(), 1); + EXPECT_STREQ(yaml_msg[0]["name"].as().c_str(), "the_param_name"); + EXPECT_EQ(yaml_msg[0]["value"]["type"].as(), 1u); + EXPECT_EQ(yaml_msg[0]["value"]["bool_value"].as(), true); +}