summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/json/tst_qtjson.cpp
diff options
context:
space:
mode:
authorHolger Hans Peter Freyther <holger@moiji-mobile.com>2014-06-10 10:41:28 +0200
committerJędrzej Nowacki <jedrzej.nowacki@digia.com>2014-06-13 12:58:38 +0200
commitafbc2c75e32213db15f5a18ba5fe38822af49882 (patch)
tree129809607acc305d74c0f34fb7ac62c67195fd55 /tests/auto/corelib/json/tst_qtjson.cpp
parente2a7293290b7aa7090c4ce2178e35eccaea11a00 (diff)
json: Add defaultValue to QJsonValueRef toInt/toBool/toDouble/toString
Currently QJsonValue and QJsonValueRef behave differently in regard to the default values leading to confusion compile errors depending on which of the two types one is actually using. Before this change it was possible to write: QJsonValue value = jsonObject["item"]; QString name = value.toString(QStringLiteral("default")); but not: QString name = jsonObject["item"].toString(QStringLiteral("default")); Change-Id: Id1185acf339aa3a91e97848e85d068f84552df71 Reviewed-by: Lars Knoll <lars.knoll@digia.com> Reviewed-by: Jędrzej Nowacki <jedrzej.nowacki@digia.com>
Diffstat (limited to 'tests/auto/corelib/json/tst_qtjson.cpp')
-rw-r--r--tests/auto/corelib/json/tst_qtjson.cpp18
1 files changed, 18 insertions, 0 deletions
diff --git a/tests/auto/corelib/json/tst_qtjson.cpp b/tests/auto/corelib/json/tst_qtjson.cpp
index a17fe7561a..8d8a3d7e0e 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();
@@ -2425,5 +2426,22 @@ 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);
+}
+
QTEST_MAIN(tst_QtJson)
#include "tst_qtjson.moc"