diff --git a/rmw_fastrtps_shared_cpp/test/CMakeLists.txt b/rmw_fastrtps_shared_cpp/test/CMakeLists.txt index fd7d467d2..e6225dea3 100644 --- a/rmw_fastrtps_shared_cpp/test/CMakeLists.txt +++ b/rmw_fastrtps_shared_cpp/test/CMakeLists.txt @@ -18,3 +18,9 @@ if(TARGET test_rmw_init_options) ament_target_dependencies(test_rmw_init_options osrf_testing_tools_cpp rcutils rmw) target_link_libraries(test_rmw_init_options ${PROJECT_NAME}) endif() + +ament_add_gtest(test_names test_names.cpp) +if(TARGET test_names) + ament_target_dependencies(test_names rmw) + target_link_libraries(test_names ${PROJECT_NAME}) +endif() diff --git a/rmw_fastrtps_shared_cpp/test/test_names.cpp b/rmw_fastrtps_shared_cpp/test/test_names.cpp new file mode 100644 index 000000000..9e7210699 --- /dev/null +++ b/rmw_fastrtps_shared_cpp/test/test_names.cpp @@ -0,0 +1,53 @@ +// Copyright 2020 Open Source Robotics Foundation, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "gtest/gtest.h" + +#include "rmw/qos_profiles.h" + +#include "rmw_fastrtps_shared_cpp/names.hpp" +#include "rmw_fastrtps_shared_cpp/namespace_prefix.hpp" + +TEST(NamespaceTest, get_prefix) { + EXPECT_EQ("", _get_ros_prefix_if_exists("")); + for (const auto & prefix : _get_all_ros_prefixes()) { + EXPECT_EQ(prefix, _get_ros_prefix_if_exists(prefix + "/test")); + } +} + +TEST(NamespaceTest, strip_prefix) { + EXPECT_EQ("/no_prefix/test", _strip_ros_prefix_if_exists("/no_prefix/test")); + for (const auto & prefix : _get_all_ros_prefixes()) { + EXPECT_EQ("/test", _strip_ros_prefix_if_exists(prefix + "/test")); + } +} + +TEST(NamespaceTest, resolve_prefix) { + EXPECT_EQ("", _resolve_prefix("/test", "some_ros_prefix")); + EXPECT_EQ( + "/test_some_ros_prefix", + _resolve_prefix("some_ros_prefix/test_some_ros_prefix", "some_ros_prefix")); +} + +TEST(NamespaceTest, name_mangling) { + rmw_qos_profile_t qos_profile = rmw_qos_profile_unknown; + qos_profile.avoid_ros_namespace_conventions = false; + EXPECT_STREQ( + "some_ros_prefix/test__suffix", _create_topic_name( + &qos_profile, "some_ros_prefix", "/test", "__suffix").c_str()); + qos_profile.avoid_ros_namespace_conventions = true; + EXPECT_STREQ( + "/test__suffix", _create_topic_name( + &qos_profile, "some_ros_prefix", "/test", "__suffix").c_str()); +}