From 72e6e7302e87e1e4ab3661189204d51125e7b2ff Mon Sep 17 00:00:00 2001 From: Sebastian Castro Date: Wed, 7 Feb 2024 20:07:31 -0500 Subject: [PATCH] Add unit tests --- .../popf_plan_solver.hpp | 4 +- .../test/unit/popf_test.cpp | 54 ++++++++++++++++++- 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/plansys2_popf_plan_solver/include/plansys2_popf_plan_solver/popf_plan_solver.hpp b/plansys2_popf_plan_solver/include/plansys2_popf_plan_solver/popf_plan_solver.hpp index be10610a..6bb6a376 100644 --- a/plansys2_popf_plan_solver/include/plansys2_popf_plan_solver/popf_plan_solver.hpp +++ b/plansys2_popf_plan_solver/include/plansys2_popf_plan_solver/popf_plan_solver.hpp @@ -32,11 +32,11 @@ class POPFPlanSolver : public PlanSolverBase std::string output_dir_parameter_name_; rclcpp_lifecycle::LifecycleNode::SharedPtr lc_node_; - std::optional create_folders(const std::string & node_namespace); - public: POPFPlanSolver(); + std::optional create_folders(const std::string & node_namespace); + void configure(rclcpp_lifecycle::LifecycleNode::SharedPtr, const std::string &); std::optional getPlan( diff --git a/plansys2_popf_plan_solver/test/unit/popf_test.cpp b/plansys2_popf_plan_solver/test/unit/popf_test.cpp index dafd77eb..242265b3 100644 --- a/plansys2_popf_plan_solver/test/unit/popf_test.cpp +++ b/plansys2_popf_plan_solver/test/unit/popf_test.cpp @@ -54,6 +54,20 @@ void test_plan_generation(const std::string & argument = "") ASSERT_EQ(plan.value().items[2].action, "(talk leia jack jack m1)"); } +std::optional test_folder_creation( + const std::string & output_dir = "", const std::string & node_namespace = "") +{ + auto node = rclcpp_lifecycle::LifecycleNode::make_shared("test_node"); + auto planner = std::make_shared(); + + planner->configure(node, "POPF"); + if (!output_dir.empty()) { + node->set_parameter(rclcpp::Parameter("POPF.output_dir", output_dir)); + } + + return planner->create_folders(node_namespace); +} + TEST(popf_plan_solver, generate_plan_good) { test_plan_generation(); @@ -95,7 +109,6 @@ TEST(popf_plan_solver, check_1_ok_domain) ASSERT_TRUE(result); } - TEST(popf_plan_solver, check_2_error_domain) { std::string pkgpath = ament_index_cpp::get_package_share_directory("plansys2_popf_plan_solver"); @@ -113,7 +126,6 @@ TEST(popf_plan_solver, check_2_error_domain) ASSERT_FALSE(result); } - TEST(popf_plan_solver, generate_plan_unsolvable) { std::string pkgpath = ament_index_cpp::get_package_share_directory("plansys2_popf_plan_solver"); @@ -158,6 +170,44 @@ TEST(popf_plan_solver, generate_plan_error) ASSERT_FALSE(plan); } +TEST(popf_plan_solver, create_folder_default) +{ + const auto output_dir = test_folder_creation(); + ASSERT_TRUE(output_dir.has_value()); + EXPECT_EQ(output_dir.value(), std::filesystem::temp_directory_path()); +} + +TEST(popf_plan_solver, create_folder_custom_path) +{ + const auto test_path = std::filesystem::temp_directory_path() / "test" / "path" / "one"; + const auto output_dir = test_folder_creation(test_path); + ASSERT_TRUE(output_dir.has_value()); + EXPECT_EQ(output_dir.value(), test_path); +} + +TEST(popf_plan_solver, create_folder_custom_path_and_namespace) +{ + const auto test_path = std::filesystem::temp_directory_path() / "test" / "path" / "two"; + const auto test_namespace = "/test/node"; + const auto output_dir = test_folder_creation(test_path, test_namespace); + ASSERT_TRUE(output_dir.has_value()); + EXPECT_EQ(output_dir.value(), test_path / "test" / "node"); +} + +TEST(popf_plan_solver, create_folder_filesystem_error) +{ + const auto test_path = std::filesystem::temp_directory_path() / "test" / "path" / "three"; + + // Create a file at the test path to force a filesystem error + std::ofstream out(test_path); + out << "random text\n"; + out.close(); + + const auto test_namespace = "/test/node"; + const auto output_dir = test_folder_creation(test_path, test_namespace); + ASSERT_FALSE(output_dir.has_value()); +} + int main(int argc, char ** argv) { testing::InitGoogleTest(&argc, argv);