From 7ec8336431787156826185628ad3ee05dc327d2a Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Fri, 21 Mar 2025 19:00:45 +0530 Subject: Add event loop with uv --- spec/LuaRuntimeSpec.cpp | 76 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 spec/LuaRuntimeSpec.cpp (limited to 'spec/LuaRuntimeSpec.cpp') diff --git a/spec/LuaRuntimeSpec.cpp b/spec/LuaRuntimeSpec.cpp new file mode 100644 index 0000000..149e1e1 --- /dev/null +++ b/spec/LuaRuntimeSpec.cpp @@ -0,0 +1,76 @@ +#include "testUtils.h" +#include +#include +#include + +#include "LuaRuntime.hpp" + +class LuaRuntimeSpec : public QObject { + Q_OBJECT + +private slots: + void cleanupTestCase() { uv_library_shutdown(); } + + void testSanityCheckBuiltins() { + auto lua = LuaRuntime::instance(); + + it("evaluates simple expression") { + lua->startEventLoop(); + std::atomic foobar = 2; + + lua->queueTask([&foobar]() { foobar = 10; }); + + lua->evaluate(R"( + print('Hello -- '); + local timer = uv.new_timer(); + timer:start(1000, 0, function() + print('inside timer 1') + print('inside timer 2') + print('inside timer 3') + print('inside timer 4') + print('inside timer 5') + timer:close() + end); + print('-- end'); + )"); + lua->queueTask([]() { qDebug() << "---- 1"; }); + lua->queueTask([]() { qDebug() << "---- 2"; }); + lua->queueTask([]() { qDebug() << "---- 3"; }); + std::this_thread::sleep_for(std::chrono::seconds(2)); + // std::this_thread::sleep_for(std::chrono::milliseconds(20)); + lua->stopEventLoop(); + + qDebug() << "foobar" << foobar; + + QCOMPARE(1, 1); + } + } + + void testSanityCheckBuiltins2() { + auto lua = LuaRuntime::instance(); + + it("evaluates again") { + lua->startEventLoop(); + + lua->queueTask([]() { qDebug() << "---- 5"; }); + lua->evaluate(R"( + print('Hello -- '); + local timer = uv.new_timer(); + timer:start(1000, 0, function() + print('%%%%% blagb') + timer:close() + end); + print('-- end'); + )"); + lua->queueTask([]() { qDebug() << "---- 5"; }); + // TODO: Impl + std::this_thread::sleep_for(std::chrono::seconds(2)); + lua->stopEventLoop(); + + QCOMPARE(1, 1); + } + } +}; + +QTEST_REGISTER(LuaRuntimeSpec) +#include "LuaRuntimeSpec.moc" -- cgit v1.3.1