diff options
| author | Akshay Nair <phenax5@gmail.com> | 2025-04-05 19:05:01 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2025-04-05 19:05:01 +0530 |
| commit | f682527b2f81376ca462d14bcbaae9e0ce77561d (patch) | |
| tree | 1c174ad15238ed79ab483c218767ccd2e96a6754 /spec | |
| parent | be672286b3fa1056560ad1216331731751a16685 (diff) | |
| download | null-browser-f682527b2f81376ca462d14bcbaae9e0ce77561d.tar.gz null-browser-f682527b2f81376ca462d14bcbaae9e0ce77561d.zip | |
Split luaruntime spec
Diffstat (limited to 'spec')
| -rw-r--r-- | spec/LuaRuntimeApiSpec.cpp | 151 | ||||
| -rw-r--r-- | spec/LuaRuntimeSpec.cpp | 137 |
2 files changed, 152 insertions, 136 deletions
diff --git a/spec/LuaRuntimeApiSpec.cpp b/spec/LuaRuntimeApiSpec.cpp new file mode 100644 index 0000000..b7424ea --- /dev/null +++ b/spec/LuaRuntimeApiSpec.cpp @@ -0,0 +1,151 @@ +#include <QtCore> +#include <uv.h> + +#include "LuaRuntime.hpp" +#include "WindowActionRouter.hpp" +#include "events.hpp" +#include "testUtils.h" + +class TestEvent1 : public BrowserEvent { +public: + int num; + TestEvent1(int num) : num(num) { kind = "TestEvent1"; } + void lua_push(lua_State *state) const override { + lua_newtable(state); + SET_FIELD("test_data", integer, num); + } +}; + +// NOLINTBEGIN +class LuaRuntimeApiSpec : public QObject { + Q_OBJECT + +private slots: + void beforeTestCase() { LuaRuntime::instance().start_event_loop(); } + + void cleanupTestCase() { LuaRuntime::instance().stop_event_loop(); } + + void test_web_event_add_event() { + context("when events, patterns and callback are specified correctly"); + it("returns true and registers event") { + auto &lua = LuaRuntime::instance(); + QSignalSpy evaluation_completed_spy(&lua, &LuaRuntime::evaluation_completed); + + lua.evaluate(R"( + return web.event.add_listener({ 'Hello', 'World' }, { + patterns = { 'p1', 'p2' }, + callback = function() print("Called") end, + }); + )"); + evaluation_completed_spy.wait(); + + QCOMPARE(evaluation_completed_spy.count(), 1); + QVariant result = evaluation_completed_spy.takeFirst().at(0); + QCOMPARE(result, true); + } + + context("when a single event is passed"); + it("returns true and registers event") { + auto &lua = LuaRuntime::instance(); + QSignalSpy evaluation_completed_spy(&lua, &LuaRuntime::evaluation_completed); + + lua.evaluate(R"( + return web.event.add_listener('Hello', { + patterns = { 'p1', 'p2' }, + callback = function() print("Called") end, + }); + )"); + evaluation_completed_spy.wait(); + + QCOMPARE(evaluation_completed_spy.count(), 1); + QVariant result = evaluation_completed_spy.takeFirst().at(0); + QCOMPARE(result, true); + } + + context("when patterns is not passed"); + it("returns true and registers event") { + auto &lua = LuaRuntime::instance(); + QSignalSpy evaluation_completed_spy(&lua, &LuaRuntime::evaluation_completed); + + lua.evaluate(R"( + return web.event.add_listener({ 'Hello', 'World' }, { + callback = function() print("Called") end, + }); + )"); + evaluation_completed_spy.wait(); + + QCOMPARE(evaluation_completed_spy.count(), 1); + QVariant result = evaluation_completed_spy.takeFirst().at(0); + QCOMPARE(result, true); + } + + context("when events list is not passed"); + it("returns false and doesnt register event") { + auto &lua = LuaRuntime::instance(); + QSignalSpy evaluation_completed_spy(&lua, &LuaRuntime::evaluation_completed); + + lua.evaluate(R"( + return web.event.add_listener(nil, { + patterns = { 'p1', 'p2' }, + callback = function() print("Called") end, + }); + )"); + evaluation_completed_spy.wait(); + + QCOMPARE(evaluation_completed_spy.count(), 1); + QVariant result = evaluation_completed_spy.first().first(); + QCOMPARE(result, false); + } + + context("when callback is not passed"); + it("returns false and doesnt register event") { + auto &lua = LuaRuntime::instance(); + QSignalSpy evaluation_completed_spy(&lua, &LuaRuntime::evaluation_completed); + + lua.evaluate(R"( + return web.event.add_listener({'Ev'}, { + patterns = { 'p1', 'p2' }, + }); + )"); + evaluation_completed_spy.wait(); + + QCOMPARE(evaluation_completed_spy.count(), 1); + QVariant result = evaluation_completed_spy.first().first(); + QCOMPARE(result, false); + } + } + + void test_web_event_dispatching() { + context("when dispatching a registered event (without pattern)"); + it("calls the registered event handler") { + auto &lua = LuaRuntime::instance(); + QSignalSpy evaluation_completed_spy(&lua, &LuaRuntime::evaluation_completed); + lua.evaluate(R"( + _G.event1_called = false; + _G.event1_called_with = nil; + web.event.add_listener('TestEvent1', { + callback = function(opts) + _G.event1_called = true + _G.event1_called_with = opts + end, + }); + _G.event2_called = false; + web.event.add_listener('TestEvent2', { + callback = function(opts) _G.event2_called = true end, + }); + )"); + evaluation_completed_spy.wait(); + + TestEvent1 event(42); + WindowActionRouter::instance().dispatch_event(&event); + + QVERIFY(wait_for_lua_to_be_true("return _G.event1_called")); + QVERIFY(wait_for_lua_to_be_true("return _G.event1_called_with.test_data == 42")); + QVERIFY(wait_for_lua_to_be_true("return not _G.event2_called")); + } + } +}; + +QTEST_REGISTER(LuaRuntimeApiSpec) +#include "LuaRuntimeApiSpec.moc" +// NOLINTEND diff --git a/spec/LuaRuntimeSpec.cpp b/spec/LuaRuntimeSpec.cpp index d31519c..97864b2 100644 --- a/spec/LuaRuntimeSpec.cpp +++ b/spec/LuaRuntimeSpec.cpp @@ -2,20 +2,8 @@ #include <uv.h> #include "LuaRuntime.hpp" -#include "WindowActionRouter.hpp" -#include "events.hpp" #include "testUtils.h" -class TestEvent1 : public BrowserEvent { -public: - int num; - TestEvent1(int num) : num(num) { kind = "TestEvent1"; } - void lua_push(lua_State *state) const override { - lua_newtable(state); - SET_FIELD("test_data", integer, num); - } -}; - // NOLINTBEGIN class LuaRuntimeSpec : public QObject { Q_OBJECT @@ -23,10 +11,7 @@ class LuaRuntimeSpec : public QObject { private slots: void beforeTestCase() { LuaRuntime::instance().start_event_loop(); } - void cleanupTestCase() { - LuaRuntime::instance().stop_event_loop(); - uv_library_shutdown(); - } + void cleanupTestCase() { LuaRuntime::instance().stop_event_loop(); } void test_evaluate() { context("when given an expression returning a number"); @@ -131,126 +116,6 @@ private slots: QVERIFY(wait_for_lua_to_be_true("return _G.spawn_exit_code == 0")); } } - - void test_web_event_add_event() { - context("when events, patterns and callback are specified correctly"); - it("returns true and registers event") { - auto &lua = LuaRuntime::instance(); - QSignalSpy evaluation_completed_spy(&lua, &LuaRuntime::evaluation_completed); - - lua.evaluate(R"( - return web.event.add_listener({ 'Hello', 'World' }, { - patterns = { 'p1', 'p2' }, - callback = function() print("Called") end, - }); - )"); - evaluation_completed_spy.wait(); - - QCOMPARE(evaluation_completed_spy.count(), 1); - QVariant result = evaluation_completed_spy.takeFirst().at(0); - QCOMPARE(result, true); - } - - context("when a single event is passed"); - it("returns true and registers event") { - auto &lua = LuaRuntime::instance(); - QSignalSpy evaluation_completed_spy(&lua, &LuaRuntime::evaluation_completed); - - lua.evaluate(R"( - return web.event.add_listener('Hello', { - patterns = { 'p1', 'p2' }, - callback = function() print("Called") end, - }); - )"); - evaluation_completed_spy.wait(); - - QCOMPARE(evaluation_completed_spy.count(), 1); - QVariant result = evaluation_completed_spy.takeFirst().at(0); - QCOMPARE(result, true); - } - - context("when patterns is not passed"); - it("returns true and registers event") { - auto &lua = LuaRuntime::instance(); - QSignalSpy evaluation_completed_spy(&lua, &LuaRuntime::evaluation_completed); - - lua.evaluate(R"( - return web.event.add_listener({ 'Hello', 'World' }, { - callback = function() print("Called") end, - }); - )"); - evaluation_completed_spy.wait(); - - QCOMPARE(evaluation_completed_spy.count(), 1); - QVariant result = evaluation_completed_spy.takeFirst().at(0); - QCOMPARE(result, true); - } - - context("when events list is not passed"); - it("returns false and doesnt register event") { - auto &lua = LuaRuntime::instance(); - QSignalSpy evaluation_completed_spy(&lua, &LuaRuntime::evaluation_completed); - - lua.evaluate(R"( - return web.event.add_listener(nil, { - patterns = { 'p1', 'p2' }, - callback = function() print("Called") end, - }); - )"); - evaluation_completed_spy.wait(); - - QCOMPARE(evaluation_completed_spy.count(), 1); - QVariant result = evaluation_completed_spy.first().first(); - QCOMPARE(result, false); - } - - context("when callback is not passed"); - it("returns false and doesnt register event") { - auto &lua = LuaRuntime::instance(); - QSignalSpy evaluation_completed_spy(&lua, &LuaRuntime::evaluation_completed); - - lua.evaluate(R"( - return web.event.add_listener({'Ev'}, { - patterns = { 'p1', 'p2' }, - }); - )"); - evaluation_completed_spy.wait(); - - QCOMPARE(evaluation_completed_spy.count(), 1); - QVariant result = evaluation_completed_spy.first().first(); - QCOMPARE(result, false); - } - } - - void test_web_event_dispatching() { - context("when dispatching a registered event (without pattern)"); - it("calls the registered event handler") { - auto &lua = LuaRuntime::instance(); - QSignalSpy evaluation_completed_spy(&lua, &LuaRuntime::evaluation_completed); - lua.evaluate(R"( - _G.event1_called = false; - _G.event1_called_with = nil; - web.event.add_listener('TestEvent1', { - callback = function(opts) - _G.event1_called = true - _G.event1_called_with = opts - end, - }); - _G.event2_called = false; - web.event.add_listener('TestEvent2', { - callback = function(opts) _G.event2_called = true end, - }); - )"); - evaluation_completed_spy.wait(); - - TestEvent1 event(42); - WindowActionRouter::instance().dispatch_event(&event); - - QVERIFY(wait_for_lua_to_be_true("return _G.event1_called")); - QVERIFY(wait_for_lua_to_be_true("return _G.event1_called_with.test_data == 42")); - QVERIFY(wait_for_lua_to_be_true("return not _G.event2_called")); - } - } }; QTEST_REGISTER(LuaRuntimeSpec) |
