summaryrefslogtreecommitdiffstats
path: root/tests/auto
diff options
context:
space:
mode:
authorJedrzej Nowacki <jedrzej.nowacki@nokia.com>2010-11-05 10:11:50 (GMT)
committerJedrzej Nowacki <jedrzej.nowacki@nokia.com>2010-11-05 10:15:43 (GMT)
commit0fe5cbce817529bbf05f07e34f38d6116e89f819 (patch)
tree1f92ddee08b028414e24845b877d7bd5ec7d550a /tests/auto
parentec080f5186c07b67be9b3971d19ef0dc1ab6db1d (diff)
downloadQt-0fe5cbce817529bbf05f07e34f38d6116e89f819.zip
Qt-0fe5cbce817529bbf05f07e34f38d6116e89f819.tar.gz
Qt-0fe5cbce817529bbf05f07e34f38d6116e89f819.tar.bz2
Split QScriptValue::call autotest into smaller tests.
The test was testing too much. Split them into more manageable chunks. Reviewed-by: Kent Hansen
Diffstat (limited to 'tests/auto')
-rw-r--r--tests/auto/qscriptvalue/tst_qscriptvalue.cpp293
-rw-r--r--tests/auto/qscriptvalue/tst_qscriptvalue.h11
2 files changed, 173 insertions, 131 deletions
diff --git a/tests/auto/qscriptvalue/tst_qscriptvalue.cpp b/tests/auto/qscriptvalue/tst_qscriptvalue.cpp
index c7f496b..be51cf2 100644
--- a/tests/auto/qscriptvalue/tst_qscriptvalue.cpp
+++ b/tests/auto/qscriptvalue/tst_qscriptvalue.cpp
@@ -2562,83 +2562,89 @@ static QScriptValue returnInvalidValue(QScriptContext *, QScriptEngine *)
return QScriptValue();
}
-void tst_QScriptValue::call()
+void tst_QScriptValue::call_function()
{
QScriptEngine eng;
+ QScriptValue fun = eng.evaluate("(function() { return 1; })");
+ QVERIFY(fun.isFunction());
+ QScriptValue result = fun.call();
+ QVERIFY(result.isNumber());
+ QCOMPARE(result.toInt32(), 1);
+}
- {
- QScriptValue fun = eng.evaluate("(function() { return 1; })");
- QVERIFY(fun.isFunction());
- QScriptValue result = fun.call();
- QVERIFY(result.isNumber());
- QCOMPARE(result.toInt32(), 1);
- }
-
+void tst_QScriptValue::call_object()
+{
+ QScriptEngine eng;
QScriptValue Object = eng.evaluate("Object");
QCOMPARE(Object.isFunction(), true);
- {
- QScriptValue result = Object.call(Object);
- QCOMPARE(result.isObject(), true);
- }
+ QScriptValue result = Object.call(Object);
+ QCOMPARE(result.isObject(), true);
+}
+void tst_QScriptValue::call_newObjects()
+{
+ QScriptEngine eng;
// test that call() doesn't construct new objects
QScriptValue Number = eng.evaluate("Number");
+ QScriptValue Object = eng.evaluate("Object");
QCOMPARE(Object.isFunction(), true);
- {
- QScriptValueList args;
- args << QScriptValue(&eng, 123);
- QScriptValue result = Number.call(Object, args);
- QCOMPARE(result.strictlyEquals(args.at(0)), true);
- }
+ QScriptValueList args;
+ args << QScriptValue(&eng, 123);
+ QScriptValue result = Number.call(Object, args);
+ QCOMPARE(result.strictlyEquals(args.at(0)), true);
+}
+void tst_QScriptValue::call_this()
+{
+ QScriptEngine eng;
// test that correct "this" object is used
- {
- QScriptValue fun = eng.evaluate("(function() { return this; })");
- QCOMPARE(fun.isFunction(), true);
+ QScriptValue fun = eng.evaluate("(function() { return this; })");
+ QCOMPARE(fun.isFunction(), true);
- {
- QScriptValue numberObject = QScriptValue(&eng, 123.0).toObject();
- QScriptValue result = fun.call(numberObject);
- QCOMPARE(result.isObject(), true);
- QCOMPARE(result.toNumber(), 123.0);
- }
- }
+ QScriptValue numberObject = QScriptValue(&eng, 123.0).toObject();
+ QScriptValue result = fun.call(numberObject);
+ QCOMPARE(result.isObject(), true);
+ QCOMPARE(result.toNumber(), 123.0);
+}
+void tst_QScriptValue::call_arguments()
+{
+ QScriptEngine eng;
// test that correct arguments are passed
- {
- QScriptValue fun = eng.evaluate("(function() { return arguments[0]; })");
- QCOMPARE(fun.isFunction(), true);
- {
- QScriptValue result = fun.call(eng.undefinedValue());
- QCOMPARE(result.isUndefined(), true);
- }
-
- {
- QScriptValueList args;
- args << QScriptValue(&eng, 123.0);
- QScriptValue result = fun.call(eng.undefinedValue(), args);
- QCOMPARE(result.isNumber(), true);
- QCOMPARE(result.toNumber(), 123.0);
- }
-
- // V2 constructors
- {
- QScriptValueList args;
- args << QScriptValue(123.0);
- QScriptValue result = fun.call(eng.undefinedValue(), args);
- QCOMPARE(result.isNumber(), true);
- QCOMPARE(result.toNumber(), 123.0);
- }
- {
- QScriptValue args = eng.newArray();
- args.setProperty(0, 123);
- QScriptValue result = fun.call(eng.undefinedValue(), args);
- QVERIFY(result.isNumber());
- QCOMPARE(result.toNumber(), 123.0);
- }
+ QScriptValue fun = eng.evaluate("(function() { return arguments[0]; })");
+ QCOMPARE(fun.isFunction(), true);
+ {
+ QScriptValue result = fun.call(eng.undefinedValue());
+ QCOMPARE(result.isUndefined(), true);
+ }
+ {
+ QScriptValueList args;
+ args << QScriptValue(&eng, 123.0);
+ QScriptValue result = fun.call(eng.undefinedValue(), args);
+ QCOMPARE(result.isNumber(), true);
+ QCOMPARE(result.toNumber(), 123.0);
+ }
+ // V2 constructors
+ {
+ QScriptValueList args;
+ args << QScriptValue(123.0);
+ QScriptValue result = fun.call(eng.undefinedValue(), args);
+ QCOMPARE(result.isNumber(), true);
+ QCOMPARE(result.toNumber(), 123.0);
+ }
+ {
+ QScriptValue args = eng.newArray();
+ args.setProperty(0, 123);
+ QScriptValue result = fun.call(eng.undefinedValue(), args);
+ QVERIFY(result.isNumber());
+ QCOMPARE(result.toNumber(), 123.0);
}
+}
+void tst_QScriptValue::call()
+{
+ QScriptEngine eng;
{
QScriptValue fun = eng.evaluate("(function() { return arguments[1]; })");
QCOMPARE(fun.isFunction(), true);
@@ -2659,7 +2665,6 @@ void tst_QScriptValue::call()
QCOMPARE(result.toNumber(), 456.0);
}
}
-
{
QScriptValue fun = eng.evaluate("(function() { throw new Error('foo'); })");
QCOMPARE(fun.isFunction(), true);
@@ -2672,7 +2677,6 @@ void tst_QScriptValue::call()
QVERIFY(result.strictlyEquals(eng.uncaughtException()));
}
}
-
{
eng.clearExceptions();
QScriptValue fun = eng.newFunction(getArg);
@@ -2700,7 +2704,6 @@ void tst_QScriptValue::call()
QCOMPARE(result.toNumber(), 123.0);
}
}
-
{
QScriptValue fun = eng.newFunction(evaluateArg);
{
@@ -2712,11 +2715,12 @@ void tst_QScriptValue::call()
QCOMPARE(result.toNumber(), 123.0);
}
}
+}
- QScriptValue inv;
- QCOMPARE(inv.call().isValid(), false);
-
+void tst_QScriptValue::call_invalidArguments()
+{
// test that invalid arguments are handled gracefully
+ QScriptEngine eng;
{
QScriptValue fun = eng.newFunction(getArg);
{
@@ -2749,6 +2753,35 @@ void tst_QScriptValue::call()
QCOMPARE(qIsNaN(ret.toNumber()), true);
}
}
+}
+
+void tst_QScriptValue::call_invalidReturn()
+{
+ // test that invalid return value is handled gracefully
+ QScriptEngine eng;
+ QScriptValue fun = eng.newFunction(returnInvalidValue);
+ eng.globalObject().setProperty("returnInvalidValue", fun);
+ QScriptValue ret = eng.evaluate("returnInvalidValue() + returnInvalidValue()");
+ QCOMPARE(ret.isValid(), true);
+ QCOMPARE(ret.isNumber(), true);
+ QCOMPARE(qIsNaN(ret.toNumber()), true);
+}
+
+void tst_QScriptValue::call_twoEngines()
+{
+ QScriptEngine eng;
+ QScriptValue object = eng.evaluate("Object");
+ QScriptEngine otherEngine;
+ QScriptValue fun = otherEngine.evaluate("(function() { return 1; })");
+ QVERIFY(fun.isFunction());
+ QTest::ignoreMessage(QtWarningMsg, "QScriptValue::call() failed: "
+ "cannot call function with thisObject created in "
+ "a different engine");
+ QCOMPARE(fun.call(object).isValid(), false);
+ QTest::ignoreMessage(QtWarningMsg, "QScriptValue::call() failed: "
+ "cannot call function with argument created in "
+ "a different engine");
+ QCOMPARE(fun.call(QScriptValue(), QScriptValueList() << QScriptValue(&eng, 123)).isValid(), false);
{
QScriptValue fun = eng.evaluate("Object");
QVERIFY(fun.isFunction());
@@ -2759,76 +2792,74 @@ void tst_QScriptValue::call()
QTest::ignoreMessage(QtWarningMsg, "QScriptValue::call() failed: cannot call function with argument created in a different engine");
fun.call(QScriptValue(), args);
}
+}
- // test that invalid return value is handled gracefully
- {
- QScriptValue fun = eng.newFunction(returnInvalidValue);
- eng.globalObject().setProperty("returnInvalidValue", fun);
- QScriptValue ret = eng.evaluate("returnInvalidValue() + returnInvalidValue()");
- QCOMPARE(ret.isValid(), true);
- QCOMPARE(ret.isNumber(), true);
- QCOMPARE(qIsNaN(ret.toNumber()), true);
- }
+void tst_QScriptValue::call_array()
+{
+ 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));
+ // call with single array object as arguments
+ QScriptValue ret = fun.call(QScriptValue(), array);
+ QVERIFY(!eng.hasUncaughtException());
+ QCOMPARE(ret.isError(), false);
+ 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);
+ // call with arguments object as arguments
+ QScriptValue ret2 = fun.call(QScriptValue(), ret);
+ QCOMPARE(ret2.isError(), false);
+ 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);
+ // call with null as arguments
+ QScriptValue ret3 = fun.call(QScriptValue(), eng.nullValue());
+ QCOMPARE(ret3.isError(), false);
+ QCOMPARE(ret3.property("length").isNumber(), true);
+ QCOMPARE(ret3.property("length").toNumber(), 0.0);
+ // call with undefined as arguments
+ QScriptValue ret4 = fun.call(QScriptValue(), eng.undefinedValue());
+ QCOMPARE(ret4.isError(), false);
+ QCOMPARE(ret4.property("length").isNumber(), true);
+ QCOMPARE(ret4.property("length").toNumber(), 0.0);
+ // call with something else as arguments
+ QScriptValue ret5 = fun.call(QScriptValue(), QScriptValue(&eng, 123.0));
+ QCOMPARE(ret5.isError(), true);
+ // call with a non-array object as arguments
+ QScriptValue ret6 = fun.call(QScriptValue(), eng.globalObject());
+ QVERIFY(ret6.isError());
+ QCOMPARE(ret6.toString(), QString::fromLatin1("TypeError: Arguments must be an array"));
+}
+
+
+void tst_QScriptValue::call_nonFunction_data()
+{
+ newEngine();
+ QTest::addColumn<QScriptValue>("value");
- {
- QScriptEngine otherEngine;
- QScriptValue fun = otherEngine.evaluate("(function() { return 1; })");
- QVERIFY(fun.isFunction());
- QTest::ignoreMessage(QtWarningMsg, "QScriptValue::call() failed: "
- "cannot call function with thisObject created in "
- "a different engine");
- QCOMPARE(fun.call(Object).isValid(), false);
- QTest::ignoreMessage(QtWarningMsg, "QScriptValue::call() failed: "
- "cannot call function with argument created in "
- "a different engine");
- QCOMPARE(fun.call(QScriptValue(), QScriptValueList() << QScriptValue(&eng, 123)).isValid(), false);
- }
+ 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);
- {
- 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));
- // call with single array object as arguments
- QScriptValue ret = fun.call(QScriptValue(), array);
- QVERIFY(!eng.hasUncaughtException());
- QCOMPARE(ret.isError(), false);
- 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);
- // call with arguments object as arguments
- QScriptValue ret2 = fun.call(QScriptValue(), ret);
- QCOMPARE(ret2.isError(), false);
- 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);
- // call with null as arguments
- QScriptValue ret3 = fun.call(QScriptValue(), eng.nullValue());
- QCOMPARE(ret3.isError(), false);
- QCOMPARE(ret3.property("length").isNumber(), true);
- QCOMPARE(ret3.property("length").toNumber(), 0.0);
- // call with undefined as arguments
- QScriptValue ret4 = fun.call(QScriptValue(), eng.undefinedValue());
- QCOMPARE(ret4.isError(), false);
- QCOMPARE(ret4.property("length").isNumber(), true);
- QCOMPARE(ret4.property("length").toNumber(), 0.0);
- // call with something else as arguments
- QScriptValue ret5 = fun.call(QScriptValue(), QScriptValue(&eng, 123.0));
- QCOMPARE(ret5.isError(), true);
- // call with a non-array object as arguments
- QScriptValue ret6 = fun.call(QScriptValue(), eng.globalObject());
- QVERIFY(ret6.isError());
- QCOMPARE(ret6.toString(), QString::fromLatin1("TypeError: Arguments must be an array"));
- }
+ 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::call_nonFunction()
+{
// calling things that are not functions
- QVERIFY(!QScriptValue(false).call().isValid());
- QVERIFY(!QScriptValue(123).call().isValid());
- QVERIFY(!QScriptValue(QString::fromLatin1("ciao")).call().isValid());
- QVERIFY(!QScriptValue(QScriptValue::UndefinedValue).call().isValid());
- QVERIFY(!QScriptValue(QScriptValue::NullValue).call().isValid());
+ QFETCH(QScriptValue, value);
+ QVERIFY(!value.call().isValid());
}
static QScriptValue ctorReturningUndefined(QScriptContext *ctx, QScriptEngine *)
diff --git a/tests/auto/qscriptvalue/tst_qscriptvalue.h b/tests/auto/qscriptvalue/tst_qscriptvalue.h
index 977b3a5..45a109e 100644
--- a/tests/auto/qscriptvalue/tst_qscriptvalue.h
+++ b/tests/auto/qscriptvalue/tst_qscriptvalue.h
@@ -140,7 +140,18 @@ private slots:
void getSetScriptClass_JSObjectFromJS();
void getSetScriptClass_QVariant();
void getSetScriptClass_QObject();
+ void call_function();
+ void call_object();
+ void call_newObjects();
+ void call_this();
+ void call_arguments();
void call();
+ void call_invalidArguments();
+ void call_invalidReturn();
+ void call_twoEngines();
+ void call_array();
+ void call_nonFunction_data();
+ void call_nonFunction();
void construct();
void construct_constructorThrowsPrimitive();
void castToPointer();