refactor(test): testutils

This commit is contained in:
Oleksandr Smirnov 2025-02-18 16:45:43 +02:00
parent bc48bd71f2
commit 1d45a443ff
No known key found for this signature in database
6 changed files with 72 additions and 30 deletions

View file

@ -1,9 +1,12 @@
-- NOTE: there's a probably a better way to do this
local fixtures_dir = (vim.fn.expand "%:p:h") .. "/spec/fixtures/"
local base_dir = vim.fn.expand "%:p:h"
local fixtures_dir = vim.fs.joinpath(base_dir, "/spec/fixtures/")
---@class gopher.TestUtils
local testutils = {}
testutils.mininit_path = vim.fs.joinpath(base_dir, "/scripts/minimal_init.lua")
---@generic T
---@param a T
---@param b T
@ -23,21 +26,25 @@ function testutils.readfile(path)
return vim.fn.join(vim.fn.readfile(path), "\n")
end
testutils.fixtures = {}
---@param fpath string
---@param contents string
function testutils.writefile(fpath, contents)
vim.fn.writefile(vim.split(contents, "\n"), fpath)
end
---@param path string
---@return string
function testutils.readfile_from_fixture_dir(path)
return testutils.readfile(fixtures_dir .. path)
end
---@param fixture string
---@return {input: string, output: string}
function testutils.fixtures.read(fixture)
function testutils.get_fixtures(fixture)
return {
input = testutils.readfile(fixtures_dir .. fixture .. "_input.go"),
output = testutils.readfile(fixtures_dir .. fixture .. "_output.go"),
input = testutils.readfile_from_fixture_dir(fixture .. "_input.go"),
output = testutils.readfile_from_fixture_dir(fixture .. "_output.go"),
}
end
---@param fpath string
---@param fixture string
function testutils.fixtures.write(fpath, fixture)
vim.fn.writefile(vim.split(fixture, "\n"), fpath)
end
return testutils