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

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