QJsonDocument: fix comparison of valid vs default
[ChangeLog][QtCore][QJsonDocument] Fixed a bug that caused QJsonDocument's equality operator to crash if one of the operands was default-constructed and the other wasn't. Pick-to: 5.15 Fixes: QTBUG-85969 Change-Id: I5e00996d7f4b4a10bc98fffd1629f835f570ef6b Reviewed-by: Sona Kurazyan <sona.kurazyan@qt.io>
This commit is contained in:
parent
d5ef011b73
commit
e790af0e0a
@ -495,7 +495,9 @@ const QJsonValue QJsonDocument::operator[](int i) const
|
||||
*/
|
||||
bool QJsonDocument::operator==(const QJsonDocument &other) const
|
||||
{
|
||||
return (!d) ? (!other.d) : (d->value == other.d->value);
|
||||
if (d && other.d)
|
||||
return d->value == other.d->value;
|
||||
return !d == !other.d;
|
||||
}
|
||||
|
||||
/*!
|
||||
|
@ -128,6 +128,8 @@ private Q_SLOTS:
|
||||
void objectEquals();
|
||||
void arrayEquals_data();
|
||||
void arrayEquals();
|
||||
void documentEquals_data();
|
||||
void documentEquals();
|
||||
|
||||
void bom();
|
||||
void nesting();
|
||||
@ -2575,6 +2577,12 @@ void tst_QtJson::objectEquals()
|
||||
QCOMPARE(QJsonValue(left) != QJsonValue(right), !result);
|
||||
QCOMPARE(QJsonValue(right) == QJsonValue(left), result);
|
||||
QCOMPARE(QJsonValue(right) != QJsonValue(left), !result);
|
||||
|
||||
// The same, but from a QJsonDocument perspective
|
||||
QCOMPARE(QJsonDocument(left) == QJsonDocument(right), result);
|
||||
QCOMPARE(QJsonDocument(left) != QJsonDocument(right), !result);
|
||||
QCOMPARE(QJsonDocument(right) == QJsonDocument(left), result);
|
||||
QCOMPARE(QJsonDocument(right) != QJsonDocument(left), !result);
|
||||
}
|
||||
|
||||
void tst_QtJson::arrayEquals_data()
|
||||
@ -2628,6 +2636,59 @@ void tst_QtJson::arrayEquals()
|
||||
QCOMPARE(QJsonValue(left) != QJsonValue(right), !result);
|
||||
QCOMPARE(QJsonValue(right) == QJsonValue(left), result);
|
||||
QCOMPARE(QJsonValue(right) != QJsonValue(left), !result);
|
||||
|
||||
// The same but from QJsonDocument perspective
|
||||
QCOMPARE(QJsonDocument(left) == QJsonDocument(right), result);
|
||||
QCOMPARE(QJsonDocument(left) != QJsonDocument(right), !result);
|
||||
QCOMPARE(QJsonDocument(right) == QJsonDocument(left), result);
|
||||
QCOMPARE(QJsonDocument(right) != QJsonDocument(left), !result);
|
||||
}
|
||||
|
||||
void tst_QtJson::documentEquals_data()
|
||||
{
|
||||
QTest::addColumn<QJsonDocument>("left");
|
||||
QTest::addColumn<QJsonDocument>("right");
|
||||
QTest::addColumn<bool>("result");
|
||||
|
||||
QTest::newRow("two defaults") << QJsonDocument() << QJsonDocument() << true;
|
||||
|
||||
QJsonDocument emptyobj(QJsonObject{});
|
||||
QJsonDocument emptyarr(QJsonArray{});
|
||||
QTest::newRow("emptyarray vs default") << emptyarr << QJsonDocument() << false;
|
||||
QTest::newRow("emptyobject vs default") << emptyobj << QJsonDocument() << false;
|
||||
QTest::newRow("emptyarray vs emptyobject") << emptyarr << emptyobj << false;
|
||||
|
||||
QJsonDocument array1(QJsonArray{1});
|
||||
QJsonDocument array2(QJsonArray{2});
|
||||
QTest::newRow("emptyarray vs emptyarray") << emptyarr << emptyarr << true;
|
||||
QTest::newRow("emptyarray vs array") << emptyarr << array1 << false;
|
||||
QTest::newRow("array vs array") << array1 << array1 << true;
|
||||
QTest::newRow("array vs otherarray") << array1 << array2 << false;
|
||||
|
||||
QJsonDocument object1(QJsonObject{{"hello", "world"}});
|
||||
QJsonDocument object2(QJsonObject{{"hello", 2}});
|
||||
QTest::newRow("emptyobject vs emptyobject") << emptyobj << emptyobj << true;
|
||||
QTest::newRow("emptyobject vs object") << emptyobj << object1 << false;
|
||||
QTest::newRow("object vs object") << object1 << object1 << true;
|
||||
QTest::newRow("object vs otherobject") << object1 << object2 << false;
|
||||
|
||||
QTest::newRow("object vs array") << array1 << object1 << false;
|
||||
}
|
||||
|
||||
void tst_QtJson::documentEquals()
|
||||
{
|
||||
QFETCH(QJsonDocument, left);
|
||||
QFETCH(QJsonDocument, right);
|
||||
QFETCH(bool, result);
|
||||
|
||||
QCOMPARE(left == right, result);
|
||||
QCOMPARE(right == left, result);
|
||||
|
||||
// invariants checks
|
||||
QCOMPARE(left, left);
|
||||
QCOMPARE(right, right);
|
||||
QCOMPARE(left != right, !result);
|
||||
QCOMPARE(right != left, !result);
|
||||
}
|
||||
|
||||
void tst_QtJson::bom()
|
||||
|
Loading…
x
Reference in New Issue
Block a user