diff options
author | Jędrzej Nowacki <jedrzej.nowacki@digia.com> | 2014-05-21 16:22:49 +0200 |
---|---|---|
committer | Jędrzej Nowacki <jedrzej.nowacki@digia.com> | 2014-07-30 10:20:28 +0200 |
commit | 1f4b958438a7b9a7f8f3fd8139d10f5adf215a8d (patch) | |
tree | 41e774fafc78bb520e23bfdcd2fafa029ad843f5 /tests/auto/corelib/json/tst_qtjson.cpp | |
parent | 8738f09b9fc1b35e3dc78211368d87069f3071f7 (diff) |
Add initializer list support in QJsonObject.
It allows to create a QJsonObject instance in C++ by using
initializer list of pairs QString QJsonValue, for example:
QJsonObject o = {{"property1", 1}, {"property2", 2}};
[ChangeLog][QtCore][QtJson] QJsonObject now supports
C++11 initializer lists.
Task-number: QTBUG-26606
Change-Id: I67af881e175f427e563e685336c48a5f8466b476
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 | 55 |
1 files changed, 53 insertions, 2 deletions
diff --git a/tests/auto/corelib/json/tst_qtjson.cpp b/tests/auto/corelib/json/tst_qtjson.cpp index c7590fd023..ebe7333c47 100644 --- a/tests/auto/corelib/json/tst_qtjson.cpp +++ b/tests/auto/corelib/json/tst_qtjson.cpp @@ -148,6 +148,7 @@ private Q_SLOTS: void longStrings(); void arrayInitializerList(); + void objectInitializerList(); private: QString testDataDir; }; @@ -2681,22 +2682,72 @@ void tst_QtJson::arrayInitializerList() 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); + QJsonArray a4 { 1, QJsonArray{1,2,3}, QJsonArray{"hello", 2}, QJsonObject{{"one", 1}} }; + QCOMPARE(a4.count(), 4); QCOMPARE(QJsonValue(a4[0]), QJsonValue(1)); { QJsonArray a41 = a4[1].toArray(); QJsonArray a42 = a4[2].toArray(); + QJsonObject a43 = a4[3].toObject(); QCOMPARE(a41.count(), 3); QCOMPARE(a42.count(), 2); + QCOMPARE(a43.count(), 1); QCOMPARE(QJsonValue(a41[2]), QJsonValue(3)); QCOMPARE(QJsonValue(a42[1]), QJsonValue(2)); + QCOMPARE(QJsonValue(a43["one"]), QJsonValue(1)); } } #endif } +void tst_QtJson::objectInitializerList() +{ +#ifndef Q_COMPILER_INITIALIZER_LISTS + QSKIP("initializer_list is enabled only with c++11 support"); +#else + QVERIFY(QJsonObject{}.isEmpty()); + + { // one property + QJsonObject one {{"one", 1}}; + QCOMPARE(one.count(), 1); + QVERIFY(one.contains("one")); + QCOMPARE(QJsonValue(one["one"]), QJsonValue(1)); + } + { // two properties + QJsonObject two { + {"one", 1}, + {"two", 2} + }; + QCOMPARE(two.count(), 2); + QVERIFY(two.contains("one")); + QVERIFY(two.contains("two")); + QCOMPARE(QJsonValue(two["one"]), QJsonValue(1)); + QCOMPARE(QJsonValue(two["two"]), QJsonValue(2)); + } + { // nested object + QJsonObject object{{"nested", QJsonObject{{"innerProperty", 2}}}}; + QCOMPARE(object.count(), 1); + QVERIFY(object.contains("nested")); + QVERIFY(object["nested"].isObject()); + + QJsonObject nested = object["nested"].toObject(); + QCOMPARE(QJsonValue(nested["innerProperty"]), QJsonValue(2)); + } + { // nested array + QJsonObject object{{"nested", QJsonArray{"innerValue", 2.1, "bum cyk cyk"}}}; + QCOMPARE(object.count(), 1); + QVERIFY(object.contains("nested")); + QVERIFY(object["nested"].isArray()); + + QJsonArray nested = object["nested"].toArray(); + QCOMPARE(nested.count(), 3); + QCOMPARE(QJsonValue(nested[0]), QJsonValue("innerValue")); + QCOMPARE(QJsonValue(nested[1]), QJsonValue(2.1)); + } +#endif +} + QTEST_MAIN(tst_QtJson) #include "tst_qtjson.moc" |