Skip to content

Commit f4c4ce8

Browse files
committed
tests: added GTest to prove line number is recursively correct
1 parent 6b29816 commit f4c4ce8

File tree

1 file changed

+101
-1
lines changed

1 file changed

+101
-1
lines changed

tests/gtest_factory.cpp

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
#include <filesystem>
33
#include <string>
44
#include <utility>
5-
#include <vector>
5+
#include "behaviortree_cpp/bt_factory.h"
6+
#include "behaviortree_cpp/exceptions.h"
67
#include "behaviortree_cpp/xml_parsing.h"
78
#include "../sample_nodes/crossdoor_nodes.h"
89
#include "../sample_nodes/dummy_nodes.h"
@@ -102,6 +103,78 @@ static const char* xml_text_subtree_part2 = R"(
102103
</BehaviorTree>
103104
</root> )";
104105

106+
static const char* xml_text_incorrect1 = R"(
107+
<root BTCPP_format="4">
108+
<BehaviorTree ID="MainTree">
109+
<Sequence name="MainObjective">
110+
<Action ID="Script" />
111+
<Action ID="Script" />
112+
<Decorator ID="RetryUntilSuccessful" num_attempts="-1">
113+
<Decorator ID="Precondition">
114+
<Control ID="Parallel">
115+
<Control ID="IfThenElse">
116+
<Sequence>
117+
<Action ID="TriggerServer"/>
118+
<Action ID="LogMessage"/>
119+
<Action ID="Script" />
120+
</Sequence>
121+
<Action ID="AlwaysFailure" />
122+
</Control>
123+
<Sequence>
124+
<Decorator ID="Precondition">
125+
<Sequence>
126+
<Decorator ID="Precondition">
127+
<Sequence>
128+
<Action ID="LogMessage"/>
129+
<Action ID="ValveControl"/>
130+
</Sequence>
131+
</Decorator>
132+
<Action ID="TriggerServer"/>
133+
<Action ID="LogMessage"/>
134+
</Sequence>
135+
</Decorator>
136+
<Control ID="Fallback">
137+
<Sequence>
138+
<Decorator ID="Precondition">
139+
<Sequence>
140+
<Action ID="LogMessage"/>
141+
<Action ID="ValveControl"/>
142+
<Decorator ID="Delay" delay_msec="1000" />
143+
</Sequence>
144+
</Decorator>
145+
</Sequence>
146+
<Decorator ID="Inverter">
147+
<Action ID="Script" code="retry := 'false'" />
148+
</Decorator>
149+
</Control>
150+
</Sequence>
151+
</Control>
152+
</Decorator>
153+
</Decorator>
154+
<Decorator ID="Precondition" else="SUCCESS" if="retry == 'false'">
155+
<Action ID="AlwaysFailure" />
156+
</Decorator>
157+
</Sequence>
158+
</BehaviorTree>
159+
</root>
160+
)";
161+
162+
static const char* xml_text_incorrect2 = R"(
163+
164+
<root BTCPP_format="4">
165+
<BehaviorTree ID="DoorClosedSubtree">
166+
<Sequence name="door_sequence">
167+
<Decorator ID="Inverter">
168+
<Action ID="IsDoorLocked" />
169+
</Decorator>
170+
<Decorator ID="RepeatUntilSuccess" />
171+
<Action ID="OpenDoor" />
172+
<Action ID="PassThroughDoor" />
173+
<Action ID="CloseDoor" />
174+
</Sequence>
175+
</BehaviorTree>
176+
</root> )";
177+
105178
// clang-format on
106179

107180
TEST(BehaviorTreeFactory, NotRegisteredNode)
@@ -173,6 +246,33 @@ TEST(BehaviorTreeFactory, Subtree)
173246
ASSERT_EQ(subtree->nodes[4]->name(), "SmashDoor");
174247
}
175248

249+
TEST(BehaviorTreeFactory, SubtreeParsingError)
250+
{
251+
BehaviorTreeFactory factory;
252+
CrossDoor cross_door;
253+
cross_door.registerNodes(factory);
254+
try
255+
{
256+
auto tree = factory.createTreeFromText(xml_text_incorrect1);
257+
FAIL() << "Expected exception thrown";
258+
}
259+
catch(const BT::RuntimeError& e)
260+
{
261+
std::string error_msg = e.what();
262+
EXPECT_TRUE(error_msg.find("line 36") != std::string::npos);
263+
}
264+
try
265+
{
266+
auto tree = factory.createTreeFromText(xml_text_incorrect2);
267+
FAIL() << "Expected exception thrown";
268+
}
269+
catch(const BT::RuntimeError& e)
270+
{
271+
std::string error_msg = e.what();
272+
EXPECT_TRUE(error_msg.find("line 7") != std::string::npos);
273+
}
274+
}
275+
176276
TEST(BehaviorTreeFactory, Issue7)
177277
{
178278
const std::string xml_text_issue = R"(

0 commit comments

Comments
 (0)