|
2 | 2 | #include <filesystem>
|
3 | 3 | #include <string>
|
4 | 4 | #include <utility>
|
5 |
| -#include <vector> |
| 5 | +#include "behaviortree_cpp/bt_factory.h" |
| 6 | +#include "behaviortree_cpp/exceptions.h" |
6 | 7 | #include "behaviortree_cpp/xml_parsing.h"
|
7 | 8 | #include "../sample_nodes/crossdoor_nodes.h"
|
8 | 9 | #include "../sample_nodes/dummy_nodes.h"
|
@@ -102,6 +103,78 @@ static const char* xml_text_subtree_part2 = R"(
|
102 | 103 | </BehaviorTree>
|
103 | 104 | </root> )";
|
104 | 105 |
|
| 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 | + |
105 | 178 | // clang-format on
|
106 | 179 |
|
107 | 180 | TEST(BehaviorTreeFactory, NotRegisteredNode)
|
@@ -173,6 +246,33 @@ TEST(BehaviorTreeFactory, Subtree)
|
173 | 246 | ASSERT_EQ(subtree->nodes[4]->name(), "SmashDoor");
|
174 | 247 | }
|
175 | 248 |
|
| 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 | + |
176 | 276 | TEST(BehaviorTreeFactory, Issue7)
|
177 | 277 | {
|
178 | 278 | const std::string xml_text_issue = R"(
|
|
0 commit comments