refactor: some refactoring of tests (#102)

* feat(tests): add utils that does most of tests boilerplate

* refactor(tests): rewrite using new thing

* refactor(tests): clean up everywhere

* refactor(tests): remove boilerplate even further

* refactor(tests): soon it will be too far

* refactor(tests): some test renaming
This commit is contained in:
Smirnov Oleksandr 2025-03-22 21:08:33 +02:00 committed by Oleksandr Smirnov
parent 7ebe190c96
commit 3dcf708d34
No known key found for this signature in database
6 changed files with 114 additions and 161 deletions

View file

@ -6,6 +6,23 @@ local testutils = {}
testutils.mininit_path = vim.fs.joinpath(base_dir, "scripts", "minimal_init.lua")
testutils.fixtures_dir = vim.fs.joinpath(base_dir, "spec/fixtures")
---@param name string
---@return MiniTest.child, table
function testutils.setup(name)
local child = MiniTest.new_child_neovim()
local T = MiniTest.new_set {
hooks = {
post_once = child.stop,
pre_case = function()
child.restart { "-u", testutils.mininit_path }
end,
},
}
T[name] = MiniTest.new_set {}
return child, T
end
---@generic T
---@param a T
---@param b T
@ -36,8 +53,12 @@ function testutils.deletefile(fpath)
vim.fn.delete(fpath)
end
---@class gopher.TestUtilsFixtures
---@field input string
---@field output string
---@param fixture string
---@return {input: string, output: string}
---@return gopher.TestUtilsFixtures
function testutils.get_fixtures(fixture)
return {
input = testutils.readfile(vim.fs.joinpath(testutils.fixtures_dir, fixture) .. "_input.go"),
@ -45,4 +66,34 @@ function testutils.get_fixtures(fixture)
}
end
---@class gopher.TestUtilsSetup
---@field tmp string
---@field fixtures gopher.TestUtilsFixtures
---@param fixture string
---@param child MiniTest.child
---@param pos? number[]
---@return gopher.TestUtilsSetup
function testutils.setup_test(fixture, child, pos)
local tmp = testutils.tmpfile()
local fixtures = testutils.get_fixtures(fixture)
testutils.writefile(tmp, fixtures.input)
child.cmd("silent edit " .. tmp)
if pos then
child.fn.setpos(".", { child.fn.bufnr(tmp), unpack(pos) })
end
return {
tmp = tmp,
fixtures = fixtures,
}
end
---@param inp gopher.TestUtilsSetup
function testutils.cleanup(inp)
testutils.deletefile(inp.tmp)
end
return testutils