generated from mrcjkb/nvim-lua-nix-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
65 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |