QJsonArray: use new comparison helper macros

Replace public operators operator==(), operator!=() of
QJsonArray to friend methods comparesEqual().

Use QT_CORE_REMOVED_SINCE and removed_api.cpp to get rid of current
comparison methods and replace them with a friend.

Add friend method comparesEqual(QJsonArray, QJsonValue)
to the QJsonArray class, to support comparison between QJsonArray
and QJsonValue elements, see test-case fromToVariantConversions()

Task-number: QTBUG-120300
Change-Id: I8440ca0761bede8551ff792bfa7f22e47b56fa79
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Tatiana Borisova 2024-03-15 17:23:16 +01:00
parent 587003c3cc
commit 2499de8874
5 changed files with 88 additions and 18 deletions

View File

@ -959,6 +959,18 @@ bool QFileInfo::operator==(const QFileInfo &fileinfo) const
return comparesEqual(*this, fileinfo); return comparesEqual(*this, fileinfo);
} }
#include "qjsonarray.h"
bool QJsonArray::operator==(const QJsonArray &other) const
{
return comparesEqual(*this, other);
}
bool QJsonArray::operator!=(const QJsonArray &other) const
{
return !comparesEqual(*this, other);
}
#if QT_CONFIG(processenvironment) #if QT_CONFIG(processenvironment)
#include "qprocess.h" // inlined API #include "qprocess.h" // inlined API

View File

