Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sea-bass committed Feb 8, 2024
1 parent ae332cc commit 72e6e73
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ class POPFPlanSolver : public PlanSolverBase
std::string output_dir_parameter_name_;
rclcpp_lifecycle::LifecycleNode::SharedPtr lc_node_;

std::optional<std::filesystem::path> create_folders(const std::string & node_namespace);

public:
POPFPlanSolver();

std::optional<std::filesystem::path> create_folders(const std::string & node_namespace);

void configure(rclcpp_lifecycle::LifecycleNode::SharedPtr, const std::string &);

std::optional<plansys2_msgs::msg::Plan> getPlan(
Expand Down
54 changes: 52 additions & 2 deletions plansys2_popf_plan_solver/test/unit/popf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::filesystem::path> 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<plansys2::POPFPlanSolver>();

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();
Expand Down Expand Up @@ -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");
Expand All @@ -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");
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 72e6e73

Please sign in to comment.