Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

testXMLReader.cpp 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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 testHugeAttributeValue() {
  166. std::string xml = "<data name='messageContent' value='";
  167. std::string expectedValue;
  168. size_t nSize = 1000 * 1000 * 10;
  169. expectedValue.reserve(nSize);
  170. expectedValue.assign(nSize, 'x');
  171. xml.reserve(nSize + 512);
  172. xml.append(expectedValue);
  173. xml.append("'/>");
  174. CodeDweller::XMLReaderData confData(xml.data(), xml.size());
  175. CodeDweller::XMLReaderElement reader("data");
  176. std::string nameAttr, valueAttr;
  177. reader
  178. .Attribute("name", nameAttr)
  179. .Attribute("value", valueAttr, ">")
  180. .End("data");
  181. reader.initialize();
  182. if (0 == reader.interpret(confData)) {
  183. RETURN_FALSE("Error parsing XML: " + confData.Log.str());
  184. }
  185. if ("messageContent" != nameAttr) {
  186. RETURN_FALSE("\"name\" attribute read failure");
  187. }
  188. if (expectedValue != valueAttr) {
  189. RETURN_FALSE("\"value\" attribute read failure");
  190. }
  191. return true;
  192. }
  193. bool testNullAttributeValue() {
  194. std::string xml = "<data name='messageContent' value=''/>";
  195. CodeDweller::XMLReaderData confData(xml.data(), xml.size());
  196. CodeDweller::XMLReaderElement reader("data");
  197. std::string nameAttr, valueAttr, setAttr;
  198. reader
  199. .Attribute("name", nameAttr)
  200. .Attribute("value", valueAttr, ">")
  201. .Attribute("set", setAttr, ">")
  202. .End("data");
  203. reader.initialize();
  204. if (0 == reader.interpret(confData)) {
  205. RETURN_FALSE("Error parsing XML: " + confData.Log.str());
  206. }
  207. if ("messageContent" != nameAttr) {
  208. RETURN_FALSE("\"name\" attribute read failure");
  209. }
  210. if ("" != valueAttr) {
  211. std::cout << "Expected \"\", got \"" << valueAttr << "\".\n";
  212. RETURN_FALSE("\"value\" attribute read failure");
  213. }
  214. if (">" != setAttr) {
  215. RETURN_FALSE("\"set\" attribute read failure");
  216. }
  217. return true;
  218. }
  219. bool testIndicator() {
  220. std::string xml = "<top><data name='messageContent' value=''/></top>";
  221. CodeDweller::XMLReaderData confData(xml.data(), xml.size());
  222. CodeDweller::XMLReaderElement reader("top");
  223. std::string nameAttr, valueAttr, setAttr;
  224. bool foundDataElem;
  225. bool foundNameAttr, foundValueAttr, foundSetAttr;
  226. reader
  227. .Element("data")
  228. .indicator(foundDataElem)
  229. .Attribute("name", nameAttr).indicator(foundNameAttr)
  230. .Attribute("value", valueAttr, ">").indicator(foundValueAttr)
  231. .Attribute("set", setAttr, ">").indicator(foundSetAttr)
  232. .End("data")
  233. .End("top");
  234. reader.initialize();
  235. if (0 == reader.interpret(confData)) {
  236. RETURN_FALSE("Error parsing XML: " + confData.Log.str());
  237. }
  238. if ("messageContent" != nameAttr) {
  239. RETURN_FALSE("\"name\" attribute read failure");
  240. }
  241. if ("" != valueAttr) {
  242. std::cout << "Expected \"\", got \"" << valueAttr << "\".\n";
  243. RETURN_FALSE("\"value\" attribute read failure");
  244. }
  245. if (">" != setAttr) {
  246. RETURN_FALSE("\"set\" attribute read failure");
  247. }
  248. if (!foundDataElem) {
  249. RETURN_FALSE("\"XMLReaderElement::indicator\" failure");
  250. }
  251. if (!foundNameAttr) {
  252. RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
  253. }
  254. if (!foundValueAttr) {
  255. RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
  256. }
  257. if (foundSetAttr) {
  258. RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
  259. }
  260. xml = "<top><data1 name='messageContent' value=''/></top>";
  261. CodeDweller::XMLReaderData confData1(xml.data(), xml.size());
  262. reader.initialize();
  263. if (0 == reader.interpret(confData1)) {
  264. RETURN_FALSE("Error parsing XML: " + confData.Log.str());
  265. }
  266. if (foundDataElem) {
  267. RETURN_FALSE("\"XMLReaderElement::indicator\" failure");
  268. }
  269. if (foundNameAttr) {
  270. RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
  271. }
  272. if (foundValueAttr) {
  273. RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
  274. }
  275. if (foundSetAttr) {
  276. RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
  277. }
  278. return true;
  279. }
  280. class TestFunctor : public CodeDweller::AttributeFunctor {
  281. public:
  282. int numCalled = 0;
  283. void operator()(CodeDweller::XMLReaderAttribute& A,
  284. CodeDweller::XMLReaderData& D) {
  285. numCalled++;
  286. }
  287. };
  288. bool testIndicatorFunctor() {
  289. std::string xml =
  290. "<top><data name='messageContent' value='' from='hello' "
  291. "to='xx'/></top>";
  292. CodeDweller::XMLReaderData confData(xml.data(), xml.size());
  293. CodeDweller::XMLReaderElement reader("top");
  294. std::string nameAttr, valueAttr, setAttr, fromAttr, toAttr;
  295. bool foundDataElem;
  296. bool foundNameAttr, foundValueAttr, foundSetAttr, foundFromAttr,
  297. foundToAttr;
  298. TestFunctor nameFunctor, fromToFunctor;
  299. reader
  300. .Element("data")
  301. .indicator(foundDataElem)
  302. .Attribute("name", nameAttr)
  303. .indicator(foundNameAttr, nameFunctor)
  304. .Attribute("value", valueAttr, ">")
  305. .indicator(foundValueAttr)
  306. .Attribute("from", fromAttr, ">")
  307. .indicator(foundFromAttr, fromToFunctor)
  308. .Attribute("to", toAttr, ">")
  309. .indicator(foundToAttr, fromToFunctor)
  310. .Attribute("set", setAttr, ">")
  311. .indicator(foundSetAttr, nameFunctor)
  312. .End("data")
  313. .End("top");
  314. reader.initialize();
  315. if (0 == reader.interpret(confData)) {
  316. RETURN_FALSE("Error parsing XML: " + confData.Log.str());
  317. }
  318. if ("messageContent" != nameAttr) {
  319. RETURN_FALSE("\"name\" attribute read failure");
  320. }
  321. if ("" != valueAttr) {
  322. std::cout << "Expected \"\", got \"" << valueAttr << "\".\n";
  323. RETURN_FALSE("\"value\" attribute read failure");
  324. }
  325. if ("hello" != fromAttr) {
  326. std::cout << "Expected \"hello\", got \"" << fromAttr << "\".\n";
  327. RETURN_FALSE("\"from\" attribute read failure");
  328. }
  329. if ("xx" != toAttr) {
  330. std::cout << "Expected \"xx\", got \"" << fromAttr << "\".\n";
  331. RETURN_FALSE("\"to\" attribute read failure");
  332. }
  333. if (">" != setAttr) {
  334. RETURN_FALSE("\"set\" attribute read failure");
  335. }
  336. if (!foundDataElem) {
  337. RETURN_FALSE("\"XMLReaderElement::indicator\" failure");
  338. }
  339. if (!foundNameAttr) {
  340. RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
  341. }
  342. if (!foundValueAttr) {
  343. RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
  344. }
  345. if (!foundFromAttr) {
  346. RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
  347. }
  348. if (!foundToAttr) {
  349. RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
  350. }
  351. if (foundSetAttr) {
  352. RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
  353. }
  354. if (1 != nameFunctor.numCalled) {
  355. RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
  356. }
  357. if (2 != fromToFunctor.numCalled) {
  358. RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
  359. }
  360. return true;
  361. }
  362. ////////////////////////////////////////////////////////////////////////////////
  363. // End of tests ////////////////////////////////////////////////////////////////
  364. ////////////////////////////////////////////////////////////////////////////////
  365. int main()
  366. {
  367. std::cout << "CodeDweller::XMLReader unit tests" << std::endl
  368. << std::endl;
  369. RUN_TEST(testEmptyElement);
  370. RUN_TEST(testSimilarElementName);
  371. RUN_TEST(testSimilarAttributeName);
  372. RUN_TEST(testHugeAttributeValue);
  373. RUN_TEST(testNullAttributeValue);
  374. RUN_TEST(testIndicator);
  375. RUN_TEST(testIndicatorFunctor);
  376. SUMMARY;
  377. return 0;
  378. }