XmlNodeTest.cpp 31 KB

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