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-22
  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. using namespace ::testing;
  18. namespace
  19. {
  20. class XmlDocumentTest : public Test
  21. {
  22. protected:
  23. XmlDocumentTest() = default;
  24. ~XmlDocumentTest() override = default;
  25. void SetUp() override
  26. {}
  27. void TearDown() override
  28. {}
  29. };
  30. TEST_F(XmlDocumentTest, getDeclaration)
  31. {
  32. XmlDocument document{};
  33. ASSERT_TRUE(document.getDeclaration() == nullptr);
  34. }
  35. TEST_F(XmlDocumentTest, getRootElement)
  36. {
  37. XmlDocument document{};
  38. ASSERT_TRUE(document.getRootElement() == nullptr);
  39. }
  40. TEST_F(XmlDocumentTest, setDeclaration)
  41. {
  42. XmlDocument document{};
  43. ASSERT_TRUE(document.getDeclaration() == nullptr);
  44. XmlDeclaration declaration{"1.0"};
  45. document.setDeclaration(make_shared<XmlDeclaration>(declaration));
  46. ASSERT_TRUE(document.getDeclaration() != nullptr);
  47. ASSERT_STREQ("1.0", document.getDeclaration()->getVersion().c_str());
  48. }
  49. TEST_F(XmlDocumentTest, setDeclaration_no_reference)
  50. {
  51. XmlDocument document{};
  52. EXPECT_THROW(
  53. {
  54. try
  55. {
  56. document.setDeclaration(nullptr);
  57. }
  58. catch (const IllegalArgumentException &_exception)
  59. {
  60. throw;
  61. }
  62. },
  63. IllegalArgumentException);
  64. }
  65. TEST_F(XmlDocumentTest, setRootElement)
  66. {
  67. XmlDocument document{};
  68. ASSERT_TRUE(document.getRootElement() == nullptr);
  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. }