diff options
Diffstat (limited to 'tests/auto/declarative/qjsvalue/tst_qjsvalue.cpp')
-rw-r--r-- | tests/auto/declarative/qjsvalue/tst_qjsvalue.cpp | 433 |
1 files changed, 267 insertions, 166 deletions
diff --git a/tests/auto/declarative/qjsvalue/tst_qjsvalue.cpp b/tests/auto/declarative/qjsvalue/tst_qjsvalue.cpp index 5a9ccc521c..52e47732ae 100644 --- a/tests/auto/declarative/qjsvalue/tst_qjsvalue.cpp +++ b/tests/auto/declarative/qjsvalue/tst_qjsvalue.cpp @@ -2,7 +2,7 @@ ** ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). ** All rights reserved. -** Contact: Nokia Corporation (qt-info@nokia.com) +** Contact: http://www.qt-project.org/ ** ** This file is part of the test suite of the Qt Toolkit. ** @@ -123,7 +123,7 @@ void tst_QJSValue::ctor_int() { QJSValue v(int(0x43211234)); QVERIFY(v.isNumber()); - QCOMPARE(v.toInt32(), 0x43211234); + QCOMPARE(v.toInt(), 0x43211234); } { QJSValue v(int(1)); @@ -153,7 +153,7 @@ void tst_QJSValue::ctor_uint() { QJSValue v(uint(0x43211234)); QVERIFY(v.isNumber()); - QCOMPARE(v.toUInt32(), uint(0x43211234)); + QCOMPARE(v.toUInt(), uint(0x43211234)); } { QJSValue v(uint(1)); @@ -818,271 +818,271 @@ void tst_QJSValue::toInteger() QCOMPARE(inv.toInteger(), 0.0); } -void tst_QJSValue::toInt32() +void tst_QJSValue::toInt() { QJSEngine eng; { QJSValue zer0 = QJSValue(&eng, 0.0); - QCOMPARE(zer0.toInt32(), 0); + QCOMPARE(zer0.toInt(), 0); QCOMPARE(qjsvalue_cast<qint32>(zer0), 0); QJSValue number = QJSValue(&eng, 123.0); - QCOMPARE(number.toInt32(), 123); + QCOMPARE(number.toInt(), 123); QCOMPARE(qjsvalue_cast<qint32>(number), 123); QJSValue number2 = QJSValue(&eng, qSNaN()); - QCOMPARE(number2.toInt32(), 0); + QCOMPARE(number2.toInt(), 0); QCOMPARE(qjsvalue_cast<qint32>(number2), 0); QJSValue number3 = QJSValue(&eng, +qInf()); - QCOMPARE(number3.toInt32(), 0); + QCOMPARE(number3.toInt(), 0); QCOMPARE(qjsvalue_cast<qint32>(number3), 0); QJSValue number3_2 = QJSValue(&eng, -qInf()); - QCOMPARE(number3_2.toInt32(), 0); + QCOMPARE(number3_2.toInt(), 0); QCOMPARE(qjsvalue_cast<qint32>(number3_2), 0); QJSValue number4 = QJSValue(&eng, 0.5); - QCOMPARE(number4.toInt32(), 0); + QCOMPARE(number4.toInt(), 0); QCOMPARE(qjsvalue_cast<qint32>(number4), 0); QJSValue number5 = QJSValue(&eng, 123.5); - QCOMPARE(number5.toInt32(), 123); + QCOMPARE(number5.toInt(), 123); QCOMPARE(qjsvalue_cast<qint32>(number5), 123); QJSValue number6 = QJSValue(&eng, -456.5); - QCOMPARE(number6.toInt32(), -456); + QCOMPARE(number6.toInt(), -456); QCOMPARE(qjsvalue_cast<qint32>(number6), -456); QJSValue str = QJSValue(&eng, QLatin1String("123.0")); - QCOMPARE(str.toInt32(), 123); + QCOMPARE(str.toInt(), 123); QCOMPARE(qjsvalue_cast<qint32>(str), 123); QJSValue str2 = QJSValue(&eng, QLatin1String("NaN")); - QCOMPARE(str2.toInt32(), 0); + QCOMPARE(str2.toInt(), 0); QCOMPARE(qjsvalue_cast<qint32>(str2), 0); QJSValue str3 = QJSValue(&eng, QLatin1String("Infinity")); - QCOMPARE(str3.toInt32(), 0); + QCOMPARE(str3.toInt(), 0); QCOMPARE(qjsvalue_cast<qint32>(str3), 0); QJSValue str3_2 = QJSValue(&eng, QLatin1String("-Infinity")); - QCOMPARE(str3_2.toInt32(), 0); + QCOMPARE(str3_2.toInt(), 0); QCOMPARE(qjsvalue_cast<qint32>(str3_2), 0); QJSValue str4 = QJSValue(&eng, QLatin1String("0.5")); - QCOMPARE(str4.toInt32(), 0); + QCOMPARE(str4.toInt(), 0); QCOMPARE(qjsvalue_cast<qint32>(str4), 0); QJSValue str5 = QJSValue(&eng, QLatin1String("123.5")); - QCOMPARE(str5.toInt32(), 123); + QCOMPARE(str5.toInt(), 123); QCOMPARE(qjsvalue_cast<qint32>(str5), 123); QJSValue str6 = QJSValue(&eng, QLatin1String("-456.5")); - QCOMPARE(str6.toInt32(), -456); + QCOMPARE(str6.toInt(), -456); QCOMPARE(qjsvalue_cast<qint32>(str6), -456); } // V2 constructors { QJSValue zer0 = QJSValue(0.0); - QCOMPARE(zer0.toInt32(), 0); + QCOMPARE(zer0.toInt(), 0); QCOMPARE(qjsvalue_cast<qint32>(zer0), 0); QJSValue number = QJSValue(123.0); - QCOMPARE(number.toInt32(), 123); + QCOMPARE(number.toInt(), 123); QCOMPARE(qjsvalue_cast<qint32>(number), 123); QJSValue number2 = QJSValue(qSNaN()); - QCOMPARE(number2.toInt32(), 0); + QCOMPARE(number2.toInt(), 0); QCOMPARE(qjsvalue_cast<qint32>(number2), 0); QJSValue number3 = QJSValue(+qInf()); - QCOMPARE(number3.toInt32(), 0); + QCOMPARE(number3.toInt(), 0); QCOMPARE(qjsvalue_cast<qint32>(number3), 0); QJSValue number3_2 = QJSValue(-qInf()); - QCOMPARE(number3_2.toInt32(), 0); + QCOMPARE(number3_2.toInt(), 0); QCOMPARE(qjsvalue_cast<qint32>(number3_2), 0); QJSValue number4 = QJSValue(0.5); - QCOMPARE(number4.toInt32(), 0); + QCOMPARE(number4.toInt(), 0); QCOMPARE(qjsvalue_cast<qint32>(number4), 0); QJSValue number5 = QJSValue(123.5); - QCOMPARE(number5.toInt32(), 123); + QCOMPARE(number5.toInt(), 123); QCOMPARE(qjsvalue_cast<qint32>(number5), 123); QJSValue number6 = QJSValue(-456.5); - QCOMPARE(number6.toInt32(), -456); + QCOMPARE(number6.toInt(), -456); QCOMPARE(qjsvalue_cast<qint32>(number6), -456); QJSValue number7 = QJSValue(0x43211234); - QCOMPARE(number7.toInt32(), 0x43211234); + QCOMPARE(number7.toInt(), 0x43211234); QJSValue str = QJSValue("123.0"); - QCOMPARE(str.toInt32(), 123); + QCOMPARE(str.toInt(), 123); QCOMPARE(qjsvalue_cast<qint32>(str), 123); QJSValue str2 = QJSValue("NaN"); - QCOMPARE(str2.toInt32(), 0); + QCOMPARE(str2.toInt(), 0); QCOMPARE(qjsvalue_cast<qint32>(str2), 0); QJSValue str3 = QJSValue("Infinity"); - QCOMPARE(str3.toInt32(), 0); + QCOMPARE(str3.toInt(), 0); QCOMPARE(qjsvalue_cast<qint32>(str3), 0); QJSValue str3_2 = QJSValue("-Infinity"); - QCOMPARE(str3_2.toInt32(), 0); + QCOMPARE(str3_2.toInt(), 0); QCOMPARE(qjsvalue_cast<qint32>(str3_2), 0); QJSValue str4 = QJSValue("0.5"); - QCOMPARE(str4.toInt32(), 0); + QCOMPARE(str4.toInt(), 0); QCOMPARE(qjsvalue_cast<qint32>(str4), 0); QJSValue str5 = QJSValue("123.5"); - QCOMPARE(str5.toInt32(), 123); + QCOMPARE(str5.toInt(), 123); QCOMPARE(qjsvalue_cast<qint32>(str5), 123); QJSValue str6 = QJSValue("-456.5"); - QCOMPARE(str6.toInt32(), -456); + QCOMPARE(str6.toInt(), -456); QCOMPARE(qjsvalue_cast<qint32>(str6), -456); } QJSValue inv; - QCOMPARE(inv.toInt32(), 0); + QCOMPARE(inv.toInt(), 0); QCOMPARE(qjsvalue_cast<qint32>(inv), 0); } -void tst_QJSValue::toUInt32() +void tst_QJSValue::toUInt() { QJSEngine eng; { - QJSValue zer0 = QJSValue(&eng, 0.0); - QCOMPARE(zer0.toUInt32(), quint32(0)); + QJSValue zer0 = eng.toScriptValue(0.0); + QCOMPARE(zer0.toUInt(), quint32(0)); QCOMPARE(qjsvalue_cast<quint32>(zer0), quint32(0)); - QJSValue number = QJSValue(&eng, 123.0); - QCOMPARE(number.toUInt32(), quint32(123)); + QJSValue number = eng.toScriptValue(123.0); + QCOMPARE(number.toUInt(), quint32(123)); QCOMPARE(qjsvalue_cast<quint32>(number), quint32(123)); - QJSValue number2 = QJSValue(&eng, qSNaN()); - QCOMPARE(number2.toUInt32(), quint32(0)); + QJSValue number2 = eng.toScriptValue(qSNaN()); + QCOMPARE(number2.toUInt(), quint32(0)); QCOMPARE(qjsvalue_cast<quint32>(number2), quint32(0)); - QJSValue number3 = QJSValue(&eng, +qInf()); - QCOMPARE(number3.toUInt32(), quint32(0)); + QJSValue number3 = eng.toScriptValue(+qInf()); + QCOMPARE(number3.toUInt(), quint32(0)); QCOMPARE(qjsvalue_cast<quint32>(number3), quint32(0)); - QJSValue number3_2 = QJSValue(&eng, -qInf()); - QCOMPARE(number3_2.toUInt32(), quint32(0)); + QJSValue number3_2 = eng.toScriptValue(-qInf()); + QCOMPARE(number3_2.toUInt(), quint32(0)); QCOMPARE(qjsvalue_cast<quint32>(number3_2), quint32(0)); - QJSValue number4 = QJSValue(&eng, 0.5); - QCOMPARE(number4.toUInt32(), quint32(0)); + QJSValue number4 = eng.toScriptValue(0.5); + QCOMPARE(number4.toUInt(), quint32(0)); - QJSValue number5 = QJSValue(&eng, 123.5); - QCOMPARE(number5.toUInt32(), quint32(123)); + QJSValue number5 = eng.toScriptValue(123.5); + QCOMPARE(number5.toUInt(), quint32(123)); - QJSValue number6 = QJSValue(&eng, -456.5); - QCOMPARE(number6.toUInt32(), quint32(-456)); + QJSValue number6 = eng.toScriptValue(-456.5); + QCOMPARE(number6.toUInt(), quint32(-456)); QCOMPARE(qjsvalue_cast<quint32>(number6), quint32(-456)); - QJSValue str = QJSValue(&eng, QLatin1String("123.0")); - QCOMPARE(str.toUInt32(), quint32(123)); + QJSValue str = eng.toScriptValue(QString::fromLatin1("123.0")); + QCOMPARE(str.toUInt(), quint32(123)); QCOMPARE(qjsvalue_cast<quint32>(str), quint32(123)); - QJSValue str2 = QJSValue(&eng, QLatin1String("NaN")); - QCOMPARE(str2.toUInt32(), quint32(0)); + QJSValue str2 = eng.toScriptValue(QString::fromLatin1("NaN")); + QCOMPARE(str2.toUInt(), quint32(0)); QCOMPARE(qjsvalue_cast<quint32>(str2), quint32(0)); - QJSValue str3 = QJSValue(&eng, QLatin1String("Infinity")); - QCOMPARE(str3.toUInt32(), quint32(0)); + QJSValue str3 = eng.toScriptValue(QString::fromLatin1("Infinity")); + QCOMPARE(str3.toUInt(), quint32(0)); QCOMPARE(qjsvalue_cast<quint32>(str3), quint32(0)); - QJSValue str3_2 = QJSValue(&eng, QLatin1String("-Infinity")); - QCOMPARE(str3_2.toUInt32(), quint32(0)); + QJSValue str3_2 = eng.toScriptValue(QString::fromLatin1("-Infinity")); + QCOMPARE(str3_2.toUInt(), quint32(0)); QCOMPARE(qjsvalue_cast<quint32>(str3_2), quint32(0)); - QJSValue str4 = QJSValue(&eng, QLatin1String("0.5")); - QCOMPARE(str4.toUInt32(), quint32(0)); + QJSValue str4 = eng.toScriptValue(QString::fromLatin1("0.5")); + QCOMPARE(str4.toUInt(), quint32(0)); QCOMPARE(qjsvalue_cast<quint32>(str4), quint32(0)); - QJSValue str5 = QJSValue(&eng, QLatin1String("123.5")); - QCOMPARE(str5.toUInt32(), quint32(123)); + QJSValue str5 = eng.toScriptValue(QString::fromLatin1("123.5")); + QCOMPARE(str5.toUInt(), quint32(123)); QCOMPARE(qjsvalue_cast<quint32>(str5), quint32(123)); - QJSValue str6 = QJSValue(&eng, QLatin1String("-456.5")); - QCOMPARE(str6.toUInt32(), quint32(-456)); + QJSValue str6 = eng.toScriptValue(QString::fromLatin1("-456.5")); + QCOMPARE(str6.toUInt(), quint32(-456)); QCOMPARE(qjsvalue_cast<quint32>(str6), quint32(-456)); } // V2 constructors { QJSValue zer0 = QJSValue(0.0); - QCOMPARE(zer0.toUInt32(), quint32(0)); + QCOMPARE(zer0.toUInt(), quint32(0)); QCOMPARE(qjsvalue_cast<quint32>(zer0), quint32(0)); QJSValue number = QJSValue(123.0); - QCOMPARE(number.toUInt32(), quint32(123)); + QCOMPARE(number.toUInt(), quint32(123)); QCOMPARE(qjsvalue_cast<quint32>(number), quint32(123)); QJSValue number2 = QJSValue(qSNaN()); - QCOMPARE(number2.toUInt32(), quint32(0)); + QCOMPARE(number2.toUInt(), quint32(0)); QCOMPARE(qjsvalue_cast<quint32>(number2), quint32(0)); QJSValue number3 = QJSValue(+qInf()); - QCOMPARE(number3.toUInt32(), quint32(0)); + QCOMPARE(number3.toUInt(), quint32(0)); QCOMPARE(qjsvalue_cast<quint32>(number3), quint32(0)); QJSValue number3_2 = QJSValue(-qInf()); - QCOMPARE(number3_2.toUInt32(), quint32(0)); + QCOMPARE(number3_2.toUInt(), quint32(0)); QCOMPARE(qjsvalue_cast<quint32>(number3_2), quint32(0)); QJSValue number4 = QJSValue(0.5); - QCOMPARE(number4.toUInt32(), quint32(0)); + QCOMPARE(number4.toUInt(), quint32(0)); QJSValue number5 = QJSValue(123.5); - QCOMPARE(number5.toUInt32(), quint32(123)); + QCOMPARE(number5.toUInt(), quint32(123)); QJSValue number6 = QJSValue(-456.5); - QCOMPARE(number6.toUInt32(), quint32(-456)); + QCOMPARE(number6.toUInt(), quint32(-456)); QCOMPARE(qjsvalue_cast<quint32>(number6), quint32(-456)); QJSValue number7 = QJSValue(0x43211234); - QCOMPARE(number7.toUInt32(), quint32(0x43211234)); + QCOMPARE(number7.toUInt(), quint32(0x43211234)); QJSValue str = QJSValue(QLatin1String("123.0")); - QCOMPARE(str.toUInt32(), quint32(123)); + QCOMPARE(str.toUInt(), quint32(123)); QCOMPARE(qjsvalue_cast<quint32>(str), quint32(123)); QJSValue str2 = QJSValue(QLatin1String("NaN")); - QCOMPARE(str2.toUInt32(), quint32(0)); + QCOMPARE(str2.toUInt(), quint32(0)); QCOMPARE(qjsvalue_cast<quint32>(str2), quint32(0)); QJSValue str3 = QJSValue(QLatin1String("Infinity")); - QCOMPARE(str3.toUInt32(), quint32(0)); + QCOMPARE(str3.toUInt(), quint32(0)); QCOMPARE(qjsvalue_cast<quint32>(str3), quint32(0)); QJSValue str3_2 = QJSValue(QLatin1String("-Infinity")); - QCOMPARE(str3_2.toUInt32(), quint32(0)); + QCOMPARE(str3_2.toUInt(), quint32(0)); QCOMPARE(qjsvalue_cast<quint32>(str3_2), quint32(0)); QJSValue str4 = QJSValue(QLatin1String("0.5")); - QCOMPARE(str4.toUInt32(), quint32(0)); + QCOMPARE(str4.toUInt(), quint32(0)); QCOMPARE(qjsvalue_cast<quint32>(str4), quint32(0)); QJSValue str5 = QJSValue(QLatin1String("123.5")); - QCOMPARE(str5.toUInt32(), quint32(123)); + QCOMPARE(str5.toUInt(), quint32(123)); QCOMPARE(qjsvalue_cast<quint32>(str5), quint32(123)); QJSValue str6 = QJSValue(QLatin1String("-456.5")); - QCOMPARE(str6.toUInt32(), quint32(-456)); + QCOMPARE(str6.toUInt(), quint32(-456)); QCOMPARE(qjsvalue_cast<quint32>(str6), quint32(-456)); } QJSValue inv; - QCOMPARE(inv.toUInt32(), quint32(0)); + QCOMPARE(inv.toUInt(), quint32(0)); QCOMPARE(qjsvalue_cast<quint32>(inv), quint32(0)); } @@ -1340,7 +1340,7 @@ void tst_QJSValue::toVariant() listIn << 123 << "hello"; QJSValue array = qScriptValueFromValue(&eng, listIn); QVERIFY(array.isArray()); - QCOMPARE(array.property("length").toInt32(), 2); + QCOMPARE(array.property("length").toInt(), 2); QVariant ret = array.toVariant(); QCOMPARE(ret.type(), QVariant::List); QVariantList listOut = ret.toList(); @@ -1350,8 +1350,8 @@ void tst_QJSValue::toVariant() // round-trip conversion QJSValue array2 = qScriptValueFromValue(&eng, ret); QVERIFY(array2.isArray()); - QCOMPARE(array2.property("length").toInt32(), array.property("length").toInt32()); - for (int i = 0; i < array.property("length").toInt32(); ++i) + QCOMPARE(array2.property("length").toInt(), array.property("length").toInt()); + for (int i = 0; i < array.property("length").toInt(); ++i) QVERIFY(array2.property(i).strictlyEquals(array.property(i))); } #endif @@ -1530,7 +1530,7 @@ void tst_QJSValue::toObject() { QJSValue tmp = eng.toObject(number); QVERIFY(tmp.isObject()); - QCOMPARE(tmp.toInt32(), number.toInt32()); + QCOMPARE(tmp.toInt(), number.toInt()); } QVERIFY(number.isNumber()); @@ -1698,7 +1698,7 @@ void tst_QJSValue::isError_propertiesOfGlobalObject() QJSEngine eng; for (int i = 0; i < errors.size(); ++i) { QJSValue ctor = eng.globalObject().property(errors.at(i)); - QVERIFY(ctor.isFunction()); + QVERIFY(ctor.isCallable()); QVERIFY(ctor.property("prototype").isError()); } } @@ -1792,6 +1792,101 @@ static QJSValue getSet__proto__(QScriptContext *ctx, QScriptEngine *) } #endif +void tst_QJSValue::hasProperty_basic() +{ + QJSEngine eng; + QJSValue obj = eng.newObject(); + QVERIFY(obj.hasProperty("hasOwnProperty")); // inherited from Object.prototype + QVERIFY(!obj.hasOwnProperty("hasOwnProperty")); + + QVERIFY(!obj.hasProperty("foo")); + QVERIFY(!obj.hasOwnProperty("foo")); + obj.setProperty("foo", 123); + QVERIFY(obj.hasProperty("foo")); + QVERIFY(obj.hasOwnProperty("foo")); + + QVERIFY(!obj.hasProperty("bar")); + QVERIFY(!obj.hasOwnProperty("bar")); +} + +void tst_QJSValue::hasProperty_globalObject() +{ + QJSEngine eng; + QJSValue global = eng.globalObject(); + QVERIFY(global.hasProperty("Math")); + QVERIFY(global.hasOwnProperty("Math")); + QVERIFY(!global.hasProperty("NoSuchStandardProperty")); + QVERIFY(!global.hasOwnProperty("NoSuchStandardProperty")); + + QVERIFY(!global.hasProperty("foo")); + QVERIFY(!global.hasOwnProperty("foo")); + global.setProperty("foo", 123); + QVERIFY(global.hasProperty("foo")); + QVERIFY(global.hasOwnProperty("foo")); +} + +void tst_QJSValue::hasProperty_changePrototype() +{ + QJSEngine eng; + QJSValue obj = eng.newObject(); + QJSValue proto = eng.newObject(); + obj.setPrototype(proto); + + QVERIFY(!obj.hasProperty("foo")); + QVERIFY(!obj.hasOwnProperty("foo")); + proto.setProperty("foo", 123); + QVERIFY(obj.hasProperty("foo")); + QVERIFY(!obj.hasOwnProperty("foo")); + + obj.setProperty("foo", 456); // override prototype property + QVERIFY(obj.hasProperty("foo")); + QVERIFY(obj.hasOwnProperty("foo")); +} + +void tst_QJSValue::deleteProperty_basic() +{ + QJSEngine eng; + QJSValue obj = eng.newObject(); + // deleteProperty() behavior matches JS delete operator + QVERIFY(obj.deleteProperty("foo")); + + obj.setProperty("foo", 123); + QVERIFY(obj.deleteProperty("foo")); + QVERIFY(!obj.hasOwnProperty("foo")); +} + +void tst_QJSValue::deleteProperty_globalObject() +{ + QJSEngine eng; + QJSValue global = eng.globalObject(); + // deleteProperty() behavior matches JS delete operator + QVERIFY(global.deleteProperty("foo")); + + global.setProperty("foo", 123); + QVERIFY(global.deleteProperty("foo")); + QVERIFY(!global.hasProperty("foo")); + + QVERIFY(global.deleteProperty("Math")); + QVERIFY(!global.hasProperty("Math")); + + QVERIFY(!global.deleteProperty("NaN")); // read-only + QVERIFY(global.hasProperty("NaN")); +} + +void tst_QJSValue::deleteProperty_inPrototype() +{ + QJSEngine eng; + QJSValue obj = eng.newObject(); + QJSValue proto = eng.newObject(); + obj.setPrototype(proto); + + proto.setProperty("foo", 123); + QVERIFY(obj.hasProperty("foo")); + // deleteProperty() behavior matches JS delete operator + QVERIFY(obj.deleteProperty("foo")); + QVERIFY(obj.hasProperty("foo")); +} + void tst_QJSValue::getSetProperty_HooliganTask162051() { QJSEngine eng; @@ -2102,9 +2197,9 @@ void tst_QJSValue::getSetProperty_gettersAndSettersChange() eng.globalObject().setProperty("object", object); QJSValue res = eng.evaluate("object.x = 89; var a = object.foo; object.foo = 65; a"); - QCOMPARE(res.toInt32(), 89); - QCOMPARE(object.property("x").toInt32(), 65); - QCOMPARE(object.property("foo").toInt32(), 65); + QCOMPARE(res.toInt(), 89); + QCOMPARE(object.property("x").toInt(), 65); + QCOMPARE(object.property("foo").toInt(), 65); #endif } @@ -2119,13 +2214,13 @@ void tst_QJSValue::getSetProperty_array() array.setProperty(0, num); QCOMPARE(array.property(0).toNumber(), num.toNumber()); QCOMPARE(array.property("0").toNumber(), num.toNumber()); - QCOMPARE(array.property("length").toUInt32(), quint32(1)); + QCOMPARE(array.property("length").toUInt(), quint32(1)); array.setProperty(1, str); QCOMPARE(array.property(1).toString(), str.toString()); QCOMPARE(array.property("1").toString(), str.toString()); - QCOMPARE(array.property("length").toUInt32(), quint32(2)); + QCOMPARE(array.property("length").toUInt(), quint32(2)); array.setProperty("length", QJSValue(&eng, 1)); - QCOMPARE(array.property("length").toUInt32(), quint32(1)); + QCOMPARE(array.property("length").toUInt(), quint32(1)); QCOMPARE(array.property(1).isValid(), false); } @@ -2487,7 +2582,7 @@ void tst_QJSValue::getSetScope() { QJSValue ret = object2.property("foo", QJSValue::ResolveScope); QVERIFY(ret.isNumber()); - QCOMPARE(ret.toInt32(), 123); + QCOMPARE(ret.toInt(), 123); } QJSValue inv; @@ -2721,18 +2816,18 @@ void tst_QJSValue::call_function() { QJSEngine eng; QJSValue fun = eng.evaluate("(function() { return 1; })"); - QVERIFY(fun.isFunction()); + QVERIFY(fun.isCallable()); QJSValue result = fun.call(); QVERIFY(result.isNumber()); - QCOMPARE(result.toInt32(), 1); + QCOMPARE(result.toInt(), 1); } void tst_QJSValue::call_object() { QJSEngine eng; QJSValue Object = eng.evaluate("Object"); - QCOMPARE(Object.isFunction(), true); - QJSValue result = Object.call(Object); + QCOMPARE(Object.isCallable(), true); + QJSValue result = Object.callWithInstance(Object); QCOMPARE(result.isObject(), true); } @@ -2742,10 +2837,10 @@ void tst_QJSValue::call_newObjects() // test that call() doesn't construct new objects QJSValue Number = eng.evaluate("Number"); QJSValue Object = eng.evaluate("Object"); - QCOMPARE(Object.isFunction(), true); + QCOMPARE(Object.isCallable(), true); QJSValueList args; args << QJSValue(&eng, 123); - QJSValue result = Number.call(Object, args); + QJSValue result = Number.callWithInstance(Object, args); QCOMPARE(result.strictlyEquals(args.at(0)), true); } @@ -2754,10 +2849,10 @@ void tst_QJSValue::call_this() QJSEngine eng; // test that correct "this" object is used QJSValue fun = eng.evaluate("(function() { return this; })"); - QCOMPARE(fun.isFunction(), true); + QCOMPARE(fun.isCallable(), true); QJSValue numberObject = QJSValue(&eng, 123.0).toObject(); - QJSValue result = fun.call(numberObject); + QJSValue result = fun.callWithInstance(numberObject); QCOMPARE(result.isObject(), true); QCOMPARE(result.toNumber(), 123.0); } @@ -2768,15 +2863,15 @@ void tst_QJSValue::call_arguments() // test that correct arguments are passed QJSValue fun = eng.evaluate("(function() { return arguments[0]; })"); - QCOMPARE(fun.isFunction(), true); + QCOMPARE(fun.isCallable(), true); { - QJSValue result = fun.call(eng.undefinedValue()); + QJSValue result = fun.callWithInstance(eng.undefinedValue()); QCOMPARE(result.isUndefined(), true); } { QJSValueList args; args << QJSValue(&eng, 123.0); - QJSValue result = fun.call(eng.undefinedValue(), args); + QJSValue result = fun.callWithInstance(eng.undefinedValue(), args); QCOMPARE(result.isNumber(), true); QCOMPARE(result.toNumber(), 123.0); } @@ -2784,7 +2879,7 @@ void tst_QJSValue::call_arguments() { QJSValueList args; args << QJSValue(123.0); - QJSValue result = fun.call(eng.undefinedValue(), args); + QJSValue result = fun.callWithInstance(eng.undefinedValue(), args); QCOMPARE(result.isNumber(), true); QCOMPARE(result.toNumber(), 123.0); } @@ -2792,7 +2887,7 @@ void tst_QJSValue::call_arguments() { QJSValue args = eng.newArray(); args.setProperty(0, 123); - QJSValue result = fun.call(eng.undefinedValue(), args); + QJSValue result = fun.callWithInstance(eng.undefinedValue(), args); QVERIFY(result.isNumber()); QCOMPARE(result.toNumber(), 123.0); } @@ -2804,12 +2899,12 @@ void tst_QJSValue::call() QJSEngine eng; { QJSValue fun = eng.evaluate("(function() { return arguments[1]; })"); - QCOMPARE(fun.isFunction(), true); + QCOMPARE(fun.isCallable(), true); { QJSValueList args; args << QJSValue(&eng, 123.0) << QJSValue(&eng, 456.0); - QJSValue result = fun.call(eng.undefinedValue(), args); + QJSValue result = fun.callWithInstance(eng.undefinedValue(), args); QCOMPARE(result.isNumber(), true); QCOMPARE(result.toNumber(), 456.0); } @@ -2818,7 +2913,7 @@ void tst_QJSValue::call() QJSValue args = eng.newArray(); args.setProperty(0, 123); args.setProperty(1, 456); - QJSValue result = fun.call(eng.undefinedValue(), args); + QJSValue result = fun.callWithInstance(eng.undefinedValue(), args); QVERIFY(result.isNumber()); QCOMPARE(result.toNumber(), 456.0); } @@ -2826,7 +2921,7 @@ void tst_QJSValue::call() } { QJSValue fun = eng.evaluate("(function() { throw new Error('foo'); })"); - QCOMPARE(fun.isFunction(), true); + QCOMPARE(fun.isCallable(), true); QVERIFY(!eng.hasUncaughtException()); { @@ -2843,7 +2938,7 @@ void tst_QJSValue::call() { QJSValueList args; args << QJSValue(&eng, 123.0); - QJSValue result = fun.call(eng.undefinedValue(), args); + QJSValue result = fun.callWithInstance(eng.undefinedValue(), args); QVERIFY(!eng.hasUncaughtException()); QCOMPARE(result.isNumber(), true); QCOMPARE(result.toNumber(), 123.0); @@ -2852,7 +2947,7 @@ void tst_QJSValue::call() { QJSValueList args; args << QJSValue(123.0); - QJSValue result = fun.call(eng.undefinedValue(), args); + QJSValue result = fun.callWithInstance(eng.undefinedValue(), args); QCOMPARE(result.isNumber(), true); QCOMPARE(result.toNumber(), 123.0); } @@ -2860,7 +2955,7 @@ void tst_QJSValue::call() { QJSValue args = eng.newArray(); args.setProperty(0, 123); - QJSValue result = fun.call(eng.undefinedValue(), args); + QJSValue result = fun.callWithInstance(eng.undefinedValue(), args); QVERIFY(result.isNumber()); QCOMPARE(result.toNumber(), 123.0); } @@ -2871,7 +2966,7 @@ void tst_QJSValue::call() { QJSValueList args; args << QJSValue(&eng, 123.0); - QJSValue result = fun.call(eng.undefinedValue(), args); + QJSValue result = fun.callWithInstance(eng.undefinedValue(), args); QVERIFY(!eng.hasUncaughtException()); QCOMPARE(result.isNumber(), true); QCOMPARE(result.toNumber(), 123.0); @@ -2890,7 +2985,7 @@ void tst_QJSValue::call_invalidArguments() { QJSValueList args; args << QJSValue(); - QJSValue ret = fun.call(QJSValue(), args); + QJSValue ret = fun.callWithInstance(args); QVERIFY(!eng.hasUncaughtException()); QCOMPARE(ret.isValid(), true); QCOMPARE(ret.isUndefined(), true); @@ -2901,7 +2996,7 @@ void tst_QJSValue::call_invalidArguments() { QJSValueList args; args << QJSValue(); - QJSValue ret = fun.call(QJSValue(), args); + QJSValue ret = fun.call(args); QCOMPARE(ret.isValid(), true); QCOMPARE(ret.isUndefined(), true); } @@ -2911,7 +3006,7 @@ void tst_QJSValue::call_invalidArguments() { QJSValueList args; args << QJSValue() << QJSValue(); - QJSValue ret = fun.call(QJSValue(), args); + QJSValue ret = fun.call(args); QCOMPARE(ret.isValid(), true); QCOMPARE(ret.isNumber(), true); QCOMPARE(qIsNaN(ret.toNumber()), true); @@ -2940,25 +3035,25 @@ void tst_QJSValue::call_twoEngines() QJSValue object = eng.evaluate("Object"); QJSEngine otherEngine; QJSValue fun = otherEngine.evaluate("(function() { return 1; })"); - QVERIFY(fun.isFunction()); + QVERIFY(fun.isCallable()); QTest::ignoreMessage(QtWarningMsg, "JSValue can't be rassigned to an another engine."); QTest::ignoreMessage(QtWarningMsg, "QJSValue::call() failed: " "cannot call function with thisObject created in " "a different engine"); - QCOMPARE(fun.call(object).isValid(), false); + QCOMPARE(fun.callWithInstance(object).isValid(), false); QTest::ignoreMessage(QtWarningMsg, "QJSValue::call() failed: " "cannot call function with argument created in " "a different engine"); - QCOMPARE(fun.call(QJSValue(), QJSValueList() << QJSValue(&eng, 123)).isValid(), false); + QCOMPARE(fun.call(QJSValueList() << QJSValue(&eng, 123)).isValid(), false); { QJSValue fun = eng.evaluate("Object"); - QVERIFY(fun.isFunction()); + QVERIFY(fun.isCallable()); QJSEngine eng2; QJSValue objectInDifferentEngine = eng2.newObject(); QJSValueList args; args << objectInDifferentEngine; QTest::ignoreMessage(QtWarningMsg, "QJSValue::call() failed: cannot call function with argument created in a different engine"); - fun.call(QJSValue(), args); + fun.call(args); } } @@ -2967,7 +3062,7 @@ void tst_QJSValue::call_array() #if 0 // FIXME: The feature of interpreting an array as argument list has been removed from the API QScriptEngine eng; QJSValue fun = eng.evaluate("(function() { return arguments; })"); - QVERIFY(fun.isFunction()); + QVERIFY(fun.isCallable()); QJSValue array = eng.newArray(3); array.setProperty(0, QJSValue(&eng, 123.0)); array.setProperty(1, QJSValue(&eng, 456.0)); @@ -3069,18 +3164,18 @@ void tst_QJSValue::construct_nonFunction_data() void tst_QJSValue::construct_nonFunction() { QFETCH(QJSValue, value); - QVERIFY(!value.construct().isValid()); + QVERIFY(!value.callAsConstructor().isValid()); } void tst_QJSValue::construct_simple() { QJSEngine eng; QJSValue fun = eng.evaluate("(function () { this.foo = 123; })"); - QVERIFY(fun.isFunction()); - QJSValue ret = fun.construct(); + QVERIFY(fun.isCallable()); + QJSValue ret = fun.callAsConstructor(); QVERIFY(ret.isObject()); QVERIFY(ret.instanceOf(fun)); - QCOMPARE(ret.property("foo").toInt32(), 123); + QCOMPARE(ret.property("foo").toInt(), 123); } void tst_QJSValue::construct_newObjectJS() @@ -3088,11 +3183,11 @@ void tst_QJSValue::construct_newObjectJS() QJSEngine eng; // returning a different object overrides the default-constructed one QJSValue fun = eng.evaluate("(function () { return { bar: 456 }; })"); - QVERIFY(fun.isFunction()); - QJSValue ret = fun.construct(); + QVERIFY(fun.isCallable()); + QJSValue ret = fun.callAsConstructor(); QVERIFY(ret.isObject()); QVERIFY(!ret.instanceOf(fun)); - QCOMPARE(ret.property("bar").toInt32(), 456); + QCOMPARE(ret.property("bar").toInt(), 456); } #if 0 // FIXME: no c-style callbacks @@ -3100,20 +3195,20 @@ void tst_QJSValue::construct_undefined() { QScriptEngine eng; QJSValue fun = eng.newFunction(ctorReturningUndefined); - QJSValue ret = fun.construct(); + QJSValue ret = fun.callAsConstructor(); QVERIFY(ret.isObject()); QVERIFY(ret.instanceOf(fun)); - QCOMPARE(ret.property("foo").toInt32(), 123); + QCOMPARE(ret.property("foo").toInt(), 123); } void tst_QJSValue::construct_newObjectCpp() { QScriptEngine eng; QJSValue fun = eng.newFunction(ctorReturningNewObject); - QJSValue ret = fun.construct(); + QJSValue ret = fun.callAsConstructor(); QVERIFY(ret.isObject()); QVERIFY(!ret.instanceOf(fun)); - QCOMPARE(ret.property("bar").toInt32(), 456); + QCOMPARE(ret.property("bar").toInt(), 456); } #endif @@ -3121,10 +3216,10 @@ void tst_QJSValue::construct_arg() { QJSEngine eng; QJSValue Number = eng.evaluate("Number"); - QCOMPARE(Number.isFunction(), true); + QCOMPARE(Number.isCallable(), true); QJSValueList args; args << QJSValue(&eng, 123); - QJSValue ret = Number.construct(args); + QJSValue ret = Number.callAsConstructor(args); QCOMPARE(ret.isObject(), true); QCOMPARE(ret.toNumber(), args.at(0).toNumber()); } @@ -3134,9 +3229,9 @@ void tst_QJSValue::construct_proto() QJSEngine eng; // test that internal prototype is set correctly QJSValue fun = eng.evaluate("(function() { return this.__proto__; })"); - QCOMPARE(fun.isFunction(), true); + QCOMPARE(fun.isCallable(), true); QCOMPARE(fun.property("prototype").isObject(), true); - QJSValue ret = fun.construct(); + QJSValue ret = fun.callAsConstructor(); QCOMPARE(fun.property("prototype").strictlyEquals(ret), true); } @@ -3145,8 +3240,8 @@ void tst_QJSValue::construct_returnInt() QJSEngine eng; // test that we return the new object even if a non-object value is returned from the function QJSValue fun = eng.evaluate("(function() { return 123; })"); - QCOMPARE(fun.isFunction(), true); - QJSValue ret = fun.construct(); + QCOMPARE(fun.isCallable(), true); + QJSValue ret = fun.callAsConstructor(); QCOMPARE(ret.isObject(), true); } @@ -3154,8 +3249,8 @@ void tst_QJSValue::construct_throw() { QJSEngine eng; QJSValue fun = eng.evaluate("(function() { throw new Error('foo'); })"); - QCOMPARE(fun.isFunction(), true); - QJSValue ret = fun.construct(); + QCOMPARE(fun.isCallable(), true); + QJSValue ret = fun.callAsConstructor(); QCOMPARE(ret.isError(), true); QCOMPARE(eng.hasUncaughtException(), true); QVERIFY(ret.strictlyEquals(eng.uncaughtException())); @@ -3166,13 +3261,13 @@ void tst_QJSValue::construct() { QScriptEngine eng; QJSValue fun = eng.evaluate("(function() { return arguments; })"); - QVERIFY(fun.isFunction()); + QVERIFY(fun.isCallable()); QJSValue array = eng.newArray(3); array.setProperty(0, QJSValue(&eng, 123.0)); array.setProperty(1, QJSValue(&eng, 456.0)); array.setProperty(2, QJSValue(&eng, 789.0)); // construct with single array object as arguments - QJSValue ret = fun.construct(array); + QJSValue ret = fun.callAsConstructor(array); QVERIFY(!eng.hasUncaughtException()); QVERIFY(ret.isValid()); QVERIFY(ret.isObject()); @@ -3180,25 +3275,25 @@ void tst_QJSValue::construct() QCOMPARE(ret.property(1).strictlyEquals(array.property(1)), true); QCOMPARE(ret.property(2).strictlyEquals(array.property(2)), true); // construct with arguments object as arguments - QJSValue ret2 = fun.construct(ret); + QJSValue ret2 = fun.callAsConstructor(ret); QCOMPARE(ret2.property(0).strictlyEquals(ret.property(0)), true); QCOMPARE(ret2.property(1).strictlyEquals(ret.property(1)), true); QCOMPARE(ret2.property(2).strictlyEquals(ret.property(2)), true); // construct with null as arguments - QJSValue ret3 = fun.construct(eng.nullValue()); + QJSValue ret3 = fun.callAsConstructor(eng.nullValue()); QCOMPARE(ret3.isError(), false); QCOMPARE(ret3.property("length").isNumber(), true); QCOMPARE(ret3.property("length").toNumber(), 0.0); // construct with undefined as arguments - QJSValue ret4 = fun.construct(eng.undefinedValue()); + QJSValue ret4 = fun.callAsConstructor(eng.undefinedValue()); QCOMPARE(ret4.isError(), false); QCOMPARE(ret4.property("length").isNumber(), true); QCOMPARE(ret4.property("length").toNumber(), 0.0); // construct with something else as arguments - QJSValue ret5 = fun.construct(QJSValue(&eng, 123.0)); + QJSValue ret5 = fun.callAsConstructor(QJSValue(&eng, 123.0)); QCOMPARE(ret5.isError(), true); // construct with a non-array object as arguments - QJSValue ret6 = fun.construct(eng.globalObject()); + QJSValue ret6 = fun.callAsConstructor(eng.globalObject()); QVERIFY(ret6.isError()); QCOMPARE(ret6.toString(), QString::fromLatin1("TypeError: Arguments must be an array")); } @@ -3210,20 +3305,20 @@ void tst_QJSValue::construct_twoEngines() QJSEngine otherEngine; QJSValue ctor = engine.evaluate("(function (a, b) { this.foo = 123; })"); QJSValue arg(&otherEngine, 124567); - QTest::ignoreMessage(QtWarningMsg, "QJSValue::construct() failed: cannot construct function with argument created in a different engine"); - QVERIFY(!ctor.construct(QJSValueList() << arg).isValid()); - QTest::ignoreMessage(QtWarningMsg, "QJSValue::construct() failed: cannot construct function with argument created in a different engine"); - QVERIFY(!ctor.construct(QJSValueList() << arg << otherEngine.newObject()).isValid()); + QTest::ignoreMessage(QtWarningMsg, "QJSValue::callAsConstructor() failed: cannot construct function with argument created in a different engine"); + QVERIFY(!ctor.callAsConstructor(QJSValueList() << arg).isValid()); + QTest::ignoreMessage(QtWarningMsg, "QJSValue::callAsConstructor() failed: cannot construct function with argument created in a different engine"); + QVERIFY(!ctor.callAsConstructor(QJSValueList() << arg << otherEngine.newObject()).isValid()); } void tst_QJSValue::construct_constructorThrowsPrimitive() { QJSEngine eng; QJSValue fun = eng.evaluate("(function() { throw 123; })"); - QVERIFY(fun.isFunction()); + QVERIFY(fun.isCallable()); // construct(QJSValueList) { - QJSValue ret = fun.construct(); + QJSValue ret = fun.callAsConstructor(); QVERIFY(ret.isNumber()); QCOMPARE(ret.toNumber(), 123.0); QVERIFY(eng.hasUncaughtException()); @@ -3233,7 +3328,7 @@ void tst_QJSValue::construct_constructorThrowsPrimitive() #if 0 // FIXME: The feature of interpreting an array as argument list has been removed from the API // construct(QJSValue) { - QJSValue ret = fun.construct(eng.newArray()); + QJSValue ret = fun.callAsConstructor(eng.newArray()); QVERIFY(ret.isNumber()); QCOMPARE(ret.toNumber(), 123.0); QVERIFY(eng.hasUncaughtException()); @@ -3443,17 +3538,17 @@ void tst_QJSValue::equals() QVERIFY(!qobj2.equals(obj2)); // compares the QObject pointers QJSValue compareFun = eng.evaluate("(function(a, b) { return a == b; })"); - QVERIFY(compareFun.isFunction()); + QVERIFY(compareFun.isCallable()); { - QJSValue ret = compareFun.call(QJSValue(), QJSValueList() << qobj1 << qobj2); + QJSValue ret = compareFun.call(QJSValueList() << qobj1 << qobj2); QVERIFY(ret.isBool()); - ret = compareFun.call(QJSValue(), QJSValueList() << qobj1 << qobj3); + ret = compareFun.call(QJSValueList() << qobj1 << qobj3); QVERIFY(ret.isBool()); QVERIFY(!ret.toBool()); - ret = compareFun.call(QJSValue(), QJSValueList() << qobj1 << qobj4); + ret = compareFun.call(QJSValueList() << qobj1 << qobj4); QVERIFY(ret.isBool()); QVERIFY(!ret.toBool()); - ret = compareFun.call(QJSValue(), QJSValueList() << qobj1 << obj1); + ret = compareFun.call(QJSValueList() << qobj1 << obj1); QVERIFY(ret.isBool()); QVERIFY(!ret.toBool()); } @@ -3897,7 +3992,7 @@ void tst_QJSValue::prettyPrinter() QFETCH(QString, expected); QJSEngine eng; QJSValue val = eng.evaluate("(" + function + ")"); - QVERIFY(val.isFunction()); + QVERIFY(val.isCallable()); QString actual = val.toString(); int count = qMin(actual.size(), expected.size()); // qDebug() << actual << expected; @@ -3945,7 +4040,7 @@ void tst_QJSValue::valueOfWithClosure() { QJSValue obj = eng.evaluate("o = {}; (function(foo) { o.valueOf = function() { return foo; } })(123); o"); QVERIFY(obj.isObject()); - QCOMPARE(obj.toInt32(), 123); + QCOMPARE(obj.toInt(), 123); } // toString() { @@ -4023,6 +4118,12 @@ void tst_QJSValue::nestedObjectToVariant_data() // Object literals { QVariantMap m; + QTest::newRow("{}") + << QString::fromLatin1("({})") + << QVariant(m); + } + { + QVariantMap m; m["a"] = QVariantMap(); QTest::newRow("{ a:{} }") << QString::fromLatin1("({ a:{} })") |