Skip to content

Commit

Permalink
Complete test coverage.
Browse files Browse the repository at this point in the history
Signed-off-by: Michel Hidalgo <[email protected]>
  • Loading branch information
hidmic committed May 18, 2020
1 parent be31779 commit b029a31
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ _create_topic_name(
const char * base,
const char * suffix = nullptr)
{
assert(qos_profile);
assert(base);
if (qos_profile->avoid_ros_namespace_conventions) {
prefix = nullptr;
}
Expand Down
6 changes: 4 additions & 2 deletions rmw_fastrtps_shared_cpp/src/namespace_prefix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ const std::vector<std::string> _ros_prefixes =
std::string
_resolve_prefix(const std::string & name, const std::string & prefix)
{
if (name.rfind(prefix, 0) == 0 && name.at(prefix.length()) == '/') {
return name.substr(prefix.length());
if (!name.empty()) {

This comment has been minimized.

Copy link
@brawner

brawner May 18, 2020

Contributor

Maybe I'm missing something, but why doesn't this check need to be added to the other functions?

if (name.rfind(prefix, 0) == 0 && name.at(prefix.length()) == '/') {
return name.substr(prefix.length());
}
}
return "";
}
Expand Down
37 changes: 34 additions & 3 deletions rmw_fastrtps_shared_cpp/test/test_names.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,62 @@

TEST(NamespaceTest, get_prefix) {
EXPECT_EQ("", _get_ros_prefix_if_exists(""));
EXPECT_EQ("", _get_ros_prefix_if_exists("not/a_ros_prefix"));
for (const auto & prefix : _get_all_ros_prefixes()) {
EXPECT_EQ(prefix, _get_ros_prefix_if_exists(prefix + "/test"));
EXPECT_EQ("", _get_ros_prefix_if_exists(prefix + "_should_not_match"));
EXPECT_EQ("", _get_ros_prefix_if_exists("this/should_not_match/" + prefix));
EXPECT_EQ(prefix, _get_ros_prefix_if_exists(prefix + "/should_match"));
}
}

TEST(NamespaceTest, strip_prefix) {
EXPECT_EQ("/no_prefix/test", _strip_ros_prefix_if_exists("/no_prefix/test"));
EXPECT_EQ("", _strip_ros_prefix_if_exists(""));
EXPECT_EQ("/no_ros_prefix/test", _strip_ros_prefix_if_exists("/no_ros_prefix/test"));
for (const auto & prefix : _get_all_ros_prefixes()) {
EXPECT_EQ("/test", _strip_ros_prefix_if_exists(prefix + "/test"));
EXPECT_EQ(
prefix + "should_not_be_stripped",
_strip_ros_prefix_if_exists(prefix + "should_not_be_stripped"));
EXPECT_EQ(
"this/should_not_be_stripped/" + prefix,
_strip_ros_prefix_if_exists("this/should_not_be_stripped/" + prefix));
EXPECT_EQ("/should_be_stripped", _strip_ros_prefix_if_exists(prefix + "/should_be_stripped"));
}
}

TEST(NamespaceTest, resolve_prefix) {
EXPECT_EQ("", _resolve_prefix("", ""));
EXPECT_EQ("", _resolve_prefix("", "some_ros_prefix"));
EXPECT_EQ("", _resolve_prefix("/test", "some_ros_prefix"));
EXPECT_EQ("/test", _resolve_prefix("/test", ""));
EXPECT_EQ(
"/test_some_ros_prefix",
_resolve_prefix("some_ros_prefix/test_some_ros_prefix", "some_ros_prefix"));
EXPECT_EQ(
"", _resolve_prefix("some_ros_prefix_test", "some_ros_prefix"));
EXPECT_EQ(
"", _resolve_prefix("another_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_DEATH(_create_topic_name(nullptr, "", "", ""), "qos_profile");

EXPECT_DEATH(_create_topic_name(&qos_profile, "", nullptr, ""), "base");

EXPECT_STREQ(
"some_ros_prefix/test__suffix", _create_topic_name(
&qos_profile, "some_ros_prefix", "/test", "__suffix").c_str());

EXPECT_STREQ(
"/test__suffix", _create_topic_name(
&qos_profile, nullptr, "/test", "__suffix").c_str());

EXPECT_STREQ(
"some_ros_prefix/test", _create_topic_name(
&qos_profile, "some_ros_prefix", "/test", nullptr).c_str());

qos_profile.avoid_ros_namespace_conventions = true;
EXPECT_STREQ(
"/test__suffix", _create_topic_name(
Expand Down

0 comments on commit b029a31

Please sign in to comment.