From 00efc9757475b3b347fee0ce2aaccfbf4b1c33b5 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Fri, 2 May 2025 00:21:32 +0530 Subject: Add a simple lua test suite + add utils spec + fix keymap spec --- spec/KeymapEvaluatorSpec.cpp | 7 ++++ spec/lua/utils_spec.lua | 90 ++++++++++++++++++++++++++++++++++++++++++++ spec/main.cpp | 40 +++++++++++++++++++- spec/testUtils.cpp | 5 +-- spec/testUtils.h | 2 +- 5 files changed, 138 insertions(+), 6 deletions(-) create mode 100644 spec/lua/utils_spec.lua (limited to 'spec') diff --git a/spec/KeymapEvaluatorSpec.cpp b/spec/KeymapEvaluatorSpec.cpp index 37f7ec8..5c2202b 100644 --- a/spec/KeymapEvaluatorSpec.cpp +++ b/spec/KeymapEvaluatorSpec.cpp @@ -13,6 +13,7 @@ private slots: it("calls mapping") { auto keymap_was_called = false; KeymapEvaluator evaluator; + evaluator.set_current_mode("n"); evaluator.add_keymap("n", "", [&keymap_was_called]() { keymap_was_called = true; }); evaluator.evaluate(Qt::ControlModifier, Qt::Key_T); @@ -37,6 +38,7 @@ private slots: it("calls mapping") { auto keymap_was_called = false; KeymapEvaluator evaluator; + evaluator.set_current_mode("n"); evaluator.add_keymap("n", "a", [&keymap_was_called]() { keymap_was_called = true; }); evaluator.evaluate(Qt::ControlModifier, Qt::Key_T); @@ -49,6 +51,7 @@ private slots: it("does not call mapping") { auto keymap_was_called = false; KeymapEvaluator evaluator; + evaluator.set_current_mode("n"); evaluator.add_keymap("n", "a", [&keymap_was_called]() { keymap_was_called = true; }); evaluator.evaluate(Qt::ControlModifier, Qt::Key_T); @@ -60,6 +63,7 @@ private slots: it("does not call mapping") { auto keymap_was_called = false; KeymapEvaluator evaluator; + evaluator.set_current_mode("n"); evaluator.add_keymap("n", "a", [&keymap_was_called]() { keymap_was_called = true; }); evaluator.evaluate(Qt::ControlModifier, Qt::Key_K); @@ -71,6 +75,7 @@ private slots: it("does not call mapping") { auto keymap_was_called = false; KeymapEvaluator evaluator; + evaluator.set_current_mode("n"); evaluator.add_keymap("n", "a", [&keymap_was_called]() { keymap_was_called = true; }); evaluator.evaluate(Qt::ControlModifier, Qt::Key_T); @@ -83,6 +88,7 @@ private slots: it("calls mapping") { auto keymap_was_called = false; KeymapEvaluator evaluator; + evaluator.set_current_mode("n"); evaluator.add_keymap("n", "a", [&keymap_was_called]() { keymap_was_called = true; }); evaluator.evaluate(Qt::ControlModifier, Qt::Key_T); @@ -98,6 +104,7 @@ private slots: it("does not call mapping") { auto keymap_was_called = false; KeymapEvaluator evaluator; + evaluator.set_current_mode("n"); evaluator.add_keymap("n", "a", [&keymap_was_called]() { keymap_was_called = true; }); evaluator.evaluate(Qt::ControlModifier, Qt::Key_T); diff --git a/spec/lua/utils_spec.lua b/spec/lua/utils_spec.lua new file mode 100644 index 0000000..5ea2d78 --- /dev/null +++ b/spec/lua/utils_spec.lua @@ -0,0 +1,90 @@ +describe('web.utils.string_trim', function() + it('trims whitespace from the start and end of string', function() + expect(web.utils.string_trim(' hello')).to_equal 'hello' + expect(web.utils.string_trim('hello ')).to_equal 'hello' + expect(web.utils.string_trim(' hello ')).to_equal 'hello' + end) +end) + +describe('web.utils.table_merge', function() + context('when both tables have integer keys', function() + it('returns merged table', function() + expect(web.utils.table_merge({ a = 0 }, { b = 1 }, { c = 2 })) + .to_equal { a = 0, b = 1, c = 2 } + end) + end) + + context('when both tables unique keys', function() + it('returns merged table', function() + expect(web.utils.table_merge({ a = 0 }, { b = 1 }, { c = 2 })) + .to_equal { a = 0, b = 1, c = 2 } + end) + end) + + context('when both tables have a key in common', function() + it('returns merged table', function() + expect(web.utils.table_merge({ foo = 1, a = 0 }, { foo = 2 }, { foo = 3 })) + .to_equal { foo = 3, a = 0 } + end) + + it('mutates the first table', function() + local tbl = { foo = 1, a = 0 } + web.utils.table_merge(tbl, { foo = 2 }) + expect(tbl).to_equal { foo = 2, a = 0 } + end) + end) +end) + +describe('web.utils.equals', function() + context('when values are primitives', function() + it('checks for equality', function() + expect(web.utils.equals(1, 1)).to_be_true() + expect(web.utils.equals(3, 1)).to_be_false() + expect(web.utils.equals(nil, 1)).to_be_false() + expect(web.utils.equals(1, nil)).to_be_false() + expect(web.utils.equals(nil, nil)).to_be_true() + end) + end) + + context('when values are tables', function() + it('checks for equality', function() + expect(web.utils.equals({}, 0)).to_be_false() + expect(web.utils.equals(0, {})).to_be_false() + expect(web.utils.equals({}, {})).to_be_true() + expect(web.utils.equals({ a = 1 }, { a = 1 })).to_be_true() + expect(web.utils.equals({ a = 2 }, { a = 1 })).to_be_false() + expect(web.utils.equals({}, { a = 1 })).to_be_false() + expect(web.utils.equals({ a = 1 }, {})).to_be_false() + end) + + context('when tables are deeply nested', function() + it('checks for equality', function() + expect(web.utils.equals({ a = { b = 2 } }, { a = { b = 2 } })).to_be_true() + expect(web.utils.equals({ a = { b = 3 } }, { a = { b = 2 } })).to_be_false() + end) + end) + end) +end) + +describe('web.utils.table_keys', function() + it('returns keys of given table', function() + local result = web.utils.table_keys({ a = 1, b = 2 }) + table.sort(result) -- Sort for deterministic ordering + expect(result).to_equal({ 'a', 'b' }) + end) + + context('with index keys', function() + it('returns indexes of given table', function() + expect(web.utils.table_keys({ 'hello', 'world' })).to_equal({ 1, 2 }) + expect(web.utils.table_keys({ 'hello', a = 1, 'world' })).to_equal({ 1, 2, 'a' }) + end) + end) +end) + +describe('web.inspect', function() + it('returns string representation of value', function() + expect(web.inspect('hello')).to_equal '"hello"' + expect(web.inspect(5)).to_equal '5' + expect(web.inspect({ a = '200', b = 5 })).to_equal '{\n a = "200",\n b = 5\n}' + end) +end) diff --git a/spec/main.cpp b/spec/main.cpp index 15d0b60..1f60aea 100644 --- a/spec/main.cpp +++ b/spec/main.cpp @@ -1,8 +1,44 @@ +#include + +#include "LuaRuntime.hpp" #include "testUtils.h" -#include + +int run_lua_tests() { + QString test_name = getenv("TEST_NAME"); + if (test_name.startsWith("lua ")) { + test_name = test_name.remove(0, 4); + } else if (!test_name.isEmpty()) + return 0; + + QDir dir("../spec/lua"); // TODO: relative to root instead of build/? + QStringList spec_files = dir.entryList(QDir::Files); + auto &lua = LuaRuntime::instance(); + + lua.start_event_loop(); + lua.require_module("null-browser.test-utils"); + + for (auto &file : spec_files) { + auto is_spec = file.endsWith("_spec.lua"); + auto should_run_spec = test_name.isEmpty() || file.contains(test_name); + if (is_spec && should_run_spec) { + qDebug() << "Running suite: " << file; + lua.load_file_sync(dir.filePath(file)); + } + } + + lua.stop_event_loop(); + + qDebug() << "Tests ran successfully"; + return 0; +} int main(int argc, char **argv) { QApplication app(argc, argv); - return run_all_tests(); + int exit_code_app = run_app_tests(); + int exit_code_lua = run_lua_tests(); + + if (exit_code_app != 0) + return exit_code_app; + return exit_code_lua; } diff --git a/spec/testUtils.cpp b/spec/testUtils.cpp index 8f348bd..661f6a3 100644 --- a/spec/testUtils.cpp +++ b/spec/testUtils.cpp @@ -8,7 +8,7 @@ std::vector> &get_qtest_registry() { return registry; } -int run_all_tests() { +int run_app_tests() { int exit_code = 0; QString test_name = getenv("TEST_NAME"); @@ -27,8 +27,7 @@ int run_all_tests() { bool wait_for_lua_to_be_true(QString lua_code) { return QTest::qWaitFor([&lua_code]() { auto &lua = LuaRuntime::instance(); - QSignalSpy evaluation_completed_spy(&lua, - &LuaRuntime::evaluation_completed); + QSignalSpy evaluation_completed_spy(&lua, &LuaRuntime::evaluation_completed); lua.evaluate(lua_code); evaluation_completed_spy.wait(); return evaluation_completed_spy.first().first().toBool(); diff --git a/spec/testUtils.h b/spec/testUtils.h index a20291c..bfc7374 100644 --- a/spec/testUtils.h +++ b/spec/testUtils.h @@ -34,7 +34,7 @@ #define STRINGIFY(x) #x std::vector> &get_qtest_registry(); -int run_all_tests(); +int run_app_tests(); #define QTEST_REGISTER(klass) \ namespace { \ -- cgit v1.3.1