refactor(test): make helper test function more ergonomic

In my opinion, requiring user to access tests via `T["module_name"]`
was too fragile and typos prone.
This commit is contained in:
Olexandr Smirnov 2025-08-27 21:45:05 +03:00
parent 53ab4274c2
commit e90f766ea3
No known key found for this signature in database
8 changed files with 40 additions and 38 deletions

View file

@ -1,5 +1,5 @@
local t = require "spec.testutils"
local child, T = t.setup "comment"
local child, T, comment = t.setup "comment"
local function do_the_test(fixture, pos)
local rs = t.setup_test("comment/" .. fixture, child, pos)
@ -10,27 +10,27 @@ local function do_the_test(fixture, pos)
t.cleanup(rs)
end
T["comment"]["should add comment to package"] = function()
comment["should add comment to package"] = function()
do_the_test("package", { 1, 1 })
end
T["comment"]["should add comment to struct"] = function()
comment["should add comment to struct"] = function()
do_the_test("struct", { 4, 1 })
end
T["comment"]["should add comment to function"] = function()
comment["should add comment to function"] = function()
do_the_test("func", { 3, 1 })
end
T["comment"]["should add comment to method"] = function()
comment["should add comment to method"] = function()
do_the_test("method", { 5, 1 })
end
T["comment"]["should add comment to interface"] = function()
comment["should add comment to interface"] = function()
do_the_test("interface", { 3, 6 })
end
T["comment"]["otherwise should add // above cursor"] = function()
comment["otherwise should add // above cursor"] = function()
do_the_test("empty", { 1, 1 })
end

View file

@ -1,5 +1,5 @@
local t = require "spec.testutils"
local child, T = t.setup "gotests"
local child, T, gotests = t.setup "gotests"
--- NOTE: :GoTestAdd is the only place that has actual logic
--- All other parts are handled `gotests` itself.
@ -10,7 +10,7 @@ local function read_testfile(fpath)
return t.readfile(fpath:gsub(".go", "_test.go"))
end
T["gotests"]["should add test for function under cursor"] = function()
gotests["should add test for function under cursor"] = function()
local rs = t.setup_test("tests/function", child, { 3, 5 })
child.cmd "GoTestAdd"
@ -18,7 +18,7 @@ T["gotests"]["should add test for function under cursor"] = function()
t.cleanup(rs)
end
T["gotests"]["should add test for method under cursor"] = function()
gotests["should add test for method under cursor"] = function()
local rs = t.setup_test("tests/method", child, { 5, 19 })
child.cmd "GoTestAdd"

View file

@ -1,7 +1,7 @@
local t = require "spec.testutils"
local child, T = t.setup "iferr"
local child, T, iferr = t.setup "iferr"
T["iferr"]["should add if != nil {"] = function()
iferr["should add if != nil {"] = function()
local rs = t.setup_test("iferr/iferr", child, { 8, 2 })
child.cmd "GoIfErr"
child.cmd "write"
@ -10,7 +10,7 @@ T["iferr"]["should add if != nil {"] = function()
t.cleanup(rs)
end
T["iferr"]["should add if err with custom message"] = function()
iferr["should add if err with custom message"] = function()
child.lua [[
require("gopher").setup {
iferr = { message = 'fmt.Errorf("failed to %w", err)' }

View file

@ -1,7 +1,7 @@
local t = require "spec.testutils"
local child, T = t.setup "impl"
local child, T, impl = t.setup "impl"
T["impl"]["should do impl with 'w io.Writer'"] = function()
impl["should do impl with 'w io.Writer'"] = function()
local rs = t.setup_test("impl/writer", child, { 3, 0 })
child.cmd "GoImpl w io.Writer"
child.cmd "write"
@ -12,7 +12,7 @@ T["impl"]["should do impl with 'w io.Writer'"] = function()
t.cleanup(rs)
end
T["impl"]["should work with full input, 'r Read io.Reader'"] = function()
impl["should work with full input, 'r Read io.Reader'"] = function()
local rs = t.setup_test("impl/reader", child)
child.cmd "GoImpl r Read io.Reader"
child.cmd "write"
@ -22,7 +22,7 @@ T["impl"]["should work with full input, 'r Read io.Reader'"] = function()
t.cleanup(rs)
end
T["impl"]["should work with minimal input 'io.Closer'"] = function()
impl["should work with minimal input 'io.Closer'"] = function()
local rs = t.setup_test("impl/closer", child, { 3, 6 })
child.cmd "GoImpl io.Closer"
child.cmd "write"

View file

@ -1,7 +1,7 @@
local t = require "spec.testutils"
local child, T = t.setup "struct_tags"
local child, T, struct_tags = t.setup "struct_tags"
T["struct_tags"]["should add tag"] = function()
struct_tags["should add tag"] = function()
local rs = t.setup_test("tags/add", child, { 3, 6 })
child.cmd "GoTagAdd json"
child.cmd "write"
@ -10,7 +10,7 @@ T["struct_tags"]["should add tag"] = function()
t.cleanup(rs)
end
T["struct_tags"]["should remove tag"] = function()
struct_tags["should remove tag"] = function()
local rs = t.setup_test("tags/remove", child, { 4, 6 })
child.cmd "GoTagRm json"
child.cmd "write"
@ -19,7 +19,7 @@ T["struct_tags"]["should remove tag"] = function()
t.cleanup(rs)
end
T["struct_tags"]["should be able to handle many structs"] = function()
struct_tags["should be able to handle many structs"] = function()
local rs = t.setup_test("tags/many", child, { 10, 3 })
child.cmd "GoTagAdd testing"
child.cmd "write"
@ -28,7 +28,7 @@ T["struct_tags"]["should be able to handle many structs"] = function()
t.cleanup(rs)
end
T["struct_tags"]["should clear struct"] = function()
struct_tags["should clear struct"] = function()
local rs = t.setup_test("tags/clear", child, { 3, 1 })
child.cmd "GoTagClear"
child.cmd "write"
@ -37,7 +37,7 @@ T["struct_tags"]["should clear struct"] = function()
t.cleanup(rs)
end
T["struct_tags"]["should add more than one tag"] = function()
struct_tags["should add more than one tag"] = function()
local tmp = t.tmpfile()
local fixtures = t.get_fixtures "tags/add_many"
t.writefile(tmp, fixtures.input)
@ -60,7 +60,7 @@ T["struct_tags"]["should add more than one tag"] = function()
t.cleanup { tmp = tmp }
end
T["struct_tags"]["should add tags on var"] = function()
struct_tags["should add tags on var"] = function()
local rs = t.setup_test("tags/var", child, { 5, 6 })
child.cmd "GoTagAdd yaml"
child.cmd "write"
@ -69,7 +69,7 @@ T["struct_tags"]["should add tags on var"] = function()
t.cleanup(rs)
end
T["struct_tags"]["should add tags on short declr var"] = function()
struct_tags["should add tags on short declr var"] = function()
local rs = t.setup_test("tags/svar", child, { 4, 3 })
child.cmd "GoTagAdd xml"
child.cmd "write"