summaryrefslogtreecommitdiffstats
path: root/tests/auto/qscriptvalue
diff options
context:
space:
mode:
Diffstat (limited to 'tests/auto/qscriptvalue')
-rw-r--r--tests/auto/qscriptvalue/tst_qscriptvalue.cpp89
-rw-r--r--tests/auto/qscriptvalue/tst_qscriptvalue.h2
2 files changed, 89 insertions, 2 deletions
diff --git a/tests/auto/qscriptvalue/tst_qscriptvalue.cpp b/tests/auto/qscriptvalue/tst_qscriptvalue.cpp
index aa9fa15..8aa4e711 100644
--- a/tests/auto/qscriptvalue/tst_qscriptvalue.cpp
+++ b/tests/auto/qscriptvalue/tst_qscriptvalue.cpp
@@ -1270,7 +1270,7 @@ void tst_QScriptValue::toVariant_old()
QCOMPARE(opaque.toVariant(), var);
QScriptValue object = eng.newObject();
- QCOMPARE(object.toVariant(), QVariant(QString("[object Object]")));
+ QCOMPARE(object.toVariant(), QVariant(QVariantMap()));
QScriptValue qobject = eng.newQObject(this);
{
@@ -2296,7 +2296,7 @@ void tst_QScriptValue::getSetScriptClass()
QCOMPARE(obj.scriptClass(), (QScriptClass*)&testClass);
QVERIFY(obj.isObject());
QVERIFY(!obj.isVariant());
- QVERIFY(!obj.toVariant().isValid());
+ QCOMPARE(obj.toVariant(), QVariant(QVariantMap()));
}
{
QScriptValue obj = eng.newQObject(this);
@@ -3472,4 +3472,89 @@ void tst_QScriptValue::objectId()
QVERIFY(obj.strictlyEquals(eng.globalObject()));
}
+void tst_QScriptValue::nestedObjectToVariant_data()
+{
+ QTest::addColumn<QString>("program");
+ QTest::addColumn<QVariant>("expected");
+
+ // Array literals
+ QTest::newRow("[[]]")
+ << QString::fromLatin1("[[]]")
+ << QVariant(QVariantList() << (QVariant(QVariantList())));
+ QTest::newRow("[[123]]")
+ << QString::fromLatin1("[[123]]")
+ << QVariant(QVariantList() << (QVariant(QVariantList() << 123)));
+ QTest::newRow("[[], 123]")
+ << QString::fromLatin1("[[], 123]")
+ << QVariant(QVariantList() << QVariant(QVariantList()) << 123);
+
+ // Cyclic arrays
+ QTest::newRow("var a=[]; a.push(a)")
+ << QString::fromLatin1("var a=[]; a.push(a); a")
+ << QVariant(QVariantList() << QVariant(QVariantList()));
+ QTest::newRow("var a=[]; a.push(123, a)")
+ << QString::fromLatin1("var a=[]; a.push(123, a); a")
+ << QVariant(QVariantList() << 123 << QVariant(QVariantList()));
+ QTest::newRow("var a=[]; var b=[]; a.push(b); b.push(a)")
+ << QString::fromLatin1("var a=[]; var b=[]; a.push(b); b.push(a); a")
+ << QVariant(QVariantList() << QVariant(QVariantList() << QVariant(QVariantList())));
+ QTest::newRow("var a=[]; var b=[]; a.push(123, b); b.push(456, a)")
+ << QString::fromLatin1("var a=[]; var b=[]; a.push(123, b); b.push(456, a); a")
+ << QVariant(QVariantList() << 123 << QVariant(QVariantList() << 456 << QVariant(QVariantList())));
+
+ // Object literals
+ {
+ QVariantMap m;
+ m["a"] = QVariantMap();
+ QTest::newRow("{ a:{} }")
+ << QString::fromLatin1("({ a:{} })")
+ << QVariant(m);
+ }
+ {
+ QVariantMap m, m2;
+ m2["b"] = 10;
+ m2["c"] = 20;
+ m["a"] = m2;
+ QTest::newRow("{ a:{b:10, c:20} }")
+ << QString::fromLatin1("({ a:{b:10, c:20} })")
+ << QVariant(m);
+ }
+ {
+ QVariantMap m;
+ m["a"] = 10;
+ m["b"] = QVariantList() << 20 << 30;
+ QTest::newRow("{ a:10, b:[20, 30]}")
+ << QString::fromLatin1("({ a:10, b:[20,30]})")
+ << QVariant(m);
+ }
+
+ // Cyclic objects
+ {
+ QVariantMap m;
+ m["p"] = QVariantMap();
+ QTest::newRow("var o={}; o.p=o")
+ << QString::fromLatin1("var o={}; o.p=o; o")
+ << QVariant(m);
+ }
+ {
+ QVariantMap m;
+ m["p"] = 123;
+ m["q"] = QVariantMap();
+ QTest::newRow("var o={}; o.p=123; o.q=o")
+ << QString::fromLatin1("var o={}; o.p=123; o.q=o; o")
+ << QVariant(m);
+ }
+}
+
+void tst_QScriptValue::nestedObjectToVariant()
+{
+ QScriptEngine eng;
+ QFETCH(QString, program);
+ QFETCH(QVariant, expected);
+ QScriptValue o = eng.evaluate(program);
+ QVERIFY(!o.isError());
+ QVERIFY(o.isObject());
+ QCOMPARE(o.toVariant(), expected);
+}
+
QTEST_MAIN(tst_QScriptValue)
diff --git a/tests/auto/qscriptvalue/tst_qscriptvalue.h b/tests/auto/qscriptvalue/tst_qscriptvalue.h
index aae35b2..8bfaa6a 100644
--- a/tests/auto/qscriptvalue/tst_qscriptvalue.h
+++ b/tests/auto/qscriptvalue/tst_qscriptvalue.h
@@ -225,6 +225,8 @@ private slots:
void engineDeleted();
void valueOfWithClosure();
void objectId();
+ void nestedObjectToVariant_data();
+ void nestedObjectToVariant();
private:
typedef void (tst_QScriptValue::*InitDataFunction)();