driver.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. // file : tests/parser/driver.cxx
  2. // copyright : Copyright (c) 2013-2014 Code Synthesis Tools CC
  3. // license : MIT; see accompanying LICENSE file
  4. #ifdef NDEBUG
  5. # error tests require enabled assert(); un-define the NDEBUG macro
  6. #endif
  7. #include <string>
  8. #include <vector>
  9. #include <cassert>
  10. #include <iostream>
  11. #include <sstream>
  12. #include <xml/parser>
  13. using namespace std;
  14. using namespace xml;
  15. int
  16. main ()
  17. {
  18. // Test error handling.
  19. //
  20. try
  21. {
  22. istringstream is ("<root><nested>X</nasted></root>");
  23. parser p (is, "test");
  24. assert (p.next () == parser::start_element);
  25. assert (p.next () == parser::start_element);
  26. assert (p.next () == parser::characters && p.value () == "X");
  27. p.next ();
  28. assert (false);
  29. }
  30. catch (const xml::exception&)
  31. {
  32. // cerr << e.what () << endl;
  33. }
  34. try
  35. {
  36. istringstream is ("<root/>");
  37. is.exceptions (ios_base::badbit | ios_base::failbit);
  38. parser p (is, "test");
  39. is.setstate (ios_base::badbit);
  40. p.next ();
  41. assert (false);
  42. }
  43. catch (const ios_base::failure&)
  44. {
  45. }
  46. // Test the next_expect() functionality.
  47. //
  48. {
  49. istringstream is ("<root/>");
  50. parser p (is, "test");
  51. p.next_expect (parser::start_element, "root");
  52. p.next_expect (parser::end_element);
  53. }
  54. try
  55. {
  56. istringstream is ("<root/>");
  57. parser p (is, "test");
  58. p.next_expect (parser::end_element);
  59. assert (false);
  60. }
  61. catch (const xml::exception&)
  62. {
  63. // cerr << e.what () << endl;
  64. }
  65. try
  66. {
  67. istringstream is ("<root/>");
  68. parser p (is, "test");
  69. p.next_expect (parser::start_element, "root1");
  70. assert (false);
  71. }
  72. catch (const xml::exception&)
  73. {
  74. // cerr << e.what () << endl;
  75. }
  76. // Test next_expect() with content setting.
  77. //
  78. {
  79. istringstream is ("<root> </root>");
  80. parser p (is, "empty");
  81. p.next_expect (parser::start_element, "root", content::empty);
  82. p.next_expect (parser::end_element);
  83. p.next_expect (parser::eof);
  84. }
  85. // Test namespace declarations.
  86. //
  87. {
  88. // Followup end element event that should be precedeeded by end
  89. // namespace declaration.
  90. //
  91. istringstream is ("<root xmlns:a='a'/>");
  92. parser p (is,
  93. "test",
  94. parser::receive_default |
  95. parser::receive_namespace_decls);
  96. p.next_expect (parser::start_element, "root");
  97. p.next_expect (parser::start_namespace_decl);
  98. p.next_expect (parser::end_namespace_decl);
  99. p.next_expect (parser::end_element);
  100. }
  101. // Test value extraction.
  102. //
  103. {
  104. istringstream is ("<root>123</root>");
  105. parser p (is, "test");
  106. p.next_expect (parser::start_element, "root");
  107. p.next_expect (parser::characters);
  108. assert (p.value<int> () == 123);
  109. p.next_expect (parser::end_element);
  110. }
  111. // Test attribute maps.
  112. //
  113. {
  114. istringstream is ("<root a='a' b='b' d='123' t='true'/>");
  115. parser p (is, "test");
  116. p.next_expect (parser::start_element, "root");
  117. assert (p.attribute ("a") == "a");
  118. assert (p.attribute ("b", "B") == "b");
  119. assert (p.attribute ("c", "C") == "C");
  120. assert (p.attribute<int> ("d") == 123);
  121. assert (p.attribute<bool> ("t") == true);
  122. assert (p.attribute ("f", false) == false);
  123. p.next_expect (parser::end_element);
  124. }
  125. {
  126. istringstream is ("<root a='a'><nested a='A'><inner/></nested></root>");
  127. parser p (is, "test");
  128. p.next_expect (parser::start_element, "root");
  129. assert (p.attribute ("a") == "a");
  130. assert (p.peek () == parser::start_element && p.name () == "nested");
  131. assert (p.attribute ("a") == "a");
  132. p.next_expect (parser::start_element, "nested");
  133. assert (p.attribute ("a") == "A");
  134. p.next_expect (parser::start_element, "inner");
  135. assert (p.attribute ("a", "") == "");
  136. p.next_expect (parser::end_element);
  137. assert (p.attribute ("a") == "A");
  138. assert (p.peek () == parser::end_element);
  139. assert (p.attribute ("a") == "A"); // Still valid.
  140. p.next_expect (parser::end_element);
  141. assert (p.attribute ("a") == "a");
  142. p.next_expect (parser::end_element);
  143. assert (p.attribute ("a", "") == "");
  144. }
  145. try
  146. {
  147. istringstream is ("<root a='a' b='b'/>");
  148. parser p (is, "test");
  149. p.next_expect (parser::start_element, "root");
  150. assert (p.attribute ("a") == "a");
  151. p.next_expect (parser::end_element);
  152. assert (false);
  153. }
  154. catch (const xml::exception&)
  155. {
  156. // cerr << e.what () << endl;
  157. }
  158. try
  159. {
  160. istringstream is ("<root a='abc'/>");
  161. parser p (is, "test");
  162. p.next_expect (parser::start_element, "root");
  163. p.attribute<int> ("a");
  164. assert (false);
  165. }
  166. catch (const xml::exception&)
  167. {
  168. // cerr << e.what () << endl;
  169. }
  170. // Test peeking and getting the current event.
  171. //
  172. {
  173. istringstream is ("<root x='x'>x<nested/></root>");
  174. parser p (is, "peek",
  175. parser::receive_default | parser::receive_attributes_event);
  176. assert (p.event () == parser::eof);
  177. assert (p.peek () == parser::start_element);
  178. assert (p.next () == parser::start_element);
  179. assert (p.event () == parser::start_element);
  180. assert (p.peek () == parser::start_attribute);
  181. assert (p.event () == parser::start_attribute);
  182. assert (p.next () == parser::start_attribute);
  183. assert (p.peek () == parser::characters && p.value () == "x");
  184. assert (p.next () == parser::characters && p.value () == "x");
  185. assert (p.event () == parser::characters && p.value () == "x");
  186. assert (p.peek () == parser::end_attribute);
  187. assert (p.event () == parser::end_attribute);
  188. assert (p.next () == parser::end_attribute);
  189. assert (p.peek () == parser::characters && p.value () == "x");
  190. assert (p.next () == parser::characters && p.value () == "x");
  191. assert (p.event () == parser::characters && p.value () == "x");
  192. assert (p.peek () == parser::start_element);
  193. assert (p.next () == parser::start_element);
  194. assert (p.event () == parser::start_element);
  195. assert (p.peek () == parser::end_element);
  196. assert (p.next () == parser::end_element);
  197. assert (p.event () == parser::end_element);
  198. assert (p.peek () == parser::end_element);
  199. assert (p.next () == parser::end_element);
  200. assert (p.event () == parser::end_element);
  201. assert (p.peek () == parser::eof);
  202. assert (p.next () == parser::eof);
  203. assert (p.event () == parser::eof);
  204. }
  205. // Test content processing.
  206. //
  207. // empty
  208. //
  209. {
  210. istringstream is ("<root x=' x '> \n\t </root>");
  211. parser p (is, "empty",
  212. parser::receive_default | parser::receive_attributes_event);
  213. assert (p.next () == parser::start_element);
  214. p.content (content::empty);
  215. assert (p.next () == parser::start_attribute);
  216. assert (p.next () == parser::characters && p.value () == " x ");
  217. assert (p.next () == parser::end_attribute);
  218. assert (p.next () == parser::end_element);
  219. assert (p.next () == parser::eof);
  220. }
  221. try
  222. {
  223. istringstream is ("<root> \n &amp; X \t </root>");
  224. parser p (is, "empty");
  225. assert (p.next () == parser::start_element);
  226. p.content (content::empty);
  227. p.next ();
  228. assert (false);
  229. }
  230. catch (const xml::exception&)
  231. {
  232. // cerr << e.what () << endl;
  233. }
  234. // simple
  235. //
  236. {
  237. istringstream is ("<root> X </root>");
  238. parser p (is, "simple");
  239. assert (p.next () == parser::start_element);
  240. p.content (content::simple);
  241. assert (p.next () == parser::characters && p.value () == " X ");
  242. assert (p.next () == parser::end_element);
  243. assert (p.next () == parser::eof);
  244. }
  245. try
  246. {
  247. istringstream is ("<root> ? <nested/></root>");
  248. parser p (is, "simple");
  249. assert (p.next () == parser::start_element);
  250. p.content (content::simple);
  251. assert (p.next () == parser::characters && p.value () == " ? ");
  252. p.next ();
  253. assert (false);
  254. }
  255. catch (const xml::exception&)
  256. {
  257. // cerr << e.what () << endl;
  258. }
  259. {
  260. // Test content accumulation in simple content.
  261. //
  262. istringstream is ("<root xmlns:a='a'>1&#x32;3</root>");
  263. parser p (is,
  264. "simple",
  265. parser::receive_default |
  266. parser::receive_namespace_decls);
  267. assert (p.next () == parser::start_element);
  268. p.next_expect (parser::start_namespace_decl);
  269. p.content (content::simple);
  270. assert (p.next () == parser::characters && p.value () == "123");
  271. p.next_expect (parser::end_namespace_decl);
  272. assert (p.next () == parser::end_element);
  273. assert (p.next () == parser::eof);
  274. }
  275. try
  276. {
  277. // Test error handling in accumulation in simple content.
  278. //
  279. istringstream is ("<root xmlns:a='a'>1&#x32;<nested/>3</root>");
  280. parser p (is,
  281. "simple",
  282. parser::receive_default |
  283. parser::receive_namespace_decls);
  284. assert (p.next () == parser::start_element);
  285. p.next_expect (parser::start_namespace_decl);
  286. p.content (content::simple);
  287. p.next ();
  288. assert (false);
  289. }
  290. catch (const xml::exception&)
  291. {
  292. // cerr << e.what () << endl;
  293. }
  294. // complex
  295. //
  296. {
  297. istringstream is ("<root x=' x '>\n"
  298. " <nested>\n"
  299. " <inner/>\n"
  300. " <inner> X </inner>\n"
  301. " </nested>\n"
  302. "</root>\n");
  303. parser p (is, "complex",
  304. parser::receive_default | parser::receive_attributes_event);
  305. assert (p.next () == parser::start_element); // root
  306. p.content (content::complex);
  307. assert (p.next () == parser::start_attribute);
  308. assert (p.next () == parser::characters && p.value () == " x ");
  309. assert (p.next () == parser::end_attribute);
  310. assert (p.next () == parser::start_element); // nested
  311. p.content (content::complex);
  312. assert (p.next () == parser::start_element); // inner
  313. p.content (content::empty);
  314. assert (p.next () == parser::end_element); // inner
  315. assert (p.next () == parser::start_element); // inner
  316. p.content (content::simple);
  317. assert (p.next () == parser::characters && p.value () == " X ");
  318. assert (p.next () == parser::end_element); // inner
  319. assert (p.next () == parser::end_element); // nested
  320. assert (p.next () == parser::end_element); // root
  321. assert (p.next () == parser::eof);
  322. }
  323. try
  324. {
  325. istringstream is ("<root> \n<n/> X <n> X </n> </root>");
  326. parser p (is, "complex");
  327. assert (p.next () == parser::start_element);
  328. p.content (content::complex);
  329. assert (p.next () == parser::start_element);
  330. assert (p.next () == parser::end_element);
  331. p.next ();
  332. assert (false);
  333. }
  334. catch (const xml::exception&)
  335. {
  336. // cerr << e.what () << endl;
  337. }
  338. // Test element with simple content helpers.
  339. //
  340. {
  341. istringstream is ("<root>"
  342. " <nested>X</nested>"
  343. " <nested/>"
  344. " <nested>123</nested>"
  345. " <nested>Y</nested>"
  346. " <t:nested xmlns:t='test'>Z</t:nested>"
  347. " <nested>234</nested>"
  348. " <t:nested xmlns:t='test'>345</t:nested>"
  349. " <nested>A</nested>"
  350. " <t:nested xmlns:t='test'>B</t:nested>"
  351. " <nested1>A</nested1>"
  352. " <t:nested1 xmlns:t='test'>B</t:nested1>"
  353. " <nested>1</nested>"
  354. " <t:nested xmlns:t='test'>2</t:nested>"
  355. " <nested1>1</nested1>"
  356. " <t:nested1 xmlns:t='test'>2</t:nested1>"
  357. "</root>");
  358. parser p (is, "element");
  359. p.next_expect (parser::start_element, "root", content::complex);
  360. p.next_expect (parser::start_element, "nested");
  361. assert (p.element () == "X");
  362. p.next_expect (parser::start_element, "nested");
  363. assert (p.element () == "");
  364. p.next_expect (parser::start_element, "nested");
  365. assert (p.element<unsigned int> () == 123);
  366. assert (p.element ("nested") == "Y");
  367. assert (p.element (qname ("test", "nested")) == "Z");
  368. assert (p.element<unsigned int> ("nested") == 234);
  369. assert (p.element<unsigned int> (qname ("test", "nested")) == 345);
  370. assert (p.element ("nested", "a") == "A");
  371. assert (p.element (qname ("test", "nested"), "b") == "B");
  372. assert (p.element ("nested", "a") == "a" &&
  373. p.element ("nested1") == "A");
  374. assert (p.element (qname ("test", "nested"), "b") == "b" &&
  375. p.element (qname ("test", "nested1")) == "B");
  376. assert (p.element<unsigned int> ("nested", 10) == 1);
  377. assert (p.element<unsigned int> (qname ("test", "nested"), 20) == 2);
  378. assert (p.element<unsigned int> ("nested", 10) == 10 &&
  379. p.element<unsigned int> ("nested1") == 1);
  380. assert (p.element<unsigned int> (qname ("test", "nested"), 20) == 20 &&
  381. p.element<unsigned int> (qname ("test", "nested1")) == 2);
  382. p.next_expect (parser::end_element);
  383. }
  384. // Test the iterator interface.
  385. //
  386. {
  387. istringstream is ("<root><nested>X</nested></root>");
  388. parser p (is, "iterator");
  389. vector<parser::event_type> v;
  390. for (parser::iterator i (p.begin ()); i != p.end (); ++i)
  391. v.push_back (*i);
  392. //for (parser::event_type e: p)
  393. // v.push_back (e);
  394. assert (v.size () == 5);
  395. assert (v[0] == parser::start_element);
  396. assert (v[1] == parser::start_element);
  397. assert (v[2] == parser::characters);
  398. assert (v[3] == parser::end_element);
  399. assert (v[4] == parser::end_element);
  400. }
  401. // Test space extraction into the std::string value.
  402. //
  403. {
  404. istringstream is ("<root a=' a '> b </root>");
  405. parser p (is, "test");
  406. p.next_expect (parser::start_element, "root");
  407. assert (p.attribute<std::string> ("a") == " a ");
  408. p.next_expect (parser::characters);
  409. assert (p.value<std::string> () == " b ");
  410. p.next_expect (parser::end_element);
  411. }
  412. }