Skip to content

Commit

Permalink
Add unit test passing a vector through ports
Browse files Browse the repository at this point in the history
  • Loading branch information
dyackzan committed Nov 6, 2024
1 parent 9ce8b86 commit 55a9f5a
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions tests/gtest_ports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -705,3 +705,79 @@ TEST(PortTest, DefaultWronglyOverriden)
// This is correct
ASSERT_NO_THROW(auto tree = factory.createTreeFromText(xml_txt_correct));
}

class OutputVectorNode : public SyncActionNode
{
public:
OutputVectorNode(const std::string& name, const NodeConfig& config)
: SyncActionNode(name, config)
{}
static PortsList providedPorts()
{
return { InputPort<std::string>("string1", "val1", "First string"),
InputPort<std::string>("string2", "val2", "Second string"),
OutputPort<std::vector<std::string>>("string_vector", "{string_vector}",
"Vector of strings.") };
}

NodeStatus tick() override
{
auto string1 = getInput<std::string>("string1");
auto string2 = getInput<std::string>("string2");

std::vector<std::string> out = { string1.value(), string2.value() };
setOutput("string_vector", out);
return NodeStatus::SUCCESS;
}
};

class InputVectorNode : public SyncActionNode
{
public:
InputVectorNode(const std::string& name, const NodeConfig& config)
: SyncActionNode(name, config)
{}
static PortsList providedPorts()
{
return { InputPort<std::vector<std::string>>("string_vector", "{string_vector}",
"Vector of strings.") };
}

NodeStatus tick() override
{
std::vector<std::string> expected_vec = { "val1", "val2" };
auto actual_vec = getInput<std::vector<std::string>>("string_vector");
if(expected_vec == actual_vec.value())
{
return NodeStatus::SUCCESS;
}
else
{
return NodeStatus::FAILURE;
}
}
};

TEST(PortTest, VectorAny)
{
BT::BehaviorTreeFactory factory;
factory.registerNodeType<OutputVectorNode>("OutputVectorNode");
factory.registerNodeType<InputVectorNode>("InputVectorNode");

std::string xml_txt = R"(
<root BTCPP_format="4" >
<BehaviorTree>
<Sequence name="root_sequence">
<OutputVectorNode/>
<InputVectorNode/>
</Sequence>
</BehaviorTree>
</root>)";

BT::Tree tree;
ASSERT_NO_THROW(tree = factory.createTreeFromText(xml_txt));

BT::NodeStatus status;
ASSERT_NO_THROW(status = tree.tickOnce());
ASSERT_EQ(status, NodeStatus::SUCCESS);
}

0 comments on commit 55a9f5a

Please sign in to comment.