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
32 32
33 33
34 34
35
36
37
35void ScriptEngineTests::initTestCase() {38void ScriptEngineTests::initTestCase() {
36 // AudioClient starts networking, but for the purposes of the tests here we don't care,39 // AudioClient starts networking, but for the purposes of the tests here we don't care,
37 // so just got to use some port.40 // so just got to use some port.
@@ -175,6 +178,47 @@ void ScriptEngineTests::testJSThrow() { · 137 unmodified lines
175 QVERIFY(runtime_ex && runtime_ex->thrownValue.toInt32() == 42);178 QVERIFY(runtime_ex && runtime_ex->thrownValue.toInt32() == 42);
176}179}
177 180
181void 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
199void 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
210void 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
178void ScriptEngineTests::scriptTest() {222void ScriptEngineTests::scriptTest() {
179 return;223 return;
180 224