From 7c2b218370cc03117ad6a90b9b9bc910cdcd56d2 Mon Sep 17 00:00:00 2001 From: Smirnov Oleksandr Date: Wed, 14 Aug 2024 16:25:23 +0300 Subject: [PATCH] feat(testutil): add testutils --- lua/gopher/_utils/testutil/init.lua | 35 +++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 lua/gopher/_utils/testutil/init.lua diff --git a/lua/gopher/_utils/testutil/init.lua b/lua/gopher/_utils/testutil/init.lua new file mode 100644 index 0000000..f0a44a0 --- /dev/null +++ b/lua/gopher/_utils/testutil/init.lua @@ -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