-
Notifications
You must be signed in to change notification settings - Fork 826
Expand file tree
/
Copy pathgtest_duplicate_tree.cpp
More file actions
218 lines (183 loc) · 5.51 KB
/
gtest_duplicate_tree.cpp
File metadata and controls
218 lines (183 loc) · 5.51 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/* Copyright (C) 2026 - duplicate BehaviorTree ID registration tests */
#include "behaviortree_cpp/bt_factory.h"
#include <chrono>
#include <filesystem>
#include <fstream>
#include <string>
#include <gtest/gtest.h>
using namespace BT;
namespace
{
class TempSubdir
{
public:
TempSubdir()
{
const auto stamp = std::chrono::steady_clock::now().time_since_epoch().count();
path = std::filesystem::temp_directory_path() /
("bt_duplicate_tree_" + std::to_string(stamp));
std::filesystem::create_directories(path);
}
~TempSubdir()
{
std::error_code ec;
std::filesystem::remove_all(path, ec);
}
std::filesystem::path path;
TempSubdir(const TempSubdir&) = delete;
TempSubdir& operator=(const TempSubdir&) = delete;
};
void writeFile(const std::filesystem::path& p, const std::string& content)
{
std::ofstream out(p.string());
out << content;
}
std::string minimalTreeXml(const std::string& tree_id)
{
return std::string(R"(
<root BTCPP_format="4">
<BehaviorTree ID=")" +
tree_id + R"(">
<AlwaysSuccess/>
</BehaviorTree>
</root>
)");
}
} // namespace
TEST(DuplicateBehaviorTreeId, DuplicateIdAcrossFiles)
{
TempSubdir dir;
const auto path_a = dir.path / "a.xml";
const auto path_b = dir.path / "b.xml";
writeFile(path_a, minimalTreeXml("DupTree"));
writeFile(path_b, minimalTreeXml("DupTree"));
const std::string abs_a = std::filesystem::absolute(path_a).string();
const std::string abs_b = std::filesystem::absolute(path_b).string();
BehaviorTreeFactory factory;
ASSERT_NO_THROW(factory.registerBehaviorTreeFromFile(path_a));
try
{
factory.registerBehaviorTreeFromFile(path_b);
FAIL() << "expected RuntimeError";
}
catch(const RuntimeError& e)
{
const std::string msg = e.what();
EXPECT_NE(msg.find(abs_a), std::string::npos) << msg;
EXPECT_NE(msg.find(abs_b), std::string::npos) << msg;
}
}
TEST(DuplicateBehaviorTreeId, DuplicateIdWithinSameText)
{
const std::string xml = R"(
<root BTCPP_format="4">
<BehaviorTree ID="foo"><AlwaysSuccess/></BehaviorTree>
<BehaviorTree ID="foo"><AlwaysFailure/></BehaviorTree>
</root>
)";
BehaviorTreeFactory factory;
try
{
factory.registerBehaviorTreeFromText(xml);
FAIL() << "expected RuntimeError";
}
catch(const RuntimeError& e)
{
const std::string msg = e.what();
EXPECT_NE(msg.find("<inline XML>"), std::string::npos) << msg;
}
}
TEST(DuplicateBehaviorTreeId, DuplicateIdAcrossFileAndText)
{
TempSubdir dir;
const auto path = dir.path / "one.xml";
writeFile(path, minimalTreeXml("shared_id"));
const std::string abs_path = std::filesystem::absolute(path).string();
BehaviorTreeFactory factory;
ASSERT_NO_THROW(factory.registerBehaviorTreeFromFile(path));
const std::string xml = minimalTreeXml("shared_id");
try
{
factory.registerBehaviorTreeFromText(xml);
FAIL() << "expected RuntimeError";
}
catch(const RuntimeError& e)
{
const std::string msg = e.what();
EXPECT_NE(msg.find(abs_path), std::string::npos) << msg;
EXPECT_NE(msg.find("<inline XML>"), std::string::npos) << msg;
}
}
TEST(DuplicateBehaviorTreeId, DuplicateIdSamePathTwice)
{
TempSubdir dir;
const auto path = dir.path / "reload.xml";
writeFile(path, minimalTreeXml("same"));
const std::string abs_path = std::filesystem::absolute(path).string();
BehaviorTreeFactory factory;
ASSERT_NO_THROW(factory.registerBehaviorTreeFromFile(path));
try
{
factory.registerBehaviorTreeFromFile(path);
FAIL() << "expected RuntimeError";
}
catch(const RuntimeError& e)
{
const std::string msg = e.what();
EXPECT_NE(msg.find(abs_path), std::string::npos) << msg;
}
}
TEST(DuplicateBehaviorTreeId, ClearAllowsReload)
{
TempSubdir dir;
const auto path = dir.path / "clear.xml";
writeFile(path, minimalTreeXml("cleared"));
BehaviorTreeFactory factory;
ASSERT_NO_THROW(factory.registerBehaviorTreeFromFile(path));
factory.clearRegisteredBehaviorTrees();
ASSERT_NO_THROW(factory.registerBehaviorTreeFromFile(path));
ASSERT_NO_THROW(static_cast<void>(factory.createTree("cleared")));
}
TEST(DuplicateBehaviorTreeId, DifferentIdsCoexist)
{
TempSubdir dir;
const auto path_a = dir.path / "tree_a.xml";
const auto path_b = dir.path / "tree_b.xml";
writeFile(path_a, minimalTreeXml("TreeA"));
writeFile(path_b, minimalTreeXml("TreeB"));
BehaviorTreeFactory factory;
ASSERT_NO_THROW(factory.registerBehaviorTreeFromFile(path_a));
ASSERT_NO_THROW(factory.registerBehaviorTreeFromFile(path_b));
const auto ids = factory.registeredBehaviorTrees();
ASSERT_EQ(ids.size(), 2);
ASSERT_NO_THROW(static_cast<void>(factory.createTree("TreeA")));
ASSERT_NO_THROW(static_cast<void>(factory.createTree("TreeB")));
}
TEST(DuplicateBehaviorTreeId, IncludeDuplicateThrows)
{
TempSubdir dir;
const auto inner_path = dir.path / "inner.xml";
const auto outer_path = dir.path / "outer.xml";
writeFile(inner_path, minimalTreeXml("foo"));
const std::string outer_xml = R"(
<root BTCPP_format="4">
<include path="inner.xml"/>
<BehaviorTree ID="foo">
<AlwaysSuccess/>
</BehaviorTree>
</root>
)";
writeFile(outer_path, outer_xml);
const std::string inner_abs = std::filesystem::absolute(inner_path).string();
BehaviorTreeFactory factory;
try
{
factory.registerBehaviorTreeFromFile(std::filesystem::absolute(outer_path));
FAIL() << "expected RuntimeError";
}
catch(const RuntimeError& e)
{
const std::string msg = e.what();
EXPECT_NE(msg.find(inner_abs), std::string::npos) << msg;
}
}