XML.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzCore/XML/rapidxml.h>
  9. #include <AzCore/UnitTest/TestTypes.h>
  10. #include <AzCore/std/string/string.h>
  11. using namespace AZ;
  12. namespace UnitTest
  13. {
  14. /**
  15. * Rapid XML parser
  16. */
  17. class RapidXML
  18. : public LeakDetectionFixture
  19. {
  20. public:
  21. RapidXML() {}
  22. void run()
  23. {
  24. AZStd::string xmlData("<?xml version=\"1.0\" encoding=\"utf-8\"?>\
  25. <rootnode version=\"1.0\" type=\"example\">\
  26. <childnode entry=\"1\">\
  27. <evendeepernode attr1=\"cat\" attr2=\"dog\"/>\
  28. <evendeepernode attr1=\"lion\" attr2=\"wolf\"/>\
  29. </childnode>\
  30. <childnode entry=\"2\">\
  31. </childnode>\
  32. </rootnode>");
  33. AZStd::string xmlBadData1("?");
  34. AZStd::string xmlBadData2("</autooptimizefile=0 /preset=ReferenceImage_Linear /reduce=-1 /colorspace=linear,linear");
  35. AZStd::string xmlBadData3("<?xml version=\"1.0\" encoding=\"UTF - 8\"?>\
  36. <note>\
  37. <to>Tove\
  38. <from>Jani\
  39. </to>\
  40. </from>\
  41. <heading>Reminder</heading>\
  42. <body>Don't forget me this weekend!</body>\
  43. </note>");
  44. rapidxml::xml_document<> doc;
  45. // Test error control with some bad XML input
  46. AZ_TEST_ASSERT(doc.parse<rapidxml::parse_full>(xmlBadData1.data()) == false);
  47. AZ_TEST_ASSERT(doc.isError());
  48. AZ_TEST_ASSERT(strlen(doc.getError()) > 0);
  49. AZ_TEST_ASSERT(doc.parse<rapidxml::parse_full>(xmlBadData2.data()) == false);
  50. AZ_TEST_ASSERT(doc.isError());
  51. AZ_TEST_ASSERT(strlen(doc.getError()) > 0);
  52. AZ_TEST_ASSERT(doc.parse<rapidxml::parse_full>(xmlBadData3.data()) == false);
  53. AZ_TEST_ASSERT(doc.isError());
  54. AZ_TEST_ASSERT(strlen(doc.getError()) > 0);
  55. // now test with correct XML
  56. AZ_TEST_ASSERT(doc.parse<rapidxml::parse_full>(xmlData.data()));
  57. AZ_TEST_ASSERT(doc.isError() == false);
  58. AZ_TEST_ASSERT(strlen(doc.getError()) == 0)
  59. // since we have parsed the XML declaration, it is the first node
  60. // (otherwise the first node would be our root node)
  61. const char* encoding = doc.first_node()->first_attribute("encoding")->value();
  62. AZ_TEST_ASSERT(strcmp("utf-8", encoding) == 0);
  63. // we didn't keep track of our previous traversal, so let's start again
  64. // we can match nodes by name, skipping the xml declaration entirely
  65. rapidxml::xml_node<>* cur_node = doc.first_node("rootnode");
  66. const char* rootnode_type = cur_node->first_attribute("type")->value();
  67. AZ_TEST_ASSERT(strcmp("example", rootnode_type) == 0);
  68. // go straight to the first evendeepernode
  69. cur_node = cur_node->first_node("childnode")->first_node("evendeepernode");
  70. const char* attr2 = cur_node->first_attribute("attr2")->value();
  71. AZ_TEST_ASSERT(strcmp("dog", attr2) == 0);
  72. // and then to the second evendeepernode
  73. cur_node = cur_node->next_sibling("evendeepernode");
  74. attr2 = cur_node->first_attribute("attr2")->value();
  75. AZ_TEST_ASSERT(strcmp("wolf", attr2) == 0);
  76. }
  77. };
  78. }
  79. AZ_TEST_SUITE(XML)
  80. AZ_TEST(UnitTest::RapidXML)
  81. AZ_TEST_SUITE_END