From 8f974223f4318ed9a0d3035b4c11d0f5b015efec Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Fri, 2 May 2025 15:37:12 +0530 Subject: Refactor dmenu to be instantiable + add luassert for assertions --- lua/null-browser/defaults/vi.lua | 18 ++++---- lua/null-browser/extras/dmenu.lua | 68 ++++++++++++++++++++------- lua/null-browser/test-utils.lua | 96 +++++++++++++++++++++++---------------- lua/null-browser/utils.lua | 2 +- 4 files changed, 119 insertions(+), 65 deletions(-) (limited to 'lua') 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', '', 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', '', 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 -- cgit v1.3.1