mirror of
https://github.com/overte-org/overte.git
synced 2025-08-08 01:10:44 +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,62 +47,10 @@ void ScriptEngineTests::initTestCase() {
|
|||
DependencyManager::set<ScriptInitializers>();
|
||||
// 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;
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
|
||||
void ScriptEngineTests::scriptTest() {
|
||||
QSharedPointer<ScriptEngines> ac = DependencyManager::get<ScriptEngines>();
|
||||
QVERIFY(!ac.isNull());
|
||||
|
||||
|
||||
QDir testScriptsDir("tests");
|
||||
QStringList testScripts = testScriptsDir.entryList(QStringList() << "*.js", QDir::Files);
|
||||
testScripts.sort();
|
||||
|
||||
for(QString scriptFilename : testScripts) {
|
||||
scriptFilename = "tests/" + scriptFilename;
|
||||
qInfo() << "Running test script: " << scriptFilename;
|
||||
|
||||
QString scriptSource;
|
||||
|
||||
{
|
||||
QFile scriptFile(scriptFilename);
|
||||
scriptFile.open(QIODevice::ReadOnly);
|
||||
QTextStream scriptStream(&scriptFile);
|
||||
scriptSource.append(scriptStream.readAll());
|
||||
|
||||
// Scripts keep on running until Script.stop() is called. For our tests here,
|
||||
// that's not desirable, so we append an automatic stop at the end of every
|
||||
// script.
|
||||
scriptSource.append("\nScript.stop(true);\n");
|
||||
}
|
||||
|
||||
|
||||
//qDebug() << "Source: " << scriptSource;
|
||||
|
||||
ScriptManagerPointer ScriptEngineTests::makeManager(const QString &scriptSource, const QString &scriptFilename) {
|
||||
ScriptManagerPointer sm = newScriptManager(ScriptManager::NETWORKLESS_TEST_SCRIPT, scriptSource, scriptFilename);
|
||||
|
||||
|
||||
|
@ -139,7 +87,101 @@ void ScriptEngineTests::scriptTest() {
|
|||
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() {
|
||||
return;
|
||||
|
||||
QSharedPointer<ScriptEngines> ac = DependencyManager::get<ScriptEngines>();
|
||||
QVERIFY(!ac.isNull());
|
||||
|
||||
|
||||
QDir testScriptsDir("tests");
|
||||
QStringList testScripts = testScriptsDir.entryList(QStringList() << "*.js", QDir::Files);
|
||||
testScripts.sort();
|
||||
|
||||
for(QString scriptFilename : testScripts) {
|
||||
scriptFilename = "tests/" + scriptFilename;
|
||||
qInfo() << "Running test script: " << scriptFilename;
|
||||
|
||||
QString scriptSource;
|
||||
|
||||
{
|
||||
QFile scriptFile(scriptFilename);
|
||||
scriptFile.open(QIODevice::ReadOnly);
|
||||
QTextStream scriptStream(&scriptFile);
|
||||
scriptSource.append(scriptStream.readAll());
|
||||
|
||||
// Scripts keep on running until Script.stop() is called. For our tests here,
|
||||
// that's not desirable, so we append an automatic stop at the end of every
|
||||
// script.
|
||||
scriptSource.append("\nScript.stop(true);\n");
|
||||
}
|
||||
|
||||
|
||||
//qDebug() << "Source: " << scriptSource;
|
||||
|
||||
ScriptManagerPointer sm = makeManager(scriptSource, scriptFilename);
|
||||
sm->run();
|
||||
}
|
||||
|
||||
|
|
|
@ -14,14 +14,22 @@
|
|||
#define overte_ScriptingEngineTests_h
|
||||
|
||||
#include <QtTest/QtTest>
|
||||
#include "ScriptManager.h"
|
||||
|
||||
|
||||
using ScriptManagerPointer = std::shared_ptr<ScriptManager>;
|
||||
|
||||
class ScriptEngineTests : public QObject {
|
||||
Q_OBJECT
|
||||
private slots:
|
||||
void initTestCase();
|
||||
void scriptTest();
|
||||
void testTrivial();
|
||||
void testSyntaxError();
|
||||
|
||||
|
||||
private:
|
||||
ScriptManagerPointer makeManager(const QString &source, const QString &filename);
|
||||
|
||||
};
|
||||
|
||||
#endif // overte_ScriptingEngineTests_h
|
||||
|
|
Loading…
Reference in a new issue