diff --git a/tests/script-engine/src/ScriptEngineTests.cpp b/tests/script-engine/src/ScriptEngineTests.cpp index 0d53aa6f24..3efc763e5e 100644 --- a/tests/script-engine/src/ScriptEngineTests.cpp +++ b/tests/script-engine/src/ScriptEngineTests.cpp @@ -32,6 +32,9 @@ QTEST_MAIN(ScriptEngineTests) + + + void ScriptEngineTests::initTestCase() { // AudioClient starts networking, but for the purposes of the tests here we don't care, // so just got to use some port. @@ -175,6 +178,47 @@ void ScriptEngineTests::testJSThrow() { QVERIFY(runtime_ex && runtime_ex->thrownValue.toInt32() == 42); } +void ScriptEngineTests::testRegisterClass() { + QString printed; + auto sm = makeManager("print(testClass.invokableFunc(4)); Script.stop(true);", "testClass.js"); + connect(sm.get(), &ScriptManager::printedMessage, [&printed](const QString& message, const QString& engineName){ + printed.append(message); + }); + + sm->engine()->registerGlobalObject("testClass", new TestClass()); + + sm->run(); + + auto ex = sm->getUncaughtException(); + + QVERIFY(!ex); + QVERIFY(printed == "14"); + +} + +void ScriptEngineTests::testInvokeNonInvokable() { + auto sm = makeManager("print(testClass.nonInvokableFunc(4)); Script.stop(true);", "testClass.js"); + sm->engine()->registerGlobalObject("testClass", new TestClass()); + + sm->run(); + auto ex = sm->getUncaughtException(); + + QVERIFY(ex); + QVERIFY(ex && ex->errorMessage.contains("TypeError")); +} + +void ScriptEngineTests::testRaiseException() { + auto sm = makeManager("testClass.doRaiseTest(); Script.stop(true);", "testRaise.js"); + sm->engine()->registerGlobalObject("testClass", new TestClass(sm->engine())); + + sm->run(); + auto ex = sm->getUncaughtException(); + + QVERIFY(ex); + QVERIFY(ex && ex->errorMessage.contains("Exception test")); +} + + void ScriptEngineTests::scriptTest() { return; diff --git a/tests/script-engine/src/ScriptEngineTests.h b/tests/script-engine/src/ScriptEngineTests.h index 3b7921c45f..52e568efe1 100644 --- a/tests/script-engine/src/ScriptEngineTests.h +++ b/tests/script-engine/src/ScriptEngineTests.h @@ -15,9 +15,41 @@ #include #include "ScriptManager.h" +#include "ScriptEngine.h" using ScriptManagerPointer = std::shared_ptr; + +class TestClass : public QObject { + Q_OBJECT + + public: + TestClass() {}; + + TestClass(ScriptEnginePointer ptr) : _engine(ptr) {}; + + Q_INVOKABLE int invokableFunc(int val) { + qDebug() << "invokableFunc called with value" << val; + return val + 10; + } + + Q_INVOKABLE void doRaiseTest() { + qDebug() << "About to raise an exception"; + _engine->raiseException("Exception test!"); + } + + + int nonInvokableFunc(int val) { + qCritical() << "nonInvokableFunc called with value" << val; + return val + 20; + } + + private: + ScriptEnginePointer _engine; + +}; + + class ScriptEngineTests : public QObject { Q_OBJECT private slots: @@ -27,6 +59,9 @@ private slots: void testSyntaxError(); void testRuntimeError(); void testJSThrow(); + void testRegisterClass(); + void testInvokeNonInvokable(); + void testRaiseException(); private: