diff options
author | Jedrzej Nowacki <jedrzej.nowacki@nokia.com> | 2010-11-03 08:53:20 (GMT) |
---|---|---|
committer | Jedrzej Nowacki <jedrzej.nowacki@nokia.com> | 2010-11-03 09:57:49 (GMT) |
commit | c04c3fe2bc6a7ffd02052e0c0208b8716907063a (patch) | |
tree | 278cd71dc72aace878ded3c2b6e843594dd0258e /tests | |
parent | ec76f77b1ed567c36ca33923996c007c72e928ea (diff) | |
download | Qt-c04c3fe2bc6a7ffd02052e0c0208b8716907063a.zip Qt-c04c3fe2bc6a7ffd02052e0c0208b8716907063a.tar.gz Qt-c04c3fe2bc6a7ffd02052e0c0208b8716907063a.tar.bz2 |
Refactor tst_QScriptContext::throwError test.
The test should use a data set instead of executing the same code block
one by one.
New function returning a string was added to the check.
New case, replacing QSE::evaluate call by QSV::call, was added.
Reviewed-by: Kent Hansen
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/qscriptcontext/tst_qscriptcontext.cpp | 120 |
1 files changed, 61 insertions, 59 deletions
diff --git a/tests/auto/qscriptcontext/tst_qscriptcontext.cpp b/tests/auto/qscriptcontext/tst_qscriptcontext.cpp index 99558eb..cbcd16a 100644 --- a/tests/auto/qscriptcontext/tst_qscriptcontext.cpp +++ b/tests/auto/qscriptcontext/tst_qscriptcontext.cpp @@ -68,7 +68,11 @@ private slots: void arguments(); void thisObject(); void returnValue(); - void throwError(); + void throwError_data(); + void throwError_fromEvaluate_data(); + void throwError_fromEvaluate(); + void throwError_fromCpp_data(); + void throwError_fromCpp(); void throwValue(); void evaluateInFunction(); void pushAndPopContext(); @@ -362,73 +366,71 @@ static QScriptValue throw_ErrorAndReturnUndefined(QScriptContext *ctx, QScriptEn return eng->undefinedValue(); } -void tst_QScriptContext::throwError() +static QScriptValue throw_ErrorAndReturnString(QScriptContext *ctx, QScriptEngine *) { - QScriptEngine eng; + return ctx->throwError(QScriptContext::UnknownError, "foo").toString(); +} - { - QScriptValue fun = eng.newFunction(throw_Error); - eng.globalObject().setProperty("throw_Error", fun); - QScriptValue result = eng.evaluate("throw_Error()"); - QCOMPARE(eng.hasUncaughtException(), true); - QCOMPARE(result.isError(), true); - QCOMPARE(result.toString(), QString("Error: foo")); - } +static QScriptValue throw_ErrorAndReturnObject(QScriptContext *ctx, QScriptEngine *eng) +{ + ctx->throwError(QScriptContext::UnknownError, "foo"); + return eng->newObject(); +} - { - QScriptValue fun = eng.newFunction(throw_TypeError); - eng.globalObject().setProperty("throw_TypeError", fun); - QScriptValue result = eng.evaluate("throw_TypeError()"); - QCOMPARE(eng.hasUncaughtException(), true); - QCOMPARE(result.isError(), true); - QCOMPARE(result.toString(), QString("TypeError: foo")); - } +void tst_QScriptContext::throwError_data() +{ + QTest::addColumn<void*>("nativeFunctionPtr"); + QTest::addColumn<QString>("stringRepresentation"); + + QTest::newRow("Error") << reinterpret_cast<void*>(throw_Error) << QString("Error: foo"); + QTest::newRow("TypeError") << reinterpret_cast<void*>(throw_TypeError) << QString("TypeError: foo"); + QTest::newRow("ReferenceError") << reinterpret_cast<void*>(throw_ReferenceError) << QString("ReferenceError: foo"); + QTest::newRow("SyntaxError") << reinterpret_cast<void*>(throw_SyntaxError) << QString("SyntaxError: foo"); + QTest::newRow("RangeError") << reinterpret_cast<void*>(throw_RangeError) << QString("RangeError: foo"); + QTest::newRow("URIError") << reinterpret_cast<void*>(throw_URIError) << QString("URIError: foo"); + QTest::newRow("ErrorAndReturnUndefined") << reinterpret_cast<void*>(throw_ErrorAndReturnUndefined) << QString("Error: foo"); + QTest::newRow("ErrorAndReturnString") << reinterpret_cast<void*>(throw_ErrorAndReturnString) << QString("Error: foo"); + QTest::newRow("ErrorAndReturnObject") << reinterpret_cast<void*>(throw_ErrorAndReturnObject) << QString("Error: foo"); +} - { - QScriptValue fun = eng.newFunction(throw_ReferenceError); - eng.globalObject().setProperty("throw_ReferenceError", fun); - QScriptValue result = eng.evaluate("throw_ReferenceError()"); - QCOMPARE(eng.hasUncaughtException(), true); - QCOMPARE(result.isError(), true); - QCOMPARE(result.toString(), QString("ReferenceError: foo")); - } +void tst_QScriptContext::throwError_fromEvaluate_data() +{ + throwError_data(); +} - { - QScriptValue fun = eng.newFunction(throw_SyntaxError); - eng.globalObject().setProperty("throw_SyntaxError", fun); - QScriptValue result = eng.evaluate("throw_SyntaxError()"); - QCOMPARE(eng.hasUncaughtException(), true); - QCOMPARE(result.isError(), true); - QCOMPARE(result.toString(), QString("SyntaxError: foo")); - } +void tst_QScriptContext::throwError_fromEvaluate() +{ + QFETCH(void*, nativeFunctionPtr); + QScriptEngine::FunctionSignature nativeFunction = reinterpret_cast<QScriptEngine::FunctionSignature>(nativeFunctionPtr); + QFETCH(QString, stringRepresentation); + QScriptEngine engine; - { - QScriptValue fun = eng.newFunction(throw_RangeError); - eng.globalObject().setProperty("throw_RangeError", fun); - QScriptValue result = eng.evaluate("throw_RangeError()"); - QCOMPARE(eng.hasUncaughtException(), true); - QCOMPARE(result.isError(), true); - QCOMPARE(result.toString(), QString("RangeError: foo")); - } + QScriptValue fun = engine.newFunction(nativeFunction); + engine.globalObject().setProperty("throw_Error", fun); + QScriptValue result = engine.evaluate("throw_Error()"); + QCOMPARE(engine.hasUncaughtException(), true); + QCOMPARE(result.isError(), true); + QCOMPARE(result.toString(), stringRepresentation); +} - { - QScriptValue fun = eng.newFunction(throw_URIError); - eng.globalObject().setProperty("throw_URIError", fun); - QScriptValue result = eng.evaluate("throw_URIError()"); - QCOMPARE(eng.hasUncaughtException(), true); - QCOMPARE(result.isError(), true); - QCOMPARE(result.toString(), QString("URIError: foo")); - } +void tst_QScriptContext::throwError_fromCpp_data() +{ + throwError_data(); +} - { - QScriptValue fun = eng.newFunction(throw_ErrorAndReturnUndefined); - eng.globalObject().setProperty("throw_ErrorAndReturnUndefined", fun); - QScriptValue result = eng.evaluate("throw_ErrorAndReturnUndefined()"); - QVERIFY(eng.hasUncaughtException()); - QVERIFY(result.isError()); - QCOMPARE(result.toString(), QString("Error: foo")); - } +void tst_QScriptContext::throwError_fromCpp() +{ + QFETCH(void*, nativeFunctionPtr); + QScriptEngine::FunctionSignature nativeFunction = reinterpret_cast<QScriptEngine::FunctionSignature>(nativeFunctionPtr); + QFETCH(QString, stringRepresentation); + QScriptEngine engine; + QScriptValue fun = engine.newFunction(nativeFunction); + engine.globalObject().setProperty("throw_Error", fun); + QScriptValue result = fun.call(); + QCOMPARE(engine.hasUncaughtException(), true); + QCOMPARE(result.isError(), true); + QCOMPARE(result.toString(), stringRepresentation); } static QScriptValue throw_value(QScriptContext *ctx, QScriptEngine *) |