Skip to content

Commit a67b52a

Browse files
committed
Add regression test for #858: getInput with default port value
1 parent b3dbe30 commit a67b52a

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

tests/gtest_ports.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,3 +581,58 @@ TEST(PortTest, DefaultWronglyOverriden)
581581
// This is correct
582582
ASSERT_NO_THROW(auto tree = factory.createTreeFromText(xml_txt_correct));
583583
}
584+
585+
// Issue #858: getInput should return the default value declared in
586+
// providedPorts when the XML does not specify the port.
587+
class ActionWithDefaultPort : public SyncActionNode
588+
{
589+
public:
590+
ActionWithDefaultPort(const std::string& name, const NodeConfig& config)
591+
: SyncActionNode(name, config)
592+
{}
593+
594+
NodeStatus tick() override
595+
{
596+
auto res = getInput<std::string>("log_name");
597+
if(!res)
598+
{
599+
throw RuntimeError("getInput failed: " + res.error());
600+
}
601+
result = res.value();
602+
return NodeStatus::SUCCESS;
603+
}
604+
605+
static PortsList providedPorts()
606+
{
607+
return { InputPort<std::string>("log_name", "my_default_logger", "Logger name"),
608+
InputPort<std::string>("message", "Message to be logged") };
609+
}
610+
611+
std::string result;
612+
};
613+
614+
TEST(PortTest, GetInputDefaultValue_Issue858)
615+
{
616+
// XML does NOT specify "log_name" — should use the default from providedPorts
617+
std::string xml_txt = R"(
618+
<root BTCPP_format="4" >
619+
<BehaviorTree ID="Main">
620+
<ActionWithDefaultPort message="hello"/>
621+
</BehaviorTree>
622+
</root>)";
623+
624+
BehaviorTreeFactory factory;
625+
factory.registerNodeType<ActionWithDefaultPort>("ActionWithDefaultPort");
626+
auto tree = factory.createTreeFromText(xml_txt);
627+
auto status = tree.tickWhileRunning();
628+
629+
ASSERT_EQ(status, NodeStatus::SUCCESS);
630+
631+
for(const auto& node : tree.subtrees.front()->nodes)
632+
{
633+
if(auto action = dynamic_cast<ActionWithDefaultPort*>(node.get()))
634+
{
635+
ASSERT_EQ("my_default_logger", action->result);
636+
}
637+
}
638+
}

0 commit comments

Comments
 (0)