You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

testXMLReader.cpp 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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. RETURN_FALSE("RawData() failure for root element");
  110. }
  111. if ("<stage>Content</stage>" != stageXml) {
  112. RETURN_FALSE("RawData() failure for child element");
  113. }
  114. if ("Content" != stageContent) {
  115. RETURN_FALSE("content failure for child element");
  116. }
  117. return true;
  118. }
  119. bool testSimilarElementName() {
  120. std::string stageContent, stage1Content, stage2Content;
  121. CodeDweller::XMLReaderElement reader("elem");
  122. reader
  123. .Element("stage", stageContent)
  124. .End("stage")
  125. .Element("stage1", stage1Content)
  126. .End("stage1")
  127. .Element("stage2", stage2Content)
  128. .End("stage2")
  129. .End("elem");
  130. std::string xml;
  131. std::string expectedStageContent = "StageContent";
  132. std::string expectedStage1Content = "Stage1Content";
  133. std::string expectedStage2Content = "Stage2Content";
  134. xml =
  135. "<elem>\n"
  136. " <stage2>" + expectedStage2Content + "</stage2>\n"
  137. " <stage>" + expectedStageContent + "</stage>\n"
  138. " <stage1>" + expectedStage1Content + "</stage1>\n"
  139. "</elem>";
  140. CodeDweller::XMLReaderData confData(xml.data(), xml.size());
  141. reader.initialize();
  142. if (0 == reader.interpret(confData)) {
  143. RETURN_FALSE("Error parsing XML: " + confData.Log.str());
  144. }
  145. if (expectedStageContent != stageContent) {
  146. RETURN_FALSE("<stage> content read failure");
  147. }
  148. if (expectedStage1Content != stage1Content) {
  149. RETURN_FALSE("<stage1> content read failure");
  150. }
  151. if (expectedStage2Content != stage2Content) {
  152. RETURN_FALSE("<stage2> content read failure");
  153. }
  154. return true;
  155. }
  156. bool testSimilarAttributeName() {
  157. std::string nameAttr, naAttr, nameLongAttr;
  158. CodeDweller::XMLReaderElement reader("elem");
  159. reader
  160. .Element("stage")
  161. .Attribute("name", nameAttr)
  162. .Attribute("na", naAttr)
  163. .Attribute("nameLong", nameLongAttr)
  164. .End("stage")
  165. .End("elem");
  166. std::string xml;
  167. std::string expectedNameAttr = "NameValue";
  168. std::string expectedNaAttr = "NaValue";
  169. std::string expectedNameLongAttr = "NameLongValue";
  170. xml =
  171. "<elem>\n"
  172. " <stage\n"
  173. " name='" + expectedNameAttr + "'\n"
  174. " na='" + expectedNaAttr + "'\n"
  175. " nameLong='" + expectedNameLongAttr + "'\n"
  176. "/></elem>";
  177. CodeDweller::XMLReaderData confData(xml.data(), xml.size());
  178. reader.initialize();
  179. if (0 == reader.interpret(confData)) {
  180. RETURN_FALSE("Error parsing XML: " + confData.Log.str());
  181. }
  182. if (expectedNameAttr != nameAttr) {
  183. RETURN_FALSE("\"name\" attribute read failure");
  184. }
  185. if (expectedNaAttr != naAttr) {
  186. RETURN_FALSE("\"na\" attribute read failure");
  187. }
  188. if (expectedNameLongAttr != nameLongAttr) {
  189. RETURN_FALSE("\"nameLong\" attribute read failure");
  190. }
  191. return true;
  192. }
  193. bool testHugeAttributeValue() {
  194. std::string xml = "<data name='messageContent' value='";
  195. std::string expectedValue;
  196. size_t nSize = 1000 * 1000 * 10;
  197. expectedValue.reserve(nSize);
  198. expectedValue.assign(nSize, 'x');
  199. xml.reserve(nSize + 512);
  200. xml.append(expectedValue);
  201. xml.append("'/>");
  202. CodeDweller::XMLReaderData confData(xml.data(), xml.size());
  203. CodeDweller::XMLReaderElement reader("data");
  204. std::string nameAttr, valueAttr;
  205. reader
  206. .Attribute("name", nameAttr)
  207. .Attribute("value", valueAttr, ">")
  208. .End("data");
  209. reader.initialize();
  210. if (0 == reader.interpret(confData)) {
  211. RETURN_FALSE("Error parsing XML: " + confData.Log.str());
  212. }
  213. if ("messageContent" != nameAttr) {
  214. RETURN_FALSE("\"name\" attribute read failure");
  215. }
  216. if (expectedValue != valueAttr) {
  217. RETURN_FALSE("\"value\" attribute read failure");
  218. }
  219. return true;
  220. }
  221. bool testNullAttributeValue() {
  222. std::string xml = "<data name='messageContent' value=''/>";
  223. CodeDweller::XMLReaderData confData(xml.data(), xml.size());
  224. CodeDweller::XMLReaderElement reader("data");
  225. std::string nameAttr, valueAttr, setAttr;
  226. reader
  227. .Attribute("name", nameAttr)
  228. .Attribute("value", valueAttr, ">")
  229. .Attribute("set", setAttr, ">")
  230. .End("data");
  231. reader.initialize();
  232. if (0 == reader.interpret(confData)) {
  233. RETURN_FALSE("Error parsing XML: " + confData.Log.str());
  234. }
  235. if ("messageContent" != nameAttr) {
  236. RETURN_FALSE("\"name\" attribute read failure");
  237. }
  238. if ("" != valueAttr) {
  239. std::cout << "Expected \"\", got \"" << valueAttr << "\".\n";
  240. RETURN_FALSE("\"value\" attribute read failure");
  241. }
  242. if (">" != setAttr) {
  243. RETURN_FALSE("\"set\" attribute read failure");
  244. }
  245. return true;
  246. }
  247. bool testIndicator() {
  248. std::string xml = "<top><data name='messageContent' value=''/></top>";
  249. CodeDweller::XMLReaderData confData(xml.data(), xml.size());
  250. CodeDweller::XMLReaderElement reader("top");
  251. std::string nameAttr, valueAttr, setAttr;
  252. bool foundDataElem;
  253. bool foundNameAttr, foundValueAttr, foundSetAttr;
  254. reader
  255. .Element("data")
  256. .indicator(foundDataElem)
  257. .Attribute("name", nameAttr).indicator(foundNameAttr)
  258. .Attribute("value", valueAttr, ">").indicator(foundValueAttr)
  259. .Attribute("set", setAttr, ">").indicator(foundSetAttr)
  260. .End("data")
  261. .End("top");
  262. reader.initialize();
  263. if (0 == reader.interpret(confData)) {
  264. RETURN_FALSE("Error parsing XML: " + confData.Log.str());
  265. }
  266. if ("messageContent" != nameAttr) {
  267. RETURN_FALSE("\"name\" attribute read failure");
  268. }
  269. if ("" != valueAttr) {
  270. std::cout << "Expected \"\", got \"" << valueAttr << "\".\n";
  271. RETURN_FALSE("\"value\" attribute read failure");
  272. }
  273. if (">" != setAttr) {
  274. RETURN_FALSE("\"set\" attribute read failure");
  275. }
  276. if (!foundDataElem) {
  277. RETURN_FALSE("\"XMLReaderElement::indicator\" failure");
  278. }
  279. if (!foundNameAttr) {
  280. RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
  281. }
  282. if (!foundValueAttr) {
  283. RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
  284. }
  285. if (foundSetAttr) {
  286. RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
  287. }
  288. xml = "<top><data1 name='messageContent' value=''/></top>";
  289. CodeDweller::XMLReaderData confData1(xml.data(), xml.size());
  290. reader.initialize();
  291. if (0 == reader.interpret(confData1)) {
  292. RETURN_FALSE("Error parsing XML: " + confData.Log.str());
  293. }
  294. if (foundDataElem) {
  295. RETURN_FALSE("\"XMLReaderElement::indicator\" failure");
  296. }
  297. if (foundNameAttr) {
  298. RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
  299. }
  300. if (foundValueAttr) {
  301. RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
  302. }
  303. if (foundSetAttr) {
  304. RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
  305. }
  306. return true;
  307. }
  308. class TestFunctor : public CodeDweller::AttributeFunctor {
  309. public:
  310. int numCalled = 0;
  311. void operator()(CodeDweller::XMLReaderAttribute& A,
  312. CodeDweller::XMLReaderData& D) {
  313. numCalled++;
  314. }
  315. };
  316. bool testIndicatorFunctor() {
  317. std::string xml =
  318. "<top><data name='messageContent' value='' from='hello' "
  319. "to='xx'/></top>";
  320. CodeDweller::XMLReaderData confData(xml.data(), xml.size());
  321. CodeDweller::XMLReaderElement reader("top");
  322. std::string nameAttr, valueAttr, setAttr, fromAttr, toAttr;
  323. bool foundDataElem;
  324. bool foundNameAttr, foundValueAttr, foundSetAttr, foundFromAttr,
  325. foundToAttr;
  326. TestFunctor nameFunctor, fromToFunctor;
  327. reader
  328. .Element("data")
  329. .indicator(foundDataElem)
  330. .Attribute("name", nameAttr)
  331. .indicator(foundNameAttr, nameFunctor)
  332. .Attribute("value", valueAttr, ">")
  333. .indicator(foundValueAttr)
  334. .Attribute("from", fromAttr, ">")
  335. .indicator(foundFromAttr, fromToFunctor)
  336. .Attribute("to", toAttr, ">")
  337. .indicator(foundToAttr, fromToFunctor)
  338. .Attribute("set", setAttr, ">")
  339. .indicator(foundSetAttr, nameFunctor)
  340. .End("data")
  341. .End("top");
  342. reader.initialize();
  343. if (0 == reader.interpret(confData)) {
  344. RETURN_FALSE("Error parsing XML: " + confData.Log.str());
  345. }
  346. if ("messageContent" != nameAttr) {
  347. RETURN_FALSE("\"name\" attribute read failure");
  348. }
  349. if ("" != valueAttr) {
  350. std::cout << "Expected \"\", got \"" << valueAttr << "\".\n";
  351. RETURN_FALSE("\"value\" attribute read failure");
  352. }
  353. if ("hello" != fromAttr) {
  354. std::cout << "Expected \"hello\", got \"" << fromAttr << "\".\n";
  355. RETURN_FALSE("\"from\" attribute read failure");
  356. }
  357. if ("xx" != toAttr) {
  358. std::cout << "Expected \"xx\", got \"" << fromAttr << "\".\n";
  359. RETURN_FALSE("\"to\" attribute read failure");
  360. }
  361. if (">" != setAttr) {
  362. RETURN_FALSE("\"set\" attribute read failure");
  363. }
  364. if (!foundDataElem) {
  365. RETURN_FALSE("\"XMLReaderElement::indicator\" failure");
  366. }
  367. if (!foundNameAttr) {
  368. RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
  369. }
  370. if (!foundValueAttr) {
  371. RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
  372. }
  373. if (!foundFromAttr) {
  374. RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
  375. }
  376. if (!foundToAttr) {
  377. RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
  378. }
  379. if (foundSetAttr) {
  380. RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
  381. }
  382. if (1 != nameFunctor.numCalled) {
  383. RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
  384. }
  385. if (2 != fromToFunctor.numCalled) {
  386. RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
  387. }
  388. return true;
  389. }
  390. ////////////////////////////////////////////////////////////////////////////////
  391. // End of tests ////////////////////////////////////////////////////////////////
  392. ////////////////////////////////////////////////////////////////////////////////
  393. int main()
  394. {
  395. std::cout << "CodeDweller::XMLReader unit tests" << std::endl
  396. << std::endl;
  397. RUN_TEST(testEmptyElement);
  398. RUN_TEST(testRawData);
  399. RUN_TEST(testSimilarElementName);
  400. RUN_TEST(testSimilarAttributeName);
  401. RUN_TEST(testHugeAttributeValue);
  402. RUN_TEST(testNullAttributeValue);
  403. RUN_TEST(testIndicator);
  404. RUN_TEST(testIndicatorFunctor);
  405. SUMMARY;
  406. return 0;
  407. }