test_xml_parser.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*************************************************************************/
  2. /* test_xml_parser.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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. #include "test_xml_parser.h"
  31. #include "core/os/os.h"
  32. namespace TestXMLParser {
  33. #define CHECK(X) \
  34. if (!(X)) { \
  35. OS::get_singleton()->print("\tFAIL at %s\n", #X); \
  36. return false; \
  37. } else { \
  38. OS::get_singleton()->print("\tPASS\n"); \
  39. }
  40. bool test_1() {
  41. String source = "<?xml version = \"1.0\" encoding=\"UTF-8\" ?>\
  42. <top attr=\"attr value\">\
  43. Text&lt;&#65;&#x42;&gt;\
  44. </top>";
  45. Vector<uint8_t> buff;
  46. for (int i = 0; i < source.length(); i++) {
  47. buff.push_back(source.get(i));
  48. }
  49. XMLParser parser;
  50. parser.open_buffer(buff);
  51. // <?xml ...?> gets parsed as NODE_UNKNOWN
  52. CHECK(parser.read() == OK);
  53. CHECK(parser.get_node_type() == XMLParser::NodeType::NODE_UNKNOWN);
  54. CHECK(parser.read() == OK);
  55. CHECK(parser.get_node_type() == XMLParser::NodeType::NODE_ELEMENT);
  56. CHECK(parser.get_node_name() == "top");
  57. CHECK(parser.has_attribute("attr"));
  58. CHECK(parser.get_attribute_value("attr") == "attr value");
  59. CHECK(parser.read() == OK);
  60. CHECK(parser.get_node_type() == XMLParser::NodeType::NODE_TEXT);
  61. CHECK(parser.get_node_data().lstrip(" \t") == "Text<AB>");
  62. CHECK(parser.read() == OK);
  63. CHECK(parser.get_node_type() == XMLParser::NodeType::NODE_ELEMENT_END);
  64. CHECK(parser.get_node_name() == "top");
  65. parser.close();
  66. return true;
  67. }
  68. typedef bool (*TestFunc)();
  69. TestFunc test_funcs[] = {
  70. test_1,
  71. nullptr
  72. };
  73. MainLoop *test() {
  74. int count = 0;
  75. int passed = 0;
  76. while (true) {
  77. if (!test_funcs[count]) {
  78. break;
  79. }
  80. bool pass = test_funcs[count]();
  81. if (pass) {
  82. passed++;
  83. }
  84. OS::get_singleton()->print("\t%s\n", pass ? "PASS" : "FAILED");
  85. count++;
  86. }
  87. OS::get_singleton()->print("\n\n\n");
  88. OS::get_singleton()->print("*************\n");
  89. OS::get_singleton()->print("***TOTALS!***\n");
  90. OS::get_singleton()->print("*************\n");
  91. OS::get_singleton()->print("Passed %i of %i tests\n", passed, count);
  92. return nullptr;
  93. }
  94. } // namespace TestXMLParser