summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/json/tst_qtjson.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tests/auto/corelib/json/tst_qtjson.cpp')
-rw-r--r--tests/auto/corelib/json/tst_qtjson.cpp70
1 files changed, 70 insertions, 0 deletions
diff --git a/tests/auto/corelib/json/tst_qtjson.cpp b/tests/auto/corelib/json/tst_qtjson.cpp
index a17fe7561a..761da2b0a3 100644
--- a/tests/auto/corelib/json/tst_qtjson.cpp
+++ b/tests/auto/corelib/json/tst_qtjson.cpp
@@ -132,6 +132,7 @@ private Q_SLOTS:
void testTrailingComma();
void testDetachBug();
+ void testJsonValueRefDefault();
void valueEquals();
@@ -139,6 +140,8 @@ private Q_SLOTS:
void nesting();
void longStrings();
+
+ void arrayInitializerList();
private:
QString testDataDir;
};
@@ -2425,5 +2428,72 @@ void tst_QtJson::longStrings()
}
}
+void tst_QtJson::testJsonValueRefDefault()
+{
+ QJsonObject empty;
+
+ QCOMPARE(empty["n/a"].toString(), QString());
+ QCOMPARE(empty["n/a"].toString("default"), QStringLiteral("default"));
+
+ QCOMPARE(empty["n/a"].toBool(), false);
+ QCOMPARE(empty["n/a"].toBool(true), true);
+
+ QCOMPARE(empty["n/a"].toInt(), 0);
+ QCOMPARE(empty["n/a"].toInt(42), 42);
+
+ QCOMPARE(empty["n/a"].toDouble(), 0.0);
+ QCOMPARE(empty["n/a"].toDouble(42.0), 42.0);
+}
+
+void tst_QtJson::arrayInitializerList()
+{
+#ifndef Q_COMPILER_INITIALIZER_LISTS
+ QSKIP("initializer_list is enabled only with c++11 support");
+#else
+ QVERIFY(QJsonArray{}.isEmpty());
+ QCOMPARE(QJsonArray{"one"}.count(), 1);
+ QCOMPARE(QJsonArray{1}.count(), 1);
+
+ {
+ QJsonArray a{1.3, "hello", 0};
+ QCOMPARE(QJsonValue(a[0]), QJsonValue(1.3));
+ QCOMPARE(QJsonValue(a[1]), QJsonValue("hello"));
+ QCOMPARE(QJsonValue(a[2]), QJsonValue(0));
+ QCOMPARE(a.count(), 3);
+ }
+ {
+ QJsonObject o;
+ o["property"] = 1;
+ QJsonArray a1 {o};
+ QCOMPARE(a1.count(), 1);
+ QCOMPARE(a1[0].toObject(), o);
+
+ QJsonArray a2 {o, 23};
+ QCOMPARE(a2.count(), 2);
+ QCOMPARE(a2[0].toObject(), o);
+ QCOMPARE(QJsonValue(a2[1]), QJsonValue(23));
+
+ QJsonArray a3 { a1, o, a2 };
+ QCOMPARE(QJsonValue(a3[0]), QJsonValue(a1));
+ QCOMPARE(QJsonValue(a3[1]), QJsonValue(o));
+ QCOMPARE(QJsonValue(a3[2]), QJsonValue(a2));
+
+ QJsonArray a4 { 1, QJsonArray{1,2,3}, QJsonArray{"hello", 2} };
+ QCOMPARE(a4.count(), 3);
+ QCOMPARE(QJsonValue(a4[0]), QJsonValue(1));
+
+ {
+ QJsonArray a41 = a4[1].toArray();
+ QJsonArray a42 = a4[2].toArray();
+ QCOMPARE(a41.count(), 3);
+ QCOMPARE(a42.count(), 2);
+
+ QCOMPARE(QJsonValue(a41[2]), QJsonValue(3));
+ QCOMPARE(QJsonValue(a42[1]), QJsonValue(2));
+ }
+ }
+#endif
+}
+
QTEST_MAIN(tst_QtJson)
#include "tst_qtjson.moc"