From 158517e2092f3c3cf221d1810cc845b066d94bca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20=C5=A0togl?= Date: Tue, 16 May 2023 23:28:56 +0200 Subject: [PATCH] Use consequently 'mock' instead of 'fake'. --- .../mock_components/generic_system.hpp | 6 +- .../src/mock_components/generic_system.cpp | 51 ++++++---- .../mock_components/test_generic_system.cpp | 93 ++++++++++++++----- 3 files changed, 107 insertions(+), 43 deletions(-) diff --git a/hardware_interface/include/mock_components/generic_system.hpp b/hardware_interface/include/mock_components/generic_system.hpp index 78ae1488a6..9506139c4a 100644 --- a/hardware_interface/include/mock_components/generic_system.hpp +++ b/hardware_interface/include/mock_components/generic_system.hpp @@ -81,12 +81,12 @@ class HARDWARE_INTERFACE_PUBLIC GenericSystem : public hardware_interface::Syste std::vector sensor_interfaces_; /// The size of this vector is (sensor_interfaces_.size() x nr_joints) - std::vector> sensor_fake_commands_; + std::vector> sensor_mock_commands_; std::vector> sensor_states_; std::vector gpio_interfaces_; /// The size of this vector is (gpio_interfaces_.size() x nr_joints) - std::vector> gpio_fake_commands_; + std::vector> gpio_mock_commands_; std::vector> gpio_commands_; std::vector> gpio_states_; @@ -108,7 +108,7 @@ class HARDWARE_INTERFACE_PUBLIC GenericSystem : public hardware_interface::Syste std::vector & interfaces, std::vector> & storage, std::vector & target_interfaces, bool using_state_interfaces); - bool use_fake_gpio_command_interfaces_; + bool use_mock_gpio_command_interfaces_; bool use_mock_sensor_command_interfaces_; double position_state_following_offset_; diff --git a/hardware_interface/src/mock_components/generic_system.cpp b/hardware_interface/src/mock_components/generic_system.cpp index d2c2e56510..d490784dbd 100644 --- a/hardware_interface/src/mock_components/generic_system.cpp +++ b/hardware_interface/src/mock_components/generic_system.cpp @@ -85,7 +85,7 @@ CallbackReturn GenericSystem::on_init(const hardware_interface::HardwareInfo & i { use_mock_sensor_command_interfaces_ = hardware_interface::parse_bool(it->second); RCUTILS_LOG_WARN_NAMED( - "fake_generic_system", + "mock_generic_system", "Parameter 'fake_sensor_commands' has been deprecated from usage. Use" "'mock_sensor_commands' instead."); } @@ -95,15 +95,28 @@ CallbackReturn GenericSystem::on_init(const hardware_interface::HardwareInfo & i } } - // check if to create fake command interface for gpio - it = info_.hardware_parameters.find("fake_gpio_commands"); + // check if to create mock command interface for gpio + it = info_.hardware_parameters.find("mock_gpio_commands"); if (it != info_.hardware_parameters.end()) { - use_fake_gpio_command_interfaces_ = hardware_interface::parse_bool(it->second); + use_mock_gpio_command_interfaces_ = hardware_interface::parse_bool(it->second); } else { - use_fake_gpio_command_interfaces_ = false; + // check if fake_gpio_commands was set instead and issue warning + it = info_.hardware_parameters.find("fake_gpio_commands"); + if (it != info_.hardware_parameters.end()) + { + use_mock_gpio_command_interfaces_ = hardware_interface::parse_bool(it->second); + RCUTILS_LOG_WARN_NAMED( + "mock_generic_system", + "Parameter 'fake_gpio_commands' has been deprecated from usage. Use" + "'mock_gpio_commands' instead."); + } + else + { + use_mock_gpio_command_interfaces_ = false; + } } // process parameters about state following @@ -188,14 +201,14 @@ CallbackReturn GenericSystem::on_init(const hardware_interface::HardwareInfo & i index_custom_interface_with_following_offset_ = std::distance(other_interfaces_.begin(), if_it); RCUTILS_LOG_INFO_NAMED( - "fake_generic_system", "Custom interface with following offset '%s' found at index: %zu.", + "mock_generic_system", "Custom interface with following offset '%s' found at index: %zu.", custom_interface_with_following_offset_.c_str(), index_custom_interface_with_following_offset_); } else { RCUTILS_LOG_WARN_NAMED( - "fake_generic_system", + "mock_generic_system", "Custom interface with following offset '%s' does not exist. Offset will not be applied", custom_interface_with_following_offset_.c_str()); } @@ -214,7 +227,7 @@ CallbackReturn GenericSystem::on_init(const hardware_interface::HardwareInfo & i } } initialize_storage_vectors( - sensor_fake_commands_, sensor_states_, sensor_interfaces_, info_.sensors); + sensor_mock_commands_, sensor_states_, sensor_interfaces_, info_.sensors); // search for gpio interfaces for (const auto & gpio : info_.gpios) @@ -226,10 +239,10 @@ CallbackReturn GenericSystem::on_init(const hardware_interface::HardwareInfo & i populate_non_standard_interfaces(gpio.state_interfaces, gpio_interfaces_); } - // Fake gpio command interfaces - if (use_fake_gpio_command_interfaces_) + // Mock gpio command interfaces + if (use_mock_gpio_command_interfaces_) { - initialize_storage_vectors(gpio_fake_commands_, gpio_states_, gpio_interfaces_, info_.gpios); + initialize_storage_vectors(gpio_mock_commands_, gpio_states_, gpio_interfaces_, info_.gpios); } // Real gpio command interfaces else @@ -309,22 +322,22 @@ std::vector GenericSystem::export_command_ } } - // Fake sensor command interfaces + // Mock sensor command interfaces if (use_mock_sensor_command_interfaces_) { if (!populate_interfaces( - info_.sensors, sensor_interfaces_, sensor_fake_commands_, command_interfaces, true)) + info_.sensors, sensor_interfaces_, sensor_mock_commands_, command_interfaces, true)) { throw std::runtime_error( "Interface is not found in the standard nor other list. This should never happen!"); } } - // Fake gpio command interfaces (consider all state interfaces for command interfaces) - if (use_fake_gpio_command_interfaces_) + // Mock gpio command interfaces (consider all state interfaces for command interfaces) + if (use_mock_gpio_command_interfaces_) { if (!populate_interfaces( - info_.gpios, gpio_interfaces_, gpio_fake_commands_, command_interfaces, true)) + info_.gpios, gpio_interfaces_, gpio_mock_commands_, command_interfaces, true)) { throw std::runtime_error( "Interface is not found in the gpio list. This should never happen!"); @@ -403,13 +416,13 @@ return_type GenericSystem::read(const rclcpp::Time & /*time*/, const rclcpp::Dur if (use_mock_sensor_command_interfaces_) { - mirror_command_to_state(sensor_states_, sensor_fake_commands_); + mirror_command_to_state(sensor_states_, sensor_mock_commands_); } // do loopback on all gpio interfaces - if (use_fake_gpio_command_interfaces_) + if (use_mock_gpio_command_interfaces_) { - mirror_command_to_state(gpio_states_, gpio_fake_commands_); + mirror_command_to_state(gpio_states_, gpio_mock_commands_); } else { diff --git a/hardware_interface/test/mock_components/test_generic_system.cpp b/hardware_interface/test/mock_components/test_generic_system.cpp index 9fb1aa4988..e82d7d1fb2 100644 --- a/hardware_interface/test/mock_components/test_generic_system.cpp +++ b/hardware_interface/test/mock_components/test_generic_system.cpp @@ -39,8 +39,9 @@ const auto PERIOD = rclcpp::Duration::from_seconds(0.01); class TestGenericSystem : public ::testing::Test { public: - void test_generic_system_with_mock_sensor_commands(std::string & urdf); void test_generic_system_with_mimic_joint(std::string & urdf); + void test_generic_system_with_mock_sensor_commands(std::string & urdf); + void test_generic_system_with_mock_gpio_commands(std::string & urdf); protected: void SetUp() override @@ -185,7 +186,7 @@ class TestGenericSystem : public ::testing::Test )"; - hardware_system_2dof_with_sensor_fake_command_ = + hardware_system_2dof_with_sensor_mock_command_ = R"( @@ -214,7 +215,7 @@ class TestGenericSystem : public ::testing::Test )"; - hardware_system_2dof_with_sensor_fake_command_True_ = + hardware_system_2dof_with_sensor_mock_command_True_ = R"( @@ -381,7 +382,41 @@ class TestGenericSystem : public ::testing::Test )"; - valid_urdf_ros2_control_system_robot_with_gpio_fake_command_ = + valid_urdf_ros2_control_system_robot_with_gpio_mock_command_ = + R"( + + + mock_components/GenericSystem + true + + + + + + + 3.45 + + + + + + + 2.78 + + + + + + + + + + + + +)"; + + valid_urdf_ros2_control_system_robot_with_gpio_mock_command_True_ = R"( @@ -457,14 +492,15 @@ class TestGenericSystem : public ::testing::Test std::string hardware_system_2dof_standard_interfaces_; std::string hardware_system_2dof_with_other_interface_; std::string hardware_system_2dof_with_sensor_; - std::string hardware_system_2dof_with_sensor_fake_command_; - std::string hardware_system_2dof_with_sensor_fake_command_True_; + std::string hardware_system_2dof_with_sensor_mock_command_; + std::string hardware_system_2dof_with_sensor_mock_command_True_; std::string hardware_system_2dof_with_mimic_joint_; std::string hardware_system_2dof_standard_interfaces_with_offset_; std::string hardware_system_2dof_standard_interfaces_with_custom_interface_for_offset_; std::string hardware_system_2dof_standard_interfaces_with_custom_interface_for_offset_missing_; std::string valid_urdf_ros2_control_system_robot_with_gpio_; - std::string valid_urdf_ros2_control_system_robot_with_gpio_fake_command_; + std::string valid_urdf_ros2_control_system_robot_with_gpio_mock_command_; + std::string valid_urdf_ros2_control_system_robot_with_gpio_mock_command_True_; std::string sensor_with_initial_value_; std::string gpio_with_initial_value_; }; @@ -485,12 +521,12 @@ class TestableResourceManager : public hardware_interface::ResourceManager FRIEND_TEST(TestGenericSystem, generic_system_2dof_asymetric_interfaces); FRIEND_TEST(TestGenericSystem, generic_system_2dof_other_interfaces); FRIEND_TEST(TestGenericSystem, generic_system_2dof_sensor); - FRIEND_TEST(TestGenericSystem, generic_system_2dof_sensor_fake_command); - FRIEND_TEST(TestGenericSystem, generic_system_2dof_sensor_fake_command_True); + FRIEND_TEST(TestGenericSystem, generic_system_2dof_sensor_mock_command); + FRIEND_TEST(TestGenericSystem, generic_system_2dof_sensor_mock_command_True); FRIEND_TEST(TestGenericSystem, hardware_system_2dof_with_mimic_joint); FRIEND_TEST(TestGenericSystem, valid_urdf_ros2_control_system_robot_with_gpio); - FRIEND_TEST(TestGenericSystem, valid_urdf_ros2_control_system_robot_with_gpio_fake_command); - FRIEND_TEST(TestGenericSystem, valid_urdf_ros2_control_system_robot_with_gpio_fake_command); + FRIEND_TEST(TestGenericSystem, valid_urdf_ros2_control_system_robot_with_gpio_mock_command); + FRIEND_TEST(TestGenericSystem, valid_urdf_ros2_control_system_robot_with_gpio_mock_command_True); TestableResourceManager() : hardware_interface::ResourceManager() {} @@ -1070,18 +1106,18 @@ void TestGenericSystem::test_generic_system_with_mock_sensor_commands(std::strin ASSERT_EQ(4.44, sty_c.get_value()); } -TEST_F(TestGenericSystem, generic_system_2dof_sensor_fake_command) +TEST_F(TestGenericSystem, generic_system_2dof_sensor_mock_command) { - auto urdf = ros2_control_test_assets::urdf_head + hardware_system_2dof_with_sensor_fake_command_ + + auto urdf = ros2_control_test_assets::urdf_head + hardware_system_2dof_with_sensor_mock_command_ + ros2_control_test_assets::urdf_tail; test_generic_system_with_mock_sensor_commands(urdf); } -TEST_F(TestGenericSystem, generic_system_2dof_sensor_fake_command_True) +TEST_F(TestGenericSystem, generic_system_2dof_sensor_mock_command_True) { auto urdf = ros2_control_test_assets::urdf_head + - hardware_system_2dof_with_sensor_fake_command_True_ + + hardware_system_2dof_with_sensor_mock_command_True_ + ros2_control_test_assets::urdf_tail; test_generic_system_with_mock_sensor_commands(urdf); @@ -1396,11 +1432,8 @@ TEST_F(TestGenericSystem, valid_urdf_ros2_control_system_robot_with_gpio) generic_system_functional_test(urdf); } -TEST_F(TestGenericSystem, valid_urdf_ros2_control_system_robot_with_gpio_fake_command) +void TestGenericSystem::test_generic_system_with_mock_gpio_commands(std::string & urdf) { - auto urdf = ros2_control_test_assets::urdf_head + - valid_urdf_ros2_control_system_robot_with_gpio_fake_command_ + - ros2_control_test_assets::urdf_tail; TestableResourceManager rm(urdf); // check is hardware is started @@ -1507,7 +1540,25 @@ TEST_F(TestGenericSystem, valid_urdf_ros2_control_system_robot_with_gpio_fake_co ASSERT_EQ(2.22, gpio2_vac_c.get_value()); } -TEST_F(TestGenericSystem, sensor_with_initial_value_) +TEST_F(TestGenericSystem, valid_urdf_ros2_control_system_robot_with_gpio_mock_command) +{ + auto urdf = ros2_control_test_assets::urdf_head + + valid_urdf_ros2_control_system_robot_with_gpio_mock_command_ + + ros2_control_test_assets::urdf_tail; + + test_generic_system_with_mock_gpio_commands(urdf); +} + +TEST_F(TestGenericSystem, valid_urdf_ros2_control_system_robot_with_gpio_mock_command_True) +{ + auto urdf = ros2_control_test_assets::urdf_head + + valid_urdf_ros2_control_system_robot_with_gpio_mock_command_True_ + + ros2_control_test_assets::urdf_tail; + + test_generic_system_with_mock_gpio_commands(urdf); +} + +TEST_F(TestGenericSystem, sensor_with_initial_value) { auto urdf = ros2_control_test_assets::urdf_head + sensor_with_initial_value_ + ros2_control_test_assets::urdf_tail; @@ -1535,7 +1586,7 @@ TEST_F(TestGenericSystem, sensor_with_initial_value_) ASSERT_EQ(0.0, force_z_s.get_value()); } -TEST_F(TestGenericSystem, gpio_with_initial_value_) +TEST_F(TestGenericSystem, gpio_with_initial_value) { auto urdf = ros2_control_test_assets::urdf_head + gpio_with_initial_value_ + ros2_control_test_assets::urdf_tail;