XmlNodeTest.cpp 31 KB

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