[mirror] Make your go dev experience better github.com/olexsmir/gopher.nvim
neovim golang

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.

olexsmir.xyz e90f766e 53ab4274

verified
+7 -7
spec/integration/comment_test.lua
··· 1 1 local t = require "spec.testutils" 2 - local child, T = t.setup "comment" 2 + local child, T, comment = t.setup "comment" 3 3 4 4 local function do_the_test(fixture, pos) 5 5 local rs = t.setup_test("comment/" .. fixture, child, pos) ··· 10 10 t.cleanup(rs) 11 11 end 12 12 13 - T["comment"]["should add comment to package"] = function() 13 + comment["should add comment to package"] = function() 14 14 do_the_test("package", { 1, 1 }) 15 15 end 16 16 17 - T["comment"]["should add comment to struct"] = function() 17 + comment["should add comment to struct"] = function() 18 18 do_the_test("struct", { 4, 1 }) 19 19 end 20 20 21 - T["comment"]["should add comment to function"] = function() 21 + comment["should add comment to function"] = function() 22 22 do_the_test("func", { 3, 1 }) 23 23 end 24 24 25 - T["comment"]["should add comment to method"] = function() 25 + comment["should add comment to method"] = function() 26 26 do_the_test("method", { 5, 1 }) 27 27 end 28 28 29 - T["comment"]["should add comment to interface"] = function() 29 + comment["should add comment to interface"] = function() 30 30 do_the_test("interface", { 3, 6 }) 31 31 end 32 32 33 - T["comment"]["otherwise should add // above cursor"] = function() 33 + comment["otherwise should add // above cursor"] = function() 34 34 do_the_test("empty", { 1, 1 }) 35 35 end 36 36
+3 -3
spec/integration/gotests_test.lua
··· 1 1 local t = require "spec.testutils" 2 - local child, T = t.setup "gotests" 2 + local child, T, gotests = t.setup "gotests" 3 3 4 4 --- NOTE: :GoTestAdd is the only place that has actual logic 5 5 --- All other parts are handled `gotests` itself. ··· 10 10 return t.readfile(fpath:gsub(".go", "_test.go")) 11 11 end 12 12 13 - T["gotests"]["should add test for function under cursor"] = function() 13 + gotests["should add test for function under cursor"] = function() 14 14 local rs = t.setup_test("tests/function", child, { 3, 5 }) 15 15 child.cmd "GoTestAdd" 16 16 ··· 18 18 t.cleanup(rs) 19 19 end 20 20 21 - T["gotests"]["should add test for method under cursor"] = function() 21 + gotests["should add test for method under cursor"] = function() 22 22 local rs = t.setup_test("tests/method", child, { 5, 19 }) 23 23 child.cmd "GoTestAdd" 24 24
+3 -3
spec/integration/iferr_test.lua
··· 1 1 local t = require "spec.testutils" 2 - local child, T = t.setup "iferr" 2 + local child, T, iferr = t.setup "iferr" 3 3 4 - T["iferr"]["should add if != nil {"] = function() 4 + iferr["should add if != nil {"] = function() 5 5 local rs = t.setup_test("iferr/iferr", child, { 8, 2 }) 6 6 child.cmd "GoIfErr" 7 7 child.cmd "write" ··· 10 10 t.cleanup(rs) 11 11 end 12 12 13 - T["iferr"]["should add if err with custom message"] = function() 13 + iferr["should add if err with custom message"] = function() 14 14 child.lua [[ 15 15 require("gopher").setup { 16 16 iferr = { message = 'fmt.Errorf("failed to %w", err)' }
+4 -4
spec/integration/impl_test.lua
··· 1 1 local t = require "spec.testutils" 2 - local child, T = t.setup "impl" 2 + local child, T, impl = t.setup "impl" 3 3 4 - T["impl"]["should do impl with 'w io.Writer'"] = function() 4 + impl["should do impl with 'w io.Writer'"] = function() 5 5 local rs = t.setup_test("impl/writer", child, { 3, 0 }) 6 6 child.cmd "GoImpl w io.Writer" 7 7 child.cmd "write" ··· 12 12 t.cleanup(rs) 13 13 end 14 14 15 - T["impl"]["should work with full input, 'r Read io.Reader'"] = function() 15 + impl["should work with full input, 'r Read io.Reader'"] = function() 16 16 local rs = t.setup_test("impl/reader", child) 17 17 child.cmd "GoImpl r Read io.Reader" 18 18 child.cmd "write" ··· 22 22 t.cleanup(rs) 23 23 end 24 24 25 - T["impl"]["should work with minimal input 'io.Closer'"] = function() 25 + impl["should work with minimal input 'io.Closer'"] = function() 26 26 local rs = t.setup_test("impl/closer", child, { 3, 6 }) 27 27 child.cmd "GoImpl io.Closer" 28 28 child.cmd "write"
+8 -8
spec/integration/struct_tags_test.lua
··· 1 1 local t = require "spec.testutils" 2 - local child, T = t.setup "struct_tags" 2 + local child, T, struct_tags = t.setup "struct_tags" 3 3 4 - T["struct_tags"]["should add tag"] = function() 4 + struct_tags["should add tag"] = function() 5 5 local rs = t.setup_test("tags/add", child, { 3, 6 }) 6 6 child.cmd "GoTagAdd json" 7 7 child.cmd "write" ··· 10 10 t.cleanup(rs) 11 11 end 12 12 13 - T["struct_tags"]["should remove tag"] = function() 13 + struct_tags["should remove tag"] = function() 14 14 local rs = t.setup_test("tags/remove", child, { 4, 6 }) 15 15 child.cmd "GoTagRm json" 16 16 child.cmd "write" ··· 19 19 t.cleanup(rs) 20 20 end 21 21 22 - T["struct_tags"]["should be able to handle many structs"] = function() 22 + struct_tags["should be able to handle many structs"] = function() 23 23 local rs = t.setup_test("tags/many", child, { 10, 3 }) 24 24 child.cmd "GoTagAdd testing" 25 25 child.cmd "write" ··· 28 28 t.cleanup(rs) 29 29 end 30 30 31 - T["struct_tags"]["should clear struct"] = function() 31 + struct_tags["should clear struct"] = function() 32 32 local rs = t.setup_test("tags/clear", child, { 3, 1 }) 33 33 child.cmd "GoTagClear" 34 34 child.cmd "write" ··· 37 37 t.cleanup(rs) 38 38 end 39 39 40 - T["struct_tags"]["should add more than one tag"] = function() 40 + struct_tags["should add more than one tag"] = function() 41 41 local tmp = t.tmpfile() 42 42 local fixtures = t.get_fixtures "tags/add_many" 43 43 t.writefile(tmp, fixtures.input) ··· 60 60 t.cleanup { tmp = tmp } 61 61 end 62 62 63 - T["struct_tags"]["should add tags on var"] = function() 63 + struct_tags["should add tags on var"] = function() 64 64 local rs = t.setup_test("tags/var", child, { 5, 6 }) 65 65 child.cmd "GoTagAdd yaml" 66 66 child.cmd "write" ··· 69 69 t.cleanup(rs) 70 70 end 71 71 72 - T["struct_tags"]["should add tags on short declr var"] = function() 72 + struct_tags["should add tags on short declr var"] = function() 73 73 local rs = t.setup_test("tags/svar", child, { 4, 3 }) 74 74 child.cmd "GoTagAdd xml" 75 75 child.cmd "write"
+7 -5
spec/testutils.lua
··· 6 6 testutils.mininit_path = vim.fs.joinpath(base_dir, "scripts", "minimal_init.lua") 7 7 testutils.fixtures_dir = vim.fs.joinpath(base_dir, "spec/fixtures") 8 8 9 - ---@param name string 10 - ---@return MiniTest.child, table 11 - function testutils.setup(name) 9 + ---@param mod string Module name for which to create a nested test set. 10 + ---@return MiniTest.child child nvim client. 11 + ---@return table T root test set created by `MiniTest.new_set()`. 12 + ---@return table mod_name nested set of tests in `T[mod]`. 13 + function testutils.setup(mod) 12 14 local child = MiniTest.new_child_neovim() 13 15 local T = MiniTest.new_set { 14 16 hooks = { ··· 19 21 }, 20 22 } 21 23 22 - T[name] = MiniTest.new_set {} 23 - return child, T 24 + T[mod] = MiniTest.new_set {} 25 + return child, T, T[mod] 24 26 end 25 27 26 28 ---@generic T
+4 -4
spec/unit/config_test.lua
··· 1 1 local t = require "spec.testutils" 2 - local _, T = t.setup "config" 2 + local _, T, config = t.setup "config" 3 3 4 - T["config"]["can be called without any arguments passed"] = function() 4 + config["can be called without any arguments passed"] = function() 5 5 ---@diagnostic disable-next-line: missing-parameter 6 6 require("gopher").setup() 7 7 end 8 8 9 - T["config"]["can be called with empty table"] = function() 9 + config["can be called with empty table"] = function() 10 10 require("gopher").setup {} 11 11 end 12 12 13 - T["config"]["should change option"] = function() 13 + config["should change option"] = function() 14 14 local log_level = 1234567890 15 15 require("gopher").setup { 16 16 log_level = log_level,
+4 -4
spec/unit/utils_test.lua
··· 1 1 local t = require "spec.testutils" 2 - local _, T = t.setup "utils" 2 + local _, T, utils = t.setup "utils" 3 3 4 - T["utils"]["should .remove_empty_lines()"] = function() 4 + utils["should .remove_empty_lines()"] = function() 5 5 local u = require "gopher._utils" 6 6 local inp = { "hi", "", "a", "", "", "asdf" } 7 7 8 8 t.eq(u.remove_empty_lines(inp), { "hi", "a", "asdf" }) 9 9 end 10 10 11 - T["utils"]["should .readfile_joined()"] = function() 11 + utils["should .readfile_joined()"] = function() 12 12 local data = "line1\nline2\nline3" 13 13 local tmp = t.tmpfile() 14 14 local u = require "gopher._utils" ··· 17 17 t.eq(u.readfile_joined(tmp), data) 18 18 end 19 19 20 - T["utils"]["should .trimend()"] = function() 20 + utils["should .trimend()"] = function() 21 21 local u = require "gopher._utils" 22 22 t.eq(u.trimend " hi ", " hi") 23 23 end