XmlDocumentTest.cpp 3.1 KB

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