aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--flake.nix3
-rw-r--r--init.lua15
-rw-r--r--lua/null-browser/defaults/vi.lua18
-rw-r--r--lua/null-browser/extras/dmenu.lua68
-rw-r--r--lua/null-browser/test-utils.lua96
-rw-r--r--lua/null-browser/utils.lua2
-rw-r--r--spec/lua/dmenu_spec.lua128
-rw-r--r--spec/lua/utils_spec.lua60
-rw-r--r--spec/main.cpp30
-rw-r--r--spec/testUtils.cpp34
-rw-r--r--spec/testUtils.h1
11 files changed, 325 insertions, 130 deletions
diff --git a/flake.nix b/flake.nix
index 88087ba..4ab2f01 100644
--- a/flake.nix
+++ b/flake.nix
@@ -17,6 +17,7 @@
lua-libluv = pkgs.callPackage (import ./nix/libluv.nix) {
inherit (myPkgs) libuv luajit;
};
+ luassert = pkgs.luajitPackages.luassert;
# lua-busted = pkgs.luajitPackages.busted;
};
@@ -26,6 +27,8 @@
luajit
libuv
lua-libluv
+
+ luassert
# lua-busted
];
in {
diff --git a/init.lua b/init.lua
index ddf87d8..4011800 100644
--- a/init.lua
+++ b/init.lua
@@ -1,6 +1,6 @@
print('hello starting up...')
-local dmenu = require 'null-browser.extras.dmenu'
+local Dmenu = require 'null-browser.extras.dmenu'
local history = require 'null-browser.extras.history'
local search_engines = require 'null-browser.extras.search-engines'
@@ -14,16 +14,23 @@ history.attach_hooks()
search_engines.urls['ld'] = 'https://lite.duckduckgo.com/?q={}'
+local menu = Dmenu:new {
+ command = os.getenv('HOME') .. '/scripts/fzfmenu.sh',
+ prompt_arg = '--prompt',
+ query_arg = '-q',
+ args = { 'input', '--layout=default' },
+}
+
require 'null-browser.defaults.vi'.setup {
- menu = dmenu,
+ menu = menu,
history = history,
transform_url_input = search_engines.transform_url_input,
}
--- web.set('permissions_persistance', 'never')
+web.set('permissions_persistance', 'never')
web.event.add_listener('PermissionRequested', {
callback = function(event)
- dmenu.select({ 'Allow', 'Deny' }, { prompt = 'Requesting permission for ' .. event.permission_type },
+ menu:select({ 'Allow', 'Deny' }, { prompt = 'Requesting permission for ' .. event.permission_type },
function(err, choice)
if err then return end
if web.utils.string_trim(choice) == 'Allow' then
diff --git a/lua/null-browser/defaults/vi.lua b/lua/null-browser/defaults/vi.lua
index cefbbd2..9c6425f 100644
--- a/lua/null-browser/defaults/vi.lua
+++ b/lua/null-browser/defaults/vi.lua
@@ -1,7 +1,9 @@
local M = {}
+local Dmenu = require 'null-browser.extras.dmenu'
+
local config = {
- menu = require 'null-browser.extras.dmenu',
+ menu = Dmenu:new {},
history = require 'null-browser.extras.history',
transform_url_input = web.utils.string_trim,
}
@@ -26,21 +28,21 @@ function M.initialize()
-- Open in new view
web.keymap.set('n', 'o', function()
- config.menu.select(config.history.list(), { prompt = 'Open view:' }, function(err, result)
+ config.menu:select(config.history.list(), { prompt = 'Open view:' }, function(err, result)
if err or not result then return end
web.view.create(config.transform_url_input(result))
end)
end)
-- Open in current view
web.keymap.set('n', '<s-o>', function()
- config.menu.select(config.history.list(), { prompt = 'Open:' }, function(err, result)
+ config.menu:select(config.history.list(), { prompt = 'Open:' }, function(err, result)
if err or not result then return end
web.view.set_url(config.transform_url_input(result))
end)
end)
-- Delete from history
web.keymap.set('n', 'dh', function()
- config.menu.select(config.history.list(), { prompt = 'Delete history:' }, function(err, result)
+ config.menu:select(config.history.list(), { prompt = 'Delete history:' }, function(err, result)
if err or not result then return end
config.history.delete(web.utils.string_trim(result))
end)
@@ -48,7 +50,7 @@ function M.initialize()
-- Search
web.keymap.set('n', '<c-f>', function()
- config.menu.input({ prompt = 'Search:', input = web.search.get_text() }, function(err, input)
+ config.menu:input({ prompt = 'Search:', query = web.search.get_text() }, function(err, input)
if err then return end
web.search.set_text(input)
end)
@@ -59,7 +61,7 @@ function M.initialize()
-- Run lua code
web.keymap.set('n', 'q', function()
- config.menu.input({ prompt = 'Lua:' }, function(err, result)
+ config.menu:input({ prompt = 'Lua:' }, function(err, result)
if err or not result then return end
local run, run_err = load(result)
if run_err then print(run_err) end
@@ -83,7 +85,7 @@ function M.initialize()
table.insert(views_list, index .. ': ' .. view.title .. ' (' .. view.url .. ')')
end
- config.menu.select(views_list, { prompt = 'Views:' }, function(err, result)
+ config.menu:select(views_list, { prompt = 'Views:' }, function(err, result)
if err or not result then return end
local index_str, _ = string.gsub(result, '%s*:.*$', '')
local index = tonumber(index_str)
@@ -127,7 +129,7 @@ function M.initialize()
local views = web.view.list()
local view = views[web.view.current_index()];
if view == nil then return end
- config.menu.select(config.history.list(), { prompt = 'Set url:', input = view.url }, function(err, result)
+ config.menu:select(config.history.list(), { prompt = 'Set url:', query = view.url }, function(err, result)
if err or not result then return end
web.view.set_url(web.utils.string_trim(result))
end)
diff --git a/lua/null-browser/extras/dmenu.lua b/lua/null-browser/extras/dmenu.lua
index fc92b35..f817294 100644
--- a/lua/null-browser/extras/dmenu.lua
+++ b/lua/null-browser/extras/dmenu.lua
@@ -1,26 +1,60 @@
-local dmenu = {}
+local Dmenu = {}
-function dmenu.select(list, opts, callback)
- local selection = nil
- local stdin = web.uv.new_pipe();
- local stdout = web.uv.new_pipe();
- local args = opts.args or {}
- if opts.prompt then
- table.insert(args, '-p')
- table.insert(args, opts.prompt)
+--- @param opts? table
+function Dmenu:new(opts)
+ opts = opts or {}
+ local default_opts = {
+ command = 'dmenu',
+ args = {},
+ prompt_arg = '-p',
+ query_arg = '-it',
+ prompt = nil,
+ query = nil,
+ }
+ local obj = {}
+ setmetatable(obj, self)
+ self.__index = self
+ obj.options = web.utils.table_merge(default_opts, opts or {})
+ return obj
+end
+
+function Dmenu:prepare_command(opts)
+ opts = opts or {}
+ local args = web.utils.table_merge({}, self.options.args or {})
+ for _, arg in ipairs(opts.args or {}) do
+ table.insert(args, arg)
end
- if opts.input then
- table.insert(args, '-it')
- table.insert(args, opts.input)
+
+ local options = web.utils.table_merge({}, self.options, opts or {})
+
+ if options.prompt then
+ table.insert(args, options.prompt_arg)
+ table.insert(args, options.prompt)
+ end
+
+ if options.query then
+ table.insert(args, options.query_arg)
+ table.insert(args, options.query)
end
- web.uv.spawn('dmenu', { args = args, stdio = { stdin, stdout, nil } }, function(code)
+ return { command = options.command, args = args }
+end
+
+function Dmenu:select(list, opts, callback)
+ local cmd = self:prepare_command(opts)
+
+ local stdin = web.uv.new_pipe();
+ local stdout = web.uv.new_pipe();
+
+ local selection = nil
+ web.uv.spawn(cmd.command, { args = cmd.args, stdio = { stdin, stdout, nil } }, function(code)
web.uv.close(stdout)
web.uv.close(stdin)
+
if code == 0 then
callback(nil, selection)
else
- callback('Exit with status code: ' .. code, selection)
+ callback('[dmenu] Exit with status code: ' .. code, selection)
end
end)
@@ -34,8 +68,8 @@ function dmenu.select(list, opts, callback)
web.uv.shutdown(stdin)
end
-function dmenu.input(opts, callback)
- dmenu.select({}, opts, callback)
+function Dmenu:input(opts, callback)
+ return self:select({}, opts, callback)
end
-return dmenu
+return Dmenu
diff --git a/lua/null-browser/test-utils.lua b/lua/null-browser/test-utils.lua
index 8871ce6..bd28c76 100644
--- a/lua/null-browser/test-utils.lua
+++ b/lua/null-browser/test-utils.lua
@@ -1,15 +1,27 @@
-local t = {}
+local luassert = require 'luassert'
+local match = require 'luassert.match'
+local spy = require 'luassert.spy'
+local stub = require 'luassert.stub'
-local indent = 0
+local t = {
+ before_each_hooks = {},
+}
-local function with_indent(func)
- indent = indent + 1
- func()
- indent = indent - 1
+local level = 0
+
+local function with_indent(func, ...)
+ level = level + 1
+ func(...)
+ t.before_each_hooks[level] = nil
+ level = level - 1
+end
+
+function t.before_each(callback)
+ t.before_each_hooks[level] = callback
end
function t.describe(description, func)
- local prefix = string.rep(' ', indent)
+ local prefix = string.rep(' ', level)
print(prefix .. '\x1b[1m' .. description .. '\x1b[0m')
io.flush()
@@ -17,58 +29,64 @@ function t.describe(description, func)
end
function t.context(description, func)
- local prefix = string.rep(' ', indent)
- print(prefix .. '│ \x1b[32m' .. description .. '\x1b[0m')
+ local prefix = string.rep(' ', level)
+ print(prefix .. '├ \x1b[32m' .. description .. '\x1b[0m')
io.flush()
+
with_indent(func)
end
function t.it(description, func)
- local prefix = string.rep(' ', indent - 1)
+ local prefix = string.rep(' ', level - 1)
print(prefix .. '└─⚪\x1b[36m' .. description .. '\x1b[0m')
io.flush()
+
+ local snapshot = assert:snapshot()
+ for _, value in pairs(t.before_each_hooks) do
+ if value and type(value) == 'function' then
+ value()
+ end
+ end
with_indent(func)
+ snapshot:revert()
end
---@diagnostic disable-next-line: unused-local
function t.xit(description, func)
- print('└─⚪\x1b[33m' .. description .. '\x1b[0m')
+ local prefix = string.rep(' ', level - 1)
+ print(prefix .. '└─\x1b[33m⚠ SKIPPED ' .. description .. '\x1b[0m')
io.flush()
end
-function t.expect(received)
- local assert = {}
-
- local function assert_error(messages)
- print('❌AssertionError:')
- for _, msg in ipairs(messages) do
- print(' ' .. msg)
- end
- error('assertion_error')
- end
-
- function assert.to_equal(expected)
- if web.utils.equals(received, expected) then return end
- assert_error({ 'Expected: ' .. web.inspect(expected), 'Received: ' .. web.inspect(received) })
- end
+_G.describe = t.describe
+_G.context = t.context
+_G.it = t.it
+_G.xit = t.xit
+_G.assert = luassert
+_G.spy = spy
+_G.match = match
+_G.stub = stub
- function assert.to_be_true()
- if received == true then return end
- assert_error({ 'Expected: ' .. web.inspect(received) .. ' to be true' })
- end
+local function deep_match(matcher, value)
+ if matcher == value then return true end
- function assert.to_be_false()
- if received == false then return end
- assert_error({ 'Expected: ' .. web.inspect(received) .. ' to be false' })
+ for key, match_val in pairs(matcher) do
+ if match_val ~= value[key] then
+ if match.is_matcher(match_val) then
+ if not match_val(value[key]) then return false end
+ elseif type(match_val) == 'table' and not deep_match(match_val, value[key]) then
+ return false
+ elseif not web.utils.equals(match_val, value[key]) then
+ return false
+ end
+ end
end
- return assert
+ return true
end
-_G.describe = t.describe
-_G.context = t.context
-_G.it = t.it
-_G.xit = t.xit
-_G.expect = t.expect
+assert:register('matcher', 'shape', function(_, arguments)
+ return function(value) return deep_match(arguments[1], value) end
+end)
return t
diff --git a/lua/null-browser/utils.lua b/lua/null-browser/utils.lua
index 2fe44e1..13b9d13 100644
--- a/lua/null-browser/utils.lua
+++ b/lua/null-browser/utils.lua
@@ -24,7 +24,7 @@ end
-- TODO: Documentation
function web.utils.table_merge(t, ...)
for i = 1, select("#", ...) do
- for k, v in pairs(select(i, ...)) do
+ for k, v in pairs(select(i, ...) or {}) do
t[k] = v
end
end
diff --git a/spec/lua/dmenu_spec.lua b/spec/lua/dmenu_spec.lua
new file mode 100644
index 0000000..3f405e6
--- /dev/null
+++ b/spec/lua/dmenu_spec.lua
@@ -0,0 +1,128 @@
+local Dmenu = require 'null-browser.extras.dmenu'
+local t = require 'null-browser.test-utils'
+
+describe('class: extras.dmenu', function()
+ t.before_each(function()
+ stub(web.uv, 'spawn')
+ stub(web.uv, 'new_pipe', 0)
+ stub(web.uv, 'close')
+ stub(web.uv, 'read_start')
+ stub(web.uv, 'shutdown')
+ stub(web.uv, 'write')
+ end)
+
+ describe('#select', function()
+ context('with default options', function()
+ it('spawns the dmenu process', function()
+ local dmenu = Dmenu:new {}
+ local spawn_proccess_stub = stub(web.uv, 'spawn')
+
+ dmenu:select({}, {}, function() end)
+
+ assert.stub(spawn_proccess_stub).was.called_with(
+ 'dmenu',
+ match.shape({ args = {}, stdio = match.table() }),
+ match.is_function()
+ )
+ end)
+ end)
+
+ context('with custom command options', function()
+ it('spawns the custom dmenu process', function()
+ local dmenu = Dmenu:new { command = 'some-other-dmenu', args = { 'my-arg' } }
+ local spawn_proccess_stub = stub(web.uv, 'spawn')
+
+ dmenu:select({}, {}, function() end)
+
+ assert.stub(spawn_proccess_stub).was.called_with(
+ 'some-other-dmenu',
+ match.shape({ args = { 'my-arg' }, stdio = match.table() }),
+ match.is_function()
+ )
+ end)
+ end)
+
+ context('when a list is provided', function()
+ it('writes the list to stdin of process', function()
+ local dmenu = Dmenu:new {}
+ local write_data_spy = stub(web.uv, 'write')
+
+ dmenu:select({ 'hello', 'world', 'testing' }, {}, function() end)
+
+ assert.stub(write_data_spy).was.called_with(0, 'hello\n')
+ assert.stub(write_data_spy).was.called_with(0, 'world\n')
+ assert.stub(write_data_spy).was.called_with(0, 'testing\n')
+ end)
+ end)
+
+ context('when process exits with status code 0', function()
+ context('when process outputs to stdout', function()
+ it('calls result callback with output line and no error', function()
+ local dmenu = Dmenu:new {}
+ local invoke_process_exit = nil
+ local on_result = spy.new()
+ stub(web.uv, 'spawn').invokes(function(_, _, callback)
+ invoke_process_exit = callback
+ end)
+ stub(web.uv, 'read_start').invokes(function(_, callback)
+ callback(nil, 'user input')
+ if invoke_process_exit then invoke_process_exit(0) end
+ end)
+
+ dmenu:select({}, {}, on_result)
+
+ assert.stub(on_result).was.called_with(nil, 'user input')
+ end)
+ end)
+
+ context('when process does not output to stdout', function()
+ it('calls result callback with nil and no error', function()
+ local dmenu = Dmenu:new {}
+ local on_result = spy.new()
+ stub(web.uv, 'spawn').invokes(function(_, _, callback)
+ callback(0)
+ end)
+
+ dmenu:select({}, {}, on_result)
+
+ assert.stub(on_result).was.called_with(nil, nil)
+ end)
+ end)
+ end)
+
+ context('when process exits with non-zero status code', function()
+ context('when process outputs to stdout', function()
+ it('calls result callback with an error and output line', function()
+ local dmenu = Dmenu:new {}
+ local invoke_process_exit = nil
+ local on_result = spy.new()
+ stub(web.uv, 'spawn').invokes(function(_, _, callback)
+ invoke_process_exit = callback
+ end)
+ stub(web.uv, 'read_start').invokes(function(_, callback)
+ callback(nil, 'user input')
+ if invoke_process_exit then invoke_process_exit(42) end
+ end)
+
+ dmenu:select({}, {}, on_result)
+
+ assert.stub(on_result).was.called_with(match.string(), 'user input')
+ end)
+ end)
+
+ context('when process does not output to stdout', function()
+ it('calls result callback with an error and nil', function()
+ local dmenu = Dmenu:new {}
+ local on_result = spy.new()
+ stub(web.uv, 'spawn').invokes(function(_, _, callback)
+ callback(42)
+ end)
+
+ dmenu:select({}, {}, on_result)
+
+ assert.stub(on_result).was.called_with(match.string(), nil)
+ end)
+ end)
+ end)
+ end)
+end)
diff --git a/spec/lua/utils_spec.lua b/spec/lua/utils_spec.lua
index 5ea2d78..de90ef5 100644
--- a/spec/lua/utils_spec.lua
+++ b/spec/lua/utils_spec.lua
@@ -1,36 +1,36 @@
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'
+ assert.are.equal(web.utils.string_trim(' hello'), 'hello')
+ assert.are.equal(web.utils.string_trim('hello '), 'hello')
+ assert.are.equal(web.utils.string_trim(' hello '), '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 }
+ local result = web.utils.table_merge({ a = 0 }, { b = 1 }, { c = 2 })
+ assert.are.same(result, { 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 }
+ local result = web.utils.table_merge({ a = 0 }, { b = 1 }, { c = 2 })
+ assert.are.same(result, { 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 }
+ local result = web.utils.table_merge({ foo = 1, a = 0 }, { foo = 2 }, { foo = 3 })
+ assert.are.same(result, { 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 }
+ assert.are.same(tbl, { foo = 2, a = 0 })
end)
end)
end)
@@ -38,29 +38,29 @@ 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()
+ assert.is_true(web.utils.equals(1, 1))
+ assert.is_false(web.utils.equals(3, 1))
+ assert.is_false(web.utils.equals(nil, 1))
+ assert.is_false(web.utils.equals(1, nil))
+ assert.is_true(web.utils.equals(nil, nil))
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()
+ assert.is_false(web.utils.equals({}, 0))
+ assert.is_false(web.utils.equals(0, {}))
+ assert.is_true(web.utils.equals({}, {}))
+ assert.is_true(web.utils.equals({ a = 1 }, { a = 1 }))
+ assert.is_false(web.utils.equals({ a = 2 }, { a = 1 }))
+ assert.is_false(web.utils.equals({}, { a = 1 }))
+ assert.is_false(web.utils.equals({ a = 1 }, {}))
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()
+ assert.is_true(web.utils.equals({ a = { b = 2 } }, { a = { b = 2 } }))
+ assert.is_false(web.utils.equals({ a = { b = 3 } }, { a = { b = 2 } }))
end)
end)
end)
@@ -70,21 +70,21 @@ 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' })
+ assert.are.same(result, { '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' })
+ assert.are.same(web.utils.table_keys({ 'hello', 'world' }), { 1, 2 })
+ assert.are.same(web.utils.table_keys({ 'hello', a = 1, 'world' }), { 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}'
+ assert.are.equal(web.inspect('hello'), '"hello"')
+ assert.are.equal(web.inspect(5), '5')
+ assert.are.equal(web.inspect({ a = '200', b = 5 }), '{\n a = "200",\n b = 5\n}')
end)
end)
diff --git a/spec/main.cpp b/spec/main.cpp
index 1f60aea..6de21e2 100644
--- a/spec/main.cpp
+++ b/spec/main.cpp
@@ -1,37 +1,7 @@
#include <QtCore>
-#include "LuaRuntime.hpp"
#include "testUtils.h"
-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);
diff --git a/spec/testUtils.cpp b/spec/testUtils.cpp
index 661f6a3..c359e91 100644
--- a/spec/testUtils.cpp
+++ b/spec/testUtils.cpp
@@ -1,8 +1,9 @@
-#include "LuaRuntime.hpp"
#include <QtTest/QtTest>
#include <QtTest/qtestcase.h>
#include <cstdio>
+#include "LuaRuntime.hpp"
+
std::vector<std::function<QObject *()>> &get_qtest_registry() {
static std::vector<std::function<QObject *()>> registry;
return registry;
@@ -12,6 +13,8 @@ int run_app_tests() {
int exit_code = 0;
QString test_name = getenv("TEST_NAME");
+ if (test_name.startsWith("lua:"))
+ return 0;
for (const auto &run_test : get_qtest_registry()) {
auto *test = run_test();
@@ -24,6 +27,35 @@ int run_app_tests() {
return exit_code;
}
+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;
+}
+
bool wait_for_lua_to_be_true(QString lua_code) {
return QTest::qWaitFor([&lua_code]() {
auto &lua = LuaRuntime::instance();
diff --git a/spec/testUtils.h b/spec/testUtils.h
index bfc7374..392f0bb 100644
--- a/spec/testUtils.h
+++ b/spec/testUtils.h
@@ -35,6 +35,7 @@
std::vector<std::function<QObject *()>> &get_qtest_registry();
int run_app_tests();
+int run_lua_tests();
#define QTEST_REGISTER(klass) \
namespace { \