refactor(tests): remove boilerplate even further

This commit is contained in:
Oleksandr Smirnov 2025-03-22 20:16:32 +02:00
parent b2780c8efb
commit 2ca302a7a5
No known key found for this signature in database
6 changed files with 35 additions and 60 deletions

View file

@ -1,17 +1,9 @@
local t = require "spec.testutils" local t = require "spec.testutils"
local child = MiniTest.new_child_neovim() local child, T = t.setup()
local T = MiniTest.new_set {
hooks = {
post_once = child.stop,
pre_case = function()
child.restart { "-u", t.mininit_path }
end,
},
}
local function do_the_test(fixture, pos) local function do_the_test(fixture, pos)
local rs = t.setup("comment/" .. fixture, child, pos) local rs = t.setup_test("comment/" .. fixture, child, pos)
child.cmd "GoCmt" child.cmd "GoCmt"
child.cmd "write" child.cmd "write"

View file

@ -1,14 +1,6 @@
local t = require "spec.testutils" local t = require "spec.testutils"
local child = MiniTest.new_child_neovim() local child, T = t.setup()
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 {} T["gotests"] = MiniTest.new_set {}
--- NOTE: :GoTestAdd is the only place that has actual logic --- NOTE: :GoTestAdd is the only place that has actual logic
@ -21,7 +13,7 @@ local function read_testfile(fpath)
end end
T["gotests"]["should add test for function under cursor"] = function() T["gotests"]["should add test for function under cursor"] = function()
local rs = t.setup("tests/function", child, { 3, 5 }) local rs = t.setup_test("tests/function", child, { 3, 5 })
child.cmd "GoTestAdd" child.cmd "GoTestAdd"
t.eq(rs.fixtures.output, read_testfile(rs.tmp)) t.eq(rs.fixtures.output, read_testfile(rs.tmp))
@ -29,7 +21,7 @@ T["gotests"]["should add test for function under cursor"] = function()
end end
T["gotests"]["should add test for method under cursor"] = function() T["gotests"]["should add test for method under cursor"] = function()
local rs = t.setup("tests/method", child, { 5, 19 }) local rs = t.setup_test("tests/method", child, { 5, 19 })
child.cmd "GoTestAdd" child.cmd "GoTestAdd"
t.eq(rs.fixtures.output, read_testfile(rs.tmp)) t.eq(rs.fixtures.output, read_testfile(rs.tmp))

View file

@ -1,17 +1,9 @@
local t = require "spec.testutils" local t = require "spec.testutils"
local child = MiniTest.new_child_neovim() local child, T = t.setup()
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"] = MiniTest.new_set {}
T["iferr"]["works"] = function() T["iferr"]["works"] = function()
local rs = t.setup("iferr/iferr", child, { 8, 2 }) local rs = t.setup_test("iferr/iferr", child, { 8, 2 })
child.cmd "GoIfErr" child.cmd "GoIfErr"
child.cmd "write" child.cmd "write"
@ -25,7 +17,7 @@ T["iferr"]["works with custom message"] = function()
iferr = { message = 'fmt.Errorf("failed to %w", err)' } iferr = { message = 'fmt.Errorf("failed to %w", err)' }
} ]] } ]]
local rs = t.setup("iferr/message", child, { 6, 2 }) local rs = t.setup_test("iferr/message", child, { 6, 2 })
child.cmd "GoIfErr" child.cmd "GoIfErr"
child.cmd "write" child.cmd "write"

View file

@ -1,17 +1,9 @@
local t = require "spec.testutils" local t = require "spec.testutils"
local child = MiniTest.new_child_neovim() local child, T = t.setup()
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"] = MiniTest.new_set {}
T["impl"]["works w io.Writer"] = function() T["impl"]["works w io.Writer"] = function()
local rs = t.setup("impl/writer", child, { 3, 0 }) local rs = t.setup_test("impl/writer", child, { 3, 0 })
child.cmd "GoImpl w io.Writer" child.cmd "GoImpl w io.Writer"
child.cmd "write" child.cmd "write"
@ -22,7 +14,7 @@ T["impl"]["works w io.Writer"] = function()
end end
T["impl"]["works r Read io.Reader"] = function() T["impl"]["works r Read io.Reader"] = function()
local rs = t.setup("impl/reader", child) local rs = t.setup_test("impl/reader", child)
child.cmd "GoImpl r Read io.Reader" child.cmd "GoImpl r Read io.Reader"
child.cmd "write" child.cmd "write"
@ -32,7 +24,7 @@ T["impl"]["works r Read io.Reader"] = function()
end end
T["impl"]["works io.Closer"] = function() T["impl"]["works io.Closer"] = function()
local rs = t.setup("impl/closer", child, { 3, 6}) local rs = t.setup_test("impl/closer", child, { 3, 6 })
child.cmd "GoImpl io.Closer" child.cmd "GoImpl io.Closer"
child.cmd "write" child.cmd "write"

