From 2541bca555bd79b1d79fab21377af0dcbe063e52 Mon Sep 17 00:00:00 2001 From: Kenta Kato Date: Wed, 14 Feb 2024 11:28:26 +0900 Subject: [PATCH] Add unit test to reproduce issue #768 --- tests/gtest_ports.cpp | 91 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 90 insertions(+), 1 deletion(-) diff --git a/tests/gtest_ports.cpp b/tests/gtest_ports.cpp index 0efbb4cc8..5ec3cd763 100644 --- a/tests/gtest_ports.cpp +++ b/tests/gtest_ports.cpp @@ -533,8 +533,66 @@ TEST(PortTest, DefaultInputStrings) ASSERT_EQ(status, NodeStatus::SUCCESS); } +struct TestStruct +{ + int a; + double b; + std::string c; +}; + +class NodeWithDefaultNullptr : public SyncActionNode +{ +public: + NodeWithDefaultNullptr(const std::string& name, const NodeConfig& config) : + SyncActionNode(name, config) {} + + NodeStatus tick() override + { + std::shared_ptr test_struct_ptr; + if (!getInput("input", test_struct_ptr)) + { + throw std::runtime_error("NodeWithDefaultNullptr: failed input"); + } + if (test_struct_ptr == nullptr) + { + std::cout << "vec_ptr is nullptr" << std::endl; + } + return NodeStatus::SUCCESS; + } + + static PortsList providedPorts() + { + return {BT::InputPort< std::shared_ptr >("input", nullptr, "default value is nullptr")}; + } +}; + +class NodeWithDefaultNullopt : public SyncActionNode +{ +public: + NodeWithDefaultNullopt(const std::string& name, const NodeConfig& config) : + SyncActionNode(name, config) {} + + NodeStatus tick() override + { + std::shared_ptr test_struct_ptr; + if (!getInput("input", test_struct_ptr)) + { + throw std::runtime_error("NodeWithDefaultNullopt: failed input"); + } + if (!test_struct_ptr) + { + std::cout << "vec_ptr is std::nullopt" << std::endl; + } + return NodeStatus::SUCCESS; + } + + static PortsList providedPorts() + { + return {BT::InputPort< std::optional >("input", std::nullopt, "default value is nullptr")}; + } +}; -TEST(PortTest, Default_Issues_767_768) +TEST(PortTest, Default_Issues_767) { using namespace BT; @@ -545,4 +603,35 @@ TEST(PortTest, Default_Issues_767_768) ASSERT_NO_THROW(auto p = InputPort>("ptr_B", nullptr, "default nullptr")); } +TEST(PortTest, Default_Issues_768) +{ + using namespace BT; + BehaviorTreeFactory factory; + factory.registerNodeType("NodeWithDefaultNullptr"); + factory.registerNodeType("NodeWithDefaultNullopt"); + BT::NodeStatus status; + BT::Tree tree; + + // Default value is nullptr + std::string xml_txt_nullptr = R"( + + + + + )"; + tree = factory.createTreeFromText(xml_txt_nullptr); + ASSERT_NO_THROW(status = tree.tickWhileRunning()); + ASSERT_EQ(status, NodeStatus::SUCCESS); + + // Default value is std::nullopt + std::string xml_txt_nullopt = R"( + + + + + )"; + tree = factory.createTreeFromText(xml_txt_nullopt); + ASSERT_NO_THROW(status = tree.tickWhileRunning()); + ASSERT_EQ(status, NodeStatus::SUCCESS); +} \ No newline at end of file