diff options
author | Kent Hansen <kent.hansen@nokia.com> | 2012-03-15 10:15:53 +0100 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-03-20 06:49:30 +0100 |
commit | d96d89c63c28f81f9c17666ed66222f523571f03 (patch) | |
tree | 085e7cbd6fe9f134d46ad00433907ed7abc35893 /tests/auto/qml | |
parent | d38e9e413ce0c9ee636ea0b4aa722668ad54d1ae (diff) |
Make QVariant conversion for JS null type symmetric
If you pass (void*)0 to QJSEngine::toScriptValue(), or you pass a
QVariant of type QMetaType::VoidStar containing a 0 value, you get
back a QJSValue of type null (isNull() returns true); that's fine.
However, if you called QJSValue::toVariant() on a JS null value, you
would get back an invalid QVariant. The expected result is a
QVariant of type QMetaType::VoidStar containing a 0 value. This
makes the conversion of the JS null type symmetric and avoids loss
of data.
Change-Id: Ifa6e788152118f80adf9c2d7be1283f053b44294
Reviewed-by: Jędrzej Nowacki <jedrzej.nowacki@nokia.com>
Reviewed-by: Simon Hausmann <simon.hausmann@nokia.com>
Diffstat (limited to 'tests/auto/qml')
-rw-r--r-- | tests/auto/qml/qjsengine/tst_qjsengine.cpp | 4 | ||||
-rw-r--r-- | tests/auto/qml/qjsvalue/tst_qjsvalue.cpp | 12 |
2 files changed, 14 insertions, 2 deletions
diff --git a/tests/auto/qml/qjsengine/tst_qjsengine.cpp b/tests/auto/qml/qjsengine/tst_qjsengine.cpp index 6f9cc93757..e34304b258 100644 --- a/tests/auto/qml/qjsengine/tst_qjsengine.cpp +++ b/tests/auto/qml/qjsengine/tst_qjsengine.cpp @@ -2369,6 +2369,8 @@ void tst_QJSEngine::valueConversion_basic() QCOMPARE(eng.fromScriptValue<QChar>(code), c); QCOMPARE(eng.fromScriptValue<QChar>(eng.toScriptValue(c)), c); } + + QVERIFY(eng.toScriptValue(static_cast<void *>(0)).isNull()); } #if 0 // FIXME: No API for custom types @@ -2588,6 +2590,8 @@ void tst_QJSEngine::valueConversion_QVariant() } QCOMPARE(qjsvalue_cast<QVariant>(QJSValue(123)), QVariant(123)); + + QVERIFY(eng.toScriptValue(QVariant(QMetaType::VoidStar, 0)).isNull()); } #if 0 // FIXME: No support for custom types diff --git a/tests/auto/qml/qjsvalue/tst_qjsvalue.cpp b/tests/auto/qml/qjsvalue/tst_qjsvalue.cpp index ad655217ad..3522f22ca2 100644 --- a/tests/auto/qml/qjsvalue/tst_qjsvalue.cpp +++ b/tests/auto/qml/qjsvalue/tst_qjsvalue.cpp @@ -993,8 +993,8 @@ void tst_QJSValue::toVariant() QCOMPARE(qjsvalue_cast<QVariant>(undefined), QVariant()); QJSValue null = eng.evaluate("null"); - QCOMPARE(null.toVariant(), QVariant()); - QCOMPARE(qjsvalue_cast<QVariant>(null), QVariant()); + QCOMPARE(null.toVariant(), QVariant(QMetaType::VoidStar, 0)); + QCOMPARE(qjsvalue_cast<QVariant>(null), QVariant(QMetaType::VoidStar, 0)); { QJSValue number = eng.toScriptValue(123.0); @@ -1064,6 +1064,14 @@ void tst_QJSValue::toVariant() QJSValue str = QJSValue(QString("ciao")); QCOMPARE(str.toVariant(), QVariant(QString("ciao"))); QCOMPARE(qjsvalue_cast<QVariant>(str), QVariant(QString("ciao"))); + + QJSValue undef = QJSValue(QJSValue::UndefinedValue); + QCOMPARE(undef.toVariant(), QVariant()); + QCOMPARE(qjsvalue_cast<QVariant>(undef), QVariant()); + + QJSValue nil = QJSValue(QJSValue::NullValue); + QCOMPARE(nil.toVariant(), QVariant(QMetaType::VoidStar, 0)); + QCOMPARE(qjsvalue_cast<QVariant>(nil), QVariant(QMetaType::VoidStar, 0)); } #if 0 // FIXME: No automatic sequence conversion |