summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRym Bouabid <rym.bouabid@qt.io>2024-02-14 16:15:19 +0100
committerRym Bouabid <rym.bouabid@qt.io>2024-02-23 19:46:02 +0100
commitcd67684c89ab6e8b951778c317d459124e6d9c5d (patch)
tree5359934955503ec164009cf2edee22418344c61f
parentddcbf02d2004baab53c88a5661b5350942ebff75 (diff)
QUrlQuery: Use new comparison helper macros
QUrlQuery had operator==() and operator!=() defined as public member functions, so use QT_CORE_REMOVED_SINCE and removed_api.cpp to get rid of these methods and replace them with a hidden friend. Use QT_TEST_ALL_EQUALITY_OPS macro in unit-tests. Use new \compares command in the documentation to describe the comparison operators provided by QUrlQuery. Task-number: QTBUG-120303 Change-Id: I083487a134887010ebbb78906d2c1982f2ad41b5 Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
-rw-r--r--src/corelib/compat/removed_api.cpp7
-rw-r--r--src/corelib/io/qurlquery.cpp30
-rw-r--r--src/corelib/io/qurlquery.h7
-rw-r--r--tests/auto/corelib/io/qurlquery/CMakeLists.txt1
-rw-r--r--tests/auto/corelib/io/qurlquery/tst_qurlquery.cpp59
5 files changed, 85 insertions, 19 deletions
diff --git a/src/corelib/compat/removed_api.cpp b/src/corelib/compat/removed_api.cpp
index 405f22e3d9..a2d8d6f63e 100644
--- a/src/corelib/compat/removed_api.cpp
+++ b/src/corelib/compat/removed_api.cpp
@@ -947,6 +947,13 @@ bool QProcessEnvironment::operator==(const QProcessEnvironment &other) const
}
#endif // QT_CONFIG(processenvironment)
+#include "qurlquery.h"
+
+bool QUrlQuery::operator==(const QUrlQuery &other) const
+{
+ return comparesEqual(*this, other);
+}
+
// #include "qotherheader.h"
// // implement removed functions from qotherheader.h
// order sections alphabetically to reduce chances of merge conflicts
diff --git a/src/corelib/io/qurlquery.cpp b/src/corelib/io/qurlquery.cpp
index 1258c00ec2..31f3ee1d90 100644
--- a/src/corelib/io/qurlquery.cpp
+++ b/src/corelib/io/qurlquery.cpp
@@ -24,6 +24,8 @@ QT_BEGIN_NAMESPACE
\ingroup network
\ingroup shared
+ \compares equality
+
It is used to parse the query strings found in URLs like the following:
\image qurl-querystring.png
@@ -399,22 +401,25 @@ QUrlQuery::~QUrlQuery()
}
/*!
- Returns \c true if this object and the \a other object contain the same
+ \fn bool QUrlQuery::operator==(const QUrlQuery &lhs, const QUrlQuery &rhs)
+
+ Returns \c true if QUrlQuery objects \a lhs and \a rhs contain the same
contents, in the same order, and use the same query delimiters.
*/
-bool QUrlQuery::operator ==(const QUrlQuery &other) const
+
+bool comparesEqual(const QUrlQuery &lhs, const QUrlQuery &rhs)
{
- if (d == other.d)
+ if (lhs.d == rhs.d)
return true;
- if (d && other.d)
+ if (lhs.d && rhs.d)
// keep in sync with qHash(QUrlQuery):
- return d->valueDelimiter == other.d->valueDelimiter &&
- d->pairDelimiter == other.d->pairDelimiter &&
- d->itemList == other.d->itemList;
+ return lhs.d->valueDelimiter == rhs.d->valueDelimiter &&
+ lhs.d->pairDelimiter == rhs.d->pairDelimiter &&
+ lhs.d->itemList == rhs.d->itemList;
- const QUrlQueryPrivate *x = d ? d.data() : other.d.data();
- return x->valueDelimiter == defaultQueryValueDelimiter() &&
- x->pairDelimiter == defaultQueryPairDelimiter() &&
+ const QUrlQueryPrivate *x = lhs.d ? lhs.d.data() : rhs.d.data();
+ return x->valueDelimiter == QUrlQuery::defaultQueryValueDelimiter() &&
+ x->pairDelimiter == QUrlQuery::defaultQueryPairDelimiter() &&
x->itemList.isEmpty();
}
@@ -810,9 +815,10 @@ void QUrlQuery::removeAllQueryItems(const QString &key)
*/
/*!
- \fn bool QUrlQuery::operator!=(const QUrlQuery &other) const
+ \fn bool QUrlQuery::operator!=(const QUrlQuery &lhs, const QUrlQuery &rhs)
- Returns \c true if \a other is not equal to this QUrlQuery. Otherwise, returns \c false.
+ Returns \c true if the QUrlQuery object \a rhs is not equal to \a lhs.
+ Otherwise, returns \c false.
\sa operator==()
*/
diff --git a/src/corelib/io/qurlquery.h b/src/corelib/io/qurlquery.h
index de2cdf4940..061107606e 100644
--- a/src/corelib/io/qurlquery.h
+++ b/src/corelib/io/qurlquery.h
@@ -5,6 +5,7 @@
#ifndef QURLQUERY_H
#define QURLQUERY_H
+#include <QtCore/qcompare.h>
#include <QtCore/qshareddata.h>
#include <QtCore/qurl.h>
@@ -34,9 +35,11 @@ public:
QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QUrlQuery)
~QUrlQuery();
+#if QT_CORE_REMOVED_SINCE(6, 8)
bool operator==(const QUrlQuery &other) const;
bool operator!=(const QUrlQuery &other) const
- { return !(*this == other); }
+ { return !operator==(other); }
+#endif
void swap(QUrlQuery &other) noexcept { d.swap(other.d); }
@@ -67,6 +70,8 @@ public:
static constexpr char16_t defaultQueryPairDelimiter() noexcept { return u'&'; }
private:
+ friend Q_CORE_EXPORT bool comparesEqual(const QUrlQuery &lhs, const QUrlQuery &rhs);
+ Q_DECLARE_EQUALITY_COMPARABLE(QUrlQuery)
friend class QUrl;
friend Q_CORE_EXPORT size_t qHash(const QUrlQuery &key, size_t seed) noexcept;
QSharedDataPointer<QUrlQueryPrivate> d;
diff --git a/tests/auto/corelib/io/qurlquery/CMakeLists.txt b/tests/auto/corelib/io/qurlquery/CMakeLists.txt
index 8b2a48a03a..c9f5491416 100644
--- a/tests/auto/corelib/io/qurlquery/CMakeLists.txt
+++ b/tests/auto/corelib/io/qurlquery/CMakeLists.txt
@@ -16,4 +16,5 @@ qt_internal_add_test(tst_qurlquery
tst_qurlquery.cpp
LIBRARIES
Qt::CorePrivate
+ Qt::TestPrivate
)
diff --git a/tests/auto/corelib/io/qurlquery/tst_qurlquery.cpp b/tests/auto/corelib/io/qurlquery/tst_qurlquery.cpp
index 12d090fa16..8a799fbf94 100644
--- a/tests/auto/corelib/io/qurlquery/tst_qurlquery.cpp
+++ b/tests/auto/corelib/io/qurlquery/tst_qurlquery.cpp
@@ -2,8 +2,10 @@
// Copyright (C) 2012 Intel Corporation.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
-#include <QtCore/QUrlQuery>
#include <QTest>
+#include <QtTest/private/qcomparisontesthelper_p.h>
+
+#include <QtCore/QUrlQuery>
typedef QList<QPair<QString, QString> > QueryItems;
Q_DECLARE_METATYPE(QueryItems)
@@ -22,6 +24,9 @@ public:
}
private Q_SLOTS:
+ void compareCompiles();
+ void compareEquality_data();
+ void compareEquality();
void constructing();
void addRemove();
void multiAddRemove();
@@ -119,6 +124,48 @@ static QUrlQuery emptyQuery()
return QUrlQuery();
}
+void tst_QUrlQuery::compareCompiles()
+{
+ QTestPrivate::testEqualityOperatorsCompile<QUrlQuery>();
+}
+
+void tst_QUrlQuery::compareEquality_data()
+{
+ QTest::addColumn<QUrlQuery>("url1");
+ QTest::addColumn<QUrlQuery>("url2");
+ QTest::addColumn<bool>("equal");
+
+ QTest::newRow("empty-empty") << QUrlQuery() << QUrlQuery() << true;
+
+ QUrlQuery notEmpty;
+ notEmpty.addQueryItem("a", "b");
+ QTest::newRow("empty-notEmpty") << QUrlQuery() << notEmpty << false;
+
+ QUrlQuery notEmpty_copy = notEmpty;
+ QTest::newRow("sameItems") << notEmpty_copy << notEmpty << true;
+
+ QUrlQuery notEmpty_modified = notEmpty;
+ notEmpty_modified.addQueryItem("c", "d");
+ QTest::newRow("addedItems") << notEmpty_copy << notEmpty_modified << false;
+
+ QUrlQuery notEmpty2;
+ notEmpty2.addQueryItem("c", "d");
+ QTest::newRow("differentItems") << notEmpty2 << notEmpty << false;
+
+ QUrlQuery differentPairDelimiters;
+ differentPairDelimiters.setQueryDelimiters('(', ')');
+ QTest::newRow("defaultDelimiters-differentDelimiters") << QUrlQuery() << differentPairDelimiters
+ << false;
+}
+
+void tst_QUrlQuery::compareEquality()
+{
+ QFETCH(QUrlQuery, url1);
+ QFETCH(QUrlQuery, url2);
+ QFETCH(bool, equal);
+ QT_TEST_EQUALITY_OPS(url1, url2, equal);
+}
+
void tst_QUrlQuery::constructing()
{
QUrlQuery empty;
@@ -137,7 +184,7 @@ void tst_QUrlQuery::constructing()
QVERIFY(!copy.isDetached());
QCOMPARE(copy, empty);
QCOMPARE(qHash(copy), qHash(empty));
- QVERIFY(!(copy != empty));
+ QT_TEST_EQUALITY_OPS(copy, empty, true);
copy = empty;
QCOMPARE(copy, empty);
@@ -170,7 +217,7 @@ void tst_QUrlQuery::constructing()
QVERIFY(!other.isEmpty());
QVERIFY(other.isDetached());
QCOMPARE_NE(other, empty);
- QVERIFY(!(other == empty));
+ QT_TEST_EQUALITY_OPS(other, empty, false);
// copy-construct
QUrlQuery copy(other);
@@ -276,7 +323,7 @@ void tst_QUrlQuery::addRemove()
QVERIFY(allItems.contains(qItem("c", "d")));
QCOMPARE_NE(query, original);
- QVERIFY(!(query == original));
+ QT_TEST_EQUALITY_OPS(query, original, false);
}
{
@@ -299,7 +346,7 @@ void tst_QUrlQuery::addRemove()
QCOMPARE(allItems.at(0).second, QString("b"));
QCOMPARE(query, original);
- QVERIFY(!(query != original));
+ QT_TEST_EQUALITY_OPS(query, original, true);
QCOMPARE(qHash(query), qHash(original));
}
@@ -323,7 +370,7 @@ void tst_QUrlQuery::addRemove()
QVERIFY(allItems.contains(qItem("e", emptyButNotNull)));
QCOMPARE_NE(query, original);
- QVERIFY(!(query == original));
+ QT_TEST_EQUALITY_OPS(query, original, false);
}
{