XmlDocumentTest.cpp 3.4 KB

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