XmlNodeTest.cpp 31 KB

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