History

Tests for runtime exceptions and throw()

Dale Glass committed 3 years ago

Commit 4beb5ec1 parent cb0c6202 files · +36 −8Browse files

Showing the changes to tests/script-engine/src/ScriptEngineTests.cpp (1 of 2 files). Show all

Unified · Split

tests/script-engine/src/ScriptEngineTests.cpp+34−8
@@ -87,11 +87,6 @@ ScriptManagerPointer ScriptEngineTests::makeManager(const QString &scriptSource, · 86 unmodified lines
8787 qInfo() << "Running state changed. Running = " << sm->isRunning() << "; Stopped = " << sm->isStopped() << "; Finished = " << sm->isFinished();
8888 });
8989
90− connect(sm->engine().get(), &ScriptEngine::exception, [](std::shared_ptr<ScriptException> exception){
91− qWarning() << "Exception from engine (direct): " << exception;
92− });
93−
94−
9590 connect(sm.get(), &ScriptManager::unhandledException, [](std::shared_ptr<ScriptException> exception){
9691 qWarning() << "Exception from engine: " << exception;
9792 });
@@ -127,9 +122,26 @@ void ScriptEngineTests::testSyntaxError() { · 29 unmodified lines
127122 auto sm = makeManager("this is not good syntax", "testSyntaxError.js");
128123 bool exceptionHappened = false;
129124
125+ connect(sm.get(), &ScriptManager::unhandledException, [&exceptionHappened](std::shared_ptr<ScriptException> exception){
126+ exceptionHappened = true;
127+ });
128+
129+
130+ sm->run();
131+
132+ std::shared_ptr<ScriptException> ex = sm->getUncaughtException();
133+
134+ qDebug() << "Exception:" << ex;
135+
136+ QVERIFY(exceptionHappened);
137+ QVERIFY(ex);
138+ QVERIFY(ex && ex->errorMessage.contains("SyntaxError"));
139+}
130140
131− //QSignalSpy spy(sm.get(), &ScriptManager::unhandledException);
132141
142+void ScriptEngineTests::testRuntimeError() {
143+ auto sm = makeManager("nonexisting();", "testRuntimeError.js");
144+ bool exceptionHappened = false;
133145
134146 connect(sm.get(), &ScriptManager::unhandledException, [&exceptionHappened](std::shared_ptr<ScriptException> exception){
135147 exceptionHappened = true;
@@ -137,7 +149,6 @@ void ScriptEngineTests::testSyntaxError() { · 1 unmodified line
137149
138150
139151 sm->run();
140− //spy.wait(1000);
141152
142153 std::shared_ptr<ScriptException> ex = sm->getUncaughtException();
143154
@@ -145,8 +156,23 @@ void ScriptEngineTests::testSyntaxError() { · 1 unmodified line
145156
146157 QVERIFY(exceptionHappened);
147158 QVERIFY(ex);
159+ QVERIFY(ex && ex->errorMessage.contains("ReferenceError"));
160+
161+}
162+
163+void ScriptEngineTests::testJSThrow() {
164+ auto sm = makeManager("throw(42);", "testThrow.js");
165+ sm->run();
166+
167+ std::shared_ptr<ScriptException> ex = sm->getUncaughtException();
148168
149− //QVERIFY(spy.count() == 1);
169+ qDebug() << "Exception:" << ex;
170+
171+ auto runtime_ex = std::dynamic_pointer_cast<ScriptRuntimeException>(ex);
172+
173+ QVERIFY(ex);
174+ QVERIFY(runtime_ex);
175+ QVERIFY(runtime_ex && runtime_ex->thrownValue.toInt32() == 42);
150176 }
151177
152178 void ScriptEngineTests::scriptTest() {