diff --git a/spec/integration/comment_test.lua b/spec/integration/comment_test.lua index 08882a1..e381908 100644 --- a/spec/integration/comment_test.lua +++ b/spec/integration/comment_test.lua @@ -1,17 +1,9 @@ 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() 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 "write" diff --git a/spec/integration/gotests_test.lua b/spec/integration/gotests_test.lua index 120b80d..a2f432f 100644 --- a/spec/integration/gotests_test.lua +++ b/spec/integration/gotests_test.lua @@ -1,14 +1,6 @@ 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() T["gotests"] = MiniTest.new_set {} --- NOTE: :GoTestAdd is the only place that has actual logic @@ -21,7 +13,7 @@ local function read_testfile(fpath) end 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" t.eq(rs.fixtures.output, read_testfile(rs.tmp)) @@ -29,7 +21,7 @@ T["gotests"]["should add test for function under cursor"] = function() end 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" t.eq(rs.fixtures.output, read_testfile(rs.tmp)) diff --git a/spec/integration/iferr_test.lua b/spec/integration/iferr_test.lua index 5f80955..c07536a 100644 --- a/spec/integration/iferr_test.lua +++ b/spec/integration/iferr_test.lua @@ -1,17 +1,9 @@ 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() T["iferr"] = MiniTest.new_set {} 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 "write" @@ -25,7 +17,7 @@ T["iferr"]["works with custom message"] = function() 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 "write" diff --git a/spec/integration/impl_test.lua b/spec/integration/impl_test.lua index 0fd259d..b2fae88 100644 --- a/spec/integration/impl_test.lua +++ b/spec/integration/impl_test.lua @@ -1,17 +1,9 @@ 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() T["impl"] = MiniTest.new_set {} 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 "write" @@ -22,7 +14,7 @@ T["impl"]["works w io.Writer"] = function() end 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 "write" @@ -32,7 +24,7 @@ T["impl"]["works r Read io.Reader"] = function() end 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 "write" diff --git a/spec/integration/struct_tags_test.lua b/spec/integration/struct_tags_test.lua index 87e40f2..bd72dab 100644 --- a/spec/integration/struct_tags_test.lua +++ b/spec/integration/struct_tags_test.lua @@ -1,17 +1,9 @@ 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() T["struct_tags"] = MiniTest.new_set {} 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 "write" @@ -20,7 +12,7 @@ T["struct_tags"]["should add tag"] = function() end 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 "write" @@ -29,7 +21,7 @@ T["struct_tags"]["should remove tag"] = function() end 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 "write" @@ -38,7 +30,7 @@ T["struct_tags"]["should be able to handle many structs"] = function() end 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 "write" @@ -70,7 +62,7 @@ T["struct_tags"]["should add more than one tag"] = function() end 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 "write" @@ -79,7 +71,7 @@ T["struct_tags"]["should add tags on var"] = function() end 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 "write" diff --git a/spec/testutils.lua b/spec/testutils.lua index 33668a7..3664402 100644 --- a/spec/testutils.lua +++ b/spec/testutils.lua @@ -6,6 +6,21 @@ local testutils = {} testutils.mininit_path = vim.fs.joinpath(base_dir, "scripts", "minimal_init.lua") 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 ---@param a T ---@param b T @@ -57,7 +72,7 @@ end ---@param child MiniTest.child ---@param pos? number[] ---@return gopher.TestUtilsSetup -function testutils.setup(fixture, child, pos) +function testutils.setup_test(fixture, child, pos) local tmp = testutils.tmpfile() local fixtures = testutils.get_fixtures(fixture)