diff --git a/README.md b/README.md index 74d3352..45c41d9 100644 --- a/README.md +++ b/README.md @@ -148,7 +148,7 @@ require("gopher").setup { ```vim - " Open alternate test file (if _test file does not exist it will create it) + " Open alternate test file :GoAlt " Open test file in horizontal split diff --git a/lua/gopher/alternate.lua b/lua/gopher/alternate.lua index f283c37..8965d8d 100644 --- a/lua/gopher/alternate.lua +++ b/lua/gopher/alternate.lua @@ -1,22 +1,27 @@ -local M = {} +local alt = {} -function M.is_test_file() +function alt.is_test_file() local file = vim.fn.expand "%" + if #file <= 1 then vim.notify("no buffer name", vim.log.levels.ERROR) return nil, false, false end + local is_test = string.find(file, "_test%.go$") local is_source = string.find(file, "%.go$") + return file, (not is_test and is_source), is_test end -function M.alternate() - local file, is_source, is_test = M.is_test_file() +function alt.alternate() + local file, is_source, is_test = alt.is_test_file() if not file then return nil end + local alt_file = file + if is_test then alt_file = string.gsub(file, "_test.go", ".go") elseif is_source then @@ -24,15 +29,16 @@ function M.alternate() else vim.notify("not a go file", vim.log.levels.ERROR) end + return alt_file end -function M.switch(bang, cmd) - local alt_file = M.alternate() - if not vim.fn.filereadable(alt_file) and not vim.fn.bufexists(alt_file) and not bang then - vim.notify("couldn't find " .. alt_file, vim.log.levels.ERROR) - return - elseif #cmd <= 1 then +function alt.switch(cmd) + cmd = cmd or "" + + local alt_file = alt.alternate() + + if #cmd <= 1 then local ocmd = "e " .. alt_file vim.cmd(ocmd) else @@ -41,4 +47,4 @@ function M.switch(bang, cmd) end end -return M +return alt diff --git a/plugin/gopher.lua b/plugin/gopher.lua index d4bb682..7144e5a 100644 --- a/plugin/gopher.lua +++ b/plugin/gopher.lua @@ -88,13 +88,13 @@ end, "?") -- :GoAlt cmd("GoAlt", function() - require("gopher.alternate").switch(true, "") + require("gopher.alternate").switch() end) cmd("GoAltV", function() - require("gopher.alternate").switch(true, "vsplit") + require("gopher.alternate").switch "vsplit" end) cmd("GoAltS", function() - require("gopher.alternate").switch(true, "split") + require("gopher.alternate").switch "split" end) diff --git a/spec/fixtures/alternate/source_input.go b/spec/fixtures/alternate/source_input.go new file mode 100644 index 0000000..06ab7d0 --- /dev/null +++ b/spec/fixtures/alternate/source_input.go @@ -0,0 +1 @@ +package main diff --git a/spec/fixtures/alternate/source_output.go b/spec/fixtures/alternate/source_output.go new file mode 100644 index 0000000..06ab7d0 --- /dev/null +++ b/spec/fixtures/alternate/source_output.go @@ -0,0 +1 @@ +package main diff --git a/spec/fixtures/alternate/test_input.go b/spec/fixtures/alternate/test_input.go new file mode 100644 index 0000000..06ab7d0 --- /dev/null +++ b/spec/fixtures/alternate/test_input.go @@ -0,0 +1 @@ +package main diff --git a/spec/fixtures/alternate/test_output.go b/spec/fixtures/alternate/test_output.go new file mode 100644 index 0000000..06ab7d0 --- /dev/null +++ b/spec/fixtures/alternate/test_output.go @@ -0,0 +1 @@ +package main diff --git a/spec/integration/alternate_test.lua b/spec/integration/alternate_test.lua new file mode 100644 index 0000000..2607968 --- /dev/null +++ b/spec/integration/alternate_test.lua @@ -0,0 +1,83 @@ +-- spec/integration/alternate_test.lua +-- +-- Integration tests for the GoAlt family of commands after removing the +-- `bang` flag. The commands now *always* open or create the sibling file. +-- +-- Conventions match comment/iferr/impl suites: use `t.setup_test()`, +-- run the Ex-command via `child.cmd`, then `write`, then assertions. + +local t = require "spec.testutils" +local child, T = t.setup "alternate" + +-- ── small helpers ──────────────────────────────────────────────────────── + +---foo.go ⇄ foo_test.go +local function sibling(path) + if path:find "_test%.go$" then + return path:gsub("_test%.go$", ".go") + else + return path:gsub("%.go$", "_test.go") + end +end + +---Run inside a tmp copy of fixture `name`. +---@param name string "source" or "test" +---@param cmd string "GoAlt", "GoAltV", "GoAltS" +---@return table rs from t.setup_test (for later cleanup) +---@return string absolute sibling path +local function do_the_test(name, cmd) + local rs = t.setup_test("alternate/" .. name, child, { 1, 1 }) + local alt = sibling(rs.tmp) + + os.remove(alt) + child.cmd(cmd) + child.cmd "write" + + return rs, alt +end + +T["GoAlt"] = MiniTest.new_set {} +T["GoAltV"] = MiniTest.new_set {} +T["GoAltS"] = MiniTest.new_set {} + +T["GoAlt"]["creates/opens sibling from source"] = function() + local rs, alt = do_the_test("source", "GoAlt") + + t.eq(child.fn.expand "%:p", alt) + t.eq(child.fn.filereadable(alt), 0) + + t.cleanup(rs) + t.deletefile(alt) +end + +T["GoAlt"]["toggles back when invoked on test file"] = function() + local rs, src = do_the_test("test", "GoAlt") + + t.eq(child.fn.expand "%:p", src) + t.eq(child.fn.filereadable(src), 0) + + t.cleanup(rs) + t.deletefile(src) +end + +T["GoAltV"]["opens sibling in vertical split"] = function() + local rs, alt = do_the_test("source", "GoAltV") + + t.eq(#child.api.nvim_list_wins(), 2) + t.eq(child.fn.expand "%:p", alt) + + t.cleanup(rs) + t.deletefile(alt) +end + +T["GoAltS"]["opens sibling in horizontal split"] = function() + local rs, alt = do_the_test("source", "GoAltS") + + t.eq(#child.api.nvim_list_wins(), 2) + t.eq(child.fn.expand "%:p", alt) + + t.cleanup(rs) + t.deletefile(alt) +end + +return T