Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

testXMLReader.cpp 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. #include <iostream>
  2. #include <string>
  3. #include "CodeDweller/XMLReader.hpp"
  4. ////////////////////////////////////////////////////////////////////////////////
  5. // Configuration ///////////////////////////////////////////////////////////////
  6. ////////////////////////////////////////////////////////////////////////////////
  7. ////////////////////////////////////////////////////////////////////////////////
  8. // End of configuration ////////////////////////////////////////////////////////
  9. ////////////////////////////////////////////////////////////////////////////////
  10. int nTotalTests = 0;
  11. int nPass = 0;
  12. int nFail = 0;
  13. bool result;
  14. #define NO_EXCEPTION_TERM(msg) \
  15. std::cout \
  16. << msg << " failed to throw exception at line " \
  17. << __LINE__ << "." << std::endl
  18. #define EXCEPTION_TERM(msg) \
  19. std::cout \
  20. << msg << " threw unexpected exception: " << e.what() << std::endl
  21. #define RETURN_FALSE(msg) \
  22. std::cout \
  23. << msg << " at line " << __LINE__ << std::endl; \
  24. return false;
  25. #define RUN_TEST(test) \
  26. std::cout << " " #test ": "; \
  27. std::cout.flush(); \
  28. result = test(); \
  29. std::cout << (result ? "ok" : "fail") << std::endl; \
  30. nTotalTests++; \
  31. if (result) nPass++; else nFail++;
  32. #define SUMMARY \
  33. std::cout \
  34. << "\nPass: " << nPass \
  35. << ", Fail: " << nFail \
  36. << ", Total: " << nTotalTests << std::endl
  37. ////////////////////////////////////////////////////////////////////////////////
  38. // Tests ///////////////////////////////////////////////////////////////////////
  39. ////////////////////////////////////////////////////////////////////////////////
  40. bool testEmptyElement() {
  41. std::string stageContent, stage1Content, stage2Content;
  42. std::string elementXml, element1Xml, element2Xml;
  43. CodeDweller::XMLReaderElement reader("elem");
  44. reader
  45. .Element("stage", stageContent)
  46. .RawData(elementXml)
  47. .End("stage")
  48. .Element("stage1", stage1Content)
  49. .RawData(element1Xml)
  50. .End("stage1")
  51. .Element("stage2", stage2Content)
  52. .RawData(element2Xml)
  53. .End("stage2")
  54. .End("elem");
  55. std::string xml;
  56. xml = "<elem/>";
  57. CodeDweller::XMLReaderData confData(xml.data(), xml.size());
  58. reader.initialize();
  59. if (0 == reader.interpret(confData)) {
  60. RETURN_FALSE("Error parsing XML: " + confData.Log.str());
  61. }
  62. if (!(stageContent.empty() &&
  63. stage1Content.empty() &&
  64. stage2Content.empty())) {
  65. RETURN_FALSE("empty element 1 read failure");
  66. }
  67. xml = "<elem></elem>";
  68. CodeDweller::XMLReaderData confData1(xml.data(), xml.size());
  69. reader.initialize();
  70. if (0 == reader.interpret(confData1)) {
  71. RETURN_FALSE("Error parsing XML: " + confData.Log.str());
  72. }
  73. if (!(stageContent.empty() &&
  74. stage1Content.empty() &&
  75. stage2Content.empty())) {
  76. RETURN_FALSE("empty element 2 read failure");
  77. }
  78. xml = "<elem>Should be<xx> abc </xx> ignored<stage></elem>";
  79. CodeDweller::XMLReaderData confData2(xml.data(), xml.size());
  80. reader.initialize();
  81. if (0 == reader.interpret(confData2)) {
  82. RETURN_FALSE("Error parsing XML: " + confData.Log.str());
  83. }
  84. if (!(stageContent.empty() &&
  85. stage1Content.empty() &&
  86. stage2Content.empty())) {
  87. RETURN_FALSE("empty element 3 read failure");
  88. }
  89. return true;
  90. }
  91. bool testRawData() {
  92. std::string stageContent;
  93. std::string elementXml, stageXml;
  94. CodeDweller::XMLReaderElement reader("elem");
  95. reader
  96. .RawData(elementXml)
  97. .Element("stage", stageContent)
  98. .RawData(stageXml)
  99. .End("stage")
  100. .End("elem");
  101. std::string xml;
  102. xml = "<elem><stage>Content</stage></elem>";
  103. CodeDweller::XMLReaderData confData(xml.data(), xml.size());
  104. reader.initialize();
  105. if (0 == reader.interpret(confData)) {
  106. RETURN_FALSE("Error parsing XML: " + confData.Log.str());
  107. }
  108. if (elementXml != xml) {
  109. std::cout << "\nExpected: '" << xml << "'\n"
  110. << "Got: '" << elementXml << "'\n";
  111. RETURN_FALSE("RawData() failure for root element");
  112. }
  113. if ("<stage>Content</stage>" != stageXml) {
  114. RETURN_FALSE("RawData() failure for child element");
  115. }
  116. if ("Content" != stageContent) {
  117. RETURN_FALSE("content failure for child element");
  118. }
  119. return true;
  120. }
  121. bool testSimilarElementName() {
  122. std::string stageContent, stage1Content, stage2Content;
  123. CodeDweller::XMLReaderElement reader("elem");
  124. reader
  125. .Element("stage", stageContent)
  126. .End("stage")
  127. .Element("stage1", stage1Content)
  128. .End("stage1")
  129. .Element("stage2", stage2Content)
  130. .End("stage2")
  131. .End("elem");
  132. std::string xml;
  133. std::string expectedStageContent = "StageContent";
  134. std::string expectedStage1Content = "Stage1Content";
  135. std::string expectedStage2Content = "Stage2Content";
  136. xml =
  137. "<elem>\n"
  138. " <stage2>" + expectedStage2Content + "</stage2>\n"
  139. " <stage>" + expectedStageContent + "</stage>\n"
  140. " <stage1>" + expectedStage1Content + "</stage1>\n"
  141. "</elem>";
  142. CodeDweller::XMLReaderData confData(xml.data(), xml.size());
  143. reader.initialize();
  144. if (0 == reader.interpret(confData)) {
  145. RETURN_FALSE("Error parsing XML: " + confData.Log.str());
  146. }
  147. if (expectedStageContent != stageContent) {
  148. RETURN_FALSE("<stage> content read failure");
  149. }
  150. if (expectedStage1Content != stage1Content) {
  151. RETURN_FALSE("<stage1> content read failure");
  152. }
  153. if (expectedStage2Content != stage2Content) {
  154. RETURN_FALSE("<stage2> content read failure");
  155. }
  156. return true;
  157. }
  158. bool testWrongClosingTag() {
  159. std::string stageContent;
  160. CodeDweller::XMLReaderElement reader("elem");
  161. reader
  162. .Element("stage", stageContent)
  163. .End("stage")
  164. .End("elem");
  165. std::string xml;
  166. std::string expectedStageContent = "StageContent";
  167. xml =
  168. "<elem>\n"
  169. " <stage>" + expectedStageContent + "</xxx>\n"
  170. "</elem>";
  171. CodeDweller::XMLReaderData confData(xml.data(), xml.size());
  172. reader.initialize();
  173. if (0 != reader.interpret(confData)) {
  174. std::cout << "\nParsing '" << xml << "'\n";
  175. RETURN_FALSE("Error: Wrong closing tag not detected.");
  176. }
  177. return true;
  178. }
  179. bool testSimilarAttributeName() {
  180. std::string nameAttr, naAttr, nameLongAttr;
  181. CodeDweller::XMLReaderElement reader("elem");
  182. reader
  183. .Element("stage")
  184. .Attribute("name", nameAttr)
  185. .Attribute("na", naAttr)
  186. .Attribute("nameLong", nameLongAttr)
  187. .End("stage")
  188. .End("elem");
  189. std::string xml;
  190. std::string expectedNameAttr = "NameValue";
  191. std::string expectedNaAttr = "NaValue";
  192. std::string expectedNameLongAttr = "NameLongValue";
  193. xml =
  194. "<elem>\n"
  195. " <stage\n"
  196. " name='" + expectedNameAttr + "'\n"
  197. " na='" + expectedNaAttr + "'\n"
  198. " nameLong='" + expectedNameLongAttr + "'\n"
  199. "/></elem>";
  200. CodeDweller::XMLReaderData confData(xml.data(), xml.size());
  201. reader.initialize();
  202. if (0 == reader.interpret(confData)) {
  203. RETURN_FALSE("Error parsing XML: " + confData.Log.str());
  204. }
  205. if (expectedNameAttr != nameAttr) {
  206. RETURN_FALSE("\"name\" attribute read failure");
  207. }
  208. if (expectedNaAttr != naAttr) {
  209. RETURN_FALSE("\"na\" attribute read failure");
  210. }
  211. if (expectedNameLongAttr != nameLongAttr) {
  212. RETURN_FALSE("\"nameLong\" attribute read failure");
  213. }
  214. return true;
  215. }
  216. bool testInvalidAttribute() {
  217. std::string nameAttr;
  218. CodeDweller::XMLReaderElement reader("elem");
  219. reader
  220. .Element("stage")
  221. .Attribute("name", nameAttr)
  222. .End("stage")
  223. .End("elem");
  224. std::string xml;
  225. xml = "<elem><stage name=/></elem>";
  226. CodeDweller::XMLReaderData confData(xml.data(), xml.size());
  227. reader.initialize();
  228. if (0 != reader.interpret(confData)) {
  229. std::cout << "\nParsing '" << xml << "'\n";
  230. RETURN_FALSE("Error: Invalid attribute not detected.");
  231. }
  232. return true;
  233. }
  234. bool testHugeAttributeValue() {
  235. std::string xml = "<data name='messageContent' value='";
  236. std::string expectedValue;
  237. size_t nSize = 1000 * 1000 * 10;
  238. expectedValue.reserve(nSize);
  239. expectedValue.assign(nSize, 'x');
  240. xml.reserve(nSize + 512);
  241. xml.append(expectedValue);
  242. xml.append("'/>");
  243. CodeDweller::XMLReaderData confData(xml.data(), xml.size());
  244. CodeDweller::XMLReaderElement reader("data");
  245. std::string nameAttr, valueAttr;
  246. reader
  247. .Attribute("name", nameAttr)
  248. .Attribute("value", valueAttr, ">")
  249. .End("data");
  250. reader.initialize();
  251. if (0 == reader.interpret(confData)) {
  252. RETURN_FALSE("Error parsing XML: " + confData.Log.str());
  253. }
  254. if ("messageContent" != nameAttr) {
  255. RETURN_FALSE("\"name\" attribute read failure");
  256. }
  257. if (expectedValue != valueAttr) {
  258. RETURN_FALSE("\"value\" attribute read failure");
  259. }
  260. return true;
  261. }
  262. bool testNullAttributeValue() {
  263. std::string xml = "<data name='messageContent' value=''/>";
  264. CodeDweller::XMLReaderData confData(xml.data(), xml.size());
  265. CodeDweller::XMLReaderElement reader("data");
  266. std::string nameAttr, valueAttr, setAttr;
  267. reader
  268. .Attribute("name", nameAttr)
  269. .Attribute("value", valueAttr, ">")
  270. .Attribute("set", setAttr, ">")
  271. .End("data");
  272. reader.initialize();
  273. if (0 == reader.interpret(confData)) {
  274. RETURN_FALSE("Error parsing XML: " + confData.Log.str());
  275. }
  276. if ("messageContent" != nameAttr) {
  277. RETURN_FALSE("\"name\" attribute read failure");
  278. }
  279. if ("" != valueAttr) {
  280. std::cout << "Expected \"\", got \"" << valueAttr << "\".\n";
  281. RETURN_FALSE("\"value\" attribute read failure");
  282. }
  283. if (">" != setAttr) {
  284. RETURN_FALSE("\"set\" attribute read failure");
  285. }
  286. return true;
  287. }
  288. bool testIndicator() {
  289. std::string xml = "<top><data name='messageContent' value=''/></top>";
  290. CodeDweller::XMLReaderData confData(xml.data(), xml.size());
  291. CodeDweller::XMLReaderElement reader("top");
  292. std::string nameAttr, valueAttr, setAttr;
  293. bool foundDataElem;
  294. bool foundNameAttr, foundValueAttr, foundSetAttr;
  295. reader
  296. .Element("data")
  297. .indicator(foundDataElem)
  298. .Attribute("name", nameAttr).indicator(foundNameAttr)
  299. .Attribute("value", valueAttr, ">").indicator(foundValueAttr)
  300. .Attribute("set", setAttr, ">").indicator(foundSetAttr)
  301. .End("data")
  302. .End("top");
  303. reader.initialize();
  304. if (0 == reader.interpret(confData)) {
  305. RETURN_FALSE("Error parsing XML: " + confData.Log.str());
  306. }
  307. if ("messageContent" != nameAttr) {
  308. RETURN_FALSE("\"name\" attribute read failure");
  309. }
  310. if ("" != valueAttr) {
  311. std::cout << "Expected \"\", got \"" << valueAttr << "\".\n";
  312. RETURN_FALSE("\"value\" attribute read failure");
  313. }
  314. if (">" != setAttr) {
  315. RETURN_FALSE("\"set\" attribute read failure");
  316. }
  317. if (!foundDataElem) {
  318. RETURN_FALSE("\"XMLReaderElement::indicator\" failure");
  319. }
  320. if (!foundNameAttr) {
  321. RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
  322. }
  323. if (!foundValueAttr) {
  324. RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
  325. }
  326. if (foundSetAttr) {
  327. RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
  328. }
  329. xml = "<top><data1 name='messageContent' value=''/></top>";
  330. CodeDweller::XMLReaderData confData1(xml.data(), xml.size());
  331. reader.initialize();
  332. if (0 == reader.interpret(confData1)) {
  333. RETURN_FALSE("Error parsing XML: " + confData.Log.str());
  334. }
  335. if (foundDataElem) {
  336. RETURN_FALSE("\"XMLReaderElement::indicator\" failure");
  337. }
  338. if (foundNameAttr) {
  339. RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
  340. }
  341. if (foundValueAttr) {
  342. RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
  343. }
  344. if (foundSetAttr) {
  345. RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
  346. }
  347. return true;
  348. }
  349. class TestFunctor : public CodeDweller::AttributeFunctor {
  350. public:
  351. int numCalled = 0;
  352. void operator()(CodeDweller::XMLReaderAttribute& A,
  353. CodeDweller::XMLReaderData& D) {
  354. numCalled++;
  355. }
  356. };
  357. bool testIndicatorFunctor() {
  358. std::string xml =
  359. "<top><data name='messageContent' value='' from='hello' "
  360. "to='xx'/></top>";
  361. CodeDweller::XMLReaderData confData(xml.data(), xml.size());
  362. CodeDweller::XMLReaderElement reader("top");
  363. std::string nameAttr, valueAttr, setAttr, fromAttr, toAttr;
  364. bool foundDataElem;
  365. bool foundNameAttr, foundValueAttr, foundSetAttr, foundFromAttr,
  366. foundToAttr;
  367. TestFunctor nameFunctor, fromToFunctor;
  368. reader
  369. .Element("data")
  370. .indicator(foundDataElem)
  371. .Attribute("name", nameAttr)
  372. .indicator(foundNameAttr, nameFunctor)
  373. .Attribute("value", valueAttr, ">")
  374. .indicator(foundValueAttr)
  375. .Attribute("from", fromAttr, ">")
  376. .indicator(foundFromAttr, fromToFunctor)
  377. .Attribute("to", toAttr, ">")
  378. .indicator(foundToAttr, fromToFunctor)
  379. .Attribute("set", setAttr, ">")
  380. .indicator(foundSetAttr, nameFunctor)
  381. .End("data")
  382. .End("top");
  383. reader.initialize();
  384. if (0 == reader.interpret(confData)) {
  385. RETURN_FALSE("Error parsing XML: " + confData.Log.str());
  386. }
  387. if ("messageContent" != nameAttr) {
  388. RETURN_FALSE("\"name\" attribute read failure");
  389. }
  390. if ("" != valueAttr) {
  391. std::cout << "Expected \"\", got \"" << valueAttr << "\".\n";
  392. RETURN_FALSE("\"value\" attribute read failure");
  393. }
  394. if ("hello" != fromAttr) {
  395. std::cout << "Expected \"hello\", got \"" << fromAttr << "\".\n";
  396. RETURN_FALSE("\"from\" attribute read failure");
  397. }
  398. if ("xx" != toAttr) {
  399. std::cout << "Expected \"xx\", got \"" << fromAttr << "\".\n";
  400. RETURN_FALSE("\"to\" attribute read failure");
  401. }
  402. if (">" != setAttr) {
  403. RETURN_FALSE("\"set\" attribute read failure");
  404. }
  405. if (!foundDataElem) {
  406. RETURN_FALSE("\"XMLReaderElement::indicator\" failure");
  407. }
  408. if (!foundNameAttr) {
  409. RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
  410. }
  411. if (!foundValueAttr) {
  412. RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
  413. }
  414. if (!foundFromAttr) {
  415. RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
  416. }
  417. if (!foundToAttr) {
  418. RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
  419. }
  420. if (foundSetAttr) {
  421. RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
  422. }
  423. if (1 != nameFunctor.numCalled) {
  424. RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
  425. }
  426. if (2 != fromToFunctor.numCalled) {
  427. RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
  428. }
  429. return true;
  430. }
  431. ////////////////////////////////////////////////////////////////////////////////
  432. // End of tests ////////////////////////////////////////////////////////////////
  433. ////////////////////////////////////////////////////////////////////////////////
  434. int main()
  435. {
  436. std::cout << "CodeDweller::XMLReader unit tests" << std::endl
  437. << std::endl;
  438. RUN_TEST(testEmptyElement);
  439. RUN_TEST(testRawData);
  440. RUN_TEST(testSimilarElementName);
  441. RUN_TEST(testWrongClosingTag);
  442. RUN_TEST(testSimilarAttributeName);
  443. RUN_TEST(testInvalidAttribute);
  444. RUN_TEST(testHugeAttributeValue);
  445. RUN_TEST(testNullAttributeValue);
  446. RUN_TEST(testIndicator);
  447. RUN_TEST(testIndicatorFunctor);
  448. SUMMARY;
  449. return 0;
  450. }