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 12KB

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