XmlDocumentTest.cpp 3.2 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: 2022-11-09
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <ls_std/ls_std_core.hpp>
  11. #include <ls_std/ls_std_io.hpp>
  12. #include <ls_std_io_test.hpp>
  13. using namespace ls::std::core;
  14. using namespace ls::std::io;
  15. using namespace ::std;
  16. using namespace ls_std_io_test;
  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. try
  53. {
  54. document.setDeclaration(nullptr);
  55. }
  56. catch (const IllegalArgumentException &_exception)
  57. {
  58. throw;
  59. }
  60. }, IllegalArgumentException);
  61. }
  62. TEST_F(XmlDocumentTest, setRootElement)
  63. {
  64. XmlDocument document{};
  65. ASSERT_TRUE(document.getRootElement() == nullptr);
  66. XmlDeclaration declaration{"1.0"};
  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. try
  76. {
  77. document.setRootElement(nullptr);
  78. }
  79. catch (const IllegalArgumentException &_exception)
  80. {
  81. throw;
  82. }
  83. }, IllegalArgumentException);
  84. }
  85. TEST_F(XmlDocumentTest, toXml)
  86. {
  87. XmlDocument document{};
  88. XmlDeclaration declaration{"1.0"};
  89. declaration.setEncoding("UTF-8");
  90. declaration.setStandalone("yes");
  91. document.setDeclaration(make_shared<XmlDeclaration>(declaration));
  92. document.setRootElement(TestDataFactory::createXmlContent());
  93. string xmlStream = document.toXml();
  94. ASSERT_TRUE(!xmlStream.empty());
  95. string expectedXmlString = R"(<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
  96. <dialog name="dungeon_001">
  97. <dialogUnit id="001">
  98. <text>Hello!</text>
  99. </dialogUnit>
  100. <dialogUnit id="002">
  101. <text>Hello again!</text>
  102. </dialogUnit>
  103. </dialog>
  104. )";
  105. ASSERT_STREQ(expectedXmlString.c_str(), xmlStream.c_str());
  106. }
  107. }