Skip to content

Commit

Permalink
refactor: add a convenience wrapper for setting up and tearing down m…
Browse files Browse the repository at this point in the history
…ocks
  • Loading branch information
wincent committed Jan 17, 2025
1 parent 2d458c8 commit 36a06ba
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
31 changes: 31 additions & 0 deletions lua/wincent/commandt/private/mocks/vim/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
-- SPDX-FileCopyrightText: Copyright 2025-present Greg Hurrell and contributors.
-- SPDX-License-Identifier: BSD-2-Clause

return function(spec)
if type(spec) == 'table' then
assert(_G.vim == nil)
_G.vim = {}
for key, value in pairs(spec) do
if key == 'inspect' then
if value then
require('wincent.commandt.private.mocks.vim.inspect').setup()
else
vim.inspect = nil
end
elseif key == 'iter' then
if value then
require('wincent.commandt.private.mocks.vim.iter').setup()
else
vim.iter = nil
end
else
error('unsupported key: ' .. key)
end
end
elseif spec == false then
-- Remove mock.
_G.vim = nil
else
error('unsupported spec: ' .. type(spec))
end
end
10 changes: 6 additions & 4 deletions lua/wincent/commandt/private/test/validate.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
-- SPDX-License-Identifier: BSD-2-Clause

local validate = require('wincent.commandt.private.validate')
local mocks = require('wincent.commandt.private.mocks.vim')

describe('validate()', function()
before(function()
assert(_G.vim == nil)
require('wincent.commandt.private.mocks.vim.inspect').setup()
require('wincent.commandt.private.mocks.vim.iter').setup()
mocks({
inspect = true,
iter = true,
})
end)

after(function()
_G.vim = nil
mocks(false)
end)

local spec = {
Expand Down

0 comments on commit 36a06ba

Please sign in to comment.