gtest_reactive.cpp
Go to the documentation of this file.
1 #include <gtest/gtest.h>
3 #include "test_helper.hpp"
5 
6 using BT::NodeStatus;
7 using std::chrono::milliseconds;
8 
10 {
11 public:
12 
13  SleepNode(const std::string& name, const BT::NodeConfiguration& config):
15 
16  NodeStatus onStart() override {
17  count_ = 0;
18  return NodeStatus::RUNNING;
19  }
20 
21  NodeStatus onRunning() override {
22  return ++count_ < 10 ? NodeStatus::RUNNING : NodeStatus::SUCCESS;
23  }
24 
25  void onHalted() override {}
26 
28  return {};
29  }
30 
31 private:
32  int count_ = 0;
33 };
34 
35 
36 TEST(Reactive, TestLogging)
37 {
38  using namespace BT;
39 
40  static const char* reactive_xml_text = R"(
41 <root>
42  <BehaviorTree ID="Main">
43  <ReactiveSequence>
44  <TestA name="testA"/>
45  <AlwaysSuccess name="success"/>
46  <Sleep/>
47  </ReactiveSequence>
48  </BehaviorTree>
49 </root>
50 )";
51 
52  BehaviorTreeFactory factory;
53 
54  factory.registerNodeType<SleepNode>("Sleep");
55 
56  std::array<int, 1> counters;
57  RegisterTestTick(factory, "Test", counters);
58 
59  auto tree = factory.createTreeFromText(reactive_xml_text);
60  StdCoutLogger logger(tree);
61 
62  auto ret = tree.tickRootWhileRunning();
63  ASSERT_EQ(ret, NodeStatus::SUCCESS);
64 
65  int num_ticks = counters[0];
66  ASSERT_GE(num_ticks, 10);
67 }
68 
69 
BT
Definition: ex01_wrap_legacy.cpp:29
SleepNode::count_
int count_
Definition: gtest_reactive.cpp:32
BT::BehaviorTreeFactory::registerNodeType
void registerNodeType(const std::string &ID)
Definition: bt_factory.h:364
SleepNode::providedPorts
static BT::PortsList providedPorts()
Definition: gtest_reactive.cpp:27
RegisterTestTick
void RegisterTestTick(BT::BehaviorTreeFactory &factory, const std::string &name_prefix, std::array< int, N > &tick_counters)
Definition: test_helper.hpp:13
BT::PortsList
std::unordered_map< std::string, PortInfo > PortsList
Definition: basic_types.h:342
bt_factory.h
BT::TreeNode::config
const NodeConfiguration & config() const
Definition: tree_node.cpp:127
BT::NodeConfiguration
Definition: tree_node.h:44
BT::StatefulActionNode::StatefulActionNode
StatefulActionNode(const std::string &name, const NodeConfiguration &config)
Definition: action_node.h:156
test_helper.hpp
TEST
TEST(Reactive, TestLogging)
Definition: gtest_reactive.cpp:36
SleepNode::onHalted
void onHalted() override
Definition: gtest_reactive.cpp:25
BT::BehaviorTreeFactory::createTreeFromText
Tree createTreeFromText(const std::string &text, Blackboard::Ptr blackboard=Blackboard::create())
Definition: bt_factory.cpp:278
SleepNode::onRunning
NodeStatus onRunning() override
method invoked by a RUNNING action.
Definition: gtest_reactive.cpp:21
bt_cout_logger.h
BT::BehaviorTreeFactory
The BehaviorTreeFactory is used to create instances of a TreeNode at run-time.
Definition: bt_factory.h:251
BT::TreeNode::name
const std::string & name() const
Name of the instance, not the type.
Definition: tree_node.cpp:101
BT::NodeStatus::SUCCESS
@ SUCCESS
SleepNode::onStart
NodeStatus onStart() override
Definition: gtest_reactive.cpp:16
SleepNode
Definition: gtest_reactive.cpp:9
BT::NodeStatus
NodeStatus
Definition: basic_types.h:35
SleepNode::SleepNode
SleepNode(const std::string &name, const BT::NodeConfiguration &config)
Definition: gtest_reactive.cpp:13
BT::StatefulActionNode
The ActionNode is the prefered way to implement asynchronous Actions. It is actually easier to use co...
Definition: action_node.h:153
BT::StdCoutLogger
AddStdCoutLoggerToTree. Give the root node of a tree, a simple callback is subscribed to any status c...
Definition: bt_cout_logger.h:19


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