Something went wrong. Try again.
**[mirror]** Make your go dev experience better github.com/olexsmir/gopher.nvim
neovim golang
Something went wrong. Try again.
2.7 kB · 108 lines
Lua
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109local base_dir = vim.env.GOPHER_DIR or vim.fn.expand "%:p:h"
---@class gopher.TestUtilslocal testutils = {}
testutils.mininit_path = vim.fs.joinpath(base_dir, "scripts", "minimal_init.lua")testutils.fixtures_dir = vim.fs.joinpath(base_dir, "spec/fixtures")
---@param mod string Module name for which to create a nested test set.---@return MiniTest.child child nvim client.---@return table T root test set created by `MiniTest.new_set()`.---@return table mod_name nested set of tests in `T[mod]`.function testutils.setup(mod) 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[mod] = MiniTest.new_set {} return child, T, T[mod]end
---@generic T---@param a T---@param b T---@return booleanfunction testutils.eq(a, b) return MiniTest.expect.equality(a, b)end
---@return stringfunction testutils.tmpfile() return vim.fn.tempname() .. ".go"end
---@param path string---@return stringfunction testutils.readfile(path) return vim.fn.join(vim.fn.readfile(path), "\n")end
---@param fpath string---@param contents stringfunction testutils.writefile(fpath, contents) vim.fn.writefile(vim.split(contents, "\n"), fpath)end
---@param fpath stringfunction testutils.deletefile(fpath) vim.fn.delete(fpath)end
---@class gopher.TestUtilsFixtures---@field input string---@field output string
---@param fixture string---@return gopher.TestUtilsFixturesfunction testutils.get_fixtures(fixture) return { input = testutils.readfile(vim.fs.joinpath(testutils.fixtures_dir, fixture) .. "_input.go"), output = testutils.readfile(vim.fs.joinpath(testutils.fixtures_dir, fixture) .. "_output.go"), }end
---@class gopher.TestUtilsSetup---@field tmp string---@field fixtures gopher.TestUtilsFixtures---@field bufnr number
---@param fixture string---@param child MiniTest.child---@param pos? number[]---@return gopher.TestUtilsSetupfunction testutils.setup_test(fixture, child, pos) vim.validate("pos", pos, "table", true)
local tmp = testutils.tmpfile() local fixtures = testutils.get_fixtures(fixture)
testutils.writefile(tmp, fixtures.input) child.cmd("silent edit " .. tmp)
local bufnr = child.fn.bufnr(tmp) if pos then assert(#pos == 2, "invalid cursor position")
child.fn.setpos(".", { bufnr, unpack(pos) }) end
return { tmp = tmp, bufnr = bufnr, fixtures = fixtures, }end
---@param inp gopher.TestUtilsSetupfunction testutils.cleanup(inp) testutils.deletefile(inp.tmp)end
return testutils