History

Add tests for object registration and exception raising

Dale Glass committed 3 years ago

Commit 03699491 parent cd02b222 files · +79 −0Browse 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+44−0
@@ -32,6 +32,9 @@ QTEST_MAIN(ScriptEngineTests) · 31 unmodified lines
3232
3333
3434
35+
36+
37+
3538 void ScriptEngineTests::initTestCase() {
3639 // AudioClient starts networking, but for the purposes of the tests here we don't care,
3740 // so just got to use some port.
@@ -175,6 +178,47 @@ void ScriptEngineTests::testJSThrow() { · 137 unmodified lines
175178 QVERIFY(runtime_ex && runtime_ex->thrownValue.toInt32() == 42);
176179 }
177180
181+void ScriptEngineTests::testRegisterClass() {
182+ QString printed;
183+ auto sm = makeManager("print(testClass.invokableFunc(4)); Script.stop(true);", "testClass.js");
184+ connect(sm.get(), &ScriptManager::printedMessage, [&printed](const QString& message, const QString& engineName){
185+ printed.append(message);
186+ });
187+
188+ sm->engine()->registerGlobalObject("testClass", new TestClass());
189+
190+ sm->run();
191+
192+ auto ex = sm->getUncaughtException();
193+
194+ QVERIFY(!ex);
195+ QVERIFY(printed == "14");
196+
197+}
198+
199+void ScriptEngineTests::testInvokeNonInvokable() {
200+ auto sm = makeManager("print(testClass.nonInvokableFunc(4)); Script.stop(true);", "testClass.js");
201+ sm->engine()->registerGlobalObject("testClass", new TestClass());
202+
203+ sm->run();
204+ auto ex = sm->getUncaughtException();
205+
206+ QVERIFY(ex);
207+ QVERIFY(ex && ex->errorMessage.contains("TypeError"));
208+}
209+
210+void ScriptEngineTests::testRaiseException() {
211+ auto sm = makeManager("testClass.doRaiseTest(); Script.stop(true);", "testRaise.js");
212+ sm->engine()->registerGlobalObject("testClass", new TestClass(sm->engine()));
213+
214+ sm->run();
215+ auto ex = sm->getUncaughtException();
216+
217+ QVERIFY(ex);
218+ QVERIFY(ex && ex->errorMessage.contains("Exception test"));
219+}
220+
221+
178222 void ScriptEngineTests::scriptTest() {
179223 return;
180224