From 1d45a443ffd89174dd192206bd6f47e09cca797e Mon Sep 17 00:00:00 2001 From: Oleksandr Smirnov Date: Tue, 18 Feb 2025 16:45:43 +0200 Subject: [PATCH] refactor(test): testutils --- spec/integration/comment_test.lua | 26 +++++++++++++++++++++++- spec/integration/gotests_test.lua | 15 ++++++++++++-- spec/integration/iferr_test.lua | 6 +++--- spec/integration/impl_test.lua | 16 +++++++-------- spec/integration/struct_tags_test.lua | 10 ++++----- spec/testutils.lua | 29 +++++++++++++++++---------- 6 files changed, 72 insertions(+), 30 deletions(-) diff --git a/spec/integration/comment_test.lua b/spec/integration/comment_test.lua index 6288615..da85496 100644 --- a/spec/integration/comment_test.lua +++ b/spec/integration/comment_test.lua @@ -5,10 +5,34 @@ local T = MiniTest.new_set { hooks = { post_once = child.stop, pre_case = function() - child.restart { "-u", "scripts/minimal_init.lua" } + child.restart { "-u", t.mininit_path } end, }, } T["comment"] = MiniTest.new_set {} +T["comment"]["should add comment to package"] = function() + MiniTest.skip "come back daddy" +end + +T["comment"]["should add comment to struct"] = function() + MiniTest.skip "come back daddy" +end + +T["comment"]["should add comment to function"] = function() + MiniTest.skip "come back daddy" +end + +T["comment"]["should add comment to method"] = function() + MiniTest.skip "come back daddy" +end + +T["comment"]["should add comment to interface"] = function() + MiniTest.skip "come back daddy" +end + +T["comment"]["otherwise should add // above cursor"] = function() + MiniTest.skip "come back daddy" +end + return T diff --git a/spec/integration/gotests_test.lua b/spec/integration/gotests_test.lua index 531d29c..b5751b5 100644 --- a/spec/integration/gotests_test.lua +++ b/spec/integration/gotests_test.lua @@ -5,10 +5,21 @@ local T = MiniTest.new_set { hooks = { post_once = child.stop, pre_case = function() - child.restart { "-u", "scripts/minimal_init.lua" } + child.restart { "-u", t.mininit_path } end, }, } -T["gotest"] = MiniTest.new_set {} +T["gotests"] = MiniTest.new_set {} + +--- NOTE: :GoTestAdd is the only place that has actual logic +--- All other parts are handled `gotests` tool itself. + +T["gotests"]["should add test for function under cursor"] = function() + MiniTest.skip "come back daddy" +end + +T["gotests"]["should add test for method under cursor"] = function() + MiniTest.skip "come back daddy" +end return T diff --git a/spec/integration/iferr_test.lua b/spec/integration/iferr_test.lua index 4cdc708..14ee08e 100644 --- a/spec/integration/iferr_test.lua +++ b/spec/integration/iferr_test.lua @@ -5,15 +5,15 @@ local T = MiniTest.new_set { hooks = { post_once = child.stop, pre_case = function() - child.restart { "-u", "scripts/minimal_init.lua" } + child.restart { "-u", t.mininit_path } end, }, } T["iferr"] = MiniTest.new_set {} T["iferr"]["works"] = function() local tmp = t.tmpfile() - local fixtures = t.fixtures.read "iferr/iferr" - t.fixtures.write(tmp, fixtures.input) + local fixtures = t.get_fixtures "iferr/iferr" + t.writefile(tmp, fixtures.input) child.cmd("silent edit " .. tmp) child.fn.setpos(".", { child.fn.bufnr "%", 8, 2, 0 }) diff --git a/spec/integration/impl_test.lua b/spec/integration/impl_test.lua index bc4a596..5376fa5 100644 --- a/spec/integration/impl_test.lua +++ b/spec/integration/impl_test.lua @@ -5,30 +5,30 @@ local T = MiniTest.new_set { hooks = { post_once = child.stop, pre_case = function() - child.restart { "-u", "scripts/minimal_init.lua" } + 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.fixtures.read "impl/writer" - t.fixtures.write(tmp, fixtures.input) + local fixtures = t.get_fixtures "impl/writer" + t.writefile(tmp, fixtures.input) child.cmd("silent edit " .. tmp) child.fn.setpos(".", { child.fn.bufnr(tmp), 3, 6 }) child.cmd "GoImpl w io.Writer" child.cmd "write" - -- since "impl" won't implement interface if it's already implemented i went with this hack + -- 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) end T["impl"]["works r Read io.Reader"] = function() local tmp = t.tmpfile() - local fixtures = t.fixtures.read "impl/reader" - t.fixtures.write(tmp, fixtures.input) + local fixtures = t.get_fixtures "impl/reader" + t.writefile(tmp, fixtures.input) child.cmd("silent edit " .. tmp) child.cmd "GoImpl r Read io.Reader" @@ -40,8 +40,8 @@ end T["impl"]["works io.Closer"] = function() local tmp = t.tmpfile() - local fixtures = t.fixtures.read "impl/closer" - t.fixtures.write(tmp, fixtures.input) + 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 }) diff --git a/spec/integration/struct_tags_test.lua b/spec/integration/struct_tags_test.lua index 0d22a00..8fb4aa3 100644 --- a/spec/integration/struct_tags_test.lua +++ b/spec/integration/struct_tags_test.lua @@ -5,15 +5,15 @@ local T = MiniTest.new_set { hooks = { post_once = child.stop, pre_case = function() - child.restart { "-u", "scripts/minimal_init.lua" } + child.restart { "-u", t.mininit_path } end, }, } T["struct_tags"] = MiniTest.new_set {} T["struct_tags"]["works add"] = function() local tmp = t.tmpfile() - local fixtures = t.fixtures.read "tags/add" - t.fixtures.write(tmp, fixtures.input) + local fixtures = t.get_fixtures "tags/add" + t.writefile(tmp, fixtures.input) child.cmd("silent edit " .. tmp) child.fn.setpos(".", { child.fn.bufnr "%", 3, 6, 0 }) @@ -24,8 +24,8 @@ end T["struct_tags"]["works remove"] = function() local tmp = t.tmpfile() - local fixtures = t.fixtures.read "tags/remove" - t.fixtures.write(tmp, fixtures.input) + local fixtures = t.get_fixtures "tags/remove" + t.writefile(tmp, fixtures.input) child.cmd("silent edit " .. tmp) child.fn.setpos(".", { child.fn.bufnr "%", 4, 6, 0 }) diff --git a/spec/testutils.lua b/spec/testutils.lua index b4ed3c1..177b9a1 100644 --- a/spec/testutils.lua +++ b/spec/testutils.lua @@ -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