diff --git a/tests/gtest_ports.cpp b/tests/gtest_ports.cpp index 2f281f87d..9ff0184b8 100644 --- a/tests/gtest_ports.cpp +++ b/tests/gtest_ports.cpp @@ -582,6 +582,61 @@ TEST(PortTest, DefaultWronglyOverriden) ASSERT_NO_THROW(auto tree = factory.createTreeFromText(xml_txt_correct)); } +// Issue #858: getInput should return the default value declared in +// providedPorts when the XML does not specify the port. +class ActionWithDefaultPort : public SyncActionNode +{ +public: + ActionWithDefaultPort(const std::string& name, const NodeConfig& config) + : SyncActionNode(name, config) + {} + + NodeStatus tick() override + { + auto res = getInput("log_name"); + if(!res) + { + throw RuntimeError("getInput failed: " + res.error()); + } + result = res.value(); + return NodeStatus::SUCCESS; + } + + static PortsList providedPorts() + { + return { InputPort("log_name", "my_default_logger", "Logger name"), + InputPort("message", "Message to be logged") }; + } + + std::string result; +}; + +TEST(PortTest, GetInputDefaultValue_Issue858) +{ + // XML does NOT specify "log_name" — should use the default from providedPorts + std::string xml_txt = R"( + + + + + )"; + + BehaviorTreeFactory factory; + factory.registerNodeType("ActionWithDefaultPort"); + auto tree = factory.createTreeFromText(xml_txt); + auto status = tree.tickWhileRunning(); + + ASSERT_EQ(status, NodeStatus::SUCCESS); + + for(const auto& node : tree.subtrees.front()->nodes) + { + if(auto action = dynamic_cast(node.get())) + { + ASSERT_EQ("my_default_logger", action->result); + } + } +} + // Helper class used by Issue #969 test class CollectDoubleAction : public SyncActionNode { diff --git a/tests/gtest_subtree.cpp b/tests/gtest_subtree.cpp index 22ef7837e..442bfbee9 100644 --- a/tests/gtest_subtree.cpp +++ b/tests/gtest_subtree.cpp @@ -759,7 +759,7 @@ TEST(SubTree, DuplicateSubTreeName_Groot2Issue56) BehaviorTreeFactory factory; // Should throw RuntimeError because of duplicate SubTree names - ASSERT_THROW(factory.createTreeFromText(xml_text), RuntimeError); + ASSERT_THROW((void)factory.createTreeFromText(xml_text), RuntimeError); } // Additional test to verify the error message content @@ -786,7 +786,7 @@ TEST(SubTree, DuplicateSubTreeName_ErrorMessage) try { - factory.createTreeFromText(xml_text); + (void)factory.createTreeFromText(xml_text); FAIL() << "Expected RuntimeError to be thrown"; } catch(const RuntimeError& e) @@ -892,5 +892,5 @@ TEST(SubTree, NestedDuplicateNames_ShouldFail) BehaviorTreeFactory factory; // Should throw RuntimeError because of duplicate SubTree names - ASSERT_THROW(factory.createTreeFromText(xml_text), RuntimeError); + ASSERT_THROW((void)factory.createTreeFromText(xml_text), RuntimeError); }