-
Notifications
You must be signed in to change notification settings - Fork 825
Expand file tree
/
Copy pathgtest_substitution.cpp
More file actions
88 lines (71 loc) · 2.27 KB
/
gtest_substitution.cpp
File metadata and controls
88 lines (71 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "behaviortree_cpp/bt_factory.h"
#include <gtest/gtest.h>
using namespace BT;
static const char* json_text = R"(
{
"TestNodeConfigs": {
"TestA": {
"async_delay": 2000,
"return_status": "SUCCESS",
"post_script": "msg ='message SUBSTITUED'"
},
"TestB": {
"return_status": "FAILURE"
}
},
"SubstitutionRules": {
"actionA": "TestA",
"actionB": "TestB",
"actionC": "NotAConfig"
}
}
)";
TEST(Substitution, Parser)
{
BehaviorTreeFactory factory;
factory.loadSubstitutionRuleFromJSON(json_text);
const auto& rules = factory.substitutionRules();
ASSERT_EQ(rules.size(), 3);
ASSERT_EQ(rules.count("actionA"), 1);
ASSERT_EQ(rules.count("actionB"), 1);
ASSERT_EQ(rules.count("actionC"), 1);
auto configA = std::get_if<TestNodeConfig>(&rules.at("actionA"));
ASSERT_EQ(configA->return_status, NodeStatus::SUCCESS);
ASSERT_EQ(configA->async_delay, std::chrono::milliseconds(2000));
ASSERT_EQ(configA->post_script, "msg ='message SUBSTITUED'");
auto configB = std::get_if<TestNodeConfig>(&rules.at("actionB"));
ASSERT_EQ(configB->return_status, NodeStatus::FAILURE);
ASSERT_EQ(configB->async_delay, std::chrono::milliseconds(0));
ASSERT_TRUE(configB->post_script.empty());
ASSERT_EQ(*std::get_if<std::string>(&rules.at("actionC")), "NotAConfig");
}
// Regression test for issue #934: segfault when substituting a SubTree node
TEST(Substitution, SubTreeNodeSubstitution)
{
static const char* parent_xml = R"(
<root BTCPP_format="4">
<BehaviorTree ID="Parent">
<SubTree ID="Child" name="child" />
</BehaviorTree>
</root>
)";
static const char* child_xml = R"(
<root BTCPP_format="4">
<BehaviorTree ID="Child">
<AlwaysSuccess />
</BehaviorTree>
</root>
)";
BehaviorTreeFactory factory;
factory.registerBehaviorTreeFromText(parent_xml);
factory.registerBehaviorTreeFromText(child_xml);
BT::TestNodeConfig config;
config.return_status = BT::NodeStatus::SUCCESS;
factory.addSubstitutionRule("child", config);
// This should not crash (was a segfault before the fix)
Tree tree;
ASSERT_NO_THROW(tree = factory.createTree("Parent"));
// The substituted tree should tick successfully
auto status = tree.tickWhileRunning();
ASSERT_EQ(status, BT::NodeStatus::SUCCESS);
}