XMLNodeTest.cpp 27 KB

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