XMLDocumentTest.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-09-30
  6. * Changed: 2020-11-29
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <ls_std/ls_std.hpp>
  11. #include <TestDataFactory.hpp>
  12. namespace {
  13. class XMLDocumentTest : public ::testing::Test {
  14. protected:
  15. XMLDocumentTest() = default;
  16. ~XMLDocumentTest() override = default;
  17. void SetUp() override {}
  18. void TearDown() override {}
  19. };
  20. TEST_F(XMLDocumentTest, getDeclaration)
  21. {
  22. ls_std::XMLDocument document {};
  23. ASSERT_TRUE(document.getDeclaration() == nullptr);
  24. }
  25. TEST_F(XMLDocumentTest, getRootElement)
  26. {
  27. ls_std::XMLDocument document {};
  28. ASSERT_TRUE(document.getRootElement() == nullptr);
  29. }
  30. TEST_F(XMLDocumentTest, setDeclaration)
  31. {
  32. ls_std::XMLDocument document {};
  33. ASSERT_TRUE(document.getDeclaration() == nullptr);
  34. ls_std::XMLDeclaration declaration {"1.0"};
  35. document.setDeclaration(std::make_shared<ls_std::XMLDeclaration>(declaration));
  36. ASSERT_TRUE(document.getDeclaration() != nullptr);
  37. ASSERT_STREQ("1.0", document.getDeclaration()->getVersion().c_str());
  38. }
  39. TEST_F(XMLDocumentTest, setRootElement)
  40. {
  41. ls_std::XMLDocument document {};
  42. ASSERT_TRUE(document.getRootElement() == nullptr);
  43. ls_std::XMLDeclaration declaration {"1.0"};
  44. document.setRootElement(ls_std_test::TestDataFactory::createXMLContent());
  45. ASSERT_TRUE(document.getRootElement() != nullptr);
  46. ASSERT_STREQ("dialog", document.getRootElement()->getName().c_str());
  47. }
  48. TEST_F(XMLDocumentTest, toXML)
  49. {
  50. ls_std::XMLDocument document {};
  51. ls_std::XMLDeclaration declaration {"1.0"};
  52. declaration.setEncoding("UTF-8");
  53. declaration.setStandalone("yes");
  54. document.setDeclaration(std::make_shared<ls_std::XMLDeclaration>(declaration));
  55. document.setRootElement(ls_std_test::TestDataFactory::createXMLContent());
  56. std::string xmlStream = document.toXML();
  57. ASSERT_TRUE(!xmlStream.empty());
  58. std::string expectedXMLContent =
  59. R"(<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
  60. <dialog name="dungeon_001">
  61. <dialogUnit id="001">
  62. <text>Hello!</text>
  63. </dialogUnit>
  64. <dialogUnit id="002">
  65. <text>Hello again!</text>
  66. </dialogUnit>
  67. </dialog>
  68. )";
  69. ASSERT_STREQ(expectedXMLContent.c_str(), xmlStream.c_str());
  70. }
  71. }