|
| 1 | +/* A load call rejected after an <include> must leave the parser as it was |
| 2 | + * before the call (issue #1282). */ |
| 3 | + |
| 4 | +#include "test_helper.hpp" |
| 5 | + |
| 6 | +#include "behaviortree_cpp/bt_factory.h" |
| 7 | +#include "behaviortree_cpp/xml_parsing.h" |
| 8 | + |
| 9 | +#include <string> |
| 10 | +#include <tuple> |
| 11 | +#include <vector> |
| 12 | + |
| 13 | +#include <gtest/gtest.h> |
| 14 | + |
| 15 | +using namespace BT; |
| 16 | +using Names = std::vector<std::string>; |
| 17 | + |
| 18 | +namespace |
| 19 | +{ |
| 20 | +std::string treeXml(const std::string& body) |
| 21 | +{ |
| 22 | + return R"(<root BTCPP_format="4">)" + body + "</root>"; |
| 23 | +} |
| 24 | + |
| 25 | +std::string tree(const std::string& tree_id, const std::string& node) |
| 26 | +{ |
| 27 | + return R"(<BehaviorTree ID=")" + tree_id + R"("><)" + node + "/></BehaviorTree>"; |
| 28 | +} |
| 29 | + |
| 30 | +// main.xml includes a valid sub.xml, then defines MainTree with `main_node` |
| 31 | +std::filesystem::path writeMainWithInclude(const TempSubdir& dir, |
| 32 | + const std::string& main_node) |
| 33 | +{ |
| 34 | + writeFile(dir.path / "sub.xml", treeXml(tree("FromInclude", "AlwaysSuccess"))); |
| 35 | + const auto main_xml = dir.path / "main.xml"; |
| 36 | + writeFile(main_xml, |
| 37 | + treeXml(R"(<include path="sub.xml"/>)" + tree("MainTree", main_node))); |
| 38 | + return main_xml; |
| 39 | +} |
| 40 | +} // namespace |
| 41 | + |
| 42 | +TEST(XMLParserTest, RejectedIncludeKeepsNoTree) |
| 43 | +{ |
| 44 | + TempSubdir dir; |
| 45 | + const auto main_xml = writeMainWithInclude(dir, "NotARegisteredNode"); |
| 46 | + |
| 47 | + BehaviorTreeFactory factory; |
| 48 | + XMLParser parser(factory); |
| 49 | + EXPECT_THROW(parser.loadFromFile(main_xml), std::exception); |
| 50 | + EXPECT_EQ(parser.registeredBehaviorTrees(), Names{}); |
| 51 | +} |
| 52 | + |
| 53 | +TEST(XMLParserTest, RejectedInlineIncludesKeepNoTree) |
| 54 | +{ |
| 55 | + TempSubdir dir; |
| 56 | + writeFile(dir.path / "sub.xml", treeXml(tree("FromInclude", "AlwaysSuccess"))); |
| 57 | + const auto text = treeXml(R"(<include path=")" + (dir.path / "sub.xml").string() + |
| 58 | + R"("/>)" + tree("MainTree", "NotARegisteredNode")); |
| 59 | + |
| 60 | + BehaviorTreeFactory factory; |
| 61 | + XMLParser parser(factory); |
| 62 | + EXPECT_THROW(parser.loadFromText(text), std::exception); |
| 63 | + EXPECT_EQ(parser.registeredBehaviorTrees(), Names{}); |
| 64 | +} |
| 65 | + |
| 66 | +TEST(XMLParserTest, AcceptedIncludeKeepsItsTree) |
| 67 | +{ |
| 68 | + TempSubdir dir; |
| 69 | + const auto main_xml = writeMainWithInclude(dir, "AlwaysSuccess"); |
| 70 | + |
| 71 | + BehaviorTreeFactory factory; |
| 72 | + XMLParser parser(factory); |
| 73 | + parser.loadFromFile(main_xml); |
| 74 | + EXPECT_EQ(parser.registeredBehaviorTrees(), (Names{ "FromInclude", "MainTree" })); |
| 75 | +} |
| 76 | + |
| 77 | +// The rejected load's documents must all be dropped: a leftover included |
| 78 | +// document would become the first one, and its main_tree_to_execute would be |
| 79 | +// picked for a later file that does not name its main tree. |
| 80 | +TEST(XMLParserTest, RejectedIncludeIsNotTheFirstDocument) |
| 81 | +{ |
| 82 | + TempSubdir dir; |
| 83 | + writeFile(dir.path / "sub.xml", |
| 84 | + R"(<root BTCPP_format="4" main_tree_to_execute="FromInclude">)" + |
| 85 | + tree("FromInclude", "AlwaysSuccess") + "</root>"); |
| 86 | + const auto main_xml = dir.path / "main.xml"; |
| 87 | + writeFile(main_xml, treeXml(R"(<include path="sub.xml"/>)" + |
| 88 | + tree("MainTree", "NotARegisteredNode"))); |
| 89 | + |
| 90 | + BehaviorTreeFactory factory; |
| 91 | + EXPECT_THROW(factory.registerBehaviorTreeFromFile(main_xml), std::exception); |
| 92 | + |
| 93 | + // two trees and no main_tree_to_execute: ambiguous, must be rejected |
| 94 | + const auto ambiguous = |
| 95 | + treeXml(tree("A", "AlwaysSuccess") + tree("FromInclude", "AlwaysFailure")); |
| 96 | + EXPECT_THROW(std::ignore = factory.createTreeFromText(ambiguous), RuntimeError); |
| 97 | +} |
| 98 | + |
| 99 | +// A tree redefined by a valid include of a rejected file must get its |
| 100 | +// previous definition back: the redefining document is erased. |
| 101 | +TEST(XMLParserTest, RejectedRedefinitionRestoresPreviousTree) |
| 102 | +{ |
| 103 | + TempSubdir dir; |
| 104 | + writeFile(dir.path / "good.xml", treeXml(tree("Shared", "AlwaysSuccess"))); |
| 105 | + writeFile(dir.path / "redefine.xml", treeXml(tree("Shared", "AlwaysFailure"))); |
| 106 | + writeFile(dir.path / "main.xml", treeXml(R"(<include path="redefine.xml"/>)" + |
| 107 | + tree("MainTree", "NotARegisteredNode"))); |
| 108 | + |
| 109 | + BehaviorTreeFactory factory; |
| 110 | + factory.registerBehaviorTreeFromFile(dir.path / "good.xml"); |
| 111 | + EXPECT_THROW(factory.registerBehaviorTreeFromFile(dir.path / "main.xml"), |
| 112 | + std::exception); |
| 113 | + |
| 114 | + auto tree_instance = factory.createTree("Shared"); |
| 115 | + EXPECT_EQ(tree_instance.tickOnce(), NodeStatus::SUCCESS); |
| 116 | +} |
0 commit comments