XmlNodeTest.cpp 36 KB

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