gtest_ports.cpp
Go to the documentation of this file.
1 #include <gtest/gtest.h>
3 
4 using namespace BT;
5 
7 {
8 public:
9  NodeWithPorts(const std::string& name, const NodeConfiguration& config) :
10  SyncActionNode(name, config)
11  {
12  std::cout << "ctor" << std::endl;
13  }
14 
16  {
17  int val_A = 0;
18  int val_B = 0;
19  if (getInput("in_port_A", val_A) && getInput("in_port_B", val_B) && val_A == 42 &&
20  val_B == 66)
21  {
22  return NodeStatus::SUCCESS;
23  }
24  return NodeStatus::FAILURE;
25  }
26 
28  {
29  return {BT::InputPort<int>("in_port_A", 42, "magic_number"), BT::InputPort<int>("in_"
30  "port"
31  "_"
32  "B")};
33  }
34 };
35 
36 TEST(PortTest, DefaultPorts)
37 {
38  std::string xml_txt = R"(
39  <root main_tree_to_execute = "MainTree" >
40  <BehaviorTree ID="MainTree">
41  <NodeWithPorts name = "first" in_port_B="66" />
42  </BehaviorTree>
43  </root>)";
44 
45  BehaviorTreeFactory factory;
46  factory.registerNodeType<NodeWithPorts>("NodeWithPorts");
47 
48  auto tree = factory.createTreeFromText(xml_txt);
49 
50  NodeStatus status = tree.tickRoot();
51  ASSERT_EQ(status, NodeStatus::SUCCESS);
52 }
53 
54 TEST(PortTest, Descriptions)
55 {
56  std::string xml_txt = R"(
57  <root main_tree_to_execute = "MainTree" >
58  <BehaviorTree ID="MainTree" _description="this is my tree" >
59  <Sequence>
60  <NodeWithPorts name="first" in_port_B="66" _description="this is my action" />
61  <SubTree ID="SubTree" name="second" _description="this is a subtree"/>
62  </Sequence>
63  </BehaviorTree>
64 
65  <BehaviorTree ID="SubTree" _description="this is a subtre" >
66  <NodeWithPorts name="third" in_port_B="99" />
67  </BehaviorTree>
68 
69  </root>)";
70 
71  BehaviorTreeFactory factory;
72  factory.registerNodeType<NodeWithPorts>("NodeWithPorts");
73 
74  auto tree = factory.createTreeFromText(xml_txt);
75 
76  NodeStatus status = tree.tickRoot();
77  ASSERT_EQ(status, NodeStatus::FAILURE); // failure because in_port_B="99"
78 }
79 
80 struct MyType
81 {
82  std::string value;
83 };
84 
86 {
87 public:
88  NodeInPorts(const std::string& name, const NodeConfiguration& config) :
89  SyncActionNode(name, config)
90  {}
91 
93  {
94  int val_A = 0;
95  MyType val_B;
96  if (getInput("int_port", val_A) && getInput("any_port", val_B))
97  {
98  return NodeStatus::SUCCESS;
99  }
100  return NodeStatus::FAILURE;
101  }
102 
104  {
105  return {BT::InputPort<int>("int_port"), BT::InputPort<MyType>("any_port")};
106  }
107 };
108 
110 {
111 public:
112  NodeOutPorts(const std::string& name, const NodeConfiguration& config) :
113  SyncActionNode(name, config)
114  {}
115 
117  {
118  return NodeStatus::SUCCESS;
119  }
120 
122  {
123  return {BT::OutputPort<int>("int_port"), BT::OutputPort<MyType>("any_port")};
124  }
125 };
126 
127 TEST(PortTest, EmptyPort)
128 {
129  std::string xml_txt = R"(
130  <root main_tree_to_execute = "MainTree" >
131  <BehaviorTree ID="MainTree">
132  <Sequence>
133  <NodeInPorts int_port="{ip}" any_port="{ap}" />
134  <NodeOutPorts int_port="{ip}" any_port="{ap}" />
135  </Sequence>
136  </BehaviorTree>
137  </root>)";
138 
139  BehaviorTreeFactory factory;
140  factory.registerNodeType<NodeOutPorts>("NodeOutPorts");
141  factory.registerNodeType<NodeInPorts>("NodeInPorts");
142 
143  auto tree = factory.createTreeFromText(xml_txt);
144 
145  NodeStatus status = tree.tickRoot();
146  // expect failure because port is not set yet
147  ASSERT_EQ(status, NodeStatus::FAILURE);
148 }
149 
151 {
152 public:
153  IllegalPorts(const std::string& name, const NodeConfiguration& config) :
154  SyncActionNode(name, config)
155  {}
156 
158  {
159  return NodeStatus::SUCCESS;
160  }
161 
163  {
164  return {BT::InputPort<std::string>("name")};
165  }
166 };
167 
168 TEST(PortTest, IllegalPorts)
169 {
170  BehaviorTreeFactory factory;
171  ASSERT_ANY_THROW(factory.registerNodeType<IllegalPorts>("nope"));
172 }
BT
Definition: ex01_wrap_legacy.cpp:29
IllegalPorts::tick
NodeStatus tick()
Method to be implemented by the user.
Definition: gtest_ports.cpp:157
BT::BehaviorTreeFactory::registerNodeType
void registerNodeType(const std::string &ID)
Definition: bt_factory.h:364
IllegalPorts
Definition: gtest_ports.cpp:150
BT::PortsList
std::unordered_map< std::string, PortInfo > PortsList
Definition: basic_types.h:342
bt_factory.h
IllegalPorts::IllegalPorts
IllegalPorts(const std::string &name, const NodeConfiguration &config)
Definition: gtest_ports.cpp:153
MyType
Definition: gtest_ports.cpp:80
BT::NodeConfiguration
Definition: tree_node.h:44
NodeOutPorts::NodeOutPorts
NodeOutPorts(const std::string &name, const NodeConfiguration &config)
Definition: gtest_ports.cpp:112
NodeWithPorts::tick
NodeStatus tick()
Method to be implemented by the user.
Definition: gtest_ports.cpp:15
BT::NodeStatus::FAILURE
@ FAILURE
NodeInPorts
Definition: gtest_ports.cpp:85
NodeInPorts::tick
NodeStatus tick()
Method to be implemented by the user.
Definition: gtest_ports.cpp:92
BT::BehaviorTreeFactory::createTreeFromText
Tree createTreeFromText(const std::string &text, Blackboard::Ptr blackboard=Blackboard::create())
Definition: bt_factory.cpp:278
NodeInPorts::providedPorts
static PortsList providedPorts()
Definition: gtest_ports.cpp:103
MyType::value
std::string value
Definition: gtest_ports.cpp:82
NodeOutPorts::tick
NodeStatus tick()
Method to be implemented by the user.
Definition: gtest_ports.cpp:116
BT::BehaviorTreeFactory
The BehaviorTreeFactory is used to create instances of a TreeNode at run-time.
Definition: bt_factory.h:251
BT::NodeStatus::SUCCESS
@ SUCCESS
NodeWithPorts
Definition: gtest_ports.cpp:6
NodeOutPorts::providedPorts
static PortsList providedPorts()
Definition: gtest_ports.cpp:121
NodeWithPorts::NodeWithPorts
NodeWithPorts(const std::string &name, const NodeConfiguration &config)
Definition: gtest_ports.cpp:9
NodeWithPorts::providedPorts
static PortsList providedPorts()
Definition: gtest_ports.cpp:27
TEST
TEST(PortTest, DefaultPorts)
Definition: gtest_ports.cpp:36
NodeInPorts::NodeInPorts
NodeInPorts(const std::string &name, const NodeConfiguration &config)
Definition: gtest_ports.cpp:88
IllegalPorts::providedPorts
static PortsList providedPorts()
Definition: gtest_ports.cpp:162
NodeOutPorts
Definition: gtest_ports.cpp:109
BT::SyncActionNode
The SyncActionNode is an ActionNode that explicitly prevents the status RUNNING and doesn't require a...
Definition: action_node.h:52
BT::NodeStatus
NodeStatus
Definition: basic_types.h:35


behaviortree_cpp_v3
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Wed Jun 26 2024 02:51:19