From c04c3fe2bc6a7ffd02052e0c0208b8716907063a Mon Sep 17 00:00:00 2001 From: Jedrzej Nowacki Date: Wed, 3 Nov 2010 10:53:20 +0200 Subject: 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 --- tests/auto/qscriptcontext/tst_qscriptcontext.cpp | 120 ++++++++++++----------- 1 file 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("nativeFunctionPtr"); + QTest::addColumn("stringRepresentation"); + + QTest::newRow("Error") << reinterpret_cast(throw_Error) << QString("Error: foo"); + QTest::newRow("TypeError") << reinterpret_cast(throw_TypeError) << QString("TypeError: foo"); + QTest::newRow("ReferenceError") << reinterpret_cast(throw_ReferenceError) << QString("ReferenceError: foo"); + QTest::newRow("SyntaxError") << reinterpret_cast(throw_SyntaxError) << QString("SyntaxError: foo"); + QTest::newRow("RangeError") << reinterpret_cast(throw_RangeError) << QString("RangeError: foo"); + QTest::newRow("URIError") << reinterpret_cast(throw_URIError) << QString("URIError: foo"); + QTest::newRow("ErrorAndReturnUndefined") << reinterpret_cast(throw_ErrorAndReturnUndefined) << QString("Error: foo"); + QTest::newRow("ErrorAndReturnString") << reinterpret_cast(throw_ErrorAndReturnString) << QString("Error: foo"); + QTest::newRow("ErrorAndReturnObject") << reinterpret_cast(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(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(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 *) -- cgit v0.12