@ -28,6 +28,10 @@ QT_BEGIN_NAMESPACE
\brief The QJsonArray class encapsulates a JSON array. \brief The QJsonArray class encapsulates a JSON array.
\compares equality
\compareswith equality QJsonValue
\endcompareswith
A JSON array is a list of values. The list can be manipulated by inserting and A JSON array is a list of values. The list can be manipulated by inserting and
removing QJsonValue's from the array. removing QJsonValue's from the array.
@ -471,36 +475,40 @@ QJsonValue QJsonArray::operator[](qsizetype i) const
return at(i); return at(i);
} }
/*! bool comparesEqual(const QJsonArray &lhs, const QJsonArray &rhs) noexcept
Returns \c true if this array is equal to \a other.
*/
bool QJsonArray::operator==(const QJsonArray &other) const
{ {
if (a == other.a) if (lhs.a == rhs.a)
return true; return true;
if (!a) if (!lhs.a)
return !other.a->elements.size(); return !rhs.a->elements.size();
if (!other.a) if (!rhs.a)
return !a->elements.size(); return !lhs.a->elements.size();
if (a->elements.size() != other.a->elements.size()) if (lhs.a->elements.size() != rhs.a->elements.size())
return false; return false;
for (qsizetype i = 0; i < a->elements.size(); ++i) { for (qsizetype i = 0; i < lhs.a->elements.size(); ++i) {
if (a->valueAt(i) != other.a->valueAt(i)) if (lhs.a->valueAt(i) != rhs.a->valueAt(i))
return false; return false;
} }
return true; return true;
} }
/*! bool comparesEqual(const QJsonArray &lhs, const QJsonValue &rhs) noexcept
Returns \c true if this array is not equal to \a other.
*/
bool QJsonArray::operator!=(const QJsonArray &other) const
{ {
return !(*this == other); return lhs == rhs.toArray();
} }
/*! \fn bool QJsonArray::operator==(const QJsonArray &lhs, const QJsonArray &rhs)
Returns \c true if \a lhs array is equal to \a rhs, \c false otherwise.
*/
/*! \fn bool QJsonArray::operator!=(const QJsonArray &lhs, const QJsonArray &rhs)
Returns \c true if \a lhs array is not equal to \a rhs, \c false otherwise.
*/
/*! \fn QJsonArray::iterator QJsonArray::begin() /*! \fn QJsonArray::iterator QJsonArray::begin()
Returns an \l{STL-style iterators}{STL-style iterator} pointing to the first item in Returns an \l{STL-style iterators}{STL-style iterator} pointing to the first item in

View File

@ -60,9 +60,10 @@ public:
QJsonValueRef operator[](qsizetype i); QJsonValueRef operator[](qsizetype i);
QJsonValue operator[](qsizetype i) const; QJsonValue operator[](qsizetype i) const;
#if QT_CORE_REMOVED_SINCE(6, 8)
bool operator==(const QJsonArray &other) const; bool operator==(const QJsonArray &other) const;
bool operator!=(const QJsonArray &other) const; bool operator!=(const QJsonArray &other) const;
#endif
void swap(QJsonArray &other) noexcept void swap(QJsonArray &other) noexcept
{ {
a.swap(other.a); a.swap(other.a);
@ -225,6 +226,14 @@ private:
friend class QCborArray; friend class QCborArray;
friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonArray &); friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonArray &);
friend Q_CORE_EXPORT bool comparesEqual(const QJsonArray &lhs,
const QJsonArray &rhs) noexcept;
friend Q_CORE_EXPORT bool comparesEqual(const QJsonArray &lhs,
const QJsonValue &rhs) noexcept;
Q_DECLARE_EQUALITY_COMPARABLE(QJsonArray)
Q_DECLARE_EQUALITY_COMPARABLE(QJsonArray, QJsonValue)
QJsonArray(QCborContainerPrivate *array); QJsonArray(QCborContainerPrivate *array);
bool detach(qsizetype reserve = 0); bool detach(qsizetype reserve = 0);

View File

@ -26,6 +26,7 @@ qt_internal_add_test(tst_json
tst_qtjson.cpp tst_qtjson.cpp
LIBRARIES LIBRARIES
Qt::CorePrivate Qt::CorePrivate
Qt::TestPrivate
TESTDATA ${json_resource_files} TESTDATA ${json_resource_files}
) )

View File

@ -3,6 +3,7 @@
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QTest> #include <QTest>
#include <QtTest/private/qcomparisontesthelper_p.h>
#include <QMap> #include <QMap>
#include <QVariantList> #include <QVariantList>
@ -27,6 +28,7 @@ class tst_QtJson: public QObject
private Q_SLOTS: private Q_SLOTS:
void initTestCase(); void initTestCase();
void compareCompiles();
void testValueSimple(); void testValueSimple();
void testNumbers(); void testNumbers();
void testNumbers_2(); void testNumbers_2();
@ -47,6 +49,8 @@ private Q_SLOTS:
void testArrayNested(); void testArrayNested();
void testArrayNestedEmpty(); void testArrayNestedEmpty();
void testArrayComfortOperators(); void testArrayComfortOperators();
void testArrayEquality_data();
void testArrayEquality();
void testObjectNestedEmpty(); void testObjectNestedEmpty();
void testValueRef(); void testValueRef();
@ -169,6 +173,12 @@ void tst_QtJson::initTestCase()
testDataDir = QCoreApplication::applicationDirPath(); testDataDir = QCoreApplication::applicationDirPath();
} }
void tst_QtJson::compareCompiles()
{
QTestPrivate::testEqualityOperatorsCompile<QJsonArray>();
QTestPrivate::testEqualityOperatorsCompile<QJsonArray, QJsonValue>();
}
void tst_QtJson::testValueSimple() void tst_QtJson::testValueSimple()
{ {
QJsonObject object; QJsonObject object;
@ -901,6 +911,36 @@ void tst_QtJson::testObjectNestedEmpty()
QCOMPARE(object.value("inner").type(), QJsonValue::Object); QCOMPARE(object.value("inner").type(), QJsonValue::Object);
} }
void tst_QtJson::testArrayEquality_data()
{
QTest::addColumn<QJsonArray>("array1");
QTest::addColumn<QJsonArray>("array2");
QTest::addColumn<bool>("expectedResult");
QTest::addRow("QJsonArray(), QJsonArray{665, 666, 667}")
<< QJsonArray() << QJsonArray{665, 666, 667} << false;
QTest::addRow("QJsonArray(), QJsonArray{}")
<< QJsonArray() << QJsonArray{} <<true;
QTest::addRow("QJsonArray(), QJsonArray{123, QLatin1String(\"foo\")}")
<< QJsonArray() << QJsonArray{123, QLatin1String("foo")} << false;
QTest::addRow(
"QJsonArray{123,QLatin1String(\"foo\")}, QJsonArray{123,QLatin1String(\"foo\")}")
<< QJsonArray{123, QLatin1String("foo")}
<< QJsonArray{123, QLatin1String("foo")}
<< true;
}
void tst_QtJson::testArrayEquality()
{
QFETCH(QJsonArray, array1);
QFETCH(QJsonArray, array2);
QFETCH(bool, expectedResult);
QJsonValue value = QJsonValue(array1);
QT_TEST_EQUALITY_OPS(array1, array2, expectedResult);
QT_TEST_EQUALITY_OPS(value, array2, expectedResult);
}
void tst_QtJson::testArrayComfortOperators() void tst_QtJson::testArrayComfortOperators()
{ {
QJsonArray first; QJsonArray first;