XMLDocumentTest.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-09-30
  6. * Changed: 2021-04-23
  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, setRootElement)
  44. {
  45. ls_std::XMLDocument document{};
  46. ASSERT_TRUE(document.getRootElement() == nullptr);
  47. ls_std::XMLDeclaration declaration{"1.0"};
  48. document.setRootElement(ls_std_test::TestDataFactory::createXMLContent());
  49. ASSERT_TRUE(document.getRootElement() != nullptr);
  50. ASSERT_STREQ("dialog", document.getRootElement()->getName().c_str());
  51. }
  52. TEST_F(XMLDocumentTest, toXML)
  53. {
  54. ls_std::XMLDocument document{};
  55. ls_std::XMLDeclaration declaration{"1.0"};
  56. declaration.setEncoding("UTF-8");
  57. declaration.setStandalone("yes");
  58. document.setDeclaration(std::make_shared<ls_std::XMLDeclaration>(declaration));
  59. document.setRootElement(ls_std_test::TestDataFactory::createXMLContent());
  60. std::string xmlStream = document.toXML();
  61. ASSERT_TRUE(!xmlStream.empty());
  62. std::string expectedXMLContent = R"(<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
  63. <dialog name="dungeon_001">
  64. <dialogUnit id="001">
  65. <text>Hello!</text>
  66. </dialogUnit>
  67. <dialogUnit id="002">
  68. <text>Hello again!</text>
  69. </dialogUnit>
  70. </dialog>
  71. )";
  72. ASSERT_STREQ(expectedXMLContent.c_str(), xmlStream.c_str());
  73. }
  74. }