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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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 testSimilarElementName() {
  92. std::string stageContent, stage1Content, stage2Content;
  93. CodeDweller::XMLReaderElement reader("elem");
  94. reader
  95. .Element("stage", stageContent)
  96. .End("stage")
  97. .Element("stage1", stage1Content)
  98. .End("stage1")
  99. .Element("stage2", stage2Content)
  100. .End("stage2")
  101. .End("elem");
  102. std::string xml;
  103. std::string expectedStageContent = "StageContent";
  104. std::string expectedStage1Content = "Stage1Content";
  105. std::string expectedStage2Content = "Stage2Content";
  106. xml =
  107. "<elem>\n"
  108. " <stage2>" + expectedStage2Content + "</stage2>\n"
  109. " <stage>" + expectedStageContent + "</stage>\n"
  110. " <stage1>" + expectedStage1Content + "</stage1>\n"
  111. "</elem>";
  112. CodeDweller::XMLReaderData confData(xml.data(), xml.size());
  113. reader.initialize();
  114. if (0 == reader.interpret(confData)) {
  115. RETURN_FALSE("Error parsing XML: " + confData.Log.str());
  116. }
  117. if (expectedStageContent != stageContent) {
  118. RETURN_FALSE("<stage> content read failure");
  119. }
  120. if (expectedStage1Content != stage1Content) {
  121. RETURN_FALSE("<stage1> content read failure");
  122. }
  123. if (expectedStage2Content != stage2Content) {
  124. RETURN_FALSE("<stage2> content read failure");
  125. }
  126. return true;
  127. }
  128. bool testSimilarAttributeName() {
  129. std::string nameAttr, naAttr, nameLongAttr;
  130. CodeDweller::XMLReaderElement reader("elem");
  131. reader
  132. .Element("stage")
  133. .Attribute("name", nameAttr)
  134. .Attribute("na", naAttr)
  135. .Attribute("nameLong", nameLongAttr)
  136. .End("stage")
  137. .End("elem");
  138. std::string xml;
  139. std::string expectedNameAttr = "NameValue";
  140. std::string expectedNaAttr = "NaValue";
  141. std::string expectedNameLongAttr = "NameLongValue";
  142. xml =
  143. "<elem>\n"
  144. " <stage\n"
  145. " name='" + expectedNameAttr + "'\n"
  146. " na='" + expectedNaAttr + "'\n"
  147. " nameLong='" + expectedNameLongAttr + "'\n"
  148. "/></elem>";
  149. CodeDweller::XMLReaderData confData(xml.data(), xml.size());
  150. reader.initialize();
  151. if (0 == reader.interpret(confData)) {
  152. RETURN_FALSE("Error parsing XML: " + confData.Log.str());
  153. }
  154. if (expectedNameAttr != nameAttr) {
  155. RETURN_FALSE("\"name\" attribute read failure");
  156. }
  157. if (expectedNaAttr != naAttr) {
  158. RETURN_FALSE("\"na\" attribute read failure");
  159. }
  160. if (expectedNameLongAttr != nameLongAttr) {
  161. RETURN_FALSE("\"nameLong\" attribute read failure");
  162. }
  163. return true;
  164. }
  165. bool testNullAttributeValue() {
  166. std::string xml = "<data name='messageContent' value=''/>";
  167. CodeDweller::XMLReaderData confData(xml.data(), xml.size());
  168. CodeDweller::XMLReaderElement reader("data");
  169. std::string nameAttr, valueAttr, setAttr;
  170. reader
  171. .Attribute("name", nameAttr)
  172. .Attribute("value", valueAttr, ">")
  173. .Attribute("set", setAttr, ">")
  174. .End("data");
  175. reader.initialize();
  176. if (0 == reader.interpret(confData)) {
  177. RETURN_FALSE("Error parsing XML: " + confData.Log.str());
  178. }
  179. if ("messageContent" != nameAttr) {
  180. RETURN_FALSE("\"name\" attribute read failure");
  181. }
  182. if ("" != valueAttr) {
  183. std::cout << "Expected \"\", got \"" << valueAttr << "\".\n";
  184. RETURN_FALSE("\"value\" attribute read failure");
  185. }
  186. if (">" != setAttr) {
  187. RETURN_FALSE("\"set\" attribute read failure");
  188. }
  189. return true;
  190. }
  191. bool testIndicator() {
  192. std::string xml = "<top><data name='messageContent' value=''/></top>";
  193. CodeDweller::XMLReaderData confData(xml.data(), xml.size());
  194. CodeDweller::XMLReaderElement reader("top");
  195. std::string nameAttr, valueAttr, setAttr;
  196. bool foundDataElem;
  197. bool foundNameAttr, foundValueAttr, foundSetAttr;
  198. reader
  199. .Element("data")
  200. .indicator(foundDataElem)
  201. .Attribute("name", nameAttr).indicator(foundNameAttr)
  202. .Attribute("value", valueAttr, ">").indicator(foundValueAttr)
  203. .Attribute("set", setAttr, ">").indicator(foundSetAttr)
  204. .End("data")
  205. .End("top");
  206. reader.initialize();
  207. if (0 == reader.interpret(confData)) {
  208. RETURN_FALSE("Error parsing XML: " + confData.Log.str());
  209. }
  210. if ("messageContent" != nameAttr) {
  211. RETURN_FALSE("\"name\" attribute read failure");
  212. }
  213. if ("" != valueAttr) {
  214. std::cout << "Expected \"\", got \"" << valueAttr << "\".\n";
  215. RETURN_FALSE("\"value\" attribute read failure");
  216. }
  217. if (">" != setAttr) {
  218. RETURN_FALSE("\"set\" attribute read failure");
  219. }
  220. if (!foundDataElem) {
  221. RETURN_FALSE("\"XMLReaderElement::indicator\" failure");
  222. }
  223. if (!foundNameAttr) {
  224. RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
  225. }
  226. if (!foundValueAttr) {
  227. RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
  228. }
  229. if (foundSetAttr) {
  230. RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
  231. }
  232. xml = "<top><data1 name='messageContent' value=''/></top>";
  233. CodeDweller::XMLReaderData confData1(xml.data(), xml.size());
  234. reader.initialize();
  235. if (0 == reader.interpret(confData1)) {
  236. RETURN_FALSE("Error parsing XML: " + confData.Log.str());
  237. }
  238. if (foundDataElem) {
  239. RETURN_FALSE("\"XMLReaderElement::indicator\" failure");
  240. }
  241. if (foundNameAttr) {
  242. RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
  243. }
  244. if (foundValueAttr) {
  245. RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
  246. }
  247. if (foundSetAttr) {
  248. RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
  249. }
  250. return true;
  251. }
  252. class TestFunctor : public CodeDweller::AttributeFunctor {
  253. public:
  254. int numCalled = 0;
  255. void operator()(CodeDweller::XMLReaderAttribute& A,
  256. CodeDweller::XMLReaderData& D) {
  257. numCalled++;
  258. }
  259. };
  260. bool testIndicatorFunctor() {
  261. std::string xml =
  262. "<top><data name='messageContent' value='' from='hello' "
  263. "to='xx'/></top>";
  264. CodeDweller::XMLReaderData confData(xml.data(), xml.size());
  265. CodeDweller::XMLReaderElement reader("top");
  266. std::string nameAttr, valueAttr, setAttr, fromAttr, toAttr;
  267. bool foundDataElem;
  268. bool foundNameAttr, foundValueAttr, foundSetAttr, foundFromAttr,
  269. foundToAttr;
  270. TestFunctor nameFunctor, fromToFunctor;
  271. reader
  272. .Element("data")
  273. .indicator(foundDataElem)
  274. .Attribute("name", nameAttr)
  275. .indicator(foundNameAttr, nameFunctor)
  276. .Attribute("value", valueAttr, ">")
  277. .indicator(foundValueAttr)
  278. .Attribute("from", fromAttr, ">")
  279. .indicator(foundFromAttr, fromToFunctor)
  280. .Attribute("to", toAttr, ">")
  281. .indicator(foundToAttr, fromToFunctor)
  282. .Attribute("set", setAttr, ">")
  283. .indicator(foundSetAttr, nameFunctor)
  284. .End("data")
  285. .End("top");
  286. reader.initialize();
  287. if (0 == reader.interpret(confData)) {
  288. RETURN_FALSE("Error parsing XML: " + confData.Log.str());
  289. }
  290. if ("messageContent" != nameAttr) {
  291. RETURN_FALSE("\"name\" attribute read failure");
  292. }
  293. if ("" != valueAttr) {
  294. std::cout << "Expected \"\", got \"" << valueAttr << "\".\n";
  295. RETURN_FALSE("\"value\" attribute read failure");
  296. }
  297. if ("hello" != fromAttr) {
  298. std::cout << "Expected \"hello\", got \"" << fromAttr << "\".\n";
  299. RETURN_FALSE("\"from\" attribute read failure");
  300. }
  301. if ("xx" != toAttr) {
  302. std::cout << "Expected \"xx\", got \"" << fromAttr << "\".\n";
  303. RETURN_FALSE("\"to\" attribute read failure");
  304. }
  305. if (">" != setAttr) {
  306. RETURN_FALSE("\"set\" attribute read failure");
  307. }
  308. if (!foundDataElem) {
  309. RETURN_FALSE("\"XMLReaderElement::indicator\" failure");
  310. }
  311. if (!foundNameAttr) {
  312. RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
  313. }
  314. if (!foundValueAttr) {
  315. RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
  316. }
  317. if (!foundFromAttr) {
  318. RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
  319. }
  320. if (!foundToAttr) {
  321. RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
  322. }
  323. if (foundSetAttr) {
  324. RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
  325. }
  326. if (1 != nameFunctor.numCalled) {
  327. RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
  328. }
  329. if (2 != fromToFunctor.numCalled) {
  330. RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
  331. }
  332. return true;
  333. }
  334. ////////////////////////////////////////////////////////////////////////////////
  335. // End of tests ////////////////////////////////////////////////////////////////
  336. ////////////////////////////////////////////////////////////////////////////////
  337. int main()
  338. {
  339. std::cout << "CodeDweller::XMLReader unit tests" << std::endl
  340. << std::endl;
  341. RUN_TEST(testEmptyElement);
  342. RUN_TEST(testSimilarElementName);
  343. RUN_TEST(testSimilarAttributeName);
  344. RUN_TEST(testNullAttributeValue);
  345. RUN_TEST(testIndicator);
  346. RUN_TEST(testIndicatorFunctor);
  347. SUMMARY;
  348. return 0;
  349. }