XmlDocumentTest.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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-23
  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. protected:
  25. XmlDocumentTest() = default;
  26. ~XmlDocumentTest() override = default;
  27. void SetUp() override
  28. {}
  29. void TearDown() override
  30. {}
  31. };
  32. TEST_F(XmlDocumentTest, getDeclaration)
  33. {
  34. XmlDocument document{};
  35. ASSERT_TRUE(document.getDeclaration() == nullptr);
  36. }
  37. TEST_F(XmlDocumentTest, getRootElement)
  38. {
  39. XmlDocument document{};
  40. ASSERT_TRUE(document.getRootElement() == nullptr);
  41. }
  42. TEST_F(XmlDocumentTest, setDeclaration)
  43. {
  44. XmlDocument document{};
  45. ASSERT_TRUE(document.getDeclaration() == nullptr);
  46. XmlDeclaration declaration{"1.0"};
  47. document.setDeclaration(make_shared<XmlDeclaration>(declaration));
  48. ASSERT_TRUE(document.getDeclaration() != nullptr);
  49. ASSERT_STREQ("1.0", document.getDeclaration()->getVersion().c_str());
  50. }
  51. TEST_F(XmlDocumentTest, setDeclaration_no_reference)
  52. {
  53. XmlDocument document{};
  54. EXPECT_THROW(
  55. {
  56. try
  57. {
  58. document.setDeclaration(nullptr);
  59. }
  60. catch (const IllegalArgumentException &_exception)
  61. {
  62. throw;
  63. }
  64. },
  65. IllegalArgumentException);
  66. }
  67. TEST_F(XmlDocumentTest, setRootElement)
  68. {
  69. XmlDocument document{};
  70. ASSERT_TRUE(document.getRootElement() == nullptr);
  71. document.setRootElement(TestDataFactory::createXmlContent());
  72. ASSERT_TRUE(document.getRootElement() != nullptr);
  73. ASSERT_STREQ("dialog", document.getRootElement()->getName().c_str());
  74. }
  75. TEST_F(XmlDocumentTest, setRootElement_no_reference)
  76. {
  77. XmlDocument document{};
  78. EXPECT_THROW(
  79. {
  80. try
  81. {
  82. document.setRootElement(nullptr);
  83. }
  84. catch (const IllegalArgumentException &_exception)
  85. {
  86. throw;
  87. }
  88. },
  89. IllegalArgumentException);
  90. }
  91. TEST_F(XmlDocumentTest, toXml)
  92. {
  93. XmlDocument document{};
  94. XmlDeclaration declaration{"1.0"};
  95. declaration.setEncoding("UTF-8");
  96. declaration.setStandalone("yes");
  97. document.setDeclaration(make_shared<XmlDeclaration>(declaration));
  98. document.setRootElement(TestDataFactory::createXmlContent());
  99. string xmlStream = document.toXml();
  100. ASSERT_TRUE(!xmlStream.empty());
  101. string expectedXmlString = R"(<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
  102. <dialog name="dungeon_001">
  103. <dialogUnit id="001">
  104. <text>Hello!</text>
  105. </dialogUnit>
  106. <dialogUnit id="002">
  107. <text>Hello again!</text>
  108. </dialogUnit>
  109. </dialog>
  110. )";
  111. ASSERT_STREQ(expectedXmlString.c_str(), xmlStream.c_str());
  112. }
  113. }