Skip to content

Commit

Permalink
Add in more tests for init_options coverage. (#1353)
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>
  • Loading branch information
clalancette authored and brawner committed Oct 9, 2020
1 parent 7b73603 commit a3dd124
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
2 changes: 1 addition & 1 deletion rclcpp/src/rclcpp/init_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ InitOptions::InitOptions(rcl_allocator_t allocator)
*init_options_ = rcl_get_zero_initialized_init_options();
rcl_ret_t ret = rcl_init_options_init(init_options_.get(), allocator);
if (RCL_RET_OK != ret) {
rclcpp::exceptions::throw_from_rcl_error(ret, "failed to initialized rcl init options");
rclcpp::exceptions::throw_from_rcl_error(ret, "failed to initialize rcl init options");
}
}

Expand Down
2 changes: 1 addition & 1 deletion rclcpp/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ endif()
ament_add_gtest(test_init_options rclcpp/test_init_options.cpp)
if(TARGET test_init_options)
ament_target_dependencies(test_init_options "rcl")
target_link_libraries(test_init_options ${PROJECT_NAME})
target_link_libraries(test_init_options ${PROJECT_NAME} mimick)
endif()
ament_add_gtest(test_parameter_client rclcpp/test_parameter_client.cpp)
if(TARGET test_parameter_client)
Expand Down
36 changes: 36 additions & 0 deletions rclcpp/test/rclcpp/test_init_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include <gtest/gtest.h>

#include <stdexcept>
#include <string>
#include <vector>

Expand All @@ -22,6 +23,8 @@

#include "rclcpp/init_options.hpp"

#include "../mocking_utils/patch.hpp"
#include "../utils/rclcpp_gtest_macros.hpp"

TEST(TestInitOptions, test_construction) {
rcl_allocator_t allocator = rcl_get_default_allocator();
Expand Down Expand Up @@ -61,3 +64,36 @@ TEST(TestInitOptions, test_initialize_logging) {
EXPECT_FALSE(options.auto_initialize_logging());
}
}

// Required for mocking_utils below
MOCKING_UTILS_BOOL_OPERATOR_RETURNS_FALSE(rcutils_allocator_t, ==)
MOCKING_UTILS_BOOL_OPERATOR_RETURNS_FALSE(rcutils_allocator_t, !=)
MOCKING_UTILS_BOOL_OPERATOR_RETURNS_FALSE(rcutils_allocator_t, <)
MOCKING_UTILS_BOOL_OPERATOR_RETURNS_FALSE(rcutils_allocator_t, >)

TEST(TestInitOptions, constructor_rcl_init_options_init_failed) {
auto mock = mocking_utils::patch_and_return(
"lib:rclcpp", rcl_init_options_init, RCL_RET_ERROR);
RCLCPP_EXPECT_THROW_EQ(
rclcpp::InitOptions(),
std::runtime_error("failed to initialize rcl init options: error not set"));
}

TEST(TestInitOptions, constructor_rcl_init_options_copy_failed) {
rcl_init_options_t rcl_opts;
auto mock = mocking_utils::patch_and_return(
"lib:rclcpp", rcl_init_options_copy, RCL_RET_ERROR);
RCLCPP_EXPECT_THROW_EQ(
new rclcpp::InitOptions(rcl_opts),
std::runtime_error("failed to copy rcl init options: error not set"));
}

TEST(TestInitOptions, copy_constructor_rcl_init_options_copy_failed) {
rclcpp::InitOptions options;
rclcpp::InitOptions options2;
auto mock = mocking_utils::patch_and_return(
"lib:rclcpp", rcl_init_options_copy, RCL_RET_ERROR);
RCLCPP_EXPECT_THROW_EQ(
options2.operator=(options),
std::runtime_error("failed to copy rcl init options: error not set"));
}

0 comments on commit a3dd124

Please sign in to comment.