Skip to content

Commit 55a9f5a

Browse files
committed
Add unit test passing a vector through ports
1 parent 9ce8b86 commit 55a9f5a

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

tests/gtest_ports.cpp

+76
Original file line numberDiff line numberDiff line change
@@ -705,3 +705,79 @@ TEST(PortTest, DefaultWronglyOverriden)
705705
// This is correct
706706
ASSERT_NO_THROW(auto tree = factory.createTreeFromText(xml_txt_correct));
707707
}
708+
709+
class OutputVectorNode : public SyncActionNode
710+
{
711+
public:
712+
OutputVectorNode(const std::string& name, const NodeConfig& config)
713+
: SyncActionNode(name, config)
714+
{}
715+
static PortsList providedPorts()
716+
{
717+
return { InputPort<std::string>("string1", "val1", "First string"),
718+
InputPort<std::string>("string2", "val2", "Second string"),
719+
OutputPort<std::vector<std::string>>("string_vector", "{string_vector}",
720+
"Vector of strings.") };
721+
}
722+
723+
NodeStatus tick() override
724+
{
725+
auto string1 = getInput<std::string>("string1");
726+
auto string2 = getInput<std::string>("string2");
727+
728+
std::vector<std::string> out = { string1.value(), string2.value() };
729+
setOutput("string_vector", out);
730+
return NodeStatus::SUCCESS;
731+
}
732+
};
733+
734+
class InputVectorNode : public SyncActionNode
735+
{
736+
public:
737+
InputVectorNode(const std::string& name, const NodeConfig& config)
738+
: SyncActionNode(name, config)
739+
{}
740+
static PortsList providedPorts()
741+
{
742+
return { InputPort<std::vector<std::string>>("string_vector", "{string_vector}",
743+
"Vector of strings.") };
744+
}
745+
746+
NodeStatus tick() override
747+
{
748+
std::vector<std::string> expected_vec = { "val1", "val2" };
749+
auto actual_vec = getInput<std::vector<std::string>>("string_vector");
750+
if(expected_vec == actual_vec.value())
751+
{
752+
return NodeStatus::SUCCESS;
753+
}
754+
else
755+
{
756+
return NodeStatus::FAILURE;
757+
}
758+
}
759+
};
760+
761+
TEST(PortTest, VectorAny)
762+
{
763+
BT::BehaviorTreeFactory factory;
764+
factory.registerNodeType<OutputVectorNode>("OutputVectorNode");
765+
factory.registerNodeType<InputVectorNode>("InputVectorNode");
766+
767+
std::string xml_txt = R"(
768+
<root BTCPP_format="4" >
769+
<BehaviorTree>
770+
<Sequence name="root_sequence">
771+
<OutputVectorNode/>
772+
<InputVectorNode/>
773+
</Sequence>
774+
</BehaviorTree>
775+
</root>)";
776+
777+
BT::Tree tree;
778+
ASSERT_NO_THROW(tree = factory.createTreeFromText(xml_txt));
779+
780+
BT::NodeStatus status;
781+
ASSERT_NO_THROW(status = tree.tickOnce());
782+
ASSERT_EQ(status, NodeStatus::SUCCESS);
783+
}

0 commit comments

Comments
 (0)