diff options
author | Marc Mutz <[email protected]> | 2024-11-04 16:45:17 +0100 |
---|---|---|
committer | Marc Mutz <[email protected]> | 2024-11-06 20:18:05 +0100 |
commit | 77d640aa0970ffc80c13cb1d8f2581abc84e2cd3 (patch) | |
tree | 7499abc71231794b34048b0015ac259b8bfe8b87 | |
parent | 3fce04157dbba1232d96821dbb166c6a5dc467a8 (diff) |
QDomNodeList: port to compare helpers
Just this one, for now, as an example, and to establish QtXml's
removed_api.cpp.
The QDomNode hierarchy will follow in a subsequent patch.
Task-number: QTBUG-130846
Change-Id: Ib095481cc57471b303cd8251194488d9875edcfb
Reviewed-by: Ivan Solovev <[email protected]>
-rw-r--r-- | src/xml/CMakeLists.txt | 3 | ||||
-rw-r--r-- | src/xml/compat/removed_api.cpp | 32 | ||||
-rw-r--r-- | src/xml/dom/qdom.cpp | 20 | ||||
-rw-r--r-- | src/xml/dom/qdom.h | 8 | ||||
-rw-r--r-- | tests/auto/xml/dom/qdom/CMakeLists.txt | 1 | ||||
-rw-r--r-- | tests/auto/xml/dom/qdom/tst_qdom.cpp | 64 |
6 files changed, 118 insertions, 10 deletions
diff --git a/src/xml/CMakeLists.txt b/src/xml/CMakeLists.txt index 65e13db7e7e..ef73bbc7e29 100644 --- a/src/xml/CMakeLists.txt +++ b/src/xml/CMakeLists.txt @@ -23,8 +23,11 @@ qt_internal_add_module(Xml qt_internal_extend_target(Xml CONDITION QT_FEATURE_dom SOURCES + compat/removed_api.cpp dom/qdom.cpp dom/qdom.h dom/qdom_p.h dom/qdomhelpers.cpp dom/qdomhelpers_p.h + NO_PCH_SOURCES + compat/removed_api.cpp ) qt_internal_extend_target(Xml CONDITION MSVC AND (TEST_architecture_arch STREQUAL "i386") diff --git a/src/xml/compat/removed_api.cpp b/src/xml/compat/removed_api.cpp new file mode 100644 index 00000000000..6214a5b3bd3 --- /dev/null +++ b/src/xml/compat/removed_api.cpp @@ -0,0 +1,32 @@ +// Copyright (C) 2024 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +#define QT_XML_BUILD_REMOVED_API + +#include "qtxmlglobal.h" + +QT_USE_NAMESPACE + +#if QT_XML_REMOVED_SINCE(6, 9) + +#if QT_CONFIG(dom) + +#include "qdom.h" + +bool QDomNodeList::operator==(const QDomNodeList &other) const +{ + return comparesEqual(*this, other); +} + +bool QDomNodeList::operator!=(const QDomNodeList &other) const +{ + return !comparesEqual(*this, other); +} + +#endif // QT_CONFIG(dom) + +// #include <qotherheader.h> +// // implement removed functions from qotherheader.h +// order sections alphabetically to reduce chances of merge conflicts + +#endif // QT_XML_REMOVED_SINCE(6, 9) diff --git a/src/xml/dom/qdom.cpp b/src/xml/dom/qdom.cpp index 64f32030c87..d7c3eb25868 100644 --- a/src/xml/dom/qdom.cpp +++ b/src/xml/dom/qdom.cpp @@ -794,26 +794,26 @@ QDomNodeList& QDomNodeList::operator=(const QDomNodeList &other) } /*! - Returns \c true if the node list \a other and this node list are equal; + \fn bool QDomNodeList::operator==(const QDomNodeList &lhs, const QDomNodeList &rhs) + + Returns \c true if the node lists \a lhs and \a rhs are equal; otherwise returns \c false. */ -bool QDomNodeList::operator==(const QDomNodeList &other) const +bool comparesEqual(const QDomNodeList &lhs, const QDomNodeList &rhs) noexcept { - if (impl == other.impl) + if (lhs.impl == rhs.impl) return true; - if (!impl || !other.impl) + if (!lhs.impl || !rhs.impl) return false; - return (*impl == *other.impl); + return *lhs.impl == *rhs.impl; } /*! - Returns \c true the node list \a other and this node list are not equal; + \fn bool QDomNodeList::operator!=(const QDomNodeList &lhs, const QDomNodeList &rhs) + + Returns \c true if the node lists \a lhs and \a rhs are not equal; otherwise returns \c false. */ -bool QDomNodeList::operator!=(const QDomNodeList &other) const -{ - return !operator==(other); -} /*! Destroys the object and frees its resources. diff --git a/src/xml/dom/qdom.h b/src/xml/dom/qdom.h index 0ac7d9130e9..22a8f41d8ca 100644 --- a/src/xml/dom/qdom.h +++ b/src/xml/dom/qdom.h @@ -5,6 +5,8 @@ #define QDOM_H #include <QtXml/qtxmlglobal.h> + +#include <QtCore/qcompare.h> #include <QtCore/qstring.h> #if QT_CONFIG(dom) @@ -215,8 +217,10 @@ public: QDomNodeList(); QDomNodeList(const QDomNodeList &nodeList); QDomNodeList& operator=(const QDomNodeList &other); +#if QT_XML_REMOVED_SINCE(6, 9) bool operator==(const QDomNodeList &other) const; bool operator!=(const QDomNodeList &other) const; +#endif ~QDomNodeList(); // DOM functions @@ -230,6 +234,10 @@ public: inline bool isEmpty() const { return length() == 0; } // Qt API consistency private: + Q_XML_EXPORT friend bool comparesEqual(const QDomNodeList &lhs, const QDomNodeList &rhs) noexcept; + Q_DECLARE_EQUALITY_COMPARABLE(QDomNodeList) + +private: QDomNodeListPrivate* impl; QDomNodeList(QDomNodeListPrivate*); diff --git a/tests/auto/xml/dom/qdom/CMakeLists.txt b/tests/auto/xml/dom/qdom/CMakeLists.txt index 4eea988cc32..f3c448f9c7f 100644 --- a/tests/auto/xml/dom/qdom/CMakeLists.txt +++ b/tests/auto/xml/dom/qdom/CMakeLists.txt @@ -25,5 +25,6 @@ qt_internal_add_test(tst_qdom LIBRARIES Qt::Xml Qt::XmlPrivate + Qt::TestPrivate TESTDATA ${test_data} ) diff --git a/tests/auto/xml/dom/qdom/tst_qdom.cpp b/tests/auto/xml/dom/qdom/tst_qdom.cpp index 3a6fc1356c3..a224b004117 100644 --- a/tests/auto/xml/dom/qdom/tst_qdom.cpp +++ b/tests/auto/xml/dom/qdom/tst_qdom.cpp @@ -2,6 +2,7 @@ // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <QtTest/qtest.h> +#include <QtTest/private/qcomparisontesthelper_p.h> #include <QtXml/qdom.h> @@ -115,6 +116,10 @@ private slots: void standalone(); void splitTextLeakMemory() const; + void testDomListComparisonCompiles(); + void testDomListComparison_data(); + void testDomListComparison(); + void cleanupTestCase() const; private: @@ -2337,5 +2342,64 @@ void tst_QDom::splitTextLeakMemory() const QCOMPARE(end.impl->ref.loadRelaxed(), 2); } +void tst_QDom::testDomListComparisonCompiles() +{ + QTestPrivate::testEqualityOperatorsCompile<QDomNodeList>(); +} + +static QDomElement findElementByName(const QDomDocument &doc, QLatin1StringView tag) +{ + const auto list = doc.elementsByTagName(tag); +#ifdef QTEST_THROW_ON_FAIL + QCOMPARE(list.size(), 1); + QCOMPARE(list.at(0).nodeType(), QDomNode::NodeType::ElementNode); +#endif + return list.at(0).toElement(); +} + +void tst_QDom::testDomListComparison_data() +{ + QTest::addColumn<QDomDocument>("doc"); + QTest::addColumn<QDomNodeList>("lhs"); + QTest::addColumn<QDomNodeList>("rhs"); + QTest::addColumn<bool>("result"); + + const auto xml = "<top><child1/><child2/><child3><cchild1/><cchild2/></child3></top>"_L1; + + QDomDocument doc; + const auto result = doc.setContent(xml); + QVERIFY2(result, result.errorMessage.toLocal8Bit().constData()); + + const QDomNodeList null; + const QDomNodeList empty = findElementByName(doc, "child1"_L1).childNodes(); + const QDomNodeList child3Children = findElementByName(doc, "child3"_L1).childNodes(); + const QDomNodeList topChildren = findElementByName(doc, "top"_L1).childNodes(); + +#define ROW(lhs, rhs, res) \ + QTest::addRow("%s <> %s", #lhs, #rhs) << doc << lhs << rhs << bool(res) + + ROW(null, null, true); + ROW(empty, empty, true); + ROW(child3Children, child3Children, true); + ROW(topChildren, topChildren, true); + + ROW(null, empty, false); + ROW(empty, child3Children, false); + ROW(child3Children, topChildren, false); + ROW(topChildren, null, false); +#undef ROW +} + +void tst_QDom::testDomListComparison() +{ + [[maybe_unused]] + QFETCH(const QDomDocument, doc); + QFETCH(const QDomNodeList, lhs); + QFETCH(const QDomNodeList, rhs); + QFETCH(const bool, result); + + QT_TEST_EQUALITY_OPS(lhs, rhs, result); +} + QTEST_MAIN(tst_QDom) #include "tst_qdom.moc" |