XmlDocumentTest.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-09-30
  6. * Changed: 2023-02-05
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <ls-std-io-test.hpp>
  11. #include <ls-std/ls-std-core.hpp>
  12. #include <ls-std/ls-std-io.hpp>
  13. using namespace ls::std::core;
  14. using namespace ls::std::io;
  15. using namespace ::std;
  16. using namespace test::io;
  17. namespace
  18. {
  19. class XmlDocumentTest : public ::testing::Test
  20. {
  21. protected:
  22. XmlDocumentTest() = default;
  23. ~XmlDocumentTest() override = default;
  24. void SetUp() override
  25. {}
  26. void TearDown() override
  27. {}
  28. };
  29. TEST_F(XmlDocumentTest, getDeclaration)
  30. {
  31. XmlDocument document{};
  32. ASSERT_TRUE(document.getDeclaration() == nullptr);
  33. }
  34. TEST_F(XmlDocumentTest, getRootElement)
  35. {
  36. XmlDocument document{};
  37. ASSERT_TRUE(document.getRootElement() == nullptr);
  38. }
  39. TEST_F(XmlDocumentTest, setDeclaration)
  40. {
  41. XmlDocument document{};
  42. ASSERT_TRUE(document.getDeclaration() == nullptr);
  43. XmlDeclaration declaration{"1.0"};
  44. document.setDeclaration(make_shared<XmlDeclaration>(declaration));
  45. ASSERT_TRUE(document.getDeclaration() != nullptr);
  46. ASSERT_STREQ("1.0", document.getDeclaration()->getVersion().c_str());
  47. }
  48. TEST_F(XmlDocumentTest, setDeclaration_no_reference)
  49. {
  50. XmlDocument document{};
  51. EXPECT_THROW(
  52. {
  53. try
  54. {
  55. document.setDeclaration(nullptr);
  56. }
  57. catch (const IllegalArgumentException &_exception)
  58. {
  59. throw;
  60. }
  61. },
  62. IllegalArgumentException);
  63. }
  64. TEST_F(XmlDocumentTest, setRootElement)
  65. {
  66. XmlDocument document{};
  67. ASSERT_TRUE(document.getRootElement() == nullptr);
  68. XmlDeclaration declaration{"1.0"};
  69. document.setRootElement(TestDataFactory::createXmlContent());
  70. ASSERT_TRUE(document.getRootElement() != nullptr);
  71. ASSERT_STREQ("dialog", document.getRootElement()->getName().c_str());
  72. }
  73. TEST_F(XmlDocumentTest, setRootElement_no_reference)
  74. {
  75. XmlDocument document{};
  76. EXPECT_THROW(
  77. {
  78. try
  79. {
  80. document.setRootElement(nullptr);
  81. }
  82. catch (const IllegalArgumentException &_exception)
  83. {
  84. throw;
  85. }
  86. },
  87. IllegalArgumentException);
  88. }
  89. TEST_F(XmlDocumentTest, toXml)
  90. {
  91. XmlDocument document{};
  92. XmlDeclaration declaration{"1.0"};
  93. declaration.setEncoding("UTF-8");
  94. declaration.setStandalone("yes");
  95. document.setDeclaration(make_shared<XmlDeclaration>(declaration));
  96. document.setRootElement(TestDataFactory::createXmlContent());
  97. string xmlStream = document.toXml();
  98. ASSERT_TRUE(!xmlStream.empty());
  99. string expectedXmlString = R"(<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
  100. <dialog name="dungeon_001">
  101. <dialogUnit id="001">
  102. <text>Hello!</text>
  103. </dialogUnit>
  104. <dialogUnit id="002">
  105. <text>Hello again!</text>
  106. </dialogUnit>
  107. </dialog>
  108. )";
  109. ASSERT_STREQ(expectedXmlString.c_str(), xmlStream.c_str());
  110. }
  111. }