/* * Author: Patrick-Christopher Mattulat * Company: Lynar Studios * E-Mail: webmaster@lynarstudios.com * Created: 2020-09-30 * Changed: 2023-03-25 * * */ #include #include #include #include using ls::std::core::IllegalArgumentException; using ls::std::io::XmlDeclaration; using ls::std::io::XmlDocument; using std::make_shared; using std::string; using test::io::TestDataFactory; using testing::Test; namespace { class XmlDocumentTest : public Test { public: XmlDocumentTest() = default; ~XmlDocumentTest() override = default; }; TEST_F(XmlDocumentTest, getDeclaration) { XmlDocument document{}; ASSERT_TRUE(document.getDeclaration() == nullptr); } TEST_F(XmlDocumentTest, getRootElement) { XmlDocument document{}; ASSERT_TRUE(document.getRootElement() == nullptr); } TEST_F(XmlDocumentTest, setDeclaration) { XmlDocument document{}; ASSERT_TRUE(document.getDeclaration() == nullptr); XmlDeclaration declaration{"1.0"}; document.setDeclaration(make_shared(declaration)); ASSERT_TRUE(document.getDeclaration() != nullptr); ASSERT_STREQ("1.0", document.getDeclaration()->getVersion().c_str()); } TEST_F(XmlDocumentTest, setDeclaration_no_reference) { XmlDocument document{}; EXPECT_THROW( { try { document.setDeclaration(nullptr); } catch (const IllegalArgumentException &_exception) { throw; } }, IllegalArgumentException); } TEST_F(XmlDocumentTest, setRootElement) { XmlDocument document{}; ASSERT_TRUE(document.getRootElement() == nullptr); document.setRootElement(TestDataFactory::createXmlContent()); ASSERT_TRUE(document.getRootElement() != nullptr); ASSERT_STREQ("dialog", document.getRootElement()->getName().c_str()); } TEST_F(XmlDocumentTest, setRootElement_no_reference) { XmlDocument document{}; EXPECT_THROW( { try { document.setRootElement(nullptr); } catch (const IllegalArgumentException &_exception) { throw; } }, IllegalArgumentException); } TEST_F(XmlDocumentTest, toXml) { XmlDocument document{}; XmlDeclaration declaration{"1.0"}; declaration.setEncoding("UTF-8"); declaration.setStandalone("yes"); document.setDeclaration(make_shared(declaration)); document.setRootElement(TestDataFactory::createXmlContent()); string xmlStream = document.toXml(); ASSERT_TRUE(!xmlStream.empty()); string expectedXmlString = R"( Hello! Hello again! )"; ASSERT_STREQ(expectedXmlString.c_str(), xmlStream.c_str()); } }