Skip to content

Commit

Permalink
tests: add cmd_spec
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb committed Jun 7, 2024
1 parent e89cd71 commit 7270d62
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lua/lz/n/handler/cmd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,12 @@ local function add_cmd(cmd)
})
end

---@param cmd string
function M.del(cmd)
pcall(vim.api.nvim_del_user_command, cmd)
---@param plugin LzPlugin
function M.del(plugin)
pcall(vim.api.nvim_del_user_command, plugin.cmd)
for _, plugins in pairs(M.pending) do
plugins[plugin.name] = nil
end
end

---@param plugin LzPlugin
Expand Down
59 changes: 59 additions & 0 deletions spec/cmd_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
---@diagnostic disable: invisible
local cmd = require("lz.n.handler.cmd")
local state = require("lz.n.state")
local loader = require("lz.n.loader")
local spy = require("luassert.spy")

describe("cmd", function()
it("Command only loads plugin once and executes plugin command", function()
---@type LzPlugin
local plugin = {
name = "foo",
cmd = { "Foo" },
}
local spy_load = spy.on(loader, "_load")
state.plugins[plugin.name] = plugin
cmd.add(plugin)
assert.is_not_nil(vim.cmd.Foo)
local counter = 0
local orig_load = loader._load
---@diagnostic disable-next-line: duplicate-set-field
loader._load = function(...)
orig_load(...)
vim.api.nvim_create_user_command("Foo", function()
counter = counter + 1
end, {})
end
vim.cmd.Foo()
vim.cmd.Foo()
assert.spy(spy_load).called(1)
assert.same(2, counter)
loader._load = orig_load
end)
it("Multiple commands only load plugin once", function()
---@param commands string[]
local function itt(commands)
local orig_load = loader._load
---@diagnostic disable-next-line: duplicate-set-field
loader._load = function(...)
orig_load(...)
vim.api.nvim_create_user_command("Foo", function() end, {})
vim.api.nvim_create_user_command("Bar", function() end, {})
end
---@type LzPlugin
local plugin = {
name = "foo",
cmd = commands,
}
local spy_load = spy.on(loader, "_load")
state.plugins[plugin.name] = plugin
cmd.add(plugin)
vim.cmd[commands[1]]()
vim.cmd[commands[2]]()
assert.spy(spy_load).called(1)
loader._load = orig_load
end
itt({ "Foo", "Bar" })
itt({ "Bar", "Foo" })
end)
end)

0 comments on commit 7270d62

Please sign in to comment.