From 9d6069f32962128f737b83a4bf7d7e7309983023 Mon Sep 17 00:00:00 2001 From: Kent Hansen Date: Mon, 15 Jun 2009 17:34:33 +0200 Subject: add a bunch of qtscript autotests Get that code coverage up. --- tests/auto/qscriptable/tst_qscriptable.cpp | 18 ++- tests/auto/qscriptcontext/tst_qscriptcontext.cpp | 18 +++ .../qscriptcontextinfo/tst_qscriptcontextinfo.cpp | 20 ++++ tests/auto/qscriptengine/tst_qscriptengine.cpp | 128 +++++++++++++++++++++ tests/auto/qscriptqobject/tst_qscriptqobject.cpp | 16 +++ tests/auto/qscriptvalue/tst_qscriptvalue.cpp | 34 +++++- 6 files changed, 232 insertions(+), 2 deletions(-) diff --git a/tests/auto/qscriptable/tst_qscriptable.cpp b/tests/auto/qscriptable/tst_qscriptable.cpp index 5eabbee..1dd975a 100644 --- a/tests/auto/qscriptable/tst_qscriptable.cpp +++ b/tests/auto/qscriptable/tst_qscriptable.cpp @@ -49,7 +49,7 @@ //TESTED_CLASS= //TESTED_FILES= -class MyScriptable : public QObject, protected QScriptable +class MyScriptable : public QObject, public QScriptable { Q_OBJECT Q_PROPERTY(int baz READ baz WRITE setBaz) @@ -87,6 +87,7 @@ public slots: QObject *zab(); QObject *setZab(QObject *); QScriptValue getArguments(); + int getArgumentCount(); signals: void sig(int); @@ -128,6 +129,11 @@ QScriptValue MyScriptable::getArguments() return context()->argumentsObject(); } +int MyScriptable::getArgumentCount() +{ + return context()->argumentCount(); +} + void MyScriptable::foo() { m_lastEngine = engine(); @@ -224,6 +230,8 @@ void tst_QScriptable::cleanupTestCase() void tst_QScriptable::engine() { + QCOMPARE(m_scriptable.engine(), (QScriptEngine*)0); + QCOMPARE(m_scriptable.context(), (QScriptContext*)0); QCOMPARE(m_scriptable.lastEngine(), (QScriptEngine *)0); // reading property @@ -360,6 +368,14 @@ void tst_QScriptable::arguments() QVERIFY(args.property("1").strictlyEquals(QScriptValue(&m_engine, 20))); QVERIFY(args.property("2").strictlyEquals(QScriptValue(&m_engine, 30))); QVERIFY(args.property("3").strictlyEquals(QScriptValue(&m_engine, "hi"))); + + QScriptValue argc = m_engine.evaluate("scriptable.getArgumentCount(1, 2, 3)"); + QVERIFY(argc.isNumber()); + QCOMPARE(argc.toInt32(), 3); + + QCOMPARE(m_scriptable.argumentCount(), -1); + QVERIFY(!m_scriptable.argument(-1).isValid()); + QVERIFY(!m_scriptable.argument(0).isValid()); } void tst_QScriptable::throwError() diff --git a/tests/auto/qscriptcontext/tst_qscriptcontext.cpp b/tests/auto/qscriptcontext/tst_qscriptcontext.cpp index bfcd6ee..e2eb8cf0 100644 --- a/tests/auto/qscriptcontext/tst_qscriptcontext.cpp +++ b/tests/auto/qscriptcontext/tst_qscriptcontext.cpp @@ -72,6 +72,7 @@ private slots: void scopeChain(); void pushAndPopScope(); void getSetActivationObject(); + void toString(); }; tst_QScriptContext::tst_QScriptContext() @@ -438,6 +439,7 @@ void tst_QScriptContext::pushAndPopContext() QCOMPARE(ctx->isCalledAsConstructor(), false); QCOMPARE(ctx->argumentCount(), 0); QCOMPARE(ctx->argument(0).isUndefined(), true); + QVERIFY(!ctx->argument(-1).isValid()); QCOMPARE(ctx->argumentsObject().isObject(), true); QCOMPARE(ctx->activationObject().isObject(), true); QCOMPARE(ctx->callee().isValid(), false); @@ -687,5 +689,21 @@ void tst_QScriptContext::getSetActivationObject() } } +static QScriptValue parentContextToString(QScriptContext *ctx, QScriptEngine *) +{ + return ctx->parentContext()->toString(); +} + +void tst_QScriptContext::toString() +{ + QScriptEngine eng; + eng.globalObject().setProperty("parentContextToString", eng.newFunction(parentContextToString)); + QScriptValue ret = eng.evaluate("function foo(first, second, third) {\n" + " return parentContextToString();\n" + "}; foo(1, 2, 3)", "script.qs"); + QVERIFY(ret.isString()); + QCOMPARE(ret.toString(), QString::fromLatin1("foo (first=1, second=2, third=3) at script.qs:2")); +} + QTEST_MAIN(tst_QScriptContext) #include "tst_qscriptcontext.moc" diff --git a/tests/auto/qscriptcontextinfo/tst_qscriptcontextinfo.cpp b/tests/auto/qscriptcontextinfo/tst_qscriptcontextinfo.cpp index df1e14f..82c66f8 100644 --- a/tests/auto/qscriptcontextinfo/tst_qscriptcontextinfo.cpp +++ b/tests/auto/qscriptcontextinfo/tst_qscriptcontextinfo.cpp @@ -95,6 +95,7 @@ private slots: void builtinFunctionNames(); void nullContext(); void streaming(); + void assignmentAndComparison(); }; tst_QScriptContextInfo::tst_QScriptContextInfo() @@ -553,5 +554,24 @@ void tst_QScriptContextInfo::streaming() } } +void tst_QScriptContextInfo::assignmentAndComparison() +{ + QScriptEngine eng; + eng.globalObject().setProperty("getContextInfoList", eng.newFunction(getContextInfoList)); + QString fileName = "ciao.qs"; + int lineNumber = 456; + QScriptValue ret = eng.evaluate("function bar(a, b, c) {\n return getContextInfoList();\n}\nbar()", + fileName, lineNumber); + QList lst = qscriptvalue_cast >(ret); + QCOMPARE(lst.size(), 3); + QScriptContextInfo ci = lst.at(0); + QScriptContextInfo same = ci; + QVERIFY(ci == same); + QVERIFY(!(ci != same)); + QScriptContextInfo other = lst.at(1); + QVERIFY(!(ci == other)); + QVERIFY(ci != other); +} + QTEST_MAIN(tst_QScriptContextInfo) #include "tst_qscriptcontextinfo.moc" diff --git a/tests/auto/qscriptengine/tst_qscriptengine.cpp b/tests/auto/qscriptengine/tst_qscriptengine.cpp index fe60cd0..230f889 100644 --- a/tests/auto/qscriptengine/tst_qscriptengine.cpp +++ b/tests/auto/qscriptengine/tst_qscriptengine.cpp @@ -64,6 +64,7 @@ public: virtual ~tst_QScriptEngine(); private slots: + void constructWithParent(); void currentContext(); void pushPopContext(); void getSetDefaultPrototype(); @@ -119,6 +120,7 @@ private slots: void getSetAgent(); void reentrancy(); void incDecNonObjectProperty(); + void installTranslatorFunctions(); }; tst_QScriptEngine::tst_QScriptEngine() @@ -129,6 +131,17 @@ tst_QScriptEngine::~tst_QScriptEngine() { } +void tst_QScriptEngine::constructWithParent() +{ + QPointer ptr; + { + QObject obj; + QScriptEngine *engine = new QScriptEngine(&obj); + ptr = engine; + } + QVERIFY(ptr == 0); +} + void tst_QScriptEngine::currentContext() { QScriptEngine eng; @@ -945,6 +958,22 @@ void tst_QScriptEngine::checkSyntax() QCOMPARE(result.errorLineNumber(), errorLineNumber); QCOMPARE(result.errorColumnNumber(), errorColumnNumber); QCOMPARE(result.errorMessage(), errorMessage); + + // assignment + { + QScriptSyntaxCheckResult copy = result; + QCOMPARE(copy.state(), result.state()); + QCOMPARE(copy.errorLineNumber(), result.errorLineNumber()); + QCOMPARE(copy.errorColumnNumber(), result.errorColumnNumber()); + QCOMPARE(copy.errorMessage(), result.errorMessage()); + } + { + QScriptSyntaxCheckResult copy(result); + QCOMPARE(copy.state(), result.state()); + QCOMPARE(copy.errorLineNumber(), result.errorLineNumber()); + QCOMPARE(copy.errorColumnNumber(), result.errorColumnNumber()); + QCOMPARE(copy.errorMessage(), result.errorMessage()); + } } void tst_QScriptEngine::canEvaluate_data() @@ -1467,6 +1496,61 @@ void tst_QScriptEngine::valueConversion() Foo foo = qScriptValueToValue(str); QCOMPARE(foo.x, 123); } + + // more built-in types + { + QScriptValue val = qScriptValueFromValue(&eng, uint(123)); + QVERIFY(val.isNumber()); + QCOMPARE(val.toInt32(), 123); + } + { + QScriptValue val = qScriptValueFromValue(&eng, qulonglong(123)); + QVERIFY(val.isNumber()); + QCOMPARE(val.toInt32(), 123); + } + { + QScriptValue val = qScriptValueFromValue(&eng, float(123)); + QVERIFY(val.isNumber()); + QCOMPARE(val.toInt32(), 123); + } + { + QScriptValue val = qScriptValueFromValue(&eng, short(123)); + QVERIFY(val.isNumber()); + QCOMPARE(val.toInt32(), 123); + } + { + QScriptValue val = qScriptValueFromValue(&eng, ushort(123)); + QVERIFY(val.isNumber()); + QCOMPARE(val.toInt32(), 123); + } + { + QScriptValue val = qScriptValueFromValue(&eng, char(123)); + QVERIFY(val.isNumber()); + QCOMPARE(val.toInt32(), 123); + } + { + QScriptValue val = qScriptValueFromValue(&eng, uchar(123)); + QVERIFY(val.isNumber()); + QCOMPARE(val.toInt32(), 123); + } + { + QDateTime in = QDateTime::currentDateTime(); + QScriptValue val = qScriptValueFromValue(&eng, in); + QVERIFY(val.isDate()); + QCOMPARE(val.toDateTime(), in); + } + { + QDate in = QDate::currentDate(); + QScriptValue val = qScriptValueFromValue(&eng, in); + QVERIFY(val.isDate()); + QCOMPARE(val.toDateTime().date(), in); + } + { + QRegExp in = QRegExp("foo"); + QScriptValue val = qScriptValueFromValue(&eng, in); + QVERIFY(val.isRegExp()); + QCOMPARE(val.toRegExp(), in); + } } static QScriptValue __import__(QScriptContext *ctx, QScriptEngine *eng) @@ -3415,5 +3499,49 @@ void tst_QScriptEngine:: incDecNonObjectProperty() } } +void tst_QScriptEngine::installTranslatorFunctions() +{ + QScriptEngine eng; + QScriptValue global = eng.globalObject(); + QVERIFY(!global.property("qsTranslate").isValid()); + QVERIFY(!global.property("QT_TRANSLATE_NOOP").isValid()); + QVERIFY(!global.property("qsTr").isValid()); + QVERIFY(!global.property("QT_TR_NOOP").isValid()); + QVERIFY(!global.property("String").property("prototype").property("arg").isValid()); + + eng.installTranslatorFunctions(); + QVERIFY(global.property("qsTranslate").isFunction()); + QVERIFY(global.property("QT_TRANSLATE_NOOP").isFunction()); + QVERIFY(global.property("qsTr").isFunction()); + QVERIFY(global.property("QT_TR_NOOP").isFunction()); + QVERIFY(global.property("String").property("prototype").property("arg").isFunction()); + + { + QScriptValue ret = eng.evaluate("qsTr('foo')"); + QVERIFY(ret.isString()); + QCOMPARE(ret.toString(), QString::fromLatin1("foo")); + } + { + QScriptValue ret = eng.evaluate("qsTranslate('foo', 'bar')"); + QVERIFY(ret.isString()); + QCOMPARE(ret.toString(), QString::fromLatin1("bar")); + } + { + QScriptValue ret = eng.evaluate("QT_TR_NOOP('foo')"); + QVERIFY(ret.isString()); + QCOMPARE(ret.toString(), QString::fromLatin1("foo")); + } + { + QScriptValue ret = eng.evaluate("QT_TRANSLATE_NOOP('foo', 'bar')"); + QVERIFY(ret.isString()); + QCOMPARE(ret.toString(), QString::fromLatin1("bar")); + } + { + QScriptValue ret = eng.evaluate("'foo%0'.arg('bar')"); + QVERIFY(ret.isString()); + QCOMPARE(ret.toString(), QString::fromLatin1("foobar")); + } +} + QTEST_MAIN(tst_QScriptEngine) #include "tst_qscriptengine.moc" diff --git a/tests/auto/qscriptqobject/tst_qscriptqobject.cpp b/tests/auto/qscriptqobject/tst_qscriptqobject.cpp index 2c47c49..24c283e 100644 --- a/tests/auto/qscriptqobject/tst_qscriptqobject.cpp +++ b/tests/auto/qscriptqobject/tst_qscriptqobject.cpp @@ -1906,6 +1906,22 @@ void tst_QScriptExtQObject::cppConnectAndDisconnect() QVERIFY(qScriptDisconnect(m_myObject, SIGNAL(mySignal()), QScriptValue(), fun)); QCOMPARE(m_myObject->disconnectedSignal().constData(), SIGNAL(mySignal())); } + + // bad args + QVERIFY(!qScriptConnect(0, SIGNAL(foo()), QScriptValue(), fun)); + QVERIFY(!qScriptConnect(&edit, 0, QScriptValue(), fun)); + QVERIFY(!qScriptConnect(&edit, SIGNAL(foo()), QScriptValue(), fun)); + QVERIFY(!qScriptConnect(&edit, SIGNAL(textChanged(QString)), QScriptValue(), QScriptValue())); + QVERIFY(!qScriptDisconnect(0, SIGNAL(foo()), QScriptValue(), fun)); + QVERIFY(!qScriptDisconnect(&edit, 0, QScriptValue(), fun)); + QVERIFY(!qScriptDisconnect(&edit, SIGNAL(foo()), QScriptValue(), fun)); + QVERIFY(!qScriptDisconnect(&edit, SIGNAL(textChanged(QString)), QScriptValue(), QScriptValue())); + { + QScriptEngine eng2; + QScriptValue receiverInDifferentEngine = eng2.newObject(); + QVERIFY(!qScriptConnect(&edit, SIGNAL(textChanged(QString)), receiverInDifferentEngine, fun)); + QVERIFY(!qScriptDisconnect(&edit, SIGNAL(textChanged(QString)), receiverInDifferentEngine, fun)); + } } void tst_QScriptExtQObject::classEnums() diff --git a/tests/auto/qscriptvalue/tst_qscriptvalue.cpp b/tests/auto/qscriptvalue/tst_qscriptvalue.cpp index ad1080a..7901b65 100644 --- a/tests/auto/qscriptvalue/tst_qscriptvalue.cpp +++ b/tests/auto/qscriptvalue/tst_qscriptvalue.cpp @@ -1482,21 +1482,43 @@ void tst_QScriptValue::toObject() { QScriptValue undefined = QScriptValue(QScriptValue::UndefinedValue); QVERIFY(!undefined.toObject().isValid()); + QVERIFY(!eng.toObject(undefined).isValid()); QScriptValue null = QScriptValue(QScriptValue::NullValue); QVERIFY(!null.toObject().isValid()); + QVERIFY(!eng.toObject(null).isValid()); QScriptValue falskt = QScriptValue(false); QVERIFY(!falskt.toObject().isValid()); + { + QScriptValue tmp = eng.toObject(falskt); + QVERIFY(tmp.isObject()); + QVERIFY(tmp.toBool()); + } QScriptValue sant = QScriptValue(true); QVERIFY(!sant.toObject().isValid()); + { + QScriptValue tmp = eng.toObject(sant); + QVERIFY(tmp.isObject()); + QVERIFY(tmp.toBool()); + } QScriptValue number = QScriptValue(123.0); QVERIFY(!number.toObject().isValid()); + { + QScriptValue tmp = eng.toObject(number); + QVERIFY(tmp.isObject()); + QCOMPARE(tmp.toInt32(), number.toInt32()); + } - QScriptValue str = QScriptValue(QString("ciao")); + QScriptValue str = QScriptValue(QString::fromLatin1("ciao")); QVERIFY(!str.toObject().isValid()); + { + QScriptValue tmp = eng.toObject(str); + QVERIFY(tmp.isObject()); + QCOMPARE(tmp.toString(), QString::fromLatin1("ciao")); + } } } @@ -2360,6 +2382,16 @@ void tst_QScriptValue::call() QCOMPARE(qIsNaN(ret.toNumber()), true); } } + { + QScriptValue fun = eng.evaluate("Object"); + QVERIFY(fun.isFunction()); + QScriptEngine eng2; + QScriptValue objectInDifferentEngine = eng2.newObject(); + QScriptValueList args; + args << objectInDifferentEngine; + 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 { -- cgit v0.12