mirror of
https://github.com/overte-org/overte.git
synced 2025-08-06 18:25:52 +02:00
Rework tests, add tests for exceptions
This commit is contained in:
parent
bb74c2ecc9
commit
2a5ec7db3d
2 changed files with 111 additions and 61 deletions
|
@ -47,33 +47,111 @@ void ScriptEngineTests::initTestCase() {
|
||||||
DependencyManager::set<ScriptInitializers>();
|
DependencyManager::set<ScriptInitializers>();
|
||||||
// DependencyManager::set<EntityScriptingInterface>(true);
|
// DependencyManager::set<EntityScriptingInterface>(true);
|
||||||
|
|
||||||
QSharedPointer<ScriptEngines> ac = DependencyManager::get<ScriptEngines>();
|
|
||||||
QVERIFY(!ac.isNull());
|
|
||||||
|
|
||||||
connect(ac.get(), &ScriptEngines::scriptLoadError, [](const QString& filename, const QString& error){
|
|
||||||
qWarning() << "Failed to load script" << filename << ":" << error;
|
|
||||||
});
|
|
||||||
|
|
||||||
connect(ac.get(), &ScriptEngines::printedMessage, [](const QString& message, const QString& engineName){
|
|
||||||
qDebug() << "Printed message from engine" << engineName << ": " << message;
|
|
||||||
});
|
|
||||||
|
|
||||||
connect(ac.get(), &ScriptEngines::infoMessage, [](const QString& message, const QString& engineName){
|
|
||||||
qInfo() << "Info message from engine" << engineName << ": " << message;
|
|
||||||
});
|
|
||||||
|
|
||||||
connect(ac.get(), &ScriptEngines::warningMessage, [](const QString& message, const QString& engineName){
|
|
||||||
qWarning() << "Warning from engine" << engineName << ": " << message;
|
|
||||||
});
|
|
||||||
|
|
||||||
connect(ac.get(), &ScriptEngines::errorMessage, [](const QString& message, const QString& engineName){
|
|
||||||
qCritical() << "Error from engine" << engineName << ": " << message;
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ScriptManagerPointer ScriptEngineTests::makeManager(const QString &scriptSource, const QString &scriptFilename) {
|
||||||
|
ScriptManagerPointer sm = newScriptManager(ScriptManager::NETWORKLESS_TEST_SCRIPT, scriptSource, scriptFilename);
|
||||||
|
|
||||||
|
|
||||||
|
connect(sm.get(), &ScriptManager::scriptLoaded, [](const QString& filename){
|
||||||
|
qWarning() << "Loaded script" << filename;
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
connect(sm.get(), &ScriptManager::errorLoadingScript, [](const QString& filename){
|
||||||
|
qWarning() << "Failed to load script" << filename;
|
||||||
|
});
|
||||||
|
|
||||||
|
connect(sm.get(), &ScriptManager::printedMessage, [](const QString& message, const QString& engineName){
|
||||||
|
qDebug() << "Printed message from engine" << engineName << ": " << message;
|
||||||
|
});
|
||||||
|
|
||||||
|
connect(sm.get(), &ScriptManager::infoMessage, [](const QString& message, const QString& engineName){
|
||||||
|
qInfo() << "Info message from engine" << engineName << ": " << message;
|
||||||
|
});
|
||||||
|
|
||||||
|
connect(sm.get(), &ScriptManager::warningMessage, [](const QString& message, const QString& engineName){
|
||||||
|
qWarning() << "Warning from engine" << engineName << ": " << message;
|
||||||
|
});
|
||||||
|
|
||||||
|
connect(sm.get(), &ScriptManager::errorMessage, [](const QString& message, const QString& engineName){
|
||||||
|
qCritical() << "Error from engine" << engineName << ": " << message;
|
||||||
|
});
|
||||||
|
|
||||||
|
connect(sm.get(), &ScriptManager::finished, [](const QString& fileNameString, ScriptManagerPointer smp){
|
||||||
|
qInfo() << "Finished running script" << fileNameString;
|
||||||
|
});
|
||||||
|
|
||||||
|
connect(sm.get(), &ScriptManager::runningStateChanged, [sm](){
|
||||||
|
qInfo() << "Running state changed. Running = " << sm->isRunning() << "; Stopped = " << sm->isStopped() << "; Finished = " << sm->isFinished();
|
||||||
|
});
|
||||||
|
|
||||||
|
connect(sm->engine().get(), &ScriptEngine::exception, [](std::shared_ptr<ScriptException> exception){
|
||||||
|
qWarning() << "Exception from engine (direct): " << exception;
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
connect(sm.get(), &ScriptManager::unhandledException, [](std::shared_ptr<ScriptException> exception){
|
||||||
|
qWarning() << "Exception from engine: " << exception;
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
return sm;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ScriptEngineTests::testTrivial() {
|
||||||
|
auto sm = makeManager("print(\"script works!\"); Script.stop(true);", "testTrivial.js");
|
||||||
|
QString printed;
|
||||||
|
|
||||||
|
QVERIFY(!sm->isRunning());
|
||||||
|
QVERIFY(!sm->isStopped());
|
||||||
|
QVERIFY(!sm->isFinished());
|
||||||
|
|
||||||
|
|
||||||
|
connect(sm.get(), &ScriptManager::printedMessage, [&printed](const QString& message, const QString& engineName){
|
||||||
|
printed.append(message);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
sm->run();
|
||||||
|
|
||||||
|
QVERIFY(!sm->isRunning());
|
||||||
|
QVERIFY(!sm->isStopped());
|
||||||
|
QVERIFY(sm->isFinished());
|
||||||
|
QVERIFY(printed == "script works!");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
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<ScriptException> exception){
|
||||||
|
exceptionHappened = true;
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
sm->run();
|
||||||
|
//spy.wait(1000);
|
||||||
|
|
||||||
|
std::shared_ptr<ScriptException> ex = sm->getUncaughtException();
|
||||||
|
|
||||||
|
qDebug() << "Exception:" << ex;
|
||||||
|
|
||||||
|
QVERIFY(exceptionHappened);
|
||||||
|
QVERIFY(ex);
|
||||||
|
|
||||||
|
//QVERIFY(spy.count() == 1);
|
||||||
|
}
|
||||||
|
|
||||||
void ScriptEngineTests::scriptTest() {
|
void ScriptEngineTests::scriptTest() {
|
||||||
|
return;
|
||||||
|
|
||||||
QSharedPointer<ScriptEngines> ac = DependencyManager::get<ScriptEngines>();
|
QSharedPointer<ScriptEngines> ac = DependencyManager::get<ScriptEngines>();
|
||||||
QVERIFY(!ac.isNull());
|
QVERIFY(!ac.isNull());
|
||||||
|
|
||||||
|
@ -103,43 +181,7 @@ void ScriptEngineTests::scriptTest() {
|
||||||
|
|
||||||
//qDebug() << "Source: " << scriptSource;
|
//qDebug() << "Source: " << scriptSource;
|
||||||
|
|
||||||
ScriptManagerPointer sm = newScriptManager(ScriptManager::NETWORKLESS_TEST_SCRIPT, scriptSource, scriptFilename);
|
ScriptManagerPointer sm = makeManager(scriptSource, scriptFilename);
|
||||||
|
|
||||||
|
|
||||||
connect(sm.get(), &ScriptManager::scriptLoaded, [](const QString& filename){
|
|
||||||
qWarning() << "Loaded script" << filename;
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
connect(sm.get(), &ScriptManager::errorLoadingScript, [](const QString& filename){
|
|
||||||
qWarning() << "Failed to load script" << filename;
|
|
||||||
});
|
|
||||||
|
|
||||||
connect(sm.get(), &ScriptManager::printedMessage, [](const QString& message, const QString& engineName){
|
|
||||||
qDebug() << "Printed message from engine" << engineName << ": " << message;
|
|
||||||
});
|
|
||||||
|
|
||||||
connect(sm.get(), &ScriptManager::infoMessage, [](const QString& message, const QString& engineName){
|
|
||||||
qInfo() << "Info message from engine" << engineName << ": " << message;
|
|
||||||
});
|
|
||||||
|
|
||||||
connect(sm.get(), &ScriptManager::warningMessage, [](const QString& message, const QString& engineName){
|
|
||||||
qWarning() << "Warning from engine" << engineName << ": " << message;
|
|
||||||
});
|
|
||||||
|
|
||||||
connect(sm.get(), &ScriptManager::errorMessage, [](const QString& message, const QString& engineName){
|
|
||||||
qCritical() << "Error from engine" << engineName << ": " << message;
|
|
||||||
});
|
|
||||||
|
|
||||||
connect(sm.get(), &ScriptManager::finished, [](const QString& fileNameString, ScriptManagerPointer smp){
|
|
||||||
qInfo() << "Finished running script" << fileNameString;
|
|
||||||
});
|
|
||||||
|
|
||||||
connect(sm.get(), &ScriptManager::runningStateChanged, [sm](){
|
|
||||||
qInfo() << "Running state changed. Running = " << sm->isRunning() << "; Stopped = " << sm->isStopped() << "; Finished = " << sm->isFinished();
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
sm->run();
|
sm->run();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -14,14 +14,22 @@
|
||||||
#define overte_ScriptingEngineTests_h
|
#define overte_ScriptingEngineTests_h
|
||||||
|
|
||||||
#include <QtTest/QtTest>
|
#include <QtTest/QtTest>
|
||||||
|
#include "ScriptManager.h"
|
||||||
|
|
||||||
|
using ScriptManagerPointer = std::shared_ptr<ScriptManager>;
|
||||||
|
|
||||||
class ScriptEngineTests : public QObject {
|
class ScriptEngineTests : public QObject {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
private slots:
|
private slots:
|
||||||
void initTestCase();
|
void initTestCase();
|
||||||
void scriptTest();
|
void scriptTest();
|
||||||
|
void testTrivial();
|
||||||
|
void testSyntaxError();
|
||||||
|
|
||||||
|
|
||||||
|
private:
|
||||||
|
ScriptManagerPointer makeManager(const QString &source, const QString &filename);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // overte_ScriptingEngineTests_h
|
#endif // overte_ScriptingEngineTests_h
|
||||||
|
|
Loading…
Reference in a new issue