XmlDocumentTest.cpp 3.2 KB

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