summaryrefslogtreecommitdiff
path: root/src/utils/xml_unittest.cc
diff options
context:
space:
mode:
authorAndrei Karas <akaras@inbox.ru>2016-01-18 02:02:21 +0300
committerAndrei Karas <akaras@inbox.ru>2016-01-18 02:02:21 +0300
commita1707daab655e01a40693cc578ad221e6d712bfc (patch)
treecbb630da1edfb6542cd790cec879ede3cde22566 /src/utils/xml_unittest.cc
parent1ad9eb156b85f42cfedf2e8588339bf608abb8ea (diff)
downloadplus-a1707daab655e01a40693cc578ad221e6d712bfc.tar.gz
plus-a1707daab655e01a40693cc578ad221e6d712bfc.tar.bz2
plus-a1707daab655e01a40693cc578ad221e6d712bfc.tar.xz
plus-a1707daab655e01a40693cc578ad221e6d712bfc.zip
Add unit tests for xml document.
Diffstat (limited to 'src/utils/xml_unittest.cc')
-rw-r--r--src/utils/xml_unittest.cc158
1 files changed, 158 insertions, 0 deletions
diff --git a/src/utils/xml_unittest.cc b/src/utils/xml_unittest.cc
new file mode 100644
index 000000000..9da507981
--- /dev/null
+++ b/src/utils/xml_unittest.cc
@@ -0,0 +1,158 @@
+/*
+ * The ManaPlus Client
+ * Copyright (C) 2014-2016 The ManaPlus Developers
+ *
+ * This file is part of The ManaPlus Client.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "catch.hpp"
+#include "client.h"
+#include "logger.h"
+
+#include "utils/physfstools.h"
+#include "utils/xml.h"
+
+#include "resources/resourcemanager.h"
+
+#include "debug.h"
+
+TEST_CASE("xml doc")
+{
+ client = new Client;
+ PHYSFS_init("manaplus");
+ dirSeparator = "/";
+ XML::initXML();
+ logger = new Logger();
+ ResourceManager::init();
+ resourceManager->addToSearchPath("data", Append_false);
+ resourceManager->addToSearchPath("../data", Append_false);
+
+ SECTION("load1")
+ {
+ XML::Document doc("graphics/gui/browserbox.xml",
+ UseResman_true,
+ SkipError_false);
+ REQUIRE(doc.isLoaded() == true);
+ REQUIRE(doc.isValid() == true);
+ REQUIRE(doc.rootNode() != nullptr);
+ REQUIRE(xmlNameEqual(doc.rootNode(), "skinset") == true);
+ }
+
+ SECTION("load2")
+ {
+ const std::string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+ "<root><data option1=\"false\" option2=\"true\"/></root>";
+ XML::Document doc(xml.c_str(), xml.size());
+ REQUIRE(doc.isLoaded() == true);
+ REQUIRE(doc.isValid() == true);
+ REQUIRE(doc.rootNode() != nullptr);
+ REQUIRE(xmlNameEqual(doc.rootNode(), "root") == true);
+ }
+
+ SECTION("properties")
+ {
+ XML::Document doc("graphics/gui/browserbox.xml",
+ UseResman_true,
+ SkipError_false);
+
+ const XmlNodePtr rootNode = doc.rootNode();
+ REQUIRE(XML::getProperty(rootNode, "image", "") == "window.png");
+ }
+
+ SECTION("for each")
+ {
+ XML::Document doc("graphics/gui/browserbox.xml",
+ UseResman_true,
+ SkipError_false);
+
+ const XmlNodePtr rootNode = doc.rootNode();
+ XmlNodePtr node = nullptr;
+ for_each_xml_child_node(widgetNode, rootNode)
+ {
+ node = widgetNode;
+ if (xmlNameEqual(node, "widget"))
+ break;
+ }
+ REQUIRE(node != nullptr);
+ REQUIRE(xmlNameEqual(node, "widget") == true);
+ for_each_xml_child_node(optionNode, node)
+ {
+ node = optionNode;
+ if (xmlNameEqual(node, "option"))
+ break;
+ }
+ REQUIRE(node != nullptr);
+ REQUIRE(xmlNameEqual(node, "option") == true);
+ REQUIRE(XML::getProperty(node, "name", "") == "padding");
+ REQUIRE(XML::langProperty(node, "name", "") == "padding");
+ REQUIRE(XML::getProperty(node, "value", 0) == 1);
+ REQUIRE(XML::getBoolProperty(node, "value", true) == true);
+ REQUIRE(XML::getBoolProperty(node, "value", false) == false);
+ REQUIRE(XML::getIntProperty(node, "value", -1, -10, 100) == 1);
+ }
+
+ SECTION("child1")
+ {
+ XML::Document doc("graphics/gui/browserbox.xml",
+ UseResman_true,
+ SkipError_false);
+
+ const XmlNodePtr rootNode = doc.rootNode();
+ XmlNodePtr node = XML::findFirstChildByName(rootNode, "widget");
+ REQUIRE(node != nullptr);
+ REQUIRE(xmlNameEqual(node, "widget") == true);
+ node = XML::findFirstChildByName(node, "option");
+ REQUIRE(node != nullptr);
+ REQUIRE(xmlNameEqual(node, "option") == true);
+ REQUIRE(XML::getProperty(node, "name", "") == "padding");
+ REQUIRE(XML::langProperty(node, "name", "") == "padding");
+ REQUIRE(XML::langProperty(node, "name123", "") == "");
+ REQUIRE(XML::getProperty(node, "value", 0) == 1);
+ REQUIRE(XML::getProperty(node, "value123", -1) == -1);
+ REQUIRE(XML::getBoolProperty(node, "value", true) == true);
+ REQUIRE(XML::getBoolProperty(node, "value", false) == false);
+ REQUIRE(XML::getBoolProperty(node, "value123", true) == true);
+ REQUIRE(XML::getIntProperty(node, "value", -1, -10, 100) == 1);
+ REQUIRE(XML::getIntProperty(node, "value123", -1, -10, 100) == -1);
+ }
+
+ SECTION("child2")
+ {
+ const std::string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+ "<root><data option1=\"false\" option2=\"true\" "
+ "option3=\"10.5\"/></root>";
+ XML::Document doc(xml.c_str(), xml.size());
+ const XmlNodePtr rootNode = doc.rootNode();
+ XmlNodePtr node = XML::findFirstChildByName(rootNode, "data");
+ REQUIRE(node != nullptr);
+ REQUIRE(xmlNameEqual(node, "data") == true);
+ REQUIRE(XML::getBoolProperty(node, "option1", true) == false);
+ REQUIRE(XML::getBoolProperty(node, "option2", false) == true);
+ const float opt3 = XML::getFloatProperty(node, "option3", 0.0);
+ REQUIRE(opt3 > 10);
+ REQUIRE(opt3 < 11);
+ }
+
+ SECTION("validate")
+ {
+// REQUIRE(XML::Document::validateXml(
+// "graphics/gui/browserbox.xml") == true);
+ REQUIRE(XML::Document::validateXml(
+ "graphics/gui/bubble.png") == false);
+ REQUIRE(XML::Document::validateXml(
+ "graphics/gui/testfile123.xml") == false);
+ }
+}