summaryrefslogtreecommitdiffstats
path: root/tests/auto/qscriptvalue
diff options
context:
space:
mode:
authorJedrzej Nowacki <jedrzej.nowacki@nokia.com>2010-11-18 09:06:36 (GMT)
committerJedrzej Nowacki <jedrzej.nowacki@nokia.com>2010-11-22 08:03:00 (GMT)
commit3e1d5bee79b4e8dcb5b6f18497bbacf9d6b6a291 (patch)
tree3f3cd9190e0539d698eb3af0e31963fafb7dc831 /tests/auto/qscriptvalue
parentf479c4110cdcd82863abb172777b1bf9113ffb49 (diff)
downloadQt-3e1d5bee79b4e8dcb5b6f18497bbacf9d6b6a291.zip
Qt-3e1d5bee79b4e8dcb5b6f18497bbacf9d6b6a291.tar.gz
Qt-3e1d5bee79b4e8dcb5b6f18497bbacf9d6b6a291.tar.bz2
Split tst_QScriptValue::construct tests into smaller chunks.
Reviewed-by: Kent Hansen
Diffstat (limited to 'tests/auto/qscriptvalue')
-rw-r--r--tests/auto/qscriptvalue/tst_qscriptvalue.cpp244
-rw-r--r--tests/auto/qscriptvalue/tst_qscriptvalue.h10
2 files changed, 147 insertions, 107 deletions
diff --git a/tests/auto/qscriptvalue/tst_qscriptvalue.cpp b/tests/auto/qscriptvalue/tst_qscriptvalue.cpp
index 1343356..3fde1a9 100644
--- a/tests/auto/qscriptvalue/tst_qscriptvalue.cpp
+++ b/tests/auto/qscriptvalue/tst_qscriptvalue.cpp
@@ -2915,127 +2915,157 @@ static QScriptValue ctorReturningNewObject(QScriptContext *, QScriptEngine *eng)
return result;
}
-void tst_QScriptValue::construct()
+void tst_QScriptValue::construct_nonFunction_data()
+{
+ newEngine();
+ QTest::addColumn<QScriptValue>("value");
+
+ QTest::newRow("invalid") << QScriptValue();
+ QTest::newRow("bool") << QScriptValue(false);
+ QTest::newRow("int") << QScriptValue(123);
+ QTest::newRow("string") << QScriptValue(QString::fromLatin1("ciao"));
+ QTest::newRow("undefined") << QScriptValue(QScriptValue::UndefinedValue);
+ QTest::newRow("null") << QScriptValue(QScriptValue::NullValue);
+
+ QTest::newRow("bool bound") << QScriptValue(engine, false);
+ QTest::newRow("int bound") << QScriptValue(engine, 123);
+ QTest::newRow("string bound") << QScriptValue(engine, QString::fromLatin1("ciao"));
+ QTest::newRow("undefined bound") << engine->undefinedValue();
+ QTest::newRow("null bound") << engine->nullValue();
+}
+
+void tst_QScriptValue::construct_nonFunction()
+{
+ QFETCH(QScriptValue, value);
+ QVERIFY(!value.construct().isValid());
+}
+
+void tst_QScriptValue::construct_simple()
{
QScriptEngine eng;
+ QScriptValue fun = eng.evaluate("(function () { this.foo = 123; })");
+ QVERIFY(fun.isFunction());
+ QScriptValue ret = fun.construct();
+ QVERIFY(ret.isObject());
+ QVERIFY(ret.instanceOf(fun));
+ QCOMPARE(ret.property("foo").toInt32(), 123);
+}
- {
- QScriptValue fun = eng.evaluate("(function () { this.foo = 123; })");
- QVERIFY(fun.isFunction());
- QScriptValue ret = fun.construct();
- QVERIFY(ret.isObject());
- QVERIFY(ret.instanceOf(fun));
- QCOMPARE(ret.property("foo").toInt32(), 123);
- }
+void tst_QScriptValue::construct_newObjectJS()
+{
+ QScriptEngine eng;
// returning a different object overrides the default-constructed one
- {
- QScriptValue fun = eng.evaluate("(function () { return { bar: 456 }; })");
- QVERIFY(fun.isFunction());
- QScriptValue ret = fun.construct();
- QVERIFY(ret.isObject());
- QVERIFY(!ret.instanceOf(fun));
- QCOMPARE(ret.property("bar").toInt32(), 456);
- }
+ QScriptValue fun = eng.evaluate("(function () { return { bar: 456 }; })");
+ QVERIFY(fun.isFunction());
+ QScriptValue ret = fun.construct();
+ QVERIFY(ret.isObject());
+ QVERIFY(!ret.instanceOf(fun));
+ QCOMPARE(ret.property("bar").toInt32(), 456);
+}
- {
- QScriptValue fun = eng.newFunction(ctorReturningUndefined);
- QScriptValue ret = fun.construct();
- QVERIFY(ret.isObject());
- QVERIFY(ret.instanceOf(fun));
- QCOMPARE(ret.property("foo").toInt32(), 123);
- }
- {
- QScriptValue fun = eng.newFunction(ctorReturningNewObject);
- QScriptValue ret = fun.construct();
- QVERIFY(ret.isObject());
- QVERIFY(!ret.instanceOf(fun));
- QCOMPARE(ret.property("bar").toInt32(), 456);
- }
+void tst_QScriptValue::construct_undefined()
+{
+ QScriptEngine eng;
+ QScriptValue fun = eng.newFunction(ctorReturningUndefined);
+ QScriptValue ret = fun.construct();
+ QVERIFY(ret.isObject());
+ QVERIFY(ret.instanceOf(fun));
+ QCOMPARE(ret.property("foo").toInt32(), 123);
+}
+void tst_QScriptValue::construct_newObjectCpp()
+{
+ QScriptEngine eng;
+ QScriptValue fun = eng.newFunction(ctorReturningNewObject);
+ QScriptValue ret = fun.construct();
+ QVERIFY(ret.isObject());
+ QVERIFY(!ret.instanceOf(fun));
+ QCOMPARE(ret.property("bar").toInt32(), 456);
+}
+
+void tst_QScriptValue::construct_arg()
+{
+ QScriptEngine eng;
QScriptValue Number = eng.evaluate("Number");
QCOMPARE(Number.isFunction(), true);
- {
- QScriptValueList args;
- args << QScriptValue(&eng, 123);
- QScriptValue ret = Number.construct(args);
- QCOMPARE(ret.isObject(), true);
- QCOMPARE(ret.toNumber(), args.at(0).toNumber());
- }
+ QScriptValueList args;
+ args << QScriptValue(&eng, 123);
+ QScriptValue ret = Number.construct(args);
+ QCOMPARE(ret.isObject(), true);
+ QCOMPARE(ret.toNumber(), args.at(0).toNumber());
+}
+void tst_QScriptValue::construct_proto()
+{
+ QScriptEngine eng;
// test that internal prototype is set correctly
- {
- QScriptValue fun = eng.evaluate("(function() { return this.__proto__; })");
- QCOMPARE(fun.isFunction(), true);
- QCOMPARE(fun.property("prototype").isObject(), true);
- QScriptValue ret = fun.construct();
- QCOMPARE(fun.property("prototype").strictlyEquals(ret), true);
- }
+ QScriptValue fun = eng.evaluate("(function() { return this.__proto__; })");
+ QCOMPARE(fun.isFunction(), true);
+ QCOMPARE(fun.property("prototype").isObject(), true);
+ QScriptValue ret = fun.construct();
+ QCOMPARE(fun.property("prototype").strictlyEquals(ret), true);
+}
+void tst_QScriptValue::construct_returnInt()
+{
+ QScriptEngine eng;
// test that we return the new object even if a non-object value is returned from the function
- {
- QScriptValue fun = eng.evaluate("(function() { return 123; })");
- QCOMPARE(fun.isFunction(), true);
- QScriptValue ret = fun.construct();
- QCOMPARE(ret.isObject(), true);
- }
-
- {
- QScriptValue fun = eng.evaluate("(function() { throw new Error('foo'); })");
- QCOMPARE(fun.isFunction(), true);
- QScriptValue ret = fun.construct();
- QCOMPARE(ret.isError(), true);
- QCOMPARE(eng.hasUncaughtException(), true);
- QVERIFY(ret.strictlyEquals(eng.uncaughtException()));
- }
+ QScriptValue fun = eng.evaluate("(function() { return 123; })");
+ QCOMPARE(fun.isFunction(), true);
+ QScriptValue ret = fun.construct();
+ QCOMPARE(ret.isObject(), true);
+}
- QScriptValue inv;
- QCOMPARE(inv.construct().isValid(), false);
+void tst_QScriptValue::construct_throw()
+{
+ QScriptEngine eng;
+ QScriptValue fun = eng.evaluate("(function() { throw new Error('foo'); })");
+ QCOMPARE(fun.isFunction(), true);
+ QScriptValue ret = fun.construct();
+ QCOMPARE(ret.isError(), true);
+ QCOMPARE(eng.hasUncaughtException(), true);
+ QVERIFY(ret.strictlyEquals(eng.uncaughtException()));
+}
- {
- QScriptValue fun = eng.evaluate("(function() { return arguments; })");
- QVERIFY(fun.isFunction());
- QScriptValue array = eng.newArray(3);
- array.setProperty(0, QScriptValue(&eng, 123.0));
- array.setProperty(1, QScriptValue(&eng, 456.0));
- array.setProperty(2, QScriptValue(&eng, 789.0));
- // construct with single array object as arguments
- QScriptValue ret = fun.construct(array);
- QVERIFY(!eng.hasUncaughtException());
- QVERIFY(ret.isValid());
- QVERIFY(ret.isObject());
- QCOMPARE(ret.property(0).strictlyEquals(array.property(0)), true);
- QCOMPARE(ret.property(1).strictlyEquals(array.property(1)), true);
- QCOMPARE(ret.property(2).strictlyEquals(array.property(2)), true);
- // construct with arguments object as arguments
- QScriptValue ret2 = fun.construct(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
- QScriptValue ret3 = fun.construct(eng.nullValue());
- QCOMPARE(ret3.isError(), false);
- QCOMPARE(ret3.property("length").isNumber(), true);
- QCOMPARE(ret3.property("length").toNumber(), 0.0);
- // construct with undefined as arguments
- QScriptValue ret4 = fun.construct(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
- QScriptValue ret5 = fun.construct(QScriptValue(&eng, 123.0));
- QCOMPARE(ret5.isError(), true);
- // construct with a non-array object as arguments
- QScriptValue ret6 = fun.construct(eng.globalObject());
- QVERIFY(ret6.isError());
- QCOMPARE(ret6.toString(), QString::fromLatin1("TypeError: Arguments must be an array"));
- }
-
- // construct on things that are not functions
- QVERIFY(!QScriptValue(false).construct().isValid());
- QVERIFY(!QScriptValue(123).construct().isValid());
- QVERIFY(!QScriptValue(QString::fromLatin1("ciao")).construct().isValid());
- QVERIFY(!QScriptValue(QScriptValue::UndefinedValue).construct().isValid());
- QVERIFY(!QScriptValue(QScriptValue::NullValue).construct().isValid());
+void tst_QScriptValue::construct()
+{
+ QScriptEngine eng;
+ QScriptValue fun = eng.evaluate("(function() { return arguments; })");
+ QVERIFY(fun.isFunction());
+ QScriptValue array = eng.newArray(3);
+ array.setProperty(0, QScriptValue(&eng, 123.0));
+ array.setProperty(1, QScriptValue(&eng, 456.0));
+ array.setProperty(2, QScriptValue(&eng, 789.0));
+ // construct with single array object as arguments
+ QScriptValue ret = fun.construct(array);
+ QVERIFY(!eng.hasUncaughtException());
+ QVERIFY(ret.isValid());
+ QVERIFY(ret.isObject());
+ QCOMPARE(ret.property(0).strictlyEquals(array.property(0)), true);
+ QCOMPARE(ret.property(1).strictlyEquals(array.property(1)), true);
+ QCOMPARE(ret.property(2).strictlyEquals(array.property(2)), true);
+ // construct with arguments object as arguments
+ QScriptValue ret2 = fun.construct(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
+ QScriptValue ret3 = fun.construct(eng.nullValue());
+ QCOMPARE(ret3.isError(), false);
+ QCOMPARE(ret3.property("length").isNumber(), true);
+ QCOMPARE(ret3.property("length").toNumber(), 0.0);
+ // construct with undefined as arguments
+ QScriptValue ret4 = fun.construct(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
+ QScriptValue ret5 = fun.construct(QScriptValue(&eng, 123.0));
+ QCOMPARE(ret5.isError(), true);
+ // construct with a non-array object as arguments
+ QScriptValue ret6 = fun.construct(eng.globalObject());
+ QVERIFY(ret6.isError());
+ QCOMPARE(ret6.toString(), QString::fromLatin1("TypeError: Arguments must be an array"));
}
void tst_QScriptValue::construct_constructorThrowsPrimitive()
diff --git a/tests/auto/qscriptvalue/tst_qscriptvalue.h b/tests/auto/qscriptvalue/tst_qscriptvalue.h
index 98d83e4..9c7b06e 100644
--- a/tests/auto/qscriptvalue/tst_qscriptvalue.h
+++ b/tests/auto/qscriptvalue/tst_qscriptvalue.h
@@ -157,6 +157,16 @@ private slots:
void call_array();
void call_nonFunction_data();
void call_nonFunction();
+ void construct_nonFunction_data();
+ void construct_nonFunction();
+ void construct_simple();
+ void construct_newObjectJS();
+ void construct_undefined();
+ void construct_newObjectCpp();
+ void construct_arg();
+ void construct_proto();
+ void construct_returnInt();
+ void construct_throw();
void construct();
void construct_constructorThrowsPrimitive();
void castToPointer();