XmlNodeTest.cpp 32 KB

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