PageRenderTime 24ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/src/parser/threaded_sax_token_parser_test.cpp

https://gitlab.com/jvsg/orcus
C++ | 116 lines | 87 code | 18 blank | 11 comment | 5 complexity | 9ec523387e005d17f3fa2598a994f3b9 MD5 | raw file
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /*
  3. * This Source Code Form is subject to the terms of the Mozilla Public
  4. * License, v. 2.0. If a copy of the MPL was not distributed with this
  5. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. */
  7. #include "orcus/threaded_sax_token_parser.hpp"
  8. #include "orcus/tokens.hpp"
  9. #include "orcus/global.hpp"
  10. #include "orcus/xml_namespace.hpp"
  11. #include <cstring>
  12. #include <cassert>
  13. using namespace std;
  14. using namespace orcus;
  15. void test_sax_token_parser_1()
  16. {
  17. // Test XML content.
  18. const char* content = "<?xml version=\"1.0\"?><root><andy/><bruce/><charlie/><david/><edward/><frank/></root>";
  19. size_t content_size = strlen(content);
  20. // Array of tokens to define for this test.
  21. const char* token_names[] = {
  22. "??", // 0
  23. "andy", // 1
  24. "bruce", // 2
  25. "charlie", // 3
  26. "david", // 4
  27. "edward" // 5
  28. };
  29. size_t token_count = ORCUS_N_ELEMENTS(token_names);
  30. // Token constants.
  31. const xml_token_t op_andy = 1;
  32. const xml_token_t op_bruce = 2;
  33. const xml_token_t op_charlie = 3;
  34. const xml_token_t op_david = 4;
  35. const xml_token_t op_edward = 5;
  36. struct check
  37. {
  38. const char* raw_name;
  39. xml_token_t token;
  40. bool start_element;
  41. };
  42. // Expected outcome.
  43. const check checks[] = {
  44. { "root", XML_UNKNOWN_TOKEN, true }, // name not on the master token list.
  45. { "andy", op_andy, true },
  46. { "andy", op_andy, false },
  47. { "bruce", op_bruce, true },
  48. { "bruce", op_bruce, false },
  49. { "charlie", op_charlie, true },
  50. { "charlie", op_charlie, false },
  51. { "david", op_david, true },
  52. { "david", op_david, false },
  53. { "edward", op_edward, true },
  54. { "edward", op_edward, false },
  55. { "frank", XML_UNKNOWN_TOKEN, true }, // name not on the master token list.
  56. { "frank", XML_UNKNOWN_TOKEN, false }, // name not on the master token list.
  57. { "root", XML_UNKNOWN_TOKEN, false }, // name not on the master token list.
  58. };
  59. class handler
  60. {
  61. const check* mp_head;
  62. const check* mp_check;
  63. public:
  64. handler(const check* p) : mp_head(p), mp_check(p) {}
  65. void start_element(const orcus::xml_token_element_t& elem)
  66. {
  67. assert(pstring(mp_check->raw_name) == elem.raw_name);
  68. assert(mp_check->token == elem.name);
  69. assert(mp_check->start_element);
  70. ++mp_check;
  71. }
  72. void end_element(const orcus::xml_token_element_t& elem)
  73. {
  74. assert(pstring(mp_check->raw_name) == elem.raw_name);
  75. assert(mp_check->token == elem.name);
  76. assert(!mp_check->start_element);
  77. ++mp_check;
  78. }
  79. void characters(const orcus::pstring& /*val*/, bool /*transient*/) {}
  80. size_t get_token_count() const
  81. {
  82. return std::distance(mp_head, mp_check);
  83. }
  84. };
  85. handler hdl(checks);
  86. tokens token_map(token_names, token_count);
  87. xmlns_repository ns_repo;
  88. xmlns_context ns_cxt = ns_repo.create_context();
  89. threaded_sax_token_parser<handler> parser(content, content_size, token_map, ns_cxt, hdl, 1, 100);
  90. parser.parse();
  91. assert(hdl.get_token_count() == ORCUS_N_ELEMENTS(checks));
  92. }
  93. int main()
  94. {
  95. test_sax_token_parser_1();
  96. return EXIT_SUCCESS;
  97. }
  98. /* vim:set shiftwidth=4 softtabstop=4 expandtab: */