XmlDocumentTest.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-09-30
  6. * Changed: 2023-05-18
  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. document.setDeclaration(make_shared<XmlDeclaration>("1.0"));
  43. ASSERT_TRUE(document.getDeclaration() != nullptr);
  44. ASSERT_STREQ("1.0", document.getDeclaration()->getVersion().c_str());
  45. }
  46. TEST_F(XmlDocumentTest, setDeclaration_no_reference)
  47. {
  48. XmlDocument document{};
  49. EXPECT_THROW(
  50. {
  51. try
  52. {
  53. document.setDeclaration(nullptr);
  54. }
  55. catch (const IllegalArgumentException &_exception)
  56. {
  57. throw;
  58. }
  59. },
  60. IllegalArgumentException);
  61. }
  62. TEST_F(XmlDocumentTest, setRootElement)
  63. {
  64. XmlDocument document{};
  65. ASSERT_TRUE(document.getRootElement() == nullptr);
  66. document.setRootElement(TestDataFactory::createXmlContent());
  67. ASSERT_TRUE(document.getRootElement() != nullptr);
  68. ASSERT_STREQ("dialog", document.getRootElement()->getName().c_str());
  69. }
  70. TEST_F(XmlDocumentTest, setRootElement_no_reference)
  71. {
  72. XmlDocument document{};
  73. EXPECT_THROW(
  74. {
  75. try
  76. {
  77. document.setRootElement(nullptr);
  78. }
  79. catch (const IllegalArgumentException &_exception)
  80. {
  81. throw;
  82. }
  83. },
  84. IllegalArgumentException);
  85. }
  86. TEST_F(XmlDocumentTest, toXml)
  87. {
  88. XmlDocument document{};
  89. XmlDeclaration declaration{"1.0"};
  90. declaration.setEncoding("UTF-8");
  91. declaration.setStandalone("yes");
  92. document.setDeclaration(make_shared<XmlDeclaration>(declaration));
  93. document.setRootElement(TestDataFactory::createXmlContent());
  94. string xmlStream = document.toXml();
  95. ASSERT_TRUE(!xmlStream.empty());
  96. string expectedXmlString = R"(<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
  97. <dialog name="dungeon_001">
  98. <dialogUnit id="001">
  99. <text>Hello!</text>
  100. </dialogUnit>
  101. <dialogUnit id="002">
  102. <text>Hello again!</text>
  103. </dialogUnit>
  104. </dialog>
  105. )";
  106. ASSERT_STREQ(expectedXmlString.c_str(), xmlStream.c_str());
  107. }
  108. }