diff options
author | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2017-08-01 16:31:53 +0200 |
---|---|---|
committer | Jani Heikkinen <jani.heikkinen@qt.io> | 2018-10-30 12:41:54 +0000 |
commit | 7f0db6d0472037a3ec005f9066f6222d78de60e1 (patch) | |
tree | 30e2acee27523678f56ae2a9dc61847cace7c287 /tests/auto/qml/qjsengine/tst_qjsengine.cpp | |
parent | b190d0d160e1cf5bbe65f8ff54752e8a7d33b49b (diff) |
Add convenience functions for creating and inspecting error objects
Serves to simplify porting from QtScript by replacing
QScriptContext::Error and QScriptContext::throwError().
Change-Id: I4bfe404c358c50aaf3b5469a4304fec97552bf24
Reviewed-by: Paul Wicking <paul.wicking@qt.io>
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Diffstat (limited to 'tests/auto/qml/qjsengine/tst_qjsengine.cpp')
-rw-r--r-- | tests/auto/qml/qjsengine/tst_qjsengine.cpp | 63 |
1 files changed, 60 insertions, 3 deletions
diff --git a/tests/auto/qml/qjsengine/tst_qjsengine.cpp b/tests/auto/qml/qjsengine/tst_qjsengine.cpp index a38224ef81..00c631141b 100644 --- a/tests/auto/qml/qjsengine/tst_qjsengine.cpp +++ b/tests/auto/qml/qjsengine/tst_qjsengine.cpp @@ -224,6 +224,8 @@ private slots: void multilineStrings(); void throwError(); + void throwErrorObject(); + void returnError(); void mathMinMax(); void importModule(); @@ -232,7 +234,9 @@ private slots: void importExportErrors(); public: - Q_INVOKABLE QJSValue throwingCppMethod(); + Q_INVOKABLE QJSValue throwingCppMethod1(); + Q_INVOKABLE void throwingCppMethod2(); + Q_INVOKABLE QJSValue throwingCppMethod3(); signals: void testSignal(); @@ -4384,7 +4388,7 @@ void tst_QJSEngine::throwError() QJSValue result = engine.evaluate( "function test(){\n" "try {\n" - " return testCase.throwingCppMethod();\n" + " return testCase.throwingCppMethod1();\n" "} catch (error) {\n" " return error;\n" "}\n" @@ -4393,17 +4397,70 @@ void tst_QJSEngine::throwError() "test();" ); QVERIFY(result.isError()); + QCOMPARE(result.errorType(), QJSValue::GenericError); QCOMPARE(result.property("lineNumber").toString(), "3"); QCOMPARE(result.property("message").toString(), "blub"); QVERIFY(!result.property("stack").isUndefined()); } -QJSValue tst_QJSEngine::throwingCppMethod() +void tst_QJSEngine::throwErrorObject() +{ + QJSEngine engine; + QJSValue wrappedThis = engine.newQObject(this); + QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership); + engine.globalObject().setProperty("testCase", wrappedThis); + + QJSValue result = engine.evaluate( + "function test(){\n" + " try {\n" + " testCase.throwingCppMethod2();\n" + " } catch (error) {\n" + " if (error instanceof TypeError) {\n" + " return error;\n" + " }\n" + " }\n" + " return null;\n" + "}\n" + "test();" + ); + QVERIFY(result.isError()); + QCOMPARE(result.errorType(), QJSValue::TypeError); + QCOMPARE(result.property("lineNumber").toString(), "3"); + QCOMPARE(result.property("message").toString(), "Wrong type"); + QVERIFY(!result.property("stack").isUndefined()); +} + +void tst_QJSEngine::returnError() +{ + QJSEngine engine; + QJSValue wrappedThis = engine.newQObject(this); + QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership); + engine.globalObject().setProperty("testCase", wrappedThis); + + QJSValue result = engine.evaluate("testCase.throwingCppMethod3()"); + QVERIFY(result.isError()); + QCOMPARE(result.errorType(), QJSValue::EvalError); + QCOMPARE(result.property("lineNumber").toString(), "1"); + QCOMPARE(result.property("message").toString(), "Something is wrong"); + QVERIFY(!result.property("stack").isUndefined()); +} + +QJSValue tst_QJSEngine::throwingCppMethod1() { qjsEngine(this)->throwError("blub"); return QJSValue(47); } +void tst_QJSEngine::throwingCppMethod2() +{ + qjsEngine(this)->throwError(QJSValue::TypeError, "Wrong type"); +} + +QJSValue tst_QJSEngine::throwingCppMethod3() +{ + return qjsEngine(this)->newErrorObject(QJSValue::EvalError, "Something is wrong"); +} + void tst_QJSEngine::mathMinMax() { QJSEngine engine; |