Fix QJsonValue comparison.

QJsonValue, while comparing two QJsonArrays, should consult also length
of the arrays, because a different than null base pointer doesn't mean
that an array is not empty.

Change-Id: If76739355a4e74b842e836289565f98d95c006d5
Reviewed-by: Lars Knoll <lars.knoll@digia.com>
This commit is contained in:
Jędrzej Nowacki 2014-07-08 14:56:25 +02:00
parent 77d7348be2
commit 364cf0bf20
2 changed files with 59 additions and 2 deletions

View File

@ -606,8 +606,10 @@ bool QJsonValue::operator==(const QJsonValue &other) const
case Array:
if (base == other.base)
return true;
if (!base || !other.base)
return false;
if (!base)
return !other.base->length;
if (!other.base)
return !base->length;
return QJsonArray(d, static_cast<QJsonPrivate::Array *>(base))
== QJsonArray(other.d, static_cast<QJsonPrivate::Array *>(other.base));
case Object:

View File

@ -139,6 +139,8 @@ private Q_SLOTS:
void valueEquals();
void objectEquals_data();
void objectEquals();
void arrayEquals_data();
void arrayEquals();
void bom();
void nesting();
@ -2460,6 +2462,59 @@ void tst_QtJson::objectEquals()
QCOMPARE(QJsonValue(right) != QJsonValue(left), !result);
}
void tst_QtJson::arrayEquals_data()
{
QTest::addColumn<QJsonArray>("left");
QTest::addColumn<QJsonArray>("right");
QTest::addColumn<bool>("result");
QTest::newRow("two defaults") << QJsonArray() << QJsonArray() << true;
QJsonArray array1;
array1.append(1);
QJsonArray array2;
array2.append(2111);
array2[0] = 1;
QJsonArray array3;
array3.insert(0, 1);
array3.insert(1, 2);
QTest::newRow("the same array (1 vs 2)") << array1 << array2 << true;
QTest::newRow("the same array (3 vs 3)") << array3 << array3 << true;
QTest::newRow("different arrays (2 vs 3)") << array2 << array3 << false;
QTest::newRow("array vs default") << array1 << QJsonArray() << false;
QJsonArray empty;
empty.append(1);
empty.takeAt(0);
QTest::newRow("default vs empty") << QJsonArray() << empty << true;
QTest::newRow("empty vs default") << empty << QJsonArray() << true;
QTest::newRow("empty vs empty") << empty << empty << true;
QTest::newRow("array vs empty") << array1 << empty << false;
}
void tst_QtJson::arrayEquals()
{
QFETCH(QJsonArray, left);
QFETCH(QJsonArray, 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);
// The same but from QJsonValue perspective
QCOMPARE(QJsonValue(left) == QJsonValue(right), result);
QCOMPARE(QJsonValue(left) != QJsonValue(right), !result);
QCOMPARE(QJsonValue(right) == QJsonValue(left), result);
QCOMPARE(QJsonValue(right) != QJsonValue(left), !result);
}
void tst_QtJson::bom()
{
QFile file(testDataDir + "/bom.json");