Showing the changes to tests/script-engine/src/ScriptEngineTests.cpp (1 of 2 files). Show all
Unified · Split
| @@ -47,33 +47,111 @@ void ScriptEngineTests::initTestCase() { · 46 unmodified lines | |||
| 47 | DependencyManager::set<ScriptInitializers>(); | 47 | DependencyManager::set<ScriptInitializers>(); |
| 48 | // DependencyManager::set<EntityScriptingInterface>(true); | 48 | // DependencyManager::set<EntityScriptingInterface>(true); |
| 49 | 49 | ||
| 50 | QSharedPointer<ScriptEngines> ac = DependencyManager::get<ScriptEngines>(); | ||
| 51 | QVERIFY(!ac.isNull()); | ||
| 52 | 50 | ||
| 53 | connect(ac.get(), &ScriptEngines::scriptLoadError, [](const QString& filename, const QString& error){ | 51 | } |
| 54 | qWarning() << "Failed to load script" << filename << ":" << error; | 52 | |
| 53 | ScriptManagerPointer ScriptEngineTests::makeManager(const QString &scriptSource, const QString &scriptFilename) { | ||
| 54 | ScriptManagerPointer sm = newScriptManager(ScriptManager::NETWORKLESS_TEST_SCRIPT, scriptSource, scriptFilename); | ||
| 55 | |||
| 56 | |||
| 57 | connect(sm.get(), &ScriptManager::scriptLoaded, [](const QString& filename){ | ||
| 58 | qWarning() << "Loaded script" << filename; | ||
| 59 | }); | ||
| 60 | |||
| 61 | |||
| 62 | connect(sm.get(), &ScriptManager::errorLoadingScript, [](const QString& filename){ | ||
| 63 | qWarning() << "Failed to load script" << filename; | ||
| 55 | }); | 64 | }); |
| 56 | 65 | ||
| 57 | connect(ac.get(), &ScriptEngines::printedMessage, [](const QString& message, const QString& engineName){ | 66 | connect(sm.get(), &ScriptManager::printedMessage, [](const QString& message, const QString& engineName){ |
| 58 | qDebug() << "Printed message from engine" << engineName << ": " << message; | 67 | qDebug() << "Printed message from engine" << engineName << ": " << message; |
| 59 | }); | 68 | }); |
| 60 | 69 | ||
| 61 | connect(ac.get(), &ScriptEngines::infoMessage, [](const QString& message, const QString& engineName){ | 70 | connect(sm.get(), &ScriptManager::infoMessage, [](const QString& message, const QString& engineName){ |
| 62 | qInfo() << "Info message from engine" << engineName << ": " << message; | 71 | qInfo() << "Info message from engine" << engineName << ": " << message; |
| 63 | }); | 72 | }); |
| 64 | 73 | ||
| 65 | connect(ac.get(), &ScriptEngines::warningMessage, [](const QString& message, const QString& engineName){ | 74 | connect(sm.get(), &ScriptManager::warningMessage, [](const QString& message, const QString& engineName){ |
| 66 | qWarning() << "Warning from engine" << engineName << ": " << message; | 75 | qWarning() << "Warning from engine" << engineName << ": " << message; |
| 67 | }); | 76 | }); |
| 68 | 77 | ||
| 69 | connect(ac.get(), &ScriptEngines::errorMessage, [](const QString& message, const QString& engineName){ | 78 | connect(sm.get(), &ScriptManager::errorMessage, [](const QString& message, const QString& engineName){ |
| 70 | qCritical() << "Error from engine" << engineName << ": " << message; | 79 | qCritical() << "Error from engine" << engineName << ": " << message; |
| 71 | }); | 80 | }); |
| 72 | 81 | ||
| 82 | connect(sm.get(), &ScriptManager::finished, [](const QString& fileNameString, ScriptManagerPointer smp){ | ||
| 83 | qInfo() << "Finished running script" << fileNameString; | ||
| 84 | }); | ||
| 85 | |||
| 86 | connect(sm.get(), &ScriptManager::runningStateChanged, [sm](){ | ||
| 87 | qInfo() << "Running state changed. Running = " << sm->isRunning() << "; Stopped = " << sm->isStopped() << "; Finished = " << sm->isFinished(); | ||
| 88 | }); | ||
| 89 | |||
| 90 | connect(sm->engine().get(), &ScriptEngine::exception, [](std::shared_ptr<ScriptException> exception){ | ||
| 91 | qWarning() << "Exception from engine (direct): " << exception; | ||
| 92 | }); | ||
| 93 | |||
| 94 | |||
| 95 | connect(sm.get(), &ScriptManager::unhandledException, [](std::shared_ptr<ScriptException> exception){ | ||
| 96 | qWarning() << "Exception from engine: " << exception; | ||
| 97 | }); | ||
| 98 | |||
| 99 | |||
| 100 | return sm; | ||
| 101 | } | ||
| 102 | |||
| 103 | void ScriptEngineTests::testTrivial() { | ||
| 104 | auto sm = makeManager("print(\"script works!\"); Script.stop(true);", "testTrivial.js"); | ||
| 105 | QString printed; | ||
| 106 | |||
| 107 | QVERIFY(!sm->isRunning()); | ||
| 108 | QVERIFY(!sm->isStopped()); | ||
| 109 | QVERIFY(!sm->isFinished()); | ||
| 110 | |||
| 111 | |||
| 112 | connect(sm.get(), &ScriptManager::printedMessage, [&printed](const QString& message, const QString& engineName){ | ||
| 113 | printed.append(message); | ||
| 114 | }); | ||
| 115 | |||
| 116 | |||
| 117 | sm->run(); | ||
| 118 | |||
| 119 | QVERIFY(!sm->isRunning()); | ||
| 120 | QVERIFY(!sm->isStopped()); | ||
| 121 | QVERIFY(sm->isFinished()); | ||
| 122 | QVERIFY(printed == "script works!"); | ||
| 123 | |||
| 124 | } | ||
| 125 | |||
| 126 | void ScriptEngineTests::testSyntaxError() { | ||
| 127 | auto sm = makeManager("this is not good syntax", "testSyntaxError.js"); | ||
| 128 | bool exceptionHappened = false; | ||
| 129 | |||
| 130 | |||
| 131 | //QSignalSpy spy(sm.get(), &ScriptManager::unhandledException); | ||
| 132 | |||
| 133 | |||
| 134 | connect(sm.get(), &ScriptManager::unhandledException, [&exceptionHappened](std::shared_ptr<ScriptException> exception){ | ||
| 135 | exceptionHappened = true; | ||
| 136 | }); | ||
| 137 | |||
| 138 | |||
| 139 | sm->run(); | ||
| 140 | //spy.wait(1000); | ||
| 141 | |||
| 142 | std::shared_ptr<ScriptException> ex = sm->getUncaughtException(); | ||
| 143 | |||
| 144 | qDebug() << "Exception:" << ex; | ||
| 145 | |||
| 146 | QVERIFY(exceptionHappened); | ||
| 147 | QVERIFY(ex); | ||
| 73 | 148 | ||
| 149 | //QVERIFY(spy.count() == 1); | ||
| 74 | } | 150 | } |
| 75 | 151 | ||
| 76 | void ScriptEngineTests::scriptTest() { | 152 | void ScriptEngineTests::scriptTest() { |
| 153 | return; | ||
| 154 | |||
| 77 | QSharedPointer<ScriptEngines> ac = DependencyManager::get<ScriptEngines>(); | 155 | QSharedPointer<ScriptEngines> ac = DependencyManager::get<ScriptEngines>(); |
| 78 | QVERIFY(!ac.isNull()); | 156 | QVERIFY(!ac.isNull()); |
| 79 | 157 | ||
| @@ -103,43 +181,7 @@ void ScriptEngineTests::scriptTest() { · 23 unmodified lines | |||
| 103 | 181 | ||
| 104 | //qDebug() << "Source: " << scriptSource; | 182 | //qDebug() << "Source: " << scriptSource; |
| 105 | 183 | ||
| 106 | ScriptManagerPointer sm = newScriptManager(ScriptManager::NETWORKLESS_TEST_SCRIPT, scriptSource, scriptFilename); | 184 | ScriptManagerPointer sm = makeManager(scriptSource, scriptFilename); |
| 107 | |||
| 108 | |||
| 109 | connect(sm.get(), &ScriptManager::scriptLoaded, [](const QString& filename){ | ||
| 110 | qWarning() << "Loaded script" << filename; | ||
| 111 | }); | ||
| 112 | |||
| 113 | |||
| 114 | connect(sm.get(), &ScriptManager::errorLoadingScript, [](const QString& filename){ | ||
| 115 | qWarning() << "Failed to load script" << filename; | ||
| 116 | }); | ||
| 117 | |||
| 118 | connect(sm.get(), &ScriptManager::printedMessage, [](const QString& message, const QString& engineName){ | ||
| 119 | qDebug() << "Printed message from engine" << engineName << ": " << message; | ||
| 120 | }); | ||
| 121 | |||
| 122 | connect(sm.get(), &ScriptManager::infoMessage, [](const QString& message, const QString& engineName){ | ||
| 123 | qInfo() << "Info message from engine" << engineName << ": " << message; | ||
| 124 | }); | ||
| 125 | |||
| 126 | connect(sm.get(), &ScriptManager::warningMessage, [](const QString& message, const QString& engineName){ | ||
| 127 | qWarning() << "Warning from engine" << engineName << ": " << message; | ||
| 128 | }); | ||
| 129 | |||
| 130 | connect(sm.get(), &ScriptManager::errorMessage, [](const QString& message, const QString& engineName){ | ||
| 131 | qCritical() << "Error from engine" << engineName << ": " << message; | ||
| 132 | }); | ||
| 133 | |||
| 134 | connect(sm.get(), &ScriptManager::finished, [](const QString& fileNameString, ScriptManagerPointer smp){ | ||
| 135 | qInfo() << "Finished running script" << fileNameString; | ||
| 136 | }); | ||
| 137 | |||
| 138 | connect(sm.get(), &ScriptManager::runningStateChanged, [sm](){ | ||
| 139 | qInfo() << "Running state changed. Running = " << sm->isRunning() << "; Stopped = " << sm->isStopped() << "; Finished = " << sm->isFinished(); | ||
| 140 | }); | ||
| 141 | |||
| 142 | |||
| 143 | sm->run(); | 185 | sm->run(); |
| 144 | } | 186 | } |
| 145 | 187 | ||