XmlNodeTest.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-09-25
  6. * Changed: 2022-05-11
  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::io::XmlNode dialogNode{"dialog"};
  27. std::shared_ptr<ls::std::io::XmlAttribute> currentAttribute{};
  28. dialogNode.addAttributeToEnd(std::make_shared<ls::std::io::XmlAttribute>("events"));
  29. dialogNode.addAttributeToEnd(std::make_shared<ls::std::io::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::io::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::io::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, addAttributeAfter_name_not_found)
  54. {
  55. ls::std::io::XmlNode dialogNode{"dialog"};
  56. ASSERT_FALSE(dialogNode.addAttributeAfter(std::make_shared<ls::std::io::XmlAttribute>("id"), "events"));
  57. }
  58. TEST_F(XmlNodeTest, addAttributeAfter_no_reference)
  59. {
  60. ls::std::io::XmlNode dialogNode{"dialog"};
  61. EXPECT_THROW({
  62. try
  63. {
  64. dialogNode.addAttributeAfter(nullptr, "assets");
  65. }
  66. catch (const ls::std::core::IllegalArgumentException &_exception)
  67. {
  68. throw;
  69. }
  70. }, ls::std::core::IllegalArgumentException);
  71. }
  72. TEST_F(XmlNodeTest, addAttributeAfter_empty_name)
  73. {
  74. ls::std::io::XmlNode dialogNode{"dialog"};
  75. EXPECT_THROW({
  76. try
  77. {
  78. dialogNode.addAttributeAfter(std::make_shared<ls::std::io::XmlAttribute>("id"), "");
  79. }
  80. catch (const ls::std::core::IllegalArgumentException &_exception)
  81. {
  82. throw;
  83. }
  84. }, ls::std::core::IllegalArgumentException);
  85. }
  86. TEST_F(XmlNodeTest, addAttributeBefore)
  87. {
  88. ls::std::io::XmlNode dialogNode{"dialog"};
  89. std::shared_ptr<ls::std::io::XmlAttribute> currentAttribute{};
  90. dialogNode.addAttributeToEnd(std::make_shared<ls::std::io::XmlAttribute>("events"));
  91. dialogNode.addAttributeToEnd(std::make_shared<ls::std::io::XmlAttribute>("assets"));
  92. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 0);
  93. ASSERT_STREQ("events", currentAttribute->getName().c_str());
  94. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 1);
  95. ASSERT_STREQ("assets", currentAttribute->getName().c_str());
  96. // add id before events
  97. ASSERT_TRUE(dialogNode.addAttributeBefore(std::make_shared<ls::std::io::XmlAttribute>("id"), "events"));
  98. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 0);
  99. ASSERT_STREQ("id", currentAttribute->getName().c_str());
  100. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 1);
  101. ASSERT_STREQ("events", currentAttribute->getName().c_str());
  102. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 2);
  103. ASSERT_STREQ("assets", currentAttribute->getName().c_str());
  104. // add tasks before assets
  105. ASSERT_TRUE(dialogNode.addAttributeBefore(std::make_shared<ls::std::io::XmlAttribute>("tasks"), "assets"));
  106. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 0);
  107. ASSERT_STREQ("id", currentAttribute->getName().c_str());
  108. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 1);
  109. ASSERT_STREQ("events", currentAttribute->getName().c_str());
  110. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 2);
  111. ASSERT_STREQ("tasks", currentAttribute->getName().c_str());
  112. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 3);
  113. ASSERT_STREQ("assets", currentAttribute->getName().c_str());
  114. }
  115. TEST_F(XmlNodeTest, addAttributeBefore_name_not_found)
  116. {
  117. ls::std::io::XmlNode dialogNode{"dialog"};
  118. ASSERT_FALSE(dialogNode.addAttributeBefore(std::make_shared<ls::std::io::XmlAttribute>("tasks"), "assets"));
  119. }
  120. TEST_F(XmlNodeTest, addAttributeBefore_no_reference)
  121. {
  122. ls::std::io::XmlNode dialogNode{"dialog"};
  123. EXPECT_THROW({
  124. try
  125. {
  126. dialogNode.addAttributeBefore(nullptr, "assets");
  127. }
  128. catch (const ls::std::core::IllegalArgumentException &_exception)
  129. {
  130. throw;
  131. }
  132. }, ls::std::core::IllegalArgumentException);
  133. }
  134. TEST_F(XmlNodeTest, addAttributeBefore_empty_name)
  135. {
  136. ls::std::io::XmlNode dialogNode{"dialog"};
  137. EXPECT_THROW({
  138. try
  139. {
  140. dialogNode.addAttributeBefore(std::make_shared<ls::std::io::XmlAttribute>("id"), "");
  141. }
  142. catch (const ls::std::core::IllegalArgumentException &_exception)
  143. {
  144. throw;
  145. }
  146. }, ls::std::core::IllegalArgumentException);
  147. }
  148. TEST_F(XmlNodeTest, addAttributeToBeginning)
  149. {
  150. ls::std::io::XmlNode dialogNode{"dialog"};
  151. std::shared_ptr<ls::std::io::XmlAttribute> currentAttribute{};
  152. dialogNode.addAttributeToBeginning(std::make_shared<ls::std::io::XmlAttribute>("id"));
  153. ASSERT_EQ(1, dialogNode.getAttributes().size());
  154. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 0);
  155. ASSERT_STREQ("id", currentAttribute->getName().c_str());
  156. dialogNode.addAttributeToBeginning(std::make_shared<ls::std::io::XmlAttribute>("assets"));
  157. ASSERT_EQ(2, dialogNode.getAttributes().size());
  158. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 0);
  159. ASSERT_STREQ("assets", currentAttribute->getName().c_str());
  160. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 1);
  161. ASSERT_STREQ("id", currentAttribute->getName().c_str());
  162. dialogNode.addAttributeToBeginning(std::make_shared<ls::std::io::XmlAttribute>("events"));
  163. ASSERT_EQ(3, dialogNode.getAttributes().size());
  164. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 0);
  165. ASSERT_STREQ("events", currentAttribute->getName().c_str());
  166. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 1);
  167. ASSERT_STREQ("assets", currentAttribute->getName().c_str());
  168. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 2);
  169. ASSERT_STREQ("id", currentAttribute->getName().c_str());
  170. }
  171. TEST_F(XmlNodeTest, addAttributeToBeginning_no_reference)
  172. {
  173. ls::std::io::XmlNode dialogNode{"dialog"};
  174. EXPECT_THROW({
  175. try
  176. {
  177. dialogNode.addAttributeToBeginning(nullptr);
  178. }
  179. catch (const ls::std::core::IllegalArgumentException &_exception)
  180. {
  181. throw;
  182. }
  183. }, ls::std::core::IllegalArgumentException);
  184. }
  185. TEST_F(XmlNodeTest, addAttributeToEnd)
  186. {
  187. ls::std::io::XmlNode dialogNode{"dialog"};
  188. std::shared_ptr<ls::std::io::XmlAttribute> currentAttribute{};
  189. dialogNode.addAttributeToEnd(std::make_shared<ls::std::io::XmlAttribute>("id"));
  190. ASSERT_EQ(1, dialogNode.getAttributes().size());
  191. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 0);
  192. ASSERT_STREQ("id", currentAttribute->getName().c_str());
  193. dialogNode.addAttributeToEnd(std::make_shared<ls::std::io::XmlAttribute>("assets"));
  194. ASSERT_EQ(2, dialogNode.getAttributes().size());
  195. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 0);
  196. ASSERT_STREQ("id", currentAttribute->getName().c_str());
  197. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 1);
  198. ASSERT_STREQ("assets", currentAttribute->getName().c_str());
  199. dialogNode.addAttributeToEnd(std::make_shared<ls::std::io::XmlAttribute>("events"));
  200. ASSERT_EQ(3, dialogNode.getAttributes().size());
  201. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 0);
  202. ASSERT_STREQ("id", currentAttribute->getName().c_str());
  203. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 1);
  204. ASSERT_STREQ("assets", currentAttribute->getName().c_str());
  205. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 2);
  206. ASSERT_STREQ("events", currentAttribute->getName().c_str());
  207. }
  208. TEST_F(XmlNodeTest, addAttributeToEnd_no_reference)
  209. {
  210. ls::std::io::XmlNode dialogNode{"dialog"};
  211. EXPECT_THROW({
  212. try
  213. {
  214. dialogNode.addAttributeToEnd(nullptr);
  215. }
  216. catch (const ls::std::core::IllegalArgumentException &_exception)
  217. {
  218. throw;
  219. }
  220. }, ls::std::core::IllegalArgumentException);
  221. }
  222. TEST_F(XmlNodeTest, addChildAfter)
  223. {
  224. ls::std::io::XmlNode dialogsNode{"dialogs"};
  225. std::shared_ptr<ls::std::io::XmlNode> currentNode{};
  226. std::shared_ptr<ls::std::io::XmlNode> dialogNodeA = std::make_shared<ls::std::io::XmlNode>("dialogNodeA");
  227. std::shared_ptr<ls::std::io::XmlNode> dialogNodeB = std::make_shared<ls::std::io::XmlNode>("dialogNodeB");
  228. std::shared_ptr<ls::std::io::XmlNode> dialogNodeC = std::make_shared<ls::std::io::XmlNode>("dialogNodeC");
  229. std::shared_ptr<ls::std::io::XmlNode> dialogNodeD = std::make_shared<ls::std::io::XmlNode>("dialogNodeD");
  230. ASSERT_TRUE(dialogsNode.addChildToEnd(dialogNodeB));
  231. ASSERT_TRUE(dialogsNode.addChildToEnd(dialogNodeC));
  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. // add A after B
  238. ASSERT_TRUE(dialogsNode.addChildAfter(dialogNodeA, dialogNodeB));
  239. currentNode = *std::next(dialogsNode.getChildren().begin(), 0);
  240. ASSERT_STREQ("dialogNodeB", currentNode->getName().c_str());
  241. currentNode = *std::next(dialogsNode.getChildren().begin(), 1);
  242. ASSERT_STREQ("dialogNodeA", currentNode->getName().c_str());
  243. currentNode = *std::next(dialogsNode.getChildren().begin(), 2);
  244. ASSERT_STREQ("dialogNodeC", currentNode->getName().c_str());
  245. // add D after C
  246. ASSERT_TRUE(dialogsNode.addChildAfter(dialogNodeD, dialogNodeC));
  247. currentNode = *std::next(dialogsNode.getChildren().begin(), 0);
  248. ASSERT_STREQ("dialogNodeB", currentNode->getName().c_str());
  249. currentNode = *std::next(dialogsNode.getChildren().begin(), 1);
  250. ASSERT_STREQ("dialogNodeA", currentNode->getName().c_str());
  251. currentNode = *std::next(dialogsNode.getChildren().begin(), 2);
  252. ASSERT_STREQ("dialogNodeC", currentNode->getName().c_str());
  253. currentNode = *std::next(dialogsNode.getChildren().begin(), 3);
  254. ASSERT_STREQ("dialogNodeD", currentNode->getName().c_str());
  255. }
  256. TEST_F(XmlNodeTest, addChildAfter_no_search_node_available)
  257. {
  258. ls::std::io::XmlNode dialogsNode{"dialogs"};
  259. std::shared_ptr<ls::std::io::XmlNode> newChild = std::make_shared<ls::std::io::XmlNode>("newChild");
  260. std::shared_ptr<ls::std::io::XmlNode> searchNode = std::make_shared<ls::std::io::XmlNode>("searchNode");
  261. ASSERT_FALSE(dialogsNode.addChildAfter(newChild, searchNode));
  262. }
  263. TEST_F(XmlNodeTest, addChildAfter_no_child_reference)
  264. {
  265. ls::std::io::XmlNode dialogNode{"dialog"};
  266. EXPECT_THROW({
  267. try
  268. {
  269. dialogNode.addChildAfter(nullptr, std::make_shared<ls::std::io::XmlNode>("children"));
  270. }
  271. catch (const ls::std::core::IllegalArgumentException &_exception)
  272. {
  273. throw;
  274. }
  275. }, ls::std::core::IllegalArgumentException);
  276. }
  277. TEST_F(XmlNodeTest, addChildAfter_no_search_reference)
  278. {
  279. ls::std::io::XmlNode dialogNode{"dialog"};
  280. EXPECT_THROW({
  281. try
  282. {
  283. dialogNode.addChildAfter(std::make_shared<ls::std::io::XmlNode>("newChild"), nullptr);
  284. }
  285. catch (const ls::std::core::IllegalArgumentException &_exception)
  286. {
  287. throw;
  288. }
  289. }, ls::std::core::IllegalArgumentException);
  290. }
  291. TEST_F(XmlNodeTest, addChildBefore)
  292. {
  293. ls::std::io::XmlNode dialogsNode{"dialogs"};
  294. std::shared_ptr<ls::std::io::XmlNode> currentNode{};
  295. std::shared_ptr<ls::std::io::XmlNode> dialogNodeA = std::make_shared<ls::std::io::XmlNode>("dialogNodeA");
  296. std::shared_ptr<ls::std::io::XmlNode> dialogNodeB = std::make_shared<ls::std::io::XmlNode>("dialogNodeB");
  297. std::shared_ptr<ls::std::io::XmlNode> dialogNodeC = std::make_shared<ls::std::io::XmlNode>("dialogNodeC");
  298. std::shared_ptr<ls::std::io::XmlNode> dialogNodeD = std::make_shared<ls::std::io::XmlNode>("dialogNodeD");
  299. ASSERT_TRUE(dialogsNode.addChildToEnd(dialogNodeB));
  300. ASSERT_TRUE(dialogsNode.addChildToEnd(dialogNodeC));
  301. ASSERT_EQ(2, dialogsNode.getChildren().size());
  302. currentNode = *std::next(dialogsNode.getChildren().begin(), 0);
  303. ASSERT_STREQ("dialogNodeB", currentNode->getName().c_str());
  304. currentNode = *std::next(dialogsNode.getChildren().begin(), 1);
  305. ASSERT_STREQ("dialogNodeC", currentNode->getName().c_str());
  306. // add A before C
  307. ASSERT_TRUE(dialogsNode.addChildBefore(dialogNodeA, dialogNodeC));
  308. currentNode = *std::next(dialogsNode.getChildren().begin(), 0);
  309. ASSERT_STREQ("dialogNodeB", currentNode->getName().c_str());
  310. currentNode = *std::next(dialogsNode.getChildren().begin(), 1);
  311. ASSERT_STREQ("dialogNodeA", currentNode->getName().c_str());
  312. currentNode = *std::next(dialogsNode.getChildren().begin(), 2);
  313. ASSERT_STREQ("dialogNodeC", currentNode->getName().c_str());
  314. // add D before B
  315. ASSERT_TRUE(dialogsNode.addChildBefore(dialogNodeD, dialogNodeB));
  316. currentNode = *std::next(dialogsNode.getChildren().begin(), 0);
  317. ASSERT_STREQ("dialogNodeD", currentNode->getName().c_str());
  318. currentNode = *std::next(dialogsNode.getChildren().begin(), 1);
  319. ASSERT_STREQ("dialogNodeB", currentNode->getName().c_str());
  320. currentNode = *std::next(dialogsNode.getChildren().begin(), 2);
  321. ASSERT_STREQ("dialogNodeA", currentNode->getName().c_str());
  322. currentNode = *std::next(dialogsNode.getChildren().begin(), 3);
  323. ASSERT_STREQ("dialogNodeC", currentNode->getName().c_str());
  324. }
  325. TEST_F(XmlNodeTest, addChildBefore_no_search_node_available)
  326. {
  327. ls::std::io::XmlNode dialogsNode{"dialogs"};
  328. std::shared_ptr<ls::std::io::XmlNode> newChild = std::make_shared<ls::std::io::XmlNode>("newChild");
  329. std::shared_ptr<ls::std::io::XmlNode> searchNode = std::make_shared<ls::std::io::XmlNode>("searchNode");
  330. ASSERT_FALSE(dialogsNode.addChildBefore(newChild, searchNode));
  331. }
  332. TEST_F(XmlNodeTest, addChildBefore_no_child_reference)
  333. {
  334. ls::std::io::XmlNode dialogNode{"dialog"};
  335. EXPECT_THROW({
  336. try
  337. {
  338. dialogNode.addChildBefore(nullptr, std::make_shared<ls::std::io::XmlNode>("children"));
  339. }
  340. catch (const ls::std::core::IllegalArgumentException &_exception)
  341. {
  342. throw;
  343. }
  344. }, ls::std::core::IllegalArgumentException);
  345. }
  346. TEST_F(XmlNodeTest, addChildBefore_no_search_reference)
  347. {
  348. ls::std::io::XmlNode dialogNode{"dialog"};
  349. EXPECT_THROW({
  350. try
  351. {
  352. dialogNode.addChildBefore(std::make_shared<ls::std::io::XmlNode>("newChild"), nullptr);
  353. }
  354. catch (const ls::std::core::IllegalArgumentException &_exception)
  355. {
  356. throw;
  357. }
  358. }, ls::std::core::IllegalArgumentException);
  359. }
  360. TEST_F(XmlNodeTest, addChildToBeginning)
  361. {
  362. ls::std::io::XmlNode dialogsNode{"dialogs"};
  363. std::shared_ptr<ls::std::io::XmlNode> currentNode{};
  364. ls::std::io::XmlNode dialogNodeA{"dialogNodeA"};
  365. ls::std::io::XmlNode dialogNodeB{"dialogNodeB"};
  366. ls::std::io::XmlNode dialogNodeC{"dialogNodeC"};
  367. ASSERT_TRUE(dialogsNode.getChildren().empty());
  368. ASSERT_EQ(0, dialogsNode.getChildren().size());
  369. // adding C
  370. ASSERT_TRUE(dialogsNode.addChildToBeginning(std::make_shared<ls::std::io::XmlNode>(dialogNodeC)));
  371. ASSERT_TRUE(!dialogsNode.getChildren().empty());
  372. ASSERT_EQ(1, dialogsNode.getChildren().size());
  373. currentNode = *std::next(dialogsNode.getChildren().begin(), 0);
  374. ASSERT_STREQ("dialogNodeC", currentNode->getName().c_str());
  375. // adding B
  376. ASSERT_TRUE(dialogsNode.addChildToBeginning(std::make_shared<ls::std::io::XmlNode>(dialogNodeB)));
  377. ASSERT_EQ(2, dialogsNode.getChildren().size());
  378. currentNode = *std::next(dialogsNode.getChildren().begin(), 0);
  379. ASSERT_STREQ("dialogNodeB", currentNode->getName().c_str());
  380. currentNode = *std::next(dialogsNode.getChildren().begin(), 1);
  381. ASSERT_STREQ("dialogNodeC", currentNode->getName().c_str());
  382. // adding A
  383. ASSERT_TRUE(dialogsNode.addChildToBeginning(std::make_shared<ls::std::io::XmlNode>(dialogNodeA)));
  384. ASSERT_EQ(3, dialogsNode.getChildren().size());
  385. currentNode = *std::next(dialogsNode.getChildren().begin(), 0);
  386. ASSERT_STREQ("dialogNodeA", currentNode->getName().c_str());
  387. currentNode = *std::next(dialogsNode.getChildren().begin(), 1);
  388. ASSERT_STREQ("dialogNodeB", currentNode->getName().c_str());
  389. currentNode = *std::next(dialogsNode.getChildren().begin(), 2);
  390. ASSERT_STREQ("dialogNodeC", currentNode->getName().c_str());
  391. }
  392. TEST_F(XmlNodeTest, addChildToBeginning_no_child_reference)
  393. {
  394. ls::std::io::XmlNode dialogNode{"dialog"};
  395. EXPECT_THROW({
  396. try
  397. {
  398. dialogNode.addChildToBeginning(nullptr);
  399. }
  400. catch (const ls::std::core::IllegalArgumentException &_exception)
  401. {
  402. throw;
  403. }
  404. }, ls::std::core::IllegalArgumentException);
  405. }
  406. TEST_F(XmlNodeTest, addChildToEnd)
  407. {
  408. ls::std::io::XmlNode dialogsNode{"dialogs"};
  409. std::shared_ptr<ls::std::io::XmlNode> currentNode{};
  410. ls::std::io::XmlNode dialogNodeA{"dialogNodeA"};
  411. ls::std::io::XmlNode dialogNodeB{"dialogNodeB"};
  412. ls::std::io::XmlNode dialogNodeC{"dialogNodeC"};
  413. ASSERT_TRUE(dialogsNode.getChildren().empty());
  414. ASSERT_EQ(0, dialogsNode.getChildren().size());
  415. // adding C
  416. ASSERT_TRUE(dialogsNode.addChildToEnd(std::make_shared<ls::std::io::XmlNode>(dialogNodeC)));
  417. ASSERT_TRUE(!dialogsNode.getChildren().empty());
  418. ASSERT_EQ(1, dialogsNode.getChildren().size());
  419. currentNode = *std::next(dialogsNode.getChildren().begin(), 0);
  420. ASSERT_STREQ("dialogNodeC", currentNode->getName().c_str());
  421. // adding B
  422. ASSERT_TRUE(dialogsNode.addChildToEnd(std::make_shared<ls::std::io::XmlNode>(dialogNodeB)));
  423. ASSERT_EQ(2, dialogsNode.getChildren().size());
  424. currentNode = *std::next(dialogsNode.getChildren().begin(), 0);
  425. ASSERT_STREQ("dialogNodeC", currentNode->getName().c_str());
  426. currentNode = *std::next(dialogsNode.getChildren().begin(), 1);
  427. ASSERT_STREQ("dialogNodeB", currentNode->getName().c_str());
  428. // adding A
  429. ASSERT_TRUE(dialogsNode.addChildToEnd(std::make_shared<ls::std::io::XmlNode>(dialogNodeA)));
  430. ASSERT_EQ(3, dialogsNode.getChildren().size());
  431. currentNode = *std::next(dialogsNode.getChildren().begin(), 0);
  432. ASSERT_STREQ("dialogNodeC", currentNode->getName().c_str());
  433. currentNode = *std::next(dialogsNode.getChildren().begin(), 1);
  434. ASSERT_STREQ("dialogNodeB", currentNode->getName().c_str());
  435. currentNode = *std::next(dialogsNode.getChildren().begin(), 2);
  436. ASSERT_STREQ("dialogNodeA", currentNode->getName().c_str());
  437. }
  438. TEST_F(XmlNodeTest, addChildToEnd_no_child_reference)
  439. {
  440. ls::std::io::XmlNode dialogNode{"dialog"};
  441. EXPECT_THROW({
  442. try
  443. {
  444. dialogNode.addChildToEnd(nullptr);
  445. }
  446. catch (const ls::std::core::IllegalArgumentException &_exception)
  447. {
  448. throw;
  449. }
  450. }, ls::std::core::IllegalArgumentException);
  451. }
  452. TEST_F(XmlNodeTest, clearValue)
  453. {
  454. ls::std::io::XmlNode dialogNode{"dialog"};
  455. dialogNode.setValue("Something");
  456. ASSERT_STREQ("Something", dialogNode.getValue().c_str());
  457. dialogNode.clearValue();
  458. ASSERT_TRUE(dialogNode.getValue().empty());
  459. ASSERT_STREQ("", dialogNode.getValue().c_str());
  460. }
  461. TEST_F(XmlNodeTest, getAttributes)
  462. {
  463. ls::std::io::XmlNode dialogNode{"dialog"};
  464. ASSERT_TRUE(dialogNode.getAttributes().empty());
  465. }
  466. TEST_F(XmlNodeTest, getChildren)
  467. {
  468. ls::std::io::XmlNode dialogNode{"dialog"};
  469. ASSERT_TRUE(dialogNode.getChildren().empty());
  470. }
  471. TEST_F(XmlNodeTest, getChildrenV2)
  472. {
  473. ls::std::io::XmlNode dialogsNode{"dialogs"};
  474. ls::std::io::XmlNode dialogNode{"dialog"};
  475. ls::std::io::XmlNode otherNode{"something"};
  476. // preparation
  477. ASSERT_TRUE(dialogsNode.addChildToEnd(std::make_shared<ls::std::io::XmlNode>(dialogNode)));
  478. ASSERT_TRUE(dialogsNode.addChildToEnd(std::make_shared<ls::std::io::XmlNode>(otherNode)));
  479. ASSERT_TRUE(dialogsNode.addChildToEnd(std::make_shared<ls::std::io::XmlNode>(otherNode)));
  480. ASSERT_TRUE(dialogsNode.addChildToEnd(std::make_shared<ls::std::io::XmlNode>(dialogNode)));
  481. ASSERT_TRUE(dialogsNode.addChildToEnd(std::make_shared<ls::std::io::XmlNode>(otherNode)));
  482. // check
  483. ASSERT_TRUE(!dialogsNode.getChildren().empty());
  484. ASSERT_EQ(5, dialogsNode.getChildren().size());
  485. ASSERT_EQ(2, dialogsNode.getChildren("dialog").size());
  486. ASSERT_EQ(3, dialogsNode.getChildren("something").size());
  487. }
  488. TEST_F(XmlNodeTest, getName)
  489. {
  490. ls::std::io::XmlNode dialogNode{"dialog"};
  491. ASSERT_STREQ("dialog", dialogNode.getName().c_str());
  492. }
  493. TEST_F(XmlNodeTest, getValue)
  494. {
  495. ls::std::io::XmlNode dialogNode{"dialog"};
  496. ASSERT_TRUE(dialogNode.getValue().empty());
  497. }
  498. TEST_F(XmlNodeTest, hasAttribute)
  499. {
  500. ls::std::io::XmlNode dialogNode{"dialogNode"};
  501. dialogNode.addAttributeToEnd(std::make_shared<ls::std::io::XmlAttribute>("id"));
  502. dialogNode.addAttributeToEnd(std::make_shared<ls::std::io::XmlAttribute>("events"));
  503. dialogNode.addAttributeToEnd(std::make_shared<ls::std::io::XmlAttribute>("assets"));
  504. ASSERT_TRUE(dialogNode.hasAttribute("id"));
  505. ASSERT_TRUE(dialogNode.hasAttribute("events"));
  506. ASSERT_TRUE(dialogNode.hasAttribute("assets"));
  507. }
  508. TEST_F(XmlNodeTest, hasAttribute_attribute_not_available)
  509. {
  510. ls::std::io::XmlNode dialogNode{"dialogNode"};
  511. ASSERT_FALSE(dialogNode.hasAttribute("fields"));
  512. }
  513. TEST_F(XmlNodeTest, hasAttribute_empty_name)
  514. {
  515. ls::std::io::XmlNode dialogNode{"dialogNode"};
  516. EXPECT_THROW({
  517. try
  518. {
  519. dialogNode.hasAttribute("");
  520. }
  521. catch (const ls::std::core::IllegalArgumentException &_exception)
  522. {
  523. throw;
  524. }
  525. }, ls::std::core::IllegalArgumentException);
  526. }
  527. TEST_F(XmlNodeTest, hasChild)
  528. {
  529. ls::std::io::XmlNode dialogsNode{"dialogsNode"};
  530. dialogsNode.addChildToEnd(std::make_shared<ls::std::io::XmlNode>("dialogA"));
  531. dialogsNode.addChildToEnd(std::make_shared<ls::std::io::XmlNode>("dialogB"));
  532. dialogsNode.addChildToEnd(std::make_shared<ls::std::io::XmlNode>("dialogC"));
  533. ASSERT_TRUE(dialogsNode.hasChild("dialogA"));
  534. ASSERT_TRUE(dialogsNode.hasChild("dialogB"));
  535. ASSERT_TRUE(dialogsNode.hasChild("dialogC"));
  536. }
  537. TEST_F(XmlNodeTest, hasChild_child_not_available)
  538. {
  539. ls::std::io::XmlNode dialogsNode{"dialogsNode"};
  540. ASSERT_FALSE(dialogsNode.hasChild("dialogD"));
  541. }
  542. TEST_F(XmlNodeTest, hasChild_empty_name)
  543. {
  544. ls::std::io::XmlNode dialogNode{"dialogNode"};
  545. EXPECT_THROW({
  546. try
  547. {
  548. dialogNode.hasChild("");
  549. }
  550. catch (const ls::std::core::IllegalArgumentException &_exception)
  551. {
  552. throw;
  553. }
  554. }, ls::std::core::IllegalArgumentException);
  555. }
  556. TEST_F(XmlNodeTest, hasChildV2)
  557. {
  558. ls::std::io::XmlNode dialogsNode{"dialogsNode"};
  559. std::shared_ptr<ls::std::io::XmlNode> searchNode = std::make_shared<ls::std::io::XmlNode>("dialogB");
  560. dialogsNode.addChildToEnd(std::make_shared<ls::std::io::XmlNode>("dialogA"));
  561. dialogsNode.addChildToEnd(searchNode);
  562. dialogsNode.addChildToEnd(std::make_shared<ls::std::io::XmlNode>("dialogC"));
  563. ASSERT_TRUE(dialogsNode.hasChild(searchNode));
  564. }
  565. TEST_F(XmlNodeTest, hasChildV2_child_not_available)
  566. {
  567. ls::std::io::XmlNode dialogsNode{"dialogsNode"};
  568. std::shared_ptr<ls::std::io::XmlNode> searchNode = std::make_shared<ls::std::io::XmlNode>("dialogB");
  569. ASSERT_FALSE(dialogsNode.hasChild(searchNode));
  570. }
  571. TEST_F(XmlNodeTest, hasChildV2_no_child_reference)
  572. {
  573. ls::std::io::XmlNode dialogNode{"dialog"};
  574. std::shared_ptr<ls::std::io::XmlNode> searchNode{};
  575. EXPECT_THROW({
  576. try
  577. {
  578. dialogNode.hasChild(searchNode);
  579. }
  580. catch (const ls::std::core::IllegalArgumentException &_exception)
  581. {
  582. throw;
  583. }
  584. }, ls::std::core::IllegalArgumentException);
  585. }
  586. TEST_F(XmlNodeTest, removeFirstAttribute)
  587. {
  588. ls::std::io::XmlNode dialogNode{"dialog"};
  589. std::shared_ptr<ls::std::io::XmlAttribute> currentAttribute{};
  590. dialogNode.addAttributeToEnd(std::make_shared<ls::std::io::XmlAttribute>("id"));
  591. dialogNode.addAttributeToEnd(std::make_shared<ls::std::io::XmlAttribute>("events"));
  592. dialogNode.addAttributeToEnd(std::make_shared<ls::std::io::XmlAttribute>("assets"));
  593. ASSERT_TRUE(!dialogNode.getAttributes().empty());
  594. ASSERT_EQ(3, dialogNode.getAttributes().size());
  595. ASSERT_TRUE(dialogNode.removeFirstAttribute());
  596. ASSERT_EQ(2, dialogNode.getAttributes().size());
  597. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 0);
  598. ASSERT_STREQ("events", currentAttribute->getName().c_str());
  599. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 1);
  600. ASSERT_STREQ("assets", currentAttribute->getName().c_str());
  601. }
  602. TEST_F(XmlNodeTest, removeFirstAttribute_no_attributes_available)
  603. {
  604. ls::std::io::XmlNode dialogNode{"dialog"};
  605. ASSERT_FALSE(dialogNode.removeFirstAttribute());
  606. }
  607. TEST_F(XmlNodeTest, removeLastAttribute)
  608. {
  609. ls::std::io::XmlNode dialogNode{"dialog"};
  610. std::shared_ptr<ls::std::io::XmlAttribute> currentAttribute{};
  611. dialogNode.addAttributeToEnd(std::make_shared<ls::std::io::XmlAttribute>("id"));
  612. dialogNode.addAttributeToEnd(std::make_shared<ls::std::io::XmlAttribute>("events"));
  613. dialogNode.addAttributeToEnd(std::make_shared<ls::std::io::XmlAttribute>("assets"));
  614. ASSERT_TRUE(!dialogNode.getAttributes().empty());
  615. ASSERT_EQ(3, dialogNode.getAttributes().size());
  616. ASSERT_TRUE(dialogNode.removeLastAttribute());
  617. ASSERT_EQ(2, dialogNode.getAttributes().size());
  618. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 0);
  619. ASSERT_STREQ("id", currentAttribute->getName().c_str());
  620. currentAttribute = *std::next(dialogNode.getAttributes().begin(), 1);
  621. ASSERT_STREQ("events", currentAttribute->getName().c_str());
  622. }
  623. TEST_F(XmlNodeTest, removeLastAttribute_no_attributes_available)
  624. {
  625. ls::std::io::XmlNode dialogNode{"dialog"};
  626. ASSERT_FALSE(dialogNode.removeLastAttribute());
  627. }
  628. TEST_F(XmlNodeTest, removeFirstChild)
  629. {
  630. ls::std::io::XmlNode dialogsNode{"dialogs"};
  631. std::shared_ptr<ls::std::io::XmlNode> currentNode{};
  632. // preparation
  633. ASSERT_TRUE(dialogsNode.addChildToEnd(std::make_shared<ls::std::io::XmlNode>("dialogA")));
  634. ASSERT_TRUE(dialogsNode.addChildToEnd(std::make_shared<ls::std::io::XmlNode>("dialogB")));
  635. ASSERT_TRUE(dialogsNode.addChildToEnd(std::make_shared<ls::std::io::XmlNode>("dialogC")));
  636. ASSERT_TRUE(dialogsNode.addChildToEnd(std::make_shared<ls::std::io::XmlNode>("additionalInfo")));
  637. ASSERT_TRUE(dialogsNode.addChildToEnd(std::make_shared<ls::std::io::XmlNode>("event")));
  638. ASSERT_EQ(5, dialogsNode.getChildren().size());
  639. currentNode = *std::next(dialogsNode.getChildren().begin(), 0);
  640. ASSERT_STREQ("dialogA", currentNode->getName().c_str());
  641. currentNode = *std::next(dialogsNode.getChildren().begin(), 1);
  642. ASSERT_STREQ("dialogB", currentNode->getName().c_str());
  643. currentNode = *std::next(dialogsNode.getChildren().begin(), 2);
  644. ASSERT_STREQ("dialogC", currentNode->getName().c_str());
  645. currentNode = *std::next(dialogsNode.getChildren().begin(), 3);
  646. ASSERT_STREQ("additionalInfo", currentNode->getName().c_str());
  647. currentNode = *std::next(dialogsNode.getChildren().begin(), 4);
  648. ASSERT_STREQ("event", currentNode->getName().c_str());
  649. // check
  650. ASSERT_TRUE(dialogsNode.removeFirstChild());
  651. currentNode = *std::next(dialogsNode.getChildren().begin(), 0);
  652. ASSERT_STREQ("dialogB", currentNode->getName().c_str());
  653. currentNode = *std::next(dialogsNode.getChildren().begin(), 1);
  654. ASSERT_STREQ("dialogC", currentNode->getName().c_str());
  655. currentNode = *std::next(dialogsNode.getChildren().begin(), 2);
  656. ASSERT_STREQ("additionalInfo", currentNode->getName().c_str());
  657. currentNode = *std::next(dialogsNode.getChildren().begin(), 3);
  658. ASSERT_STREQ("event", currentNode->getName().c_str());
  659. }
  660. TEST_F(XmlNodeTest, removeFirstChild_no_children_available)
  661. {
  662. ls::std::io::XmlNode dialogsNode{"dialogs"};
  663. ASSERT_FALSE(dialogsNode.removeFirstChild());
  664. }
  665. TEST_F(XmlNodeTest, removeLastChild)
  666. {
  667. ls::std::io::XmlNode dialogsNode{"dialogs"};
  668. std::shared_ptr<ls::std::io::XmlNode> currentNode{};
  669. // preparation
  670. ASSERT_TRUE(dialogsNode.addChildToEnd(std::make_shared<ls::std::io::XmlNode>("dialogA")));
  671. ASSERT_TRUE(dialogsNode.addChildToEnd(std::make_shared<ls::std::io::XmlNode>("dialogB")));
  672. ASSERT_TRUE(dialogsNode.addChildToEnd(std::make_shared<ls::std::io::XmlNode>("dialogC")));
  673. ASSERT_TRUE(dialogsNode.addChildToEnd(std::make_shared<ls::std::io::XmlNode>("additionalInfo")));
  674. ASSERT_TRUE(dialogsNode.addChildToEnd(std::make_shared<ls::std::io::XmlNode>("event")));
  675. ASSERT_EQ(5, dialogsNode.getChildren().size());
  676. currentNode = *std::next(dialogsNode.getChildren().begin(), 0);
  677. ASSERT_STREQ("dialogA", currentNode->getName().c_str());
  678. currentNode = *std::next(dialogsNode.getChildren().begin(), 1);
  679. ASSERT_STREQ("dialogB", currentNode->getName().c_str());
  680. currentNode = *std::next(dialogsNode.getChildren().begin(), 2);
  681. ASSERT_STREQ("dialogC", currentNode->getName().c_str());
  682. currentNode = *std::next(dialogsNode.getChildren().begin(), 3);
  683. ASSERT_STREQ("additionalInfo", currentNode->getName().c_str());
  684. currentNode = *std::next(dialogsNode.getChildren().begin(), 4);
  685. ASSERT_STREQ("event", currentNode->getName().c_str());
  686. // check
  687. ASSERT_TRUE(dialogsNode.removeLastChild());
  688. currentNode = *std::next(dialogsNode.getChildren().begin(), 0);
  689. ASSERT_STREQ("dialogA", currentNode->getName().c_str());
  690. currentNode = *std::next(dialogsNode.getChildren().begin(), 1);
  691. ASSERT_STREQ("dialogB", currentNode->getName().c_str());
  692. currentNode = *std::next(dialogsNode.getChildren().begin(), 2);
  693. ASSERT_STREQ("dialogC", currentNode->getName().c_str());
  694. currentNode = *std::next(dialogsNode.getChildren().begin(), 3);
  695. ASSERT_STREQ("additionalInfo", currentNode->getName().c_str());
  696. }
  697. TEST_F(XmlNodeTest, removeLastChild_no_children_available)
  698. {
  699. ls::std::io::XmlNode dialogsNode{"dialogs"};
  700. ASSERT_FALSE(dialogsNode.removeLastChild());
  701. }
  702. TEST_F(XmlNodeTest, setName)
  703. {
  704. ls::std::io::XmlNode dialogNode{"dialog"};
  705. dialogNode.setName("dialog2");
  706. ASSERT_STREQ("dialog2", dialogNode.getName().c_str());
  707. }
  708. TEST_F(XmlNodeTest, setName_empty_name)
  709. {
  710. ls::std::io::XmlNode dialogNode{"dialog"};
  711. EXPECT_THROW({
  712. try
  713. {
  714. dialogNode.setName("");
  715. }
  716. catch (const ls::std::core::IllegalArgumentException &_exception)
  717. {
  718. throw;
  719. }
  720. }, ls::std::core::IllegalArgumentException);
  721. }
  722. TEST_F(XmlNodeTest, setValue)
  723. {
  724. ls::std::io::XmlNode dialogNode{"dialog"};
  725. dialogNode.setValue("Something written");
  726. ASSERT_STREQ("Something written", dialogNode.getValue().c_str());
  727. }
  728. TEST_F(XmlNodeTest, setValue_empty_value)
  729. {
  730. ls::std::io::XmlNode dialogNode{"dialog"};
  731. EXPECT_THROW({
  732. try
  733. {
  734. dialogNode.setValue("");
  735. }
  736. catch (const ls::std::core::IllegalArgumentException &_exception)
  737. {
  738. throw;
  739. }
  740. }, ls::std::core::IllegalArgumentException);
  741. }
  742. TEST_F(XmlNodeTest, toXml)
  743. {
  744. auto root = ls_std_test::TestDataFactory::createXmlContent();
  745. std::string xmlContent = root->toXml();
  746. ASSERT_TRUE(!xmlContent.empty());
  747. }
  748. TEST_F(XmlNodeTest, toXml_no_value)
  749. {
  750. std::shared_ptr<ls::std::io::XmlNode> singleLineElement = std::make_shared<ls::std::io::XmlNode>("info");
  751. std::shared_ptr<ls::std::io::XmlAttribute> attribute = std::make_shared<ls::std::io::XmlAttribute>("id");
  752. attribute->setValue("important");
  753. singleLineElement->addAttributeToEnd(attribute);
  754. ls::std::boxing::String xmlContent{singleLineElement->toXml()};
  755. std::string expectedXmlString = R"(<info id="important" />)";
  756. ASSERT_TRUE(xmlContent.contains(expectedXmlString));
  757. }
  758. }