mirror of
https://github.com/overte-org/overte.git
synced 2025-08-10 04:53:25 +02:00
Add tests for object registration and exception raising
This commit is contained in:
parent
cd02b22bd7
commit
0369949d9b
2 changed files with 79 additions and 0 deletions
|
@ -32,6 +32,9 @@ QTEST_MAIN(ScriptEngineTests)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void ScriptEngineTests::initTestCase() {
|
void ScriptEngineTests::initTestCase() {
|
||||||
// AudioClient starts networking, but for the purposes of the tests here we don't care,
|
// AudioClient starts networking, but for the purposes of the tests here we don't care,
|
||||||
// so just got to use some port.
|
// so just got to use some port.
|
||||||
|
@ -175,6 +178,47 @@ void ScriptEngineTests::testJSThrow() {
|
||||||
QVERIFY(runtime_ex && runtime_ex->thrownValue.toInt32() == 42);
|
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() {
|
void ScriptEngineTests::scriptTest() {
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
|
|
@ -15,9 +15,41 @@
|
||||||
|
|
||||||
#include <QtTest/QtTest>
|
#include <QtTest/QtTest>
|
||||||
#include "ScriptManager.h"
|
#include "ScriptManager.h"
|
||||||
|
#include "ScriptEngine.h"
|
||||||
|
|
||||||
using ScriptManagerPointer = std::shared_ptr<ScriptManager>;
|
using ScriptManagerPointer = std::shared_ptr<ScriptManager>;
|
||||||
|
|
||||||
|
|
||||||
|
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 {
|
class ScriptEngineTests : public QObject {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
private slots:
|
private slots:
|
||||||
|
@ -27,6 +59,9 @@ private slots:
|
||||||
void testSyntaxError();
|
void testSyntaxError();
|
||||||
void testRuntimeError();
|
void testRuntimeError();
|
||||||
void testJSThrow();
|
void testJSThrow();
|
||||||
|
void testRegisterClass();
|
||||||
|
void testInvokeNonInvokable();
|
||||||
|
void testRaiseException();
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
Loading…
Reference in a new issue