XMLNodeTest.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-09-25
  6. * Changed: 2020-09-26
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <ls_std/ls_std.hpp>
  11. #include "../../../TestDataFactory.hpp"
  12. namespace {
  13. class XMLNodeTest : public ::testing::Test {
  14. protected:
  15. XMLNodeTest() = default;
  16. ~XMLNodeTest() override = default;
  17. void SetUp() override {}
  18. void TearDown() override {}
  19. };
  20. TEST_F(XMLNodeTest, addAttributeAfter)
  21. {
  22. ls_std::XMLNode dialogNode {"dialog"};
  23. std::shared_ptr<ls_std::XMLAttribute> currentAttribute {};
  24. dialogNode.addAttributeToEnd(std::make_shared<ls_std::XMLAttribute>("events"));
  25. dialogNode.addAttributeToEnd(std::make_shared<ls_std::XMLAttribute>("assets"));
  26. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 0);
  27. ASSERT_STREQ("events", currentAttribute->getName().c_str());
  28. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 1);
  29. ASSERT_STREQ("assets", currentAttribute->getName().c_str());
  30. // add id after events
  31. ASSERT_TRUE(dialogNode.addAttributeAfter(std::make_shared<ls_std::XMLAttribute>("id"), "events"));
  32. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 0);
  33. ASSERT_STREQ("events", currentAttribute->getName().c_str());
  34. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 1);
  35. ASSERT_STREQ("id", currentAttribute->getName().c_str());
  36. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 2);
  37. ASSERT_STREQ("assets", currentAttribute->getName().c_str());
  38. // add tasks after assets
  39. ASSERT_TRUE(dialogNode.addAttributeAfter(std::make_shared<ls_std::XMLAttribute>("tasks"), "assets"));
  40. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 0);
  41. ASSERT_STREQ("events", currentAttribute->getName().c_str());
  42. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 1);
  43. ASSERT_STREQ("id", currentAttribute->getName().c_str());
  44. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 2);
  45. ASSERT_STREQ("assets", currentAttribute->getName().c_str());
  46. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 3);
  47. ASSERT_STREQ("tasks", currentAttribute->getName().c_str());
  48. }
  49. TEST_F(XMLNodeTest, addAttributeAfterNegative)
  50. {
  51. ls_std::XMLNode dialogNode {"dialog"};
  52. dialogNode.addAttributeToEnd(std::make_shared<ls_std::XMLAttribute>("events"));
  53. ASSERT_FALSE(dialogNode.addAttributeAfter(std::make_shared<ls_std::XMLAttribute>("id"), "assets"));
  54. }
  55. TEST_F(XMLNodeTest, addAttributeBefore)
  56. {
  57. ls_std::XMLNode dialogNode {"dialog"};
  58. std::shared_ptr<ls_std::XMLAttribute> currentAttribute {};
  59. dialogNode.addAttributeToEnd(std::make_shared<ls_std::XMLAttribute>("events"));
  60. dialogNode.addAttributeToEnd(std::make_shared<ls_std::XMLAttribute>("assets"));
  61. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 0);
  62. ASSERT_STREQ("events", currentAttribute->getName().c_str());
  63. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 1);
  64. ASSERT_STREQ("assets", currentAttribute->getName().c_str());
  65. // add id before events
  66. ASSERT_TRUE(dialogNode.addAttributeBefore(std::make_shared<ls_std::XMLAttribute>("id"), "events"));
  67. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 0);
  68. ASSERT_STREQ("id", currentAttribute->getName().c_str());
  69. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 1);
  70. ASSERT_STREQ("events", currentAttribute->getName().c_str());
  71. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 2);
  72. ASSERT_STREQ("assets", currentAttribute->getName().c_str());
  73. // add tasks before assets
  74. ASSERT_TRUE(dialogNode.addAttributeBefore(std::make_shared<ls_std::XMLAttribute>("tasks"), "assets"));
  75. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 0);
  76. ASSERT_STREQ("id", currentAttribute->getName().c_str());
  77. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 1);
  78. ASSERT_STREQ("events", currentAttribute->getName().c_str());
  79. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 2);
  80. ASSERT_STREQ("tasks", currentAttribute->getName().c_str());
  81. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 3);
  82. ASSERT_STREQ("assets", currentAttribute->getName().c_str());
  83. }
  84. TEST_F(XMLNodeTest, addAttributeBeforeNegative)
  85. {
  86. ls_std::XMLNode dialogNode {"dialog"};
  87. dialogNode.addAttributeToEnd(std::make_shared<ls_std::XMLAttribute>("events"));
  88. ASSERT_FALSE(dialogNode.addAttributeBefore(std::make_shared<ls_std::XMLAttribute>("id"), "assets"));
  89. }
  90. TEST_F(XMLNodeTest, addAttributeToBeginning)
  91. {
  92. ls_std::XMLNode dialogNode {"dialog"};
  93. std::shared_ptr<ls_std::XMLAttribute> currentAttribute {};
  94. dialogNode.addAttributeToBeginning(std::make_shared<ls_std::XMLAttribute>("id"));
  95. ASSERT_EQ(1, dialogNode.getAttributes().size());
  96. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 0);
  97. ASSERT_STREQ("id", currentAttribute->getName().c_str());
  98. dialogNode.addAttributeToBeginning(std::make_shared<ls_std::XMLAttribute>("assets"));
  99. ASSERT_EQ(2, dialogNode.getAttributes().size());
  100. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 0);
  101. ASSERT_STREQ("assets", currentAttribute->getName().c_str());
  102. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 1);
  103. ASSERT_STREQ("id", currentAttribute->getName().c_str());
  104. dialogNode.addAttributeToBeginning(std::make_shared<ls_std::XMLAttribute>("events"));
  105. ASSERT_EQ(3, dialogNode.getAttributes().size());
  106. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 0);
  107. ASSERT_STREQ("events", currentAttribute->getName().c_str());
  108. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 1);
  109. ASSERT_STREQ("assets", currentAttribute->getName().c_str());
  110. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 2);
  111. ASSERT_STREQ("id", currentAttribute->getName().c_str());
  112. }
  113. TEST_F(XMLNodeTest, addAttributeToEnd)
  114. {
  115. ls_std::XMLNode dialogNode {"dialog"};
  116. std::shared_ptr<ls_std::XMLAttribute> currentAttribute {};
  117. dialogNode.addAttributeToEnd(std::make_shared<ls_std::XMLAttribute>("id"));
  118. ASSERT_EQ(1, dialogNode.getAttributes().size());
  119. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 0);
  120. ASSERT_STREQ("id", currentAttribute->getName().c_str());
  121. dialogNode.addAttributeToEnd(std::make_shared<ls_std::XMLAttribute>("assets"));
  122. ASSERT_EQ(2, dialogNode.getAttributes().size());
  123. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 0);
  124. ASSERT_STREQ("id", currentAttribute->getName().c_str());
  125. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 1);
  126. ASSERT_STREQ("assets", currentAttribute->getName().c_str());
  127. dialogNode.addAttributeToEnd(std::make_shared<ls_std::XMLAttribute>("events"));
  128. ASSERT_EQ(3, dialogNode.getAttributes().size());
  129. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 0);
  130. ASSERT_STREQ("id", currentAttribute->getName().c_str());
  131. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 1);
  132. ASSERT_STREQ("assets", currentAttribute->getName().c_str());
  133. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 2);
  134. ASSERT_STREQ("events", currentAttribute->getName().c_str());
  135. }
  136. TEST_F(XMLNodeTest, addChildAfter)
  137. {
  138. ls_std::XMLNode dialogsNode {"dialogs"};
  139. std::shared_ptr<ls_std::XMLNode> currentNode {};
  140. std::shared_ptr<ls_std::XMLNode> dialogNodeA = std::make_shared<ls_std::XMLNode>("dialogNodeA");
  141. std::shared_ptr<ls_std::XMLNode> dialogNodeB = std::make_shared<ls_std::XMLNode>("dialogNodeB");
  142. std::shared_ptr<ls_std::XMLNode> dialogNodeC = std::make_shared<ls_std::XMLNode>("dialogNodeC");
  143. std::shared_ptr<ls_std::XMLNode> dialogNodeD = std::make_shared<ls_std::XMLNode>("dialogNodeD");
  144. ASSERT_TRUE(dialogsNode.addChildToEnd(dialogNodeB));
  145. ASSERT_TRUE(dialogsNode.addChildToEnd(dialogNodeC));
  146. ASSERT_EQ(2, dialogsNode.getChildren().size());
  147. currentNode = *std::next(dialogsNode.getChildren().begin(), 0);
  148. ASSERT_STREQ("dialogNodeB", currentNode->getName().c_str());
  149. currentNode = *std::next(dialogsNode.getChildren().begin(), 1);
  150. ASSERT_STREQ("dialogNodeC", currentNode->getName().c_str());
  151. // add A after B
  152. ASSERT_TRUE(dialogsNode.addChildAfter(dialogNodeA, dialogNodeB));
  153. currentNode = *std::next(dialogsNode.getChildren().begin(), 0);
  154. ASSERT_STREQ("dialogNodeB", currentNode->getName().c_str());
  155. currentNode = *std::next(dialogsNode.getChildren().begin(), 1);
  156. ASSERT_STREQ("dialogNodeA", currentNode->getName().c_str());
  157. currentNode = *std::next(dialogsNode.getChildren().begin(), 2);
  158. ASSERT_STREQ("dialogNodeC", currentNode->getName().c_str());
  159. // add D after C
  160. ASSERT_TRUE(dialogsNode.addChildAfter(dialogNodeD, dialogNodeC));
  161. currentNode = *std::next(dialogsNode.getChildren().begin(), 0);
  162. ASSERT_STREQ("dialogNodeB", currentNode->getName().c_str());
  163. currentNode = *std::next(dialogsNode.getChildren().begin(), 1);
  164. ASSERT_STREQ("dialogNodeA", currentNode->getName().c_str());
  165. currentNode = *std::next(dialogsNode.getChildren().begin(), 2);
  166. ASSERT_STREQ("dialogNodeC", currentNode->getName().c_str());
  167. currentNode = *std::next(dialogsNode.getChildren().begin(), 3);
  168. ASSERT_STREQ("dialogNodeD", currentNode->getName().c_str());
  169. }
  170. TEST_F(XMLNodeTest, addChildAfterNegative)
  171. {
  172. ls_std::XMLNode dialogsNode {"dialogs"};
  173. std::shared_ptr<ls_std::XMLNode> dialogNodeA = std::make_shared<ls_std::XMLNode>("dialogNodeA");
  174. std::shared_ptr<ls_std::XMLNode> dialogNodeB = std::make_shared<ls_std::XMLNode>("dialogNodeB");
  175. ASSERT_FALSE(dialogsNode.addChildAfter(dialogNodeA, dialogNodeB));
  176. }
  177. TEST_F(XMLNodeTest, addChildBefore)
  178. {
  179. ls_std::XMLNode dialogsNode {"dialogs"};
  180. std::shared_ptr<ls_std::XMLNode> currentNode {};
  181. std::shared_ptr<ls_std::XMLNode> dialogNodeA = std::make_shared<ls_std::XMLNode>("dialogNodeA");
  182. std::shared_ptr<ls_std::XMLNode> dialogNodeB = std::make_shared<ls_std::XMLNode>("dialogNodeB");
  183. std::shared_ptr<ls_std::XMLNode> dialogNodeC = std::make_shared<ls_std::XMLNode>("dialogNodeC");
  184. std::shared_ptr<ls_std::XMLNode> dialogNodeD = std::make_shared<ls_std::XMLNode>("dialogNodeD");
  185. ASSERT_TRUE(dialogsNode.addChildToEnd(dialogNodeB));
  186. ASSERT_TRUE(dialogsNode.addChildToEnd(dialogNodeC));
  187. ASSERT_EQ(2, dialogsNode.getChildren().size());
  188. currentNode = *std::next(dialogsNode.getChildren().begin(), 0);
  189. ASSERT_STREQ("dialogNodeB", currentNode->getName().c_str());
  190. currentNode = *std::next(dialogsNode.getChildren().begin(), 1);
  191. ASSERT_STREQ("dialogNodeC", currentNode->getName().c_str());
  192. // add A before C
  193. ASSERT_TRUE(dialogsNode.addChildBefore(dialogNodeA, dialogNodeC));
  194. currentNode = *std::next(dialogsNode.getChildren().begin(), 0);
  195. ASSERT_STREQ("dialogNodeB", currentNode->getName().c_str());
  196. currentNode = *std::next(dialogsNode.getChildren().begin(), 1);
  197. ASSERT_STREQ("dialogNodeA", currentNode->getName().c_str());
  198. currentNode = *std::next(dialogsNode.getChildren().begin(), 2);
  199. ASSERT_STREQ("dialogNodeC", currentNode->getName().c_str());
  200. // add D before B
  201. ASSERT_TRUE(dialogsNode.addChildBefore(dialogNodeD, dialogNodeB));
  202. currentNode = *std::next(dialogsNode.getChildren().begin(), 0);
  203. ASSERT_STREQ("dialogNodeD", currentNode->getName().c_str());
  204. currentNode = *std::next(dialogsNode.getChildren().begin(), 1);
  205. ASSERT_STREQ("dialogNodeB", currentNode->getName().c_str());
  206. currentNode = *std::next(dialogsNode.getChildren().begin(), 2);
  207. ASSERT_STREQ("dialogNodeA", currentNode->getName().c_str());
  208. currentNode = *std::next(dialogsNode.getChildren().begin(), 3);
  209. ASSERT_STREQ("dialogNodeC", currentNode->getName().c_str());
  210. }
  211. TEST_F(XMLNodeTest, addChildToBeginning)
  212. {
  213. ls_std::XMLNode dialogsNode {"dialogs"};
  214. std::shared_ptr<ls_std::XMLNode> currentNode {};
  215. ls_std::XMLNode dialogNodeA {"dialogNodeA"};
  216. ls_std::XMLNode dialogNodeB {"dialogNodeB"};
  217. ls_std::XMLNode dialogNodeC {"dialogNodeC"};
  218. ASSERT_TRUE(dialogsNode.getChildren().empty());
  219. ASSERT_EQ(0, dialogsNode.getChildren().size());
  220. // adding C
  221. ASSERT_TRUE(dialogsNode.addChildToBeginning(std::make_shared<ls_std::XMLNode>(dialogNodeC)));
  222. ASSERT_TRUE(!dialogsNode.getChildren().empty());
  223. ASSERT_EQ(1, dialogsNode.getChildren().size());
  224. currentNode = *std::next(dialogsNode.getChildren().begin(), 0);
  225. ASSERT_STREQ("dialogNodeC", currentNode->getName().c_str());
  226. // adding B
  227. ASSERT_TRUE(dialogsNode.addChildToBeginning(std::make_shared<ls_std::XMLNode>(dialogNodeB)));
  228. ASSERT_EQ(2, dialogsNode.getChildren().size());
  229. currentNode = *std::next(dialogsNode.getChildren().begin(), 0);
  230. ASSERT_STREQ("dialogNodeB", currentNode->getName().c_str());
  231. currentNode = *std::next(dialogsNode.getChildren().begin(), 1);
  232. ASSERT_STREQ("dialogNodeC", currentNode->getName().c_str());
  233. // adding A
  234. ASSERT_TRUE(dialogsNode.addChildToBeginning(std::make_shared<ls_std::XMLNode>(dialogNodeA)));
  235. ASSERT_EQ(3, dialogsNode.getChildren().size());
  236. currentNode = *std::next(dialogsNode.getChildren().begin(), 0);
  237. ASSERT_STREQ("dialogNodeA", currentNode->getName().c_str());
  238. currentNode = *std::next(dialogsNode.getChildren().begin(), 1);
  239. ASSERT_STREQ("dialogNodeB", currentNode->getName().c_str());
  240. currentNode = *std::next(dialogsNode.getChildren().begin(), 2);
  241. ASSERT_STREQ("dialogNodeC", currentNode->getName().c_str());
  242. }
  243. TEST_F(XMLNodeTest, addChildToEnd)
  244. {
  245. ls_std::XMLNode dialogsNode {"dialogs"};
  246. std::shared_ptr<ls_std::XMLNode> currentNode {};
  247. ls_std::XMLNode dialogNodeA {"dialogNodeA"};
  248. ls_std::XMLNode dialogNodeB {"dialogNodeB"};
  249. ls_std::XMLNode dialogNodeC {"dialogNodeC"};
  250. ASSERT_TRUE(dialogsNode.getChildren().empty());
  251. ASSERT_EQ(0, dialogsNode.getChildren().size());
  252. // adding C
  253. ASSERT_TRUE(dialogsNode.addChildToEnd(std::make_shared<ls_std::XMLNode>(dialogNodeC)));
  254. ASSERT_TRUE(!dialogsNode.getChildren().empty());
  255. ASSERT_EQ(1, dialogsNode.getChildren().size());
  256. currentNode = *std::next(dialogsNode.getChildren().begin(), 0);
  257. ASSERT_STREQ("dialogNodeC", currentNode->getName().c_str());
  258. // adding B
  259. ASSERT_TRUE(dialogsNode.addChildToEnd(std::make_shared<ls_std::XMLNode>(dialogNodeB)));
  260. ASSERT_EQ(2, dialogsNode.getChildren().size());
  261. currentNode = *std::next(dialogsNode.getChildren().begin(), 0);
  262. ASSERT_STREQ("dialogNodeC", currentNode->getName().c_str());
  263. currentNode = *std::next(dialogsNode.getChildren().begin(), 1);
  264. ASSERT_STREQ("dialogNodeB", currentNode->getName().c_str());
  265. // adding A
  266. ASSERT_TRUE(dialogsNode.addChildToEnd(std::make_shared<ls_std::XMLNode>(dialogNodeA)));
  267. ASSERT_EQ(3, dialogsNode.getChildren().size());
  268. currentNode = *std::next(dialogsNode.getChildren().begin(), 0);
  269. ASSERT_STREQ("dialogNodeC", currentNode->getName().c_str());
  270. currentNode = *std::next(dialogsNode.getChildren().begin(), 1);
  271. ASSERT_STREQ("dialogNodeB", currentNode->getName().c_str());
  272. currentNode = *std::next(dialogsNode.getChildren().begin(), 2);
  273. ASSERT_STREQ("dialogNodeA", currentNode->getName().c_str());
  274. }
  275. TEST_F(XMLNodeTest, clearValue)
  276. {
  277. ls_std::XMLNode dialogNode {"dialog"};
  278. dialogNode.setValue("Something");
  279. ASSERT_STREQ("Something", dialogNode.getValue().c_str());
  280. dialogNode.clearValue();
  281. ASSERT_TRUE(dialogNode.getValue().empty());
  282. ASSERT_STREQ("", dialogNode.getValue().c_str());
  283. }
  284. TEST_F(XMLNodeTest, getAttributes)
  285. {
  286. ls_std::XMLNode dialogNode {"dialog"};
  287. ASSERT_TRUE(dialogNode.getAttributes().empty());
  288. }
  289. TEST_F(XMLNodeTest, getChildren)
  290. {
  291. ls_std::XMLNode dialogNode {"dialog"};
  292. ASSERT_TRUE(dialogNode.getChildren().empty());
  293. }
  294. TEST_F(XMLNodeTest, getChildrenV2)
  295. {
  296. ls_std::XMLNode dialogsNode {"dialogs"};
  297. ls_std::XMLNode dialogNode {"dialog"};
  298. ls_std::XMLNode otherNode {"something"};
  299. // preparation
  300. ASSERT_TRUE(dialogsNode.addChildToEnd(std::make_shared<ls_std::XMLNode>(dialogNode)));
  301. ASSERT_TRUE(dialogsNode.addChildToEnd(std::make_shared<ls_std::XMLNode>(otherNode)));
  302. ASSERT_TRUE(dialogsNode.addChildToEnd(std::make_shared<ls_std::XMLNode>(otherNode)));
  303. ASSERT_TRUE(dialogsNode.addChildToEnd(std::make_shared<ls_std::XMLNode>(dialogNode)));
  304. ASSERT_TRUE(dialogsNode.addChildToEnd(std::make_shared<ls_std::XMLNode>(otherNode)));
  305. // check
  306. ASSERT_TRUE(!dialogsNode.getChildren().empty());
  307. ASSERT_EQ(5, dialogsNode.getChildren().size());
  308. ASSERT_EQ(2, dialogsNode.getChildren("dialog").size());
  309. ASSERT_EQ(3, dialogsNode.getChildren("something").size());
  310. }
  311. TEST_F(XMLNodeTest, getName)
  312. {
  313. ls_std::XMLNode dialogNode {"dialog"};
  314. ASSERT_STREQ("dialog", dialogNode.getName().c_str());
  315. }
  316. TEST_F(XMLNodeTest, getValue)
  317. {
  318. ls_std::XMLNode dialogNode {"dialog"};
  319. ASSERT_TRUE(dialogNode.getValue().empty());
  320. }
  321. TEST_F(XMLNodeTest, hasAttribute)
  322. {
  323. ls_std::XMLNode dialogNode {"dialogNode"};
  324. dialogNode.addAttributeToEnd(std::make_shared<ls_std::XMLAttribute>("id"));
  325. dialogNode.addAttributeToEnd(std::make_shared<ls_std::XMLAttribute>("events"));
  326. dialogNode.addAttributeToEnd(std::make_shared<ls_std::XMLAttribute>("assets"));
  327. ASSERT_TRUE(dialogNode.hasAttribute("id"));
  328. ASSERT_TRUE(dialogNode.hasAttribute("events"));
  329. ASSERT_TRUE(dialogNode.hasAttribute("assets"));
  330. }
  331. TEST_F(XMLNodeTest, hasAttributeNegative)
  332. {
  333. ls_std::XMLNode dialogNode {"dialogNode"};
  334. dialogNode.addAttributeToEnd(std::make_shared<ls_std::XMLAttribute>("id"));
  335. dialogNode.addAttributeToEnd(std::make_shared<ls_std::XMLAttribute>("events"));
  336. dialogNode.addAttributeToEnd(std::make_shared<ls_std::XMLAttribute>("assets"));
  337. ASSERT_FALSE(dialogNode.hasAttribute("fields"));
  338. }
  339. TEST_F(XMLNodeTest, hasChild)
  340. {
  341. ls_std::XMLNode dialogsNode {"dialogsNode"};
  342. dialogsNode.addChildToEnd(std::make_shared<ls_std::XMLNode>("dialogA"));
  343. dialogsNode.addChildToEnd(std::make_shared<ls_std::XMLNode>("dialogB"));
  344. dialogsNode.addChildToEnd(std::make_shared<ls_std::XMLNode>("dialogC"));
  345. ASSERT_TRUE(dialogsNode.hasChild("dialogA"));
  346. ASSERT_TRUE(dialogsNode.hasChild("dialogB"));
  347. ASSERT_TRUE(dialogsNode.hasChild("dialogC"));
  348. }
  349. TEST_F(XMLNodeTest, hasChildNegative)
  350. {
  351. ls_std::XMLNode dialogsNode {"dialogsNode"};
  352. dialogsNode.addChildToEnd(std::make_shared<ls_std::XMLNode>("dialogA"));
  353. dialogsNode.addChildToEnd(std::make_shared<ls_std::XMLNode>("dialogB"));
  354. dialogsNode.addChildToEnd(std::make_shared<ls_std::XMLNode>("dialogC"));
  355. ASSERT_FALSE(dialogsNode.hasChild("dialogD"));
  356. ASSERT_FALSE(dialogsNode.hasChild("dialogE"));
  357. ASSERT_FALSE(dialogsNode.hasChild("dialogF"));
  358. }
  359. TEST_F(XMLNodeTest, hasChildV2)
  360. {
  361. ls_std::XMLNode dialogsNode {"dialogsNode"};
  362. std::shared_ptr<ls_std::XMLNode> searchNode = std::make_shared<ls_std::XMLNode>("dialogB");
  363. dialogsNode.addChildToEnd(std::make_shared<ls_std::XMLNode>("dialogA"));
  364. dialogsNode.addChildToEnd(searchNode);
  365. dialogsNode.addChildToEnd(std::make_shared<ls_std::XMLNode>("dialogC"));
  366. ASSERT_TRUE(dialogsNode.hasChild(searchNode));
  367. }
  368. TEST_F(XMLNodeTest, hasChildV2Negative)
  369. {
  370. ls_std::XMLNode dialogsNode {"dialogsNode"};
  371. std::shared_ptr<ls_std::XMLNode> searchNode = std::make_shared<ls_std::XMLNode>("dialogB");
  372. dialogsNode.addChildToEnd(std::make_shared<ls_std::XMLNode>("dialogA"));
  373. dialogsNode.addChildToEnd(std::make_shared<ls_std::XMLNode>("dialogC"));
  374. ASSERT_FALSE(dialogsNode.hasChild(searchNode));
  375. }
  376. TEST_F(XMLNodeTest, removeFirstAttribute)
  377. {
  378. ls_std::XMLNode dialogNode {"dialog"};
  379. std::shared_ptr<ls_std::XMLAttribute> currentAttribute {};
  380. dialogNode.addAttributeToEnd(std::make_shared<ls_std::XMLAttribute>("id"));
  381. dialogNode.addAttributeToEnd(std::make_shared<ls_std::XMLAttribute>("events"));
  382. dialogNode.addAttributeToEnd(std::make_shared<ls_std::XMLAttribute>("assets"));
  383. ASSERT_TRUE(!dialogNode.getAttributes().empty());
  384. ASSERT_EQ(3, dialogNode.getAttributes().size());
  385. dialogNode.removeFirstAttribute();
  386. ASSERT_EQ(2, dialogNode.getAttributes().size());
  387. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 0);
  388. ASSERT_STREQ("events", currentAttribute->getName().c_str());
  389. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 1);
  390. ASSERT_STREQ("assets", currentAttribute->getName().c_str());
  391. }
  392. TEST_F(XMLNodeTest, removeFirstAttributeNegative)
  393. {
  394. ls_std::XMLNode dialogNode {"dialog"};
  395. ASSERT_TRUE(dialogNode.getAttributes().empty());
  396. dialogNode.removeFirstAttribute();
  397. ASSERT_TRUE(dialogNode.getAttributes().empty());
  398. }
  399. TEST_F(XMLNodeTest, removeLastAttribute)
  400. {
  401. ls_std::XMLNode dialogNode {"dialog"};
  402. std::shared_ptr<ls_std::XMLAttribute> currentAttribute {};
  403. dialogNode.addAttributeToEnd(std::make_shared<ls_std::XMLAttribute>("id"));
  404. dialogNode.addAttributeToEnd(std::make_shared<ls_std::XMLAttribute>("events"));
  405. dialogNode.addAttributeToEnd(std::make_shared<ls_std::XMLAttribute>("assets"));
  406. ASSERT_TRUE(!dialogNode.getAttributes().empty());
  407. ASSERT_EQ(3, dialogNode.getAttributes().size());
  408. dialogNode.removeLastAttribute();
  409. ASSERT_EQ(2, dialogNode.getAttributes().size());
  410. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 0);
  411. ASSERT_STREQ("id", currentAttribute->getName().c_str());
  412. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 1);
  413. ASSERT_STREQ("events", currentAttribute->getName().c_str());
  414. }
  415. TEST_F(XMLNodeTest, removeLastAttributeNegative)
  416. {
  417. ls_std::XMLNode dialogNode {"dialog"};
  418. ASSERT_TRUE(dialogNode.getAttributes().empty());
  419. dialogNode.removeLastAttribute();
  420. ASSERT_TRUE(dialogNode.getAttributes().empty());
  421. }
  422. TEST_F(XMLNodeTest, removeFirstChild)
  423. {
  424. ls_std::XMLNode dialogsNode {"dialogs"};
  425. std::shared_ptr<ls_std::XMLNode> currentNode {};
  426. // preparation
  427. ASSERT_TRUE(dialogsNode.addChildToEnd(std::make_shared<ls_std::XMLNode>("dialogA")));
  428. ASSERT_TRUE(dialogsNode.addChildToEnd(std::make_shared<ls_std::XMLNode>("dialogB")));
  429. ASSERT_TRUE(dialogsNode.addChildToEnd(std::make_shared<ls_std::XMLNode>("dialogC")));
  430. ASSERT_TRUE(dialogsNode.addChildToEnd(std::make_shared<ls_std::XMLNode>("additionalInfo")));
  431. ASSERT_TRUE(dialogsNode.addChildToEnd(std::make_shared<ls_std::XMLNode>("event")));
  432. ASSERT_EQ(5, dialogsNode.getChildren().size());
  433. currentNode = *std::next(dialogsNode.getChildren().begin(), 0);
  434. ASSERT_STREQ("dialogA", currentNode->getName().c_str());
  435. currentNode = *std::next(dialogsNode.getChildren().begin(), 1);
  436. ASSERT_STREQ("dialogB", currentNode->getName().c_str());
  437. currentNode = *std::next(dialogsNode.getChildren().begin(), 2);
  438. ASSERT_STREQ("dialogC", currentNode->getName().c_str());
  439. currentNode = *std::next(dialogsNode.getChildren().begin(), 3);
  440. ASSERT_STREQ("additionalInfo", currentNode->getName().c_str());
  441. currentNode = *std::next(dialogsNode.getChildren().begin(), 4);
  442. ASSERT_STREQ("event", currentNode->getName().c_str());
  443. // check
  444. dialogsNode.removeFirstChild();
  445. currentNode = *std::next(dialogsNode.getChildren().begin(), 0);
  446. ASSERT_STREQ("dialogB", currentNode->getName().c_str());
  447. currentNode = *std::next(dialogsNode.getChildren().begin(), 1);
  448. ASSERT_STREQ("dialogC", currentNode->getName().c_str());
  449. currentNode = *std::next(dialogsNode.getChildren().begin(), 2);
  450. ASSERT_STREQ("additionalInfo", currentNode->getName().c_str());
  451. currentNode = *std::next(dialogsNode.getChildren().begin(), 3);
  452. ASSERT_STREQ("event", currentNode->getName().c_str());
  453. }
  454. TEST_F(XMLNodeTest, removeFirstChildNegative)
  455. {
  456. ls_std::XMLNode dialogsNode {"dialogs"};
  457. ASSERT_TRUE(dialogsNode.getChildren().empty());
  458. dialogsNode.removeFirstChild();
  459. ASSERT_TRUE(dialogsNode.getChildren().empty());
  460. }
  461. TEST_F(XMLNodeTest, removeLastChild)
  462. {
  463. ls_std::XMLNode dialogsNode {"dialogs"};
  464. std::shared_ptr<ls_std::XMLNode> currentNode {};
  465. // preparation
  466. ASSERT_TRUE(dialogsNode.addChildToEnd(std::make_shared<ls_std::XMLNode>("dialogA")));
  467. ASSERT_TRUE(dialogsNode.addChildToEnd(std::make_shared<ls_std::XMLNode>("dialogB")));
  468. ASSERT_TRUE(dialogsNode.addChildToEnd(std::make_shared<ls_std::XMLNode>("dialogC")));
  469. ASSERT_TRUE(dialogsNode.addChildToEnd(std::make_shared<ls_std::XMLNode>("additionalInfo")));
  470. ASSERT_TRUE(dialogsNode.addChildToEnd(std::make_shared<ls_std::XMLNode>("event")));
  471. ASSERT_EQ(5, dialogsNode.getChildren().size());
  472. currentNode = *std::next(dialogsNode.getChildren().begin(), 0);
  473. ASSERT_STREQ("dialogA", currentNode->getName().c_str());
  474. currentNode = *std::next(dialogsNode.getChildren().begin(), 1);
  475. ASSERT_STREQ("dialogB", currentNode->getName().c_str());
  476. currentNode = *std::next(dialogsNode.getChildren().begin(), 2);
  477. ASSERT_STREQ("dialogC", currentNode->getName().c_str());
  478. currentNode = *std::next(dialogsNode.getChildren().begin(), 3);
  479. ASSERT_STREQ("additionalInfo", currentNode->getName().c_str());
  480. currentNode = *std::next(dialogsNode.getChildren().begin(), 4);
  481. ASSERT_STREQ("event", currentNode->getName().c_str());
  482. // check
  483. dialogsNode.removeLastChild();
  484. currentNode = *std::next(dialogsNode.getChildren().begin(), 0);
  485. ASSERT_STREQ("dialogA", currentNode->getName().c_str());
  486. currentNode = *std::next(dialogsNode.getChildren().begin(), 1);
  487. ASSERT_STREQ("dialogB", currentNode->getName().c_str());
  488. currentNode = *std::next(dialogsNode.getChildren().begin(), 2);
  489. ASSERT_STREQ("dialogC", currentNode->getName().c_str());
  490. currentNode = *std::next(dialogsNode.getChildren().begin(), 3);
  491. ASSERT_STREQ("additionalInfo", currentNode->getName().c_str());
  492. }
  493. TEST_F(XMLNodeTest, removeLastChildNegative)
  494. {
  495. ls_std::XMLNode dialogsNode {"dialogs"};
  496. ASSERT_TRUE(dialogsNode.getChildren().empty());
  497. dialogsNode.removeLastChild();
  498. ASSERT_TRUE(dialogsNode.getChildren().empty());
  499. }
  500. TEST_F(XMLNodeTest, setName)
  501. {
  502. ls_std::XMLNode dialogNode {"dialog"};
  503. dialogNode.setName("dialog2");
  504. ASSERT_STREQ("dialog2", dialogNode.getName().c_str());
  505. }
  506. TEST_F(XMLNodeTest, setValue)
  507. {
  508. ls_std::XMLNode dialogNode {"dialog"};
  509. dialogNode.setValue("Something written");
  510. ASSERT_STREQ("Something written", dialogNode.getValue().c_str());
  511. }
  512. TEST_F(XMLNodeTest, toXML)
  513. {
  514. auto root = ls_std_test::TestDataFactory::createXMLContent();
  515. std::string xmlContent = root->toXML();
  516. std::string expectedXMLContent = R"("<dialog name="dungeon_001"><dialogUnit id="001"><text>Hello!</text></dialogUnit><dialogUnit id="002"><text>Hello again!</text></dialogUnit></dialog>")";
  517. ASSERT_TRUE(!xmlContent.empty());
  518. }
  519. }