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>
This commit is contained in:
Rym Bouabid 2024-02-14 16:15:19 +01:00
parent ddcbf02d20
commit cd67684c89
5 changed files with 88 additions and 22 deletions

View File

@ -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

View File

@ -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
{
if (d == other.d)
return true;
if (d && other.d)
// keep in sync with qHash(QUrlQuery):
return d->valueDelimiter == other.d->valueDelimiter &&
d->pairDelimiter == other.d->pairDelimiter &&
d->itemList == other.d->itemList;
const QUrlQueryPrivate *x = d ? d.data() : other.d.data();
return x->valueDelimiter == defaultQueryValueDelimiter() &&
x->pairDelimiter == defaultQueryPairDelimiter() &&
bool comparesEqual(const QUrlQuery &lhs, const QUrlQuery &rhs)
{
if (lhs.d == rhs.d)
return true;
if (lhs.d && rhs.d)
// keep in sync with qHash(QUrlQuery):
return lhs.d->valueDelimiter == rhs.d->valueDelimiter &&
lhs.d->pairDelimiter == rhs.d->pairDelimiter &&
lhs.d->itemList == rhs.d->itemList;
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==()
*/

View File

@ -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;

View File

@ -16,4 +16,5 @@ qt_internal_add_test(tst_qurlquery
tst_qurlquery.cpp
LIBRARIES
Qt::CorePrivate
Qt::TestPrivate
)

View File

@ -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);
}
{