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