View file

@ -1,17 +1,9 @@
local t = require "spec.testutils" local t = require "spec.testutils"
local child = MiniTest.new_child_neovim() local child, T = t.setup()
local T = MiniTest.new_set {
hooks = {
post_once = child.stop,
pre_case = function()
child.restart { "-u", t.mininit_path }
end,
},
}
T["struct_tags"] = MiniTest.new_set {} T["struct_tags"] = MiniTest.new_set {}
T["struct_tags"]["should add tag"] = function() T["struct_tags"]["should add tag"] = function()
local rs = t.setup("tags/add", child, { 3, 6 }) local rs = t.setup_test("tags/add", child, { 3, 6 })
child.cmd "GoTagAdd json" child.cmd "GoTagAdd json"
child.cmd "write" child.cmd "write"
@ -20,7 +12,7 @@ T["struct_tags"]["should add tag"] = function()
end end
T["struct_tags"]["should remove tag"] = function() T["struct_tags"]["should remove tag"] = function()
local rs = t.setup("tags/remove", child, { 4, 6 }) local rs = t.setup_test("tags/remove", child, { 4, 6 })
child.cmd "GoTagRm json" child.cmd "GoTagRm json"
child.cmd "write" child.cmd "write"
@ -29,7 +21,7 @@ T["struct_tags"]["should remove tag"] = function()
end end
T["struct_tags"]["should be able to handle many structs"] = function() T["struct_tags"]["should be able to handle many structs"] = function()
local rs = t.setup("tags/many", child, { 10, 3 }) local rs = t.setup_test("tags/many", child, { 10, 3 })
child.cmd "GoTagAdd testing" child.cmd "GoTagAdd testing"
child.cmd "write" child.cmd "write"
@ -38,7 +30,7 @@ T["struct_tags"]["should be able to handle many structs"] = function()
end end
T["struct_tags"]["should clear struct"] = function() T["struct_tags"]["should clear struct"] = function()
local rs = t.setup("tags/clear", child, { 3, 1 }) local rs = t.setup_test("tags/clear", child, { 3, 1 })
child.cmd "GoTagClear" child.cmd "GoTagClear"
child.cmd "write" child.cmd "write"
@ -70,7 +62,7 @@ T["struct_tags"]["should add more than one tag"] = function()
end end
T["struct_tags"]["should add tags on var"] = function() T["struct_tags"]["should add tags on var"] = function()
local rs = t.setup("tags/var", child, { 5, 6 }) local rs = t.setup_test("tags/var", child, { 5, 6 })
child.cmd "GoTagAdd yaml" child.cmd "GoTagAdd yaml"
child.cmd "write" child.cmd "write"
@ -79,7 +71,7 @@ T["struct_tags"]["should add tags on var"] = function()
end end
T["struct_tags"]["should add tags on short declr var"] = function() T["struct_tags"]["should add tags on short declr var"] = function()
local rs = t.setup("tags/svar", child, { 4, 3 }) local rs = t.setup_test("tags/svar", child, { 4, 3 })
child.cmd "GoTagAdd xml" child.cmd "GoTagAdd xml"
child.cmd "write" child.cmd "write"

View file

@ -6,6 +6,21 @@ local testutils = {}
testutils.mininit_path = vim.fs.joinpath(base_dir, "scripts", "minimal_init.lua") testutils.mininit_path = vim.fs.joinpath(base_dir, "scripts", "minimal_init.lua")
testutils.fixtures_dir = vim.fs.joinpath(base_dir, "spec/fixtures") testutils.fixtures_dir = vim.fs.joinpath(base_dir, "spec/fixtures")
---@return MiniTest.child, table
function testutils.setup()
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,
},
}
return child, T
end
---@generic T ---@generic T
---@param a T ---@param a T
---@param b T ---@param b T
@ -57,7 +72,7 @@ end
---@param child MiniTest.child ---@param child MiniTest.child
---@param pos? number[] ---@param pos? number[]
---@return gopher.TestUtilsSetup ---@return gopher.TestUtilsSetup
function testutils.setup(fixture, child, pos) function testutils.setup_test(fixture, child, pos)
local tmp = testutils.tmpfile() local tmp = testutils.tmpfile()
local fixtures = testutils.get_fixtures(fixture) local fixtures = testutils.get_fixtures(fixture)