From 66079688543b16efc2485119b9195476ce4960b1 Mon Sep 17 00:00:00 2001 From: David Yackzan Date: Wed, 6 Nov 2024 08:22:05 -0700 Subject: [PATCH] Add unit test passing a vector through ports --- tests/gtest_ports.cpp | 131 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) diff --git a/tests/gtest_ports.cpp b/tests/gtest_ports.cpp index 8ba750919..24a5dbd85 100644 --- a/tests/gtest_ports.cpp +++ b/tests/gtest_ports.cpp @@ -705,3 +705,134 @@ TEST(PortTest, DefaultWronglyOverriden) // This is correct ASSERT_NO_THROW(auto tree = factory.createTreeFromText(xml_txt_correct)); } + +class OutputVectorStringNode : public SyncActionNode +{ +public: + OutputVectorStringNode(const std::string& name, const NodeConfig& config) + : SyncActionNode(name, config) + {} + static PortsList providedPorts() + { + return { InputPort("string1", "val1", "First string"), + InputPort("string2", "val2", "Second string"), + OutputPort>("string_vector", "{string_vector}", + "Vector of strings.") }; + } + + NodeStatus tick() override + { + auto string1 = getInput("string1"); + auto string2 = getInput("string2"); + + std::vector out = { string1.value(), string2.value() }; + setOutput("string_vector", out); + return NodeStatus::SUCCESS; + } +}; + +class InputVectorStringNode : public SyncActionNode +{ +public: + InputVectorStringNode(const std::string& name, const NodeConfig& config) + : SyncActionNode(name, config) + {} + static PortsList providedPorts() + { + return { InputPort>("string_vector", "{string_vector}", + "Vector of strings.") }; + } + + NodeStatus tick() override + { + std::vector expected_vec = { "val1", "val2" }; + std::vector actual_vec; + + if(!getInput>("string_vector", actual_vec)) + { + return NodeStatus::FAILURE; + } + if(expected_vec == actual_vec) + { + return NodeStatus::SUCCESS; + } + else + { + return NodeStatus::FAILURE; + } + } +}; + +class InputVectorDoubleNode : public SyncActionNode +{ +public: + InputVectorDoubleNode(const std::string& name, const NodeConfig& config) + : SyncActionNode(name, config) + {} + static PortsList providedPorts() + { + return { InputPort>("double_vector", "{double_vector}", + "Vector of doubles.") }; + } + + NodeStatus tick() override + { + std::vector expected_vec = { 1.0, 2.0 }; + std::vector actual_vec; + + if(!getInput>("double_vector", actual_vec)) + { + return NodeStatus::FAILURE; + } + if(expected_vec == actual_vec) + { + return NodeStatus::SUCCESS; + } + else + { + return NodeStatus::FAILURE; + } + } +}; + +TEST(PortTest, VectorAny) +{ + BT::BehaviorTreeFactory factory; + factory.registerNodeType("OutputVectorStringNode"); + factory.registerNodeType("InputVectorStringNode"); + factory.registerNodeType("InputVectorDoubleNode"); + + std::string xml_txt_good = R"( + + + + + + + + )"; + + std::string xml_txt_bad = R"( + + + + + + + + )"; + + // Test that setting and retrieving a vector works. + BT::Tree tree; + ASSERT_NO_THROW(tree = factory.createTreeFromText(xml_txt_good)); + + BT::NodeStatus status; + ASSERT_NO_THROW(status = tree.tickOnce()); + ASSERT_EQ(status, NodeStatus::SUCCESS); + + // Test that setting a port as a vector and attempting to retrie it as a vector fails. + ASSERT_NO_THROW(tree = factory.createTreeFromText(xml_txt_bad)); + + ASSERT_NO_THROW(status = tree.tickOnce()); + ASSERT_EQ(status, NodeStatus::FAILURE); +}