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) &&
20  getInput("in_port_B", val_B) &&
21  val_A == 42 && val_B == 66)
22  {
23  return NodeStatus::SUCCESS;
24  }
25  return NodeStatus::FAILURE;
26  }
27 
29  {
30  return { BT::InputPort<int>("in_port_A", 42, "magic_number"),
31  BT::InputPort<int>("in_port_B") };
32  }
33 };
34 
35 TEST(PortTest, DefaultPorts)
36 {
37  std::string xml_txt = R"(
38  <root main_tree_to_execute = "MainTree" >
39  <BehaviorTree ID="MainTree">
40  <NodeWithPorts name = "first" in_port_B="66" />
41  </BehaviorTree>
42  </root>)";
43 
44  BehaviorTreeFactory factory;
45  factory.registerNodeType<NodeWithPorts>("NodeWithPorts");
46 
47  auto tree = factory.createTreeFromText(xml_txt);
48 
49  NodeStatus status = tree.tickRoot();
50  ASSERT_EQ( status, NodeStatus::SUCCESS );
51 }
52 
53 struct MyType
54 {
55  std::string value;
56 };
57 
58 
60 {
61  public:
62  NodeInPorts(const std::string &name, const NodeConfiguration &config)
63  : SyncActionNode(name, config)
64  {}
65 
66 
68  {
69  int val_A = 0;
70  MyType val_B;
71  if( getInput("int_port", val_A) &&
72  getInput("any_port", val_B) )
73  {
74  return NodeStatus::SUCCESS;
75  }
76  return NodeStatus::FAILURE;
77  }
78 
80  {
81  return { BT::InputPort<int>("int_port"),
82  BT::InputPort<MyType>("any_port") };
83  }
84 };
85 
87 {
88  public:
89  NodeOutPorts(const std::string &name, const NodeConfiguration &config)
90  : SyncActionNode(name, config)
91  {}
92 
93 
95  {
96  return NodeStatus::SUCCESS;
97  }
98 
100  {
101  return { BT::OutputPort<int>("int_port"),
102  BT::OutputPort<MyType>("any_port") };
103  }
104 };
105 
106 TEST(PortTest, EmptyPort)
107 {
108  std::string xml_txt = R"(
109  <root main_tree_to_execute = "MainTree" >
110  <BehaviorTree ID="MainTree">
111  <Sequence>
112  <NodeInPorts int_port="{ip}" any_port="{ap}" />
113  <NodeOutPorts int_port="{ip}" any_port="{ap}" />
114  </Sequence>
115  </BehaviorTree>
116  </root>)";
117 
118  BehaviorTreeFactory factory;
119  factory.registerNodeType<NodeOutPorts>("NodeOutPorts");
120  factory.registerNodeType<NodeInPorts>("NodeInPorts");
121 
122  auto tree = factory.createTreeFromText(xml_txt);
123 
124  NodeStatus status = tree.tickRoot();
125  // expect failure because port is not set yet
126  ASSERT_EQ( status, NodeStatus::FAILURE );
127 }
128 
void registerNodeType(const std::string &ID)
Definition: bt_factory.h:290
NodeStatus tick()
Method to be implemented by the user.
Definition: gtest_ports.cpp:94
std::string value
Definition: gtest_ports.cpp:55
static PortsList providedPorts()
Definition: gtest_ports.cpp:99
static PortsList providedPorts()
Definition: gtest_ports.cpp:79
static PortsList providedPorts()
Definition: gtest_ports.cpp:28
The SyncActionNode is an ActionNode that explicitly prevents the status RUNNING and doesn&#39;t require a...
Definition: action_node.h:53
The BehaviorTreeFactory is used to create instances of a TreeNode at run-time.
Definition: bt_factory.h:207
Tree createTreeFromText(const std::string &text, Blackboard::Ptr blackboard=Blackboard::create())
Definition: bt_factory.cpp:249
NodeStatus tick()
Method to be implemented by the user.
Definition: gtest_ports.cpp:67
std::unordered_map< std::string, PortInfo > PortsList
Definition: basic_types.h:317
NodeInPorts(const std::string &name, const NodeConfiguration &config)
Definition: gtest_ports.cpp:62
TEST(PortTest, DefaultPorts)
Definition: gtest_ports.cpp:35
NodeWithPorts(const std::string &name, const NodeConfiguration &config)
Definition: gtest_ports.cpp:9
NodeStatus
Definition: basic_types.h:35
NodeStatus tick()
Method to be implemented by the user.
Definition: gtest_ports.cpp:15
NodeOutPorts(const std::string &name, const NodeConfiguration &config)
Definition: gtest_ports.cpp:89


behaviotree_cpp_v3
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Tue May 4 2021 02:56:24