summaryrefslogtreecommitdiff
path: root/test/source/xmltesttools.cxx
diff options
context:
space:
mode:
authorTomaž Vajngerl <tomaz.vajngerl@collabora.com>2014-04-29 15:07:54 +0200
committerTomaž Vajngerl <tomaz.vajngerl@collabora.com>2014-04-29 23:57:33 +0200
commitdde833575e29afecb6b053d3be28d9d028a72bef (patch)
treed63db949265ca1577dfbcb416299033ac3b92d6e /test/source/xmltesttools.cxx
parentde9ebbaddcb9633380e6614d7e70c1007d3be1c1 (diff)
tests: add XML and HTML test tools to test module
XML and HTML parsing and XPath asserts are used by many test in sw, sc and sd modules. This commit moves this functionallity to test module, where it is available to all modules. Change-Id: I53ad9c45b24f4c9bf106cb58ea619002968bfeda
Diffstat (limited to 'test/source/xmltesttools.cxx')
-rw-r--r--test/source/xmltesttools.cxx124
1 files changed, 124 insertions, 0 deletions
diff --git a/test/source/xmltesttools.cxx b/test/source/xmltesttools.cxx
new file mode 100644
index 000000000000..98153518fcf7
--- /dev/null
+++ b/test/source/xmltesttools.cxx
@@ -0,0 +1,124 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#include <test/xmltesttools.hxx>
+
+XmlTestTools::XmlTestTools()
+{}
+
+XmlTestTools::~XmlTestTools()
+{}
+
+xmlNodeSetPtr XmlTestTools::getXPathNode(xmlDocPtr pXmlDoc, const OString& rXPath)
+{
+ xmlXPathContextPtr pXmlXpathCtx = xmlXPathNewContext(pXmlDoc);
+ registerNamespaces(pXmlXpathCtx);
+ xmlXPathObjectPtr pXmlXpathObj = xmlXPathEvalExpression(BAD_CAST(rXPath.getStr()), pXmlXpathCtx);
+ return pXmlXpathObj->nodesetval;
+}
+
+void XmlTestTools::registerNamespaces(xmlXPathContextPtr& /*pXmlXpathCtx*/)
+{
+}
+
+/**
+ * Same as the assertXPath(), but don't assert: return the string instead.
+ */
+OUString XmlTestTools::getXPath(xmlDocPtr pXmlDoc, const OString& rXPath, const OString& rAttribute)
+{
+ xmlNodeSetPtr pXmlNodes = getXPathNode(pXmlDoc, rXPath);
+ CPPUNIT_ASSERT_EQUAL_MESSAGE(OString("XPath '" + rXPath + "' number of nodes is incorrect").getStr(),
+ 1, xmlXPathNodeSetGetLength(pXmlNodes));
+ if (rAttribute.isEmpty())
+ return OUString();
+ xmlNodePtr pXmlNode = pXmlNodes->nodeTab[0];
+ return OUString::createFromAscii((const char*)xmlGetProp(pXmlNode, BAD_CAST(rAttribute.getStr())));
+}
+
+/**
+ * Same as the assertXPathContent(), but don't assert: return the string instead.
+ */
+OUString XmlTestTools::getXPathContent(xmlDocPtr pXmlDoc, const OString& rXPath)
+{
+ xmlNodeSetPtr pXmlNodes = getXPathNode(pXmlDoc, rXPath);
+
+ CPPUNIT_ASSERT_MESSAGE(OString("XPath '" + rXPath + "' not found").getStr(),
+ xmlXPathNodeSetGetLength(pXmlNodes) > 0);
+
+ xmlNodePtr pXmlNode = pXmlNodes->nodeTab[0];
+ return OUString::createFromAscii((const char*)((pXmlNode->children[0]).content));
+}
+
+/**
+ * Assert that rXPath exists, and returns exactly one node.
+ * In case rAttribute is provided, the rXPath's attribute's value must
+ * equal to the rExpected value.
+ */
+void XmlTestTools::assertXPath(xmlDocPtr pXmlDoc, const OString& rXPath, const OString& rAttribute, const OUString& rExpectedValue)
+{
+ OUString aValue = getXPath(pXmlDoc, rXPath, rAttribute);
+ CPPUNIT_ASSERT_EQUAL_MESSAGE(OString("Attribute '" + rAttribute + "' of '" + rXPath + "' incorrect value.").getStr(),
+ rExpectedValue, aValue);
+}
+
+/**
+ * Assert that rXPath exists, and returns exactly nNumberOfNodes nodes.
+ * Useful for checking that we do _not_ export some node (nNumberOfNodes == 0).
+ */
+void XmlTestTools::assertXPath(xmlDocPtr pXmlDoc, const OString& rXPath, int nNumberOfNodes)
+{
+ xmlNodeSetPtr pXmlNodes = getXPathNode(pXmlDoc, rXPath);
+ CPPUNIT_ASSERT_EQUAL_MESSAGE(OString("XPath '" + rXPath + "' number of nodes is incorrect").getStr(),
+ nNumberOfNodes, xmlXPathNodeSetGetLength(pXmlNodes));
+}
+
+/**
+ * Assert that rXPath exists, and its content equals rContent.
+ */
+void XmlTestTools::assertXPathContent(xmlDocPtr pXmlDoc, const OString& rXPath, const OUString& rContent)
+{
+ CPPUNIT_ASSERT_EQUAL_MESSAGE("XPath contents of child does not match", rContent, getXPathContent(pXmlDoc, rXPath));
+}
+
+/**
+ * Assert that rXPath exists, and has exactly nNumberOfChildNodes child nodes.
+ * Useful for checking that we do have a no child nodes to a specific node (nNumberOfChildNodes == 0).
+ */
+void XmlTestTools::assertXPathChildren(xmlDocPtr pXmlDoc, const OString& rXPath, int nNumberOfChildNodes)
+{
+ xmlNodeSetPtr pXmlNodes = getXPathNode(pXmlDoc, rXPath);
+ CPPUNIT_ASSERT_EQUAL_MESSAGE(OString("XPath '" + rXPath + "' number of nodes is incorrect").getStr(),
+ 1, xmlXPathNodeSetGetLength(pXmlNodes));
+ xmlNodePtr pXmlNode = pXmlNodes->nodeTab[0];
+ CPPUNIT_ASSERT_EQUAL_MESSAGE(OString("XPath '" + rXPath + "' number of child-nodes is incorrect").getStr(),
+ nNumberOfChildNodes, (int)xmlChildElementCount(pXmlNode));
+}
+
+/**
+ * Get the position of the child named rName of the parent node specified by rXPath.
+ * Useful for checking relative order of elements.
+ */
+int XmlTestTools::getXPathPosition(xmlDocPtr pXmlDoc, const OString& rXPath, const OUString& rChildName)
+{
+ xmlNodeSetPtr pXmlNodes = getXPathNode(pXmlDoc, rXPath);
+ CPPUNIT_ASSERT_EQUAL_MESSAGE(OString("XPath '" + rXPath + "' number of nodes is incorrect").getStr(),
+ 1,
+ xmlXPathNodeSetGetLength(pXmlNodes));
+ xmlNodePtr pXmlNode = pXmlNodes->nodeTab[0];
+ int nRet = 0;
+ for (xmlNodePtr pChild = pXmlNode->children; pChild; pChild = pChild->next)
+ {
+ if (OUString::createFromAscii((const char*)pChild->name) == rChildName)
+ break;
+ ++nRet;
+ }
+ return nRet;
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */