diff options
author | Jędrzej Nowacki <jedrzej.nowacki@digia.com> | 2014-05-21 12:28:27 +0200 |
---|---|---|
committer | Jędrzej Nowacki <jedrzej.nowacki@digia.com> | 2014-07-04 08:24:05 +0200 |
commit | 9c79f6dfb3b5724b52421f74fbee6f5c0005d0e3 (patch) | |
tree | ee4e2722c3bc40d99a95753a2cc6140b061ffaf5 /tests/auto/corelib/json/tst_qtjson.cpp | |
parent | 391b2accd87f02c9c0b1a4e766e50377d5918887 (diff) |
Add initializer list support in QJsonArray
It allows to create a QJsonArray instance in C++ by using
a similar expression to JSON. For example:
QJsonArray a = {1, 2, 4};
[ChangeLog][QtCore][QtJson] QJsonArray now supports
C++11 initializer lists.
Task-number: QTBUG-26606
Change-Id: Icc352e518d9649d24176c89e7113d200d5c50b0d
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'tests/auto/corelib/json/tst_qtjson.cpp')
-rw-r--r-- | tests/auto/corelib/json/tst_qtjson.cpp | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/tests/auto/corelib/json/tst_qtjson.cpp b/tests/auto/corelib/json/tst_qtjson.cpp index 8d8a3d7e0e..761da2b0a3 100644 --- a/tests/auto/corelib/json/tst_qtjson.cpp +++ b/tests/auto/corelib/json/tst_qtjson.cpp @@ -140,6 +140,8 @@ private Q_SLOTS: void nesting(); void longStrings(); + + void arrayInitializerList(); private: QString testDataDir; }; @@ -2443,5 +2445,55 @@ void tst_QtJson::testJsonValueRefDefault() 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" |