From 4beb5eceeb133e55bf3ca0b7d77a6c32ad21c962 Mon Sep 17 00:00:00 2001 From: Dale Glass Date: Sun, 5 Mar 2023 14:22:51 +0100 Subject: [PATCH] Tests for runtime exceptions and throw() --- tests/script-engine/src/ScriptEngineTests.cpp | 48 ++++++++++++++----- tests/script-engine/src/ScriptEngineTests.h | 2 + 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/tests/script-engine/src/ScriptEngineTests.cpp b/tests/script-engine/src/ScriptEngineTests.cpp index 204655ceed..0d53aa6f24 100644 --- a/tests/script-engine/src/ScriptEngineTests.cpp +++ b/tests/script-engine/src/ScriptEngineTests.cpp @@ -87,11 +87,6 @@ ScriptManagerPointer ScriptEngineTests::makeManager(const QString &scriptSource, qInfo() << "Running state changed. Running = " << sm->isRunning() << "; Stopped = " << sm->isStopped() << "; Finished = " << sm->isFinished(); }); - connect(sm->engine().get(), &ScriptEngine::exception, [](std::shared_ptr exception){ - qWarning() << "Exception from engine (direct): " << exception; - }); - - connect(sm.get(), &ScriptManager::unhandledException, [](std::shared_ptr exception){ qWarning() << "Exception from engine: " << exception; }); @@ -127,17 +122,12 @@ void ScriptEngineTests::testSyntaxError() { auto sm = makeManager("this is not good syntax", "testSyntaxError.js"); bool exceptionHappened = false; - - //QSignalSpy spy(sm.get(), &ScriptManager::unhandledException); - - connect(sm.get(), &ScriptManager::unhandledException, [&exceptionHappened](std::shared_ptr exception){ exceptionHappened = true; }); sm->run(); - //spy.wait(1000); std::shared_ptr ex = sm->getUncaughtException(); @@ -145,8 +135,44 @@ void ScriptEngineTests::testSyntaxError() { QVERIFY(exceptionHappened); QVERIFY(ex); + QVERIFY(ex && ex->errorMessage.contains("SyntaxError")); +} - //QVERIFY(spy.count() == 1); + +void ScriptEngineTests::testRuntimeError() { + auto sm = makeManager("nonexisting();", "testRuntimeError.js"); + bool exceptionHappened = false; + + connect(sm.get(), &ScriptManager::unhandledException, [&exceptionHappened](std::shared_ptr exception){ + exceptionHappened = true; + }); + + + sm->run(); + + std::shared_ptr ex = sm->getUncaughtException(); + + qDebug() << "Exception:" << ex; + + QVERIFY(exceptionHappened); + QVERIFY(ex); + QVERIFY(ex && ex->errorMessage.contains("ReferenceError")); + +} + +void ScriptEngineTests::testJSThrow() { + auto sm = makeManager("throw(42);", "testThrow.js"); + sm->run(); + + std::shared_ptr ex = sm->getUncaughtException(); + + qDebug() << "Exception:" << ex; + + auto runtime_ex = std::dynamic_pointer_cast(ex); + + QVERIFY(ex); + QVERIFY(runtime_ex); + QVERIFY(runtime_ex && runtime_ex->thrownValue.toInt32() == 42); } void ScriptEngineTests::scriptTest() { diff --git a/tests/script-engine/src/ScriptEngineTests.h b/tests/script-engine/src/ScriptEngineTests.h index 94c07ee566..3b7921c45f 100644 --- a/tests/script-engine/src/ScriptEngineTests.h +++ b/tests/script-engine/src/ScriptEngineTests.h @@ -25,6 +25,8 @@ private slots: void scriptTest(); void testTrivial(); void testSyntaxError(); + void testRuntimeError(); + void testJSThrow(); private: