tests
This commit is contained in:
parent
d3d51c1ef8
commit
395441bac3
8 changed files with 108 additions and 15 deletions
|
|
@ -148,7 +148,7 @@ require("gopher").setup {
|
|||
</summary>
|
||||
|
||||
```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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
1
spec/fixtures/alternate/source_input.go
vendored
Normal file
1
spec/fixtures/alternate/source_input.go
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
package main
|
||||
1
spec/fixtures/alternate/source_output.go
vendored
Normal file
1
spec/fixtures/alternate/source_output.go
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
package main
|
||||
1
spec/fixtures/alternate/test_input.go
vendored
Normal file
1
spec/fixtures/alternate/test_input.go
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
package main
|
||||
1
spec/fixtures/alternate/test_output.go
vendored
Normal file
1
spec/fixtures/alternate/test_output.go
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
package main
|
||||
83
spec/integration/alternate_test.lua
Normal file
83
spec/integration/alternate_test.lua
Normal file
|
|
@ -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 <cmd> 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue