test_xml_parser.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /**************************************************************************/
  2. /* test_xml_parser.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef TEST_XML_PARSER_H
  31. #define TEST_XML_PARSER_H
  32. #include "core/io/xml_parser.h"
  33. #include "tests/test_macros.h"
  34. namespace TestXMLParser {
  35. TEST_CASE("[XMLParser] End-to-end") {
  36. String source = "<?xml version = \"1.0\" encoding=\"UTF-8\" ?>\
  37. <top attr=\"attr value\">\
  38. Text&lt;&#65;&#x42;&gt;\
  39. </top>";
  40. Vector<uint8_t> buff = source.to_utf8_buffer();
  41. XMLParser parser;
  42. parser.open_buffer(buff);
  43. // <?xml ...?> gets parsed as NODE_UNKNOWN
  44. CHECK(parser.read() == OK);
  45. CHECK(parser.get_node_type() == XMLParser::NodeType::NODE_UNKNOWN);
  46. CHECK(parser.read() == OK);
  47. CHECK(parser.get_node_type() == XMLParser::NodeType::NODE_ELEMENT);
  48. CHECK(parser.get_node_name() == "top");
  49. CHECK(parser.has_attribute("attr"));
  50. CHECK(parser.get_named_attribute_value("attr") == "attr value");
  51. CHECK(parser.read() == OK);
  52. CHECK(parser.get_node_type() == XMLParser::NodeType::NODE_TEXT);
  53. CHECK(parser.get_node_data().lstrip(" \t") == "Text<AB>");
  54. CHECK(parser.read() == OK);
  55. CHECK(parser.get_node_type() == XMLParser::NodeType::NODE_ELEMENT_END);
  56. CHECK(parser.get_node_name() == "top");
  57. parser.close();
  58. }
  59. TEST_CASE("[XMLParser] Comments") {
  60. XMLParser parser;
  61. SUBCASE("Missing end of comment") {
  62. const String input = "<first></first><!-- foo";
  63. REQUIRE_EQ(parser.open_buffer(input.to_utf8_buffer()), OK);
  64. REQUIRE_EQ(parser.read(), OK);
  65. REQUIRE_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_ELEMENT);
  66. REQUIRE_EQ(parser.read(), OK);
  67. REQUIRE_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_ELEMENT_END);
  68. REQUIRE_EQ(parser.read(), OK);
  69. CHECK_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_COMMENT);
  70. CHECK_EQ(parser.get_node_name(), " foo");
  71. }
  72. SUBCASE("Bad start of comment") {
  73. const String input = "<first></first><!-";
  74. REQUIRE_EQ(parser.open_buffer(input.to_utf8_buffer()), OK);
  75. REQUIRE_EQ(parser.read(), OK);
  76. REQUIRE_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_ELEMENT);
  77. REQUIRE_EQ(parser.read(), OK);
  78. REQUIRE_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_ELEMENT_END);
  79. REQUIRE_EQ(parser.read(), OK);
  80. CHECK_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_COMMENT);
  81. CHECK_EQ(parser.get_node_name(), "-");
  82. }
  83. SUBCASE("Unblanced angle brackets in comment") {
  84. const String input = "<!-- example << --><next-tag></next-tag>";
  85. REQUIRE_EQ(parser.open_buffer(input.to_utf8_buffer()), OK);
  86. REQUIRE_EQ(parser.read(), OK);
  87. CHECK_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_COMMENT);
  88. CHECK_EQ(parser.get_node_name(), " example << ");
  89. }
  90. SUBCASE("Doctype") {
  91. const String input = "<!DOCTYPE greeting [<!ELEMENT greeting (#PCDATA)>]>";
  92. REQUIRE_EQ(parser.open_buffer(input.to_utf8_buffer()), OK);
  93. REQUIRE_EQ(parser.read(), OK);
  94. CHECK_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_COMMENT);
  95. CHECK_EQ(parser.get_node_name(), "DOCTYPE greeting [<!ELEMENT greeting (#PCDATA)>]");
  96. }
  97. }
  98. TEST_CASE("[XMLParser] Premature endings") {
  99. SUBCASE("Simple cases") {
  100. String input;
  101. String expected_name;
  102. XMLParser::NodeType expected_type;
  103. SUBCASE("Incomplete Unknown") {
  104. input = "<first></first><?xml";
  105. expected_type = XMLParser::NodeType::NODE_UNKNOWN;
  106. expected_name = "?xml";
  107. }
  108. SUBCASE("Incomplete CDStart") {
  109. input = "<first></first><![CD";
  110. expected_type = XMLParser::NodeType::NODE_CDATA;
  111. expected_name = "";
  112. }
  113. SUBCASE("Incomplete CData") {
  114. input = "<first></first><![CDATA[example";
  115. expected_type = XMLParser::NodeType::NODE_CDATA;
  116. expected_name = "example";
  117. }
  118. SUBCASE("Incomplete CDEnd") {
  119. input = "<first></first><![CDATA[example]]";
  120. expected_type = XMLParser::NodeType::NODE_CDATA;
  121. expected_name = "example]]";
  122. }
  123. SUBCASE("Incomplete start-tag name") {
  124. input = "<first></first><second";
  125. expected_type = XMLParser::NodeType::NODE_ELEMENT;
  126. expected_name = "second";
  127. }
  128. XMLParser parser;
  129. REQUIRE_EQ(parser.open_buffer(input.to_utf8_buffer()), OK);
  130. REQUIRE_EQ(parser.read(), OK);
  131. REQUIRE_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_ELEMENT);
  132. REQUIRE_EQ(parser.read(), OK);
  133. REQUIRE_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_ELEMENT_END);
  134. REQUIRE_EQ(parser.read(), OK);
  135. CHECK_EQ(parser.get_node_type(), expected_type);
  136. CHECK_EQ(parser.get_node_name(), expected_name);
  137. }
  138. SUBCASE("With attributes and texts") {
  139. XMLParser parser;
  140. SUBCASE("Incomplete start-tag attribute name") {
  141. const String input = "<first></first><second attr1=\"foo\" attr2";
  142. REQUIRE_EQ(parser.open_buffer(input.to_utf8_buffer()), OK);
  143. REQUIRE_EQ(parser.read(), OK);
  144. REQUIRE_EQ(parser.read(), OK);
  145. REQUIRE_EQ(parser.read(), OK);
  146. CHECK_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_ELEMENT);
  147. CHECK_EQ(parser.get_node_name(), "second");
  148. CHECK_EQ(parser.get_attribute_count(), 1);
  149. CHECK_EQ(parser.get_attribute_name(0), "attr1");
  150. CHECK_EQ(parser.get_attribute_value(0), "foo");
  151. }
  152. SUBCASE("Incomplete start-tag attribute unquoted value") {
  153. const String input = "<first></first><second attr1=\"foo\" attr2=bar";
  154. REQUIRE_EQ(parser.open_buffer(input.to_utf8_buffer()), OK);
  155. REQUIRE_EQ(parser.read(), OK);
  156. REQUIRE_EQ(parser.read(), OK);
  157. REQUIRE_EQ(parser.read(), OK);
  158. CHECK_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_ELEMENT);
  159. CHECK_EQ(parser.get_node_name(), "second");
  160. CHECK_EQ(parser.get_attribute_count(), 1);
  161. CHECK_EQ(parser.get_attribute_name(0), "attr1");
  162. CHECK_EQ(parser.get_attribute_value(0), "foo");
  163. }
  164. SUBCASE("Incomplete start-tag attribute quoted value") {
  165. const String input = "<first></first><second attr1=\"foo\" attr2=\"bar";
  166. REQUIRE_EQ(parser.open_buffer(input.to_utf8_buffer()), OK);
  167. REQUIRE_EQ(parser.read(), OK);
  168. REQUIRE_EQ(parser.read(), OK);
  169. REQUIRE_EQ(parser.read(), OK);
  170. CHECK_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_ELEMENT);
  171. CHECK_EQ(parser.get_node_name(), "second");
  172. CHECK_EQ(parser.get_attribute_count(), 2);
  173. CHECK_EQ(parser.get_attribute_name(0), "attr1");
  174. CHECK_EQ(parser.get_attribute_value(0), "foo");
  175. CHECK_EQ(parser.get_attribute_name(1), "attr2");
  176. CHECK_EQ(parser.get_attribute_value(1), "bar");
  177. }
  178. SUBCASE("Incomplete end-tag name") {
  179. const String input = "<first></fir";
  180. REQUIRE_EQ(parser.open_buffer(input.to_utf8_buffer()), OK);
  181. REQUIRE_EQ(parser.read(), OK);
  182. REQUIRE_EQ(parser.read(), OK);
  183. CHECK_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_ELEMENT_END);
  184. CHECK_EQ(parser.get_node_name(), "fir");
  185. }
  186. SUBCASE("Trailing text") {
  187. const String input = "<first></first>example";
  188. REQUIRE_EQ(parser.open_buffer(input.to_utf8_buffer()), OK);
  189. REQUIRE_EQ(parser.read(), OK);
  190. REQUIRE_EQ(parser.read(), OK);
  191. REQUIRE_EQ(parser.read(), OK);
  192. CHECK_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_TEXT);
  193. CHECK_EQ(parser.get_node_data(), "example");
  194. }
  195. }
  196. }
  197. TEST_CASE("[XMLParser] CDATA") {
  198. const String input = "<a><![CDATA[my cdata content goes here]]></a>";
  199. XMLParser parser;
  200. REQUIRE_EQ(parser.open_buffer(input.to_utf8_buffer()), OK);
  201. REQUIRE_EQ(parser.read(), OK);
  202. CHECK_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_ELEMENT);
  203. CHECK_EQ(parser.get_node_name(), "a");
  204. REQUIRE_EQ(parser.read(), OK);
  205. CHECK_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_CDATA);
  206. CHECK_EQ(parser.get_node_name(), "my cdata content goes here");
  207. REQUIRE_EQ(parser.read(), OK);
  208. CHECK_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_ELEMENT_END);
  209. CHECK_EQ(parser.get_node_name(), "a");
  210. }
  211. } // namespace TestXMLParser
  212. #endif // TEST_XML_PARSER_H