Skip to content

Commit 6607968

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

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed

tests/gtest_ports.cpp

+131
Original file line numberDiff line numberDiff line change
@@ -705,3 +705,134 @@ TEST(PortTest, DefaultWronglyOverriden)
705705
// This is correct
706706
ASSERT_NO_THROW(auto tree = factory.createTreeFromText(xml_txt_correct));
707707
}
708+
709+
class OutputVectorStringNode : public SyncActionNode
710+
{
711+
public:
712+
OutputVectorStringNode(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 InputVectorStringNode : public SyncActionNode
735+
{
736+
public:
737+
InputVectorStringNode(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+
std::vector<std::string> actual_vec;
750+
751+
if(!getInput<std::vector<std::string>>("string_vector", actual_vec))
752+
{
753+
return NodeStatus::FAILURE;
754+
}
755+
if(expected_vec == actual_vec)
756+
{
757+
return NodeStatus::SUCCESS;
758+
}
759+
else
760+
{
761+
return NodeStatus::FAILURE;
762+
}
763+
}
764+
};
765+
766+
class InputVectorDoubleNode : public SyncActionNode
767+
{
768+
public:
769+
InputVectorDoubleNode(const std::string& name, const NodeConfig& config)
770+
: SyncActionNode(name, config)
771+
{}
772+
static PortsList providedPorts()
773+
{
774+
return { InputPort<std::vector<double>>("double_vector", "{double_vector}",
775+
"Vector of doubles.") };
776+
}
777+
778+
NodeStatus tick() override
779+
{
780+
std::vector<double> expected_vec = { 1.0, 2.0 };
781+
std::vector<double> actual_vec;
782+
783+
if(!getInput<std::vector<double>>("double_vector", actual_vec))
784+
{
785+
return NodeStatus::FAILURE;
786+
}
787+
if(expected_vec == actual_vec)
788+
{
789+
return NodeStatus::SUCCESS;
790+
}
791+
else
792+
{
793+
return NodeStatus::FAILURE;
794+
}
795+
}
796+
};
797+
798+
TEST(PortTest, VectorAny)
799+
{
800+
BT::BehaviorTreeFactory factory;
801+
factory.registerNodeType<OutputVectorStringNode>("OutputVectorStringNode");
802+
factory.registerNodeType<InputVectorStringNode>("InputVectorStringNode");
803+
factory.registerNodeType<InputVectorDoubleNode>("InputVectorDoubleNode");
804+
805+
std::string xml_txt_good = R"(
806+
<root BTCPP_format="4" >
807+
<BehaviorTree>
808+
<Sequence name="root_sequence">
809+
<OutputVectorStringNode/>
810+
<InputVectorStringNode/>
811+
</Sequence>
812+
</BehaviorTree>
813+
</root>)";
814+
815+
std::string xml_txt_bad = R"(
816+
<root BTCPP_format="4" >
817+
<BehaviorTree>
818+
<Sequence name="root_sequence">
819+
<OutputVectorStringNode/>
820+
<InputVectorDoubleNode double_vector="{string_vector}"/>
821+
</Sequence>
822+
</BehaviorTree>
823+
</root>)";
824+
825+
// Test that setting and retrieving a vector<string> works.
826+
BT::Tree tree;
827+
ASSERT_NO_THROW(tree = factory.createTreeFromText(xml_txt_good));
828+
829+
BT::NodeStatus status;
830+
ASSERT_NO_THROW(status = tree.tickOnce());
831+
ASSERT_EQ(status, NodeStatus::SUCCESS);
832+
833+
// Test that setting a port as a vector<string> and attempting to retrie it as a vector<double> fails.
834+
ASSERT_NO_THROW(tree = factory.createTreeFromText(xml_txt_bad));
835+
836+
ASSERT_NO_THROW(status = tree.tickOnce());
837+
ASSERT_EQ(status, NodeStatus::FAILURE);
838+
}

0 commit comments

Comments
 (0)