From d822953cbc1d8814ac9f9eac2107177d37103542 Mon Sep 17 00:00:00 2001 From: Mike Kaganski <mike.kaganski@collabora.com> Date: Wed, 1 May 2019 09:17:14 +0300 Subject: Make getXPathPosition assert on requested child found Previously, for an XML like this: <sharedItems> <d v="2017-07-10T09:11:02"/> <d v="2017-07-10T09:11:03"/> <m/> </sharedItems> the call like int pos = getXPathPosition(pDoc, "/x:sharedItems", "absent"); gave 3. That could result in mistakes, when a test would assert on position "3" for a child element which name is mistyped. I made such a mistake when creating a unit test trying to assert on a position of the last element, and writing its name as "x:m", like in rXPath itself; the return was 3, and I initially wrongly assumed that the return is 1-based (like in xpath bracketed expressions). rChildName made const OString&, for consistency with rXPath, or with rAttribute in getXPath: child name is just a part of a longer xpath. Change-Id: I7ba9c4466c75b1b10fce1ccf26ef3b56b4e11e87 Reviewed-on: https://gerrit.libreoffice.org/71614 Tested-by: Jenkins Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com> --- test/source/xmltesttools.cxx | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'test/source/xmltesttools.cxx') diff --git a/test/source/xmltesttools.cxx b/test/source/xmltesttools.cxx index 20149cc03c37..3c186dd08d63 100644 --- a/test/source/xmltesttools.cxx +++ b/test/source/xmltesttools.cxx @@ -28,6 +28,11 @@ OUString convert(xmlChar const * string) { return s; } +OString oconvert(xmlChar const * string) +{ + return reinterpret_cast<char const *>(string); +} + } XmlTestTools::XmlTestTools() @@ -227,7 +232,7 @@ void XmlTestTools::assertXPathNoAttribute(xmlDocPtr pXmlDoc, const OString& rXPa xmlXPathFreeObject(pXmlObj); } -int XmlTestTools::getXPathPosition(xmlDocPtr pXmlDoc, const OString& rXPath, const OUString& rChildName) +int XmlTestTools::getXPathPosition(xmlDocPtr pXmlDoc, const OString& rXPath, const OString& rChildName) { xmlXPathObjectPtr pXmlObj = getXPathNode(pXmlDoc, rXPath); xmlNodeSetPtr pXmlNodes = pXmlObj->nodesetval; @@ -236,13 +241,21 @@ int XmlTestTools::getXPathPosition(xmlDocPtr pXmlDoc, const OString& rXPath, con xmlXPathNodeSetGetLength(pXmlNodes)); xmlNodePtr pXmlNode = pXmlNodes->nodeTab[0]; int nRet = 0; + bool bFound = false; for (xmlNodePtr pChild = pXmlNode->children; pChild; pChild = pChild->next) { - if (convert(pChild->name) == rChildName) + if (oconvert(pChild->name) == rChildName) + { + bFound = true; break; + } ++nRet; } xmlXPathFreeObject(pXmlObj); + CPPUNIT_ASSERT_MESSAGE(OString("In <" + OString(pXmlDoc->name) + ">, XPath '" + rXPath + + "' child '" + rChildName + "' not found") + .getStr(), + bFound); return nRet; } -- cgit