Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions tests/gtest_ports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>("log_name");
if(!res)
{
throw RuntimeError("getInput failed: " + res.error());
}
result = res.value();
return NodeStatus::SUCCESS;
}

static PortsList providedPorts()
{
return { InputPort<std::string>("log_name", "my_default_logger", "Logger name"),
InputPort<std::string>("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"(
<root BTCPP_format="4" >
<BehaviorTree ID="Main">
<ActionWithDefaultPort message="hello"/>
</BehaviorTree>
</root>)";

BehaviorTreeFactory factory;
factory.registerNodeType<ActionWithDefaultPort>("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<ActionWithDefaultPort*>(node.get()))
{
ASSERT_EQ("my_default_logger", action->result);
}
}
}

// Helper class used by Issue #969 test
class CollectDoubleAction : public SyncActionNode
{
Expand Down
6 changes: 3 additions & 3 deletions tests/gtest_subtree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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);
}
Loading