feat(testutil): add testutils
This commit is contained in:
parent
9a4417a0aa
commit
7c2b218370
1 changed files with 35 additions and 0 deletions
35
lua/gopher/_utils/testutil/init.lua
Normal file
35
lua/gopher/_utils/testutil/init.lua
Normal file
|
|
@ -0,0 +1,35 @@
|
||||||
|
local fixtures_dir = (vim.fn.expand "%:p:h") .. "/tests/fixtures/"
|
||||||
|
|
||||||
|
---@class gopher.TestUtil
|
||||||
|
local testutil = {}
|
||||||
|
|
||||||
|
---@return string
|
||||||
|
function testutil.tmp_file()
|
||||||
|
return vim.fn.tempname() .. ".go"
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param path string
|
||||||
|
---@return string
|
||||||
|
function testutil.readfile(path)
|
||||||
|
return vim.fn.join(vim.fn.readfile(path), "\n")
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param name string
|
||||||
|
---@return {input: string, output: string}
|
||||||
|
function testutil.read_fixture(name)
|
||||||
|
return {
|
||||||
|
input = testutil.readfile(fixtures_dir .. name .. "_input.go"),
|
||||||
|
output = testutil.readfile(fixtures_dir .. name .. "_output.go"),
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
function testutil.write_fixture(fpath, fixture)
|
||||||
|
vim.fn.writefile(vim.split(fixture, "\n"), fpath)
|
||||||
|
end
|
||||||
|
|
||||||
|
function testutil.cleanup(testfile)
|
||||||
|
vim.fn.delete(testfile)
|
||||||
|
vim.cmd.bd(testfile)
|
||||||
|
end
|
||||||
|
|
||||||
|
return testutil
|
||||||
Loading…
Add table
Add a link
Reference in a new issue