diff options
author | Andrei Karas <akaras@inbox.ru> | 2016-01-21 01:25:41 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2016-01-21 01:25:41 +0300 |
commit | 3439b247f9528a55b8d83e98011e305fd8ff4e0d (patch) | |
tree | 1bd315178602306c00b528c1709b32fbc3c27fc0 /src/utils | |
parent | 05611825dcdeb729f7a7da758dcc3f0d96603aef (diff) | |
download | mv-3439b247f9528a55b8d83e98011e305fd8ff4e0d.tar.gz mv-3439b247f9528a55b8d83e98011e305fd8ff4e0d.tar.bz2 mv-3439b247f9528a55b8d83e98011e305fd8ff4e0d.tar.xz mv-3439b247f9528a55b8d83e98011e305fd8ff4e0d.zip |
Add xml writing unit tests.
Diffstat (limited to 'src/utils')
-rw-r--r-- | src/utils/xml_unittest.cc | 86 |
1 files changed, 85 insertions, 1 deletions
diff --git a/src/utils/xml_unittest.cc b/src/utils/xml_unittest.cc index 24a025e4a..e5c194e21 100644 --- a/src/utils/xml_unittest.cc +++ b/src/utils/xml_unittest.cc @@ -39,6 +39,7 @@ TEST_CASE("xml doc") ResourceManager::init(); resourceManager->addToSearchPath("data", Append_false); resourceManager->addToSearchPath("../data", Append_false); + const char *const tempXmlName = "tempxml.xml"; SECTION("load1") { @@ -68,7 +69,6 @@ TEST_CASE("xml doc") REQUIRE(xmlTypeEqual(doc.rootNode(), XML_ELEMENT_NODE) == true); REQUIRE(XmlHasProp(doc.rootNode(), "option1") == false); REQUIRE(XmlHasProp(doc.rootNode(), "option123") == false); - logger->log("content: '%s'", doc.rootNode().first_child().child_value()); REQUIRE(XmlHaveChildContent(doc.rootNode()) == false); } @@ -242,4 +242,88 @@ TEST_CASE("xml doc") REQUIRE(XML::Document::validateXml( "graphics/gui/testfile123.xml") == false); } + + SECTION("save1") + { + // clean + ::remove(tempXmlName); + + // save + FILE *const testFile = fopen(tempXmlName, "w"); + REQUIRE(testFile); + fclose(testFile); + const XmlTextWriterPtr writer = XmlNewTextWriterFilename( + tempXmlName, + 0); + XmlTextWriterSetIndent(writer, 1); + XmlTextWriterStartDocument(writer, nullptr, nullptr, nullptr); + XmlTextWriterStartElement(writer, "root"); + XmlTextWriterEndDocument(writer); + XmlFreeTextWriter(writer); + + // load + XML::Document doc(tempXmlName, + UseResman_false, + SkipError_false); + REQUIRE(doc.isLoaded() == true); + REQUIRE(doc.isValid() == true); + REQUIRE(doc.rootNode() != nullptr); + REQUIRE(xmlNameEqual(doc.rootNode(), "root") == true); + REQUIRE(xmlNameEqual(doc.rootNode(), "skinset123") == false); + REQUIRE(xmlTypeEqual(doc.rootNode(), XML_ELEMENT_NODE) == true); +// REQUIRE(XmlHaveChildContent(doc.rootNode()) == true); + + // clean again + ::remove(tempXmlName); + } + + SECTION("save2") + { + // clean + ::remove(tempXmlName); + + // save + FILE *const testFile = fopen(tempXmlName, "w"); + REQUIRE(testFile); + fclose(testFile); + const XmlTextWriterPtr writer = XmlNewTextWriterFilename( + tempXmlName, + 0); + XmlTextWriterSetIndent(writer, 1); + XmlTextWriterStartDocument(writer, nullptr, nullptr, nullptr); + XmlTextWriterStartElement(writer, "root"); + + XmlTextWriterStartElement(writer, "option"); + XmlTextWriterWriteAttribute(writer, "name", "the name"); + XmlTextWriterWriteAttribute(writer, "value", "the value"); + XmlTextWriterEndElement(writer); + + XmlTextWriterEndDocument(writer); + XmlFreeTextWriter(writer); + + // load + XML::Document doc(tempXmlName, + UseResman_false, + SkipError_false); + REQUIRE(doc.isLoaded() == true); + REQUIRE(doc.isValid() == true); + REQUIRE(doc.rootNode() != nullptr); + REQUIRE(xmlNameEqual(doc.rootNode(), "root") == true); + REQUIRE(xmlNameEqual(doc.rootNode(), "skinset123") == false); + REQUIRE(xmlTypeEqual(doc.rootNode(), XML_ELEMENT_NODE) == true); +// REQUIRE(XmlHaveChildContent(doc.rootNode()) == true); + XmlNodePtr node = XML::findFirstChildByName(doc.rootNode(), "option"); + REQUIRE(node != nullptr); + REQUIRE(xmlTypeEqual(node, XML_ELEMENT_NODE) == true); + REQUIRE(xmlNameEqual(node, "option") == true); + REQUIRE(XmlHaveChildContent(node) == false); + REQUIRE(XmlHasProp(node, "name") == true); + REQUIRE(XmlHasProp(node, "value") == true); + REQUIRE(XmlHasProp(node, "option123") == false); + REQUIRE(XML::getProperty(node, "name", "") == "the name"); + REQUIRE(XML::getProperty(node, "value", "") == "the value"); + + // clean again + ::remove(tempXmlName); + } } |