Skip to content

Commit

Permalink
feat(testutil): add testutils
Browse files Browse the repository at this point in the history
  • Loading branch information
olexsmir committed Aug 14, 2024
1 parent 9a4417a commit 7c2b218
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions lua/gopher/_utils/testutil/init.lua
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 7c2b218

Please # to comment.