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-05-02
  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 expectedXmlString = 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(expectedXmlString.c_str(), xmlStream.c_str());
  73. }
  74. }