XmlDocumentTest.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-09-30
  6. * Changed: 2023-03-25
  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 ls::std::core::IllegalArgumentException;
  14. using ls::std::io::XmlDeclaration;
  15. using ls::std::io::XmlDocument;
  16. using std::make_shared;
  17. using std::string;
  18. using test::io::TestDataFactory;
  19. using testing::Test;
  20. namespace
  21. {
  22. class XmlDocumentTest : public Test
  23. {
  24. public:
  25. XmlDocumentTest() = default;
  26. ~XmlDocumentTest() override = default;
  27. };
  28. TEST_F(XmlDocumentTest, getDeclaration)
  29. {
  30. XmlDocument document{};
  31. ASSERT_TRUE(document.getDeclaration() == nullptr);
  32. }
  33. TEST_F(XmlDocumentTest, getRootElement)
  34. {
  35. XmlDocument document{};
  36. ASSERT_TRUE(document.getRootElement() == nullptr);
  37. }
  38. TEST_F(XmlDocumentTest, setDeclaration)
  39. {
  40. XmlDocument document{};
  41. ASSERT_TRUE(document.getDeclaration() == nullptr);
  42. XmlDeclaration declaration{"1.0"};
  43. document.setDeclaration(make_shared<XmlDeclaration>(declaration));
  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. string xmlStream = document.toXml();
  96. ASSERT_TRUE(!xmlStream.empty());
  97. 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. }