6 files changed,
114 insertions(+),
161 deletions(-)
Author:
Smirnov Oleksandr
ss2316544@gmail.com
Committed by:
Oleksandr Smirnov
olexsmir@gmail.com
Committed at:
2025-03-22 21:19:59 +0200
Parent:
7ebe190
M
spec/integration/comment_test.lua
@@ -1,32 +1,15 @@
local t = require "spec.testutils" - -local child = MiniTest.new_child_neovim() -local T = MiniTest.new_set { - hooks = { - post_once = child.stop, - pre_case = function() - child.restart { "-u", t.mininit_path } - end, - }, -} +local child, T = t.setup "comment" local function do_the_test(fixture, pos) - local tmp = t.tmpfile() - local fixtures = t.get_fixtures("comment/" .. fixture) - t.writefile(tmp, fixtures.input) - - child.cmd("silent edit " .. tmp) - child.fn.setpos(".", { child.fn.bufnr "%", unpack(pos) }) + local rs = t.setup_test("comment/" .. fixture, child, pos) child.cmd "GoCmt" child.cmd "write" - t.eq(t.readfile(tmp), fixtures.output) - - -- without it all other(not even from this module) tests are falling - t.deletefile(tmp) + t.eq(t.readfile(rs.tmp), rs.fixtures.output) + t.cleanup(rs) end -T["comment"] = MiniTest.new_set {} T["comment"]["should add comment to package"] = function() do_the_test("package", { 1, 1 }) end
M
spec/integration/gotests_test.lua
@@ -1,15 +1,5 @@
local t = require "spec.testutils" - -local child = MiniTest.new_child_neovim() -local T = MiniTest.new_set { - hooks = { - post_once = child.stop, - pre_case = function() - child.restart { "-u", t.mininit_path } - end, - }, -} -T["gotests"] = MiniTest.new_set {} +local child, T = t.setup "gotests" --- NOTE: :GoTestAdd is the only place that has actual logic --- All other parts are handled `gotests` itself.@@ -21,27 +11,19 @@ return t.readfile(fpath:gsub(".go", "_test.go"))
end T["gotests"]["should add test for function under cursor"] = function() - local tmp = t.tmpfile() - local fixtures = t.get_fixtures "tests/function" - t.writefile(tmp, fixtures.input) - - child.cmd("silent edit " .. tmp) - child.fn.setpos(".", { child.fn.bufnr "%", 3, 6 }) + local rs = t.setup_test("tests/function", child, { 3, 5 }) child.cmd "GoTestAdd" - t.eq(fixtures.output, read_testfile(tmp)) + t.eq(rs.fixtures.output, read_testfile(rs.tmp)) + t.cleanup(rs) end T["gotests"]["should add test for method under cursor"] = function() - local tmp = t.tmpfile() - local fixtures = t.get_fixtures "tests/method" - t.writefile(tmp, fixtures.input) - - child.cmd("silent edit " .. tmp) - child.fn.setpos(".", { child.fn.bufnr "%", 5, 19 }) + local rs = t.setup_test("tests/method", child, { 5, 19 }) child.cmd "GoTestAdd" - t.eq(fixtures.output, read_testfile(tmp)) + t.eq(rs.fixtures.output, read_testfile(rs.tmp)) + t.cleanup(rs) end return T
M
spec/integration/iferr_test.lua
@@ -1,40 +1,27 @@
local t = require "spec.testutils" - -local child = MiniTest.new_child_neovim() -local T = MiniTest.new_set { - hooks = { - post_once = child.stop, - pre_case = function() - child.restart { "-u", t.mininit_path } - end, - }, -} -T["iferr"] = MiniTest.new_set {} -T["iferr"]["works"] = function() - local tmp = t.tmpfile() - local fixtures = t.get_fixtures "iferr/iferr" - t.writefile(tmp, fixtures.input) +local child, T = t.setup "iferr" - child.cmd("silent edit " .. tmp) - child.fn.setpos(".", { child.fn.bufnr "%", 8, 2, 0 }) +T["iferr"]["should add if != nil {"] = function() + local rs = t.setup_test("iferr/iferr", child, { 8, 2 }) child.cmd "GoIfErr" child.cmd "write" - t.eq(t.readfile(tmp), fixtures.output) + t.eq(t.readfile(rs.tmp), rs.fixtures.output) + t.cleanup(rs) end -T["iferr"]["works with custom message"] = function() - local tmp = t.tmpfile() - local fixtures = t.get_fixtures "iferr/message" - t.writefile(tmp, fixtures.input) +T["iferr"]["should add if err with custom message"] = function() + child.lua [[ + require("gopher").setup { + iferr = { message = 'fmt.Errorf("failed to %w", err)' } + } ]] - child.lua [[ require("gopher").setup { iferr = { message = 'fmt.Errorf("failed to %w", err)' } } ]] - child.cmd("silent edit " .. tmp) - child.fn.setpos(".", { child.fn.bufnr "%", 6, 2, 0 }) + local rs = t.setup_test("iferr/message", child, { 6, 2 }) child.cmd "GoIfErr" child.cmd "write" - t.eq(t.readfile(tmp), fixtures.output) + t.eq(t.readfile(rs.tmp), rs.fixtures.output) + t.cleanup(rs) end return T
M
spec/integration/impl_test.lua
@@ -1,55 +1,35 @@
local t = require "spec.testutils" - -local child = MiniTest.new_child_neovim() -local T = MiniTest.new_set { - hooks = { - post_once = child.stop, - pre_case = function() - child.restart { "-u", t.mininit_path } - end, - }, -} -T["impl"] = MiniTest.new_set {} -T["impl"]["works w io.Writer"] = function() - local tmp = t.tmpfile() - local fixtures = t.get_fixtures "impl/writer" - t.writefile(tmp, fixtures.input) +local child, T = t.setup "impl" - child.cmd("silent edit " .. tmp) - child.fn.setpos(".", { child.fn.bufnr(tmp), 3, 0 }) +T["impl"]["should do impl with 'w io.Writer'"] = function() + local rs = t.setup_test("impl/writer", child, { 3, 0 }) child.cmd "GoImpl w io.Writer" child.cmd "write" -- NOTE: since "impl" won't implement interface if it's already implemented i went with this hack - local rhs = fixtures.output:gsub("Test2", "Test") - t.eq(t.readfile(tmp), rhs) + local rhs = rs.fixtures.output:gsub("Test2", "Test") + t.eq(t.readfile(rs.tmp), rhs) + t.cleanup(rs) end -T["impl"]["works r Read io.Reader"] = function() - local tmp = t.tmpfile() - local fixtures = t.get_fixtures "impl/reader" - t.writefile(tmp, fixtures.input) - - child.cmd("silent edit " .. tmp) +T["impl"]["should work with full input, 'r Read io.Reader'"] = function() + local rs = t.setup_test("impl/reader", child) child.cmd "GoImpl r Read io.Reader" child.cmd "write" - local rhs = fixtures.output:gsub("Read2", "Read") - t.eq(t.readfile(tmp), rhs) + local rhs = rs.fixtures.output:gsub("Read2", "Read") + t.eq(t.readfile(rs.tmp), rhs) + t.cleanup(rs) end -T["impl"]["works io.Closer"] = function() - local tmp = t.tmpfile() - local fixtures = t.get_fixtures "impl/closer" - t.writefile(tmp, fixtures.input) - - child.cmd("silent edit " .. tmp) - child.fn.setpos(".", { child.fn.bufnr(tmp), 3, 6 }) +T["impl"]["should work with minimal input 'io.Closer'"] = function() + local rs = t.setup_test("impl/closer", child, { 3, 6 }) child.cmd "GoImpl io.Closer" child.cmd "write" - local rhs = fixtures.output:gsub("Test2", "Test") - t.eq(t.readfile(tmp), rhs) + local rhs = rs.fixtures.output:gsub("Test2", "Test") + t.eq(t.readfile(rs.tmp), rhs) + t.cleanup(rs) end return T
M
spec/testutils.lua
@@ -6,6 +6,23 @@
testutils.mininit_path = vim.fs.joinpath(base_dir, "scripts", "minimal_init.lua") testutils.fixtures_dir = vim.fs.joinpath(base_dir, "spec/fixtures") +---@param name string +---@return MiniTest.child, table +function testutils.setup(name) + 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[name] = MiniTest.new_set {} + return child, T +end + ---@generic T ---@param a T ---@param b T@@ -36,13 +53,47 @@ function testutils.deletefile(fpath)
vim.fn.delete(fpath) end +---@class gopher.TestUtilsFixtures +---@field input string +---@field output string + ---@param fixture string ----@return {input: string, output: string} +---@return gopher.TestUtilsFixtures function 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 + +---@param fixture string +---@param child MiniTest.child +---@param pos? number[] +---@return gopher.TestUtilsSetup +function testutils.setup_test(fixture, child, pos) + local tmp = testutils.tmpfile() + local fixtures = testutils.get_fixtures(fixture) + + testutils.writefile(tmp, fixtures.input) + child.cmd("silent edit " .. tmp) + + if pos then + child.fn.setpos(".", { child.fn.bufnr(tmp), unpack(pos) }) + end + + return { + tmp = tmp, + fixtures = fixtures, + } +end + +---@param inp gopher.TestUtilsSetup +function testutils.cleanup(inp) + testutils.deletefile(inp.tmp) end return testutils