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

refactor: some refactoring of tests (#102)

* feat(tests): add utils that does most of tests boilerplate

* refactor(tests): rewrite using new thing

* refactor(tests): clean up everywhere

* refactor(tests): remove boilerplate even further

* refactor(tests): soon it will be too far

* refactor(tests): some test renaming

authored by olexsmir.xyz and committed by olexsmir.xyz 3dcf708d 7ebe190c

verified
+4 -21
spec/integration/comment_test.lua
··· 1 1 local t = require "spec.testutils" 2 - 3 - local child = MiniTest.new_child_neovim() 4 - local T = MiniTest.new_set { 5 - hooks = { 6 - post_once = child.stop, 7 - pre_case = function() 8 - child.restart { "-u", t.mininit_path } 9 - end, 10 - }, 11 - } 2 + local child, T = t.setup "comment" 12 3 13 4 local function do_the_test(fixture, pos) 14 - local tmp = t.tmpfile() 15 - local fixtures = t.get_fixtures("comment/" .. fixture) 16 - t.writefile(tmp, fixtures.input) 17 - 18 - child.cmd("silent edit " .. tmp) 19 - child.fn.setpos(".", { child.fn.bufnr "%", unpack(pos) }) 5 + local rs = t.setup_test("comment/" .. fixture, child, pos) 20 6 child.cmd "GoCmt" 21 7 child.cmd "write" 22 8 23 - t.eq(t.readfile(tmp), fixtures.output) 24 - 25 - -- without it all other(not even from this module) tests are falling 26 - t.deletefile(tmp) 9 + t.eq(t.readfile(rs.tmp), rs.fixtures.output) 10 + t.cleanup(rs) 27 11 end 28 12 29 - T["comment"] = MiniTest.new_set {} 30 13 T["comment"]["should add comment to package"] = function() 31 14 do_the_test("package", { 1, 1 }) 32 15 end
+7 -25
spec/integration/gotests_test.lua
··· 1 1 local t = require "spec.testutils" 2 - 3 - local child = MiniTest.new_child_neovim() 4 - local T = MiniTest.new_set { 5 - hooks = { 6 - post_once = child.stop, 7 - pre_case = function() 8 - child.restart { "-u", t.mininit_path } 9 - end, 10 - }, 11 - } 12 - T["gotests"] = MiniTest.new_set {} 2 + local child, T = t.setup "gotests" 13 3 14 4 --- NOTE: :GoTestAdd is the only place that has actual logic 15 5 --- All other parts are handled `gotests` itself. ··· 21 11 end 22 12 23 13 T["gotests"]["should add test for function under cursor"] = function() 24 - local tmp = t.tmpfile() 25 - local fixtures = t.get_fixtures "tests/function" 26 - t.writefile(tmp, fixtures.input) 27 - 28 - child.cmd("silent edit " .. tmp) 29 - child.fn.setpos(".", { child.fn.bufnr "%", 3, 6 }) 14 + local rs = t.setup_test("tests/function", child, { 3, 5 }) 30 15 child.cmd "GoTestAdd" 31 16 32 - t.eq(fixtures.output, read_testfile(tmp)) 17 + t.eq(rs.fixtures.output, read_testfile(rs.tmp)) 18 + t.cleanup(rs) 33 19 end 34 20 35 21 T["gotests"]["should add test for method under cursor"] = function() 36 - local tmp = t.tmpfile() 37 - local fixtures = t.get_fixtures "tests/method" 38 - t.writefile(tmp, fixtures.input) 39 - 40 - child.cmd("silent edit " .. tmp) 41 - child.fn.setpos(".", { child.fn.bufnr "%", 5, 19 }) 22 + local rs = t.setup_test("tests/method", child, { 5, 19 }) 42 23 child.cmd "GoTestAdd" 43 24 44 - t.eq(fixtures.output, read_testfile(tmp)) 25 + t.eq(rs.fixtures.output, read_testfile(rs.tmp)) 26 + t.cleanup(rs) 45 27 end 46 28 47 29 return T
+13 -26
spec/integration/iferr_test.lua
··· 1 1 local t = require "spec.testutils" 2 - 3 - local child = MiniTest.new_child_neovim() 4 - local T = MiniTest.new_set { 5 - hooks = { 6 - post_once = child.stop, 7 - pre_case = function() 8 - child.restart { "-u", t.mininit_path } 9 - end, 10 - }, 11 - } 12 - T["iferr"] = MiniTest.new_set {} 13 - T["iferr"]["works"] = function() 14 - local tmp = t.tmpfile() 15 - local fixtures = t.get_fixtures "iferr/iferr" 16 - t.writefile(tmp, fixtures.input) 2 + local child, T = t.setup "iferr" 17 3 18 - child.cmd("silent edit " .. tmp) 19 - child.fn.setpos(".", { child.fn.bufnr "%", 8, 2, 0 }) 4 + T["iferr"]["should add if != nil {"] = function() 5 + local rs = t.setup_test("iferr/iferr", child, { 8, 2 }) 20 6 child.cmd "GoIfErr" 21 7 child.cmd "write" 22 8 23 - t.eq(t.readfile(tmp), fixtures.output) 9 + t.eq(t.readfile(rs.tmp), rs.fixtures.output) 10 + t.cleanup(rs) 24 11 end 25 12 26 - T["iferr"]["works with custom message"] = function() 27 - local tmp = t.tmpfile() 28 - local fixtures = t.get_fixtures "iferr/message" 29 - t.writefile(tmp, fixtures.input) 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)' } 17 + } ]] 30 18 31 - child.lua [[ require("gopher").setup { iferr = { message = 'fmt.Errorf("failed to %w", err)' } } ]] 32 - child.cmd("silent edit " .. tmp) 33 - child.fn.setpos(".", { child.fn.bufnr "%", 6, 2, 0 }) 19 + local rs = t.setup_test("iferr/message", child, { 6, 2 }) 34 20 child.cmd "GoIfErr" 35 21 child.cmd "write" 36 22 37 - t.eq(t.readfile(tmp), fixtures.output) 23 + t.eq(t.readfile(rs.tmp), rs.fixtures.output) 24 + t.cleanup(rs) 38 25 end 39 26 40 27 return T
+16 -36
spec/integration/impl_test.lua
··· 1 1 local t = require "spec.testutils" 2 - 3 - local child = MiniTest.new_child_neovim() 4 - local T = MiniTest.new_set { 5 - hooks = { 6 - post_once = child.stop, 7 - pre_case = function() 8 - child.restart { "-u", t.mininit_path } 9 - end, 10 - }, 11 - } 12 - T["impl"] = MiniTest.new_set {} 13 - T["impl"]["works w io.Writer"] = function() 14 - local tmp = t.tmpfile() 15 - local fixtures = t.get_fixtures "impl/writer" 16 - t.writefile(tmp, fixtures.input) 2 + local child, T = t.setup "impl" 17 3 18 - child.cmd("silent edit " .. tmp) 19 - child.fn.setpos(".", { child.fn.bufnr(tmp), 3, 0 }) 4 + T["impl"]["should do impl with 'w io.Writer'"] = function() 5 + local rs = t.setup_test("impl/writer", child, { 3, 0 }) 20 6 child.cmd "GoImpl w io.Writer" 21 7 child.cmd "write" 22 8 23 9 -- NOTE: since "impl" won't implement interface if it's already implemented i went with this hack 24 - local rhs = fixtures.output:gsub("Test2", "Test") 25 - t.eq(t.readfile(tmp), rhs) 10 + local rhs = rs.fixtures.output:gsub("Test2", "Test") 11 + t.eq(t.readfile(rs.tmp), rhs) 12 + t.cleanup(rs) 26 13 end 27 14 28 - T["impl"]["works r Read io.Reader"] = function() 29 - local tmp = t.tmpfile() 30 - local fixtures = t.get_fixtures "impl/reader" 31 - t.writefile(tmp, fixtures.input) 32 - 33 - child.cmd("silent edit " .. tmp) 15 + T["impl"]["should work with full input, 'r Read io.Reader'"] = function() 16 + local rs = t.setup_test("impl/reader", child) 34 17 child.cmd "GoImpl r Read io.Reader" 35 18 child.cmd "write" 36 19 37 - local rhs = fixtures.output:gsub("Read2", "Read") 38 - t.eq(t.readfile(tmp), rhs) 20 + local rhs = rs.fixtures.output:gsub("Read2", "Read") 21 + t.eq(t.readfile(rs.tmp), rhs) 22 + t.cleanup(rs) 39 23 end 40 24 41 - T["impl"]["works io.Closer"] = function() 42 - local tmp = t.tmpfile() 43 - local fixtures = t.get_fixtures "impl/closer" 44 - t.writefile(tmp, fixtures.input) 45 - 46 - child.cmd("silent edit " .. tmp) 47 - child.fn.setpos(".", { child.fn.bufnr(tmp), 3, 6 }) 25 + T["impl"]["should work with minimal input 'io.Closer'"] = function() 26 + local rs = t.setup_test("impl/closer", child, { 3, 6 }) 48 27 child.cmd "GoImpl io.Closer" 49 28 child.cmd "write" 50 29 51 - local rhs = fixtures.output:gsub("Test2", "Test") 52 - t.eq(t.readfile(tmp), rhs) 30 + local rhs = rs.fixtures.output:gsub("Test2", "Test") 31 + t.eq(t.readfile(rs.tmp), rhs) 32 + t.cleanup(rs) 53 33 end 54 34 55 35 return T
+22 -52
spec/integration/struct_tags_test.lua
··· 1 1 local t = require "spec.testutils" 2 + local child, T = t.setup "struct_tags" 2 3 3 - local child = MiniTest.new_child_neovim() 4 - local T = MiniTest.new_set { 5 - hooks = { 6 - post_once = child.stop, 7 - pre_case = function() 8 - child.restart { "-u", t.mininit_path } 9 - end, 10 - }, 11 - } 12 - T["struct_tags"] = MiniTest.new_set {} 13 4 T["struct_tags"]["should add tag"] = function() 14 - local tmp = t.tmpfile() 15 - local fixtures = t.get_fixtures "tags/add" 16 - t.writefile(tmp, fixtures.input) 17 - 18 - child.cmd("silent edit " .. tmp) 19 - child.fn.setpos(".", { child.fn.bufnr(tmp), 3, 6, 0 }) 5 + local rs = t.setup_test("tags/add", child, { 3, 6 }) 20 6 child.cmd "GoTagAdd json" 21 7 child.cmd "write" 22 8 23 - t.eq(t.readfile(tmp), fixtures.output) 9 + t.eq(t.readfile(rs.tmp), rs.fixtures.output) 10 + t.cleanup(rs) 24 11 end 25 12 26 13 T["struct_tags"]["should remove tag"] = function() 27 - local tmp = t.tmpfile() 28 - local fixtures = t.get_fixtures "tags/remove" 29 - t.writefile(tmp, fixtures.input) 30 - 31 - child.cmd("silent edit " .. tmp) 32 - child.fn.setpos(".", { child.fn.bufnr(tmp), 4, 6, 0 }) 14 + local rs = t.setup_test("tags/remove", child, { 4, 6 }) 33 15 child.cmd "GoTagRm json" 34 16 child.cmd "write" 35 17 36 - t.eq(t.readfile(tmp), fixtures.output) 18 + t.eq(t.readfile(rs.tmp), rs.fixtures.output) 19 + t.cleanup(rs) 37 20 end 38 21 39 22 T["struct_tags"]["should be able to handle many structs"] = function() 40 - local tmp = t.tmpfile() 41 - local fixtures = t.get_fixtures "tags/many" 42 - t.writefile(tmp, fixtures.input) 43 - 44 - child.cmd("silent edit " .. tmp) 45 - child.fn.setpos(".", { child.fn.bufnr(tmp), 10, 3, 0 }) 23 + local rs = t.setup_test("tags/many", child, { 10, 3 }) 46 24 child.cmd "GoTagAdd testing" 47 25 child.cmd "write" 48 26 49 - t.eq(t.readfile(tmp), fixtures.output) 27 + t.eq(t.readfile(rs.tmp), rs.fixtures.output) 28 + t.cleanup(rs) 50 29 end 51 30 52 31 T["struct_tags"]["should clear struct"] = function() 53 - local tmp = t.tmpfile() 54 - local fixtures = t.get_fixtures "tags/clear" 55 - t.writefile(tmp, fixtures.input) 56 - 57 - child.cmd("silent edit " .. tmp) 58 - child.fn.setpos(".", { child.fn.bufnr(tmp), 3, 1, 0 }) 32 + local rs = t.setup_test("tags/clear", child, { 3, 1 }) 59 33 child.cmd "GoTagClear" 60 34 child.cmd "write" 61 35 62 - t.eq(t.readfile(tmp), fixtures.output) 36 + t.eq(t.readfile(rs.tmp), rs.fixtures.output) 37 + t.cleanup(rs) 63 38 end 64 39 65 40 T["struct_tags"]["should add more than one tag"] = function() ··· 80 55 child.cmd "write" 81 56 82 57 t.eq(t.readfile(tmp), fixtures.output) 58 + 59 + ---@diagnostic disable-next-line:missing-fields 60 + t.cleanup { tmp = tmp } 83 61 end 84 62 85 63 T["struct_tags"]["should add tags on var"] = function() 86 - local tmp = t.tmpfile() 87 - local fixtures = t.get_fixtures "tags/var" 88 - t.writefile(tmp, fixtures.input) 89 - 90 - child.cmd("silent edit " .. tmp) 91 - child.fn.setpos(".", { child.fn.bufnr(tmp), 5, 3 }) 64 + local rs = t.setup_test("tags/var", child, { 5, 6 }) 92 65 child.cmd "GoTagAdd yaml" 93 66 child.cmd "write" 94 67 95 - t.eq(t.readfile(tmp), fixtures.output) 68 + t.eq(t.readfile(rs.tmp), rs.fixtures.output) 69 + t.cleanup(rs) 96 70 end 97 71 98 72 T["struct_tags"]["should add tags on short declr var"] = function() 99 - local tmp = t.tmpfile() 100 - local fixtures = t.get_fixtures "tags/svar" 101 - t.writefile(tmp, fixtures.input) 102 - 103 - child.cmd("silent edit " .. tmp) 104 - child.fn.setpos(".", { child.fn.bufnr(tmp), 4, 3 }) 73 + local rs = t.setup_test("tags/svar", child, { 4, 3 }) 105 74 child.cmd "GoTagAdd xml" 106 75 child.cmd "write" 107 76 108 - t.eq(t.readfile(tmp), fixtures.output) 77 + t.eq(t.readfile(rs.tmp), rs.fixtures.output) 78 + t.cleanup(rs) 109 79 end 110 80 111 81 return T
+52 -1
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) 12 + local child = MiniTest.new_child_neovim() 13 + local T = MiniTest.new_set { 14 + hooks = { 15 + post_once = child.stop, 16 + pre_case = function() 17 + child.restart { "-u", testutils.mininit_path } 18 + end, 19 + }, 20 + } 21 + 22 + T[name] = MiniTest.new_set {} 23 + return child, T 24 + end 25 + 9 26 ---@generic T 10 27 ---@param a T 11 28 ---@param b T ··· 36 53 vim.fn.delete(fpath) 37 54 end 38 55 56 + ---@class gopher.TestUtilsFixtures 57 + ---@field input string 58 + ---@field output string 59 + 39 60 ---@param fixture string 40 - ---@return {input: string, output: string} 61 + ---@return gopher.TestUtilsFixtures 41 62 function testutils.get_fixtures(fixture) 42 63 return { 43 64 input = testutils.readfile(vim.fs.joinpath(testutils.fixtures_dir, fixture) .. "_input.go"), 44 65 output = testutils.readfile(vim.fs.joinpath(testutils.fixtures_dir, fixture) .. "_output.go"), 45 66 } 67 + end 68 + 69 + ---@class gopher.TestUtilsSetup 70 + ---@field tmp string 71 + ---@field fixtures gopher.TestUtilsFixtures 72 + 73 + ---@param fixture string 74 + ---@param child MiniTest.child 75 + ---@param pos? number[] 76 + ---@return gopher.TestUtilsSetup 77 + function testutils.setup_test(fixture, child, pos) 78 + local tmp = testutils.tmpfile() 79 + local fixtures = testutils.get_fixtures(fixture) 80 + 81 + testutils.writefile(tmp, fixtures.input) 82 + child.cmd("silent edit " .. tmp) 83 + 84 + if pos then 85 + child.fn.setpos(".", { child.fn.bufnr(tmp), unpack(pos) }) 86 + end 87 + 88 + return { 89 + tmp = tmp, 90 + fixtures = fixtures, 91 + } 92 + end 93 + 94 + ---@param inp gopher.TestUtilsSetup 95 + function testutils.cleanup(inp) 96 + testutils.deletefile(inp.tmp) 46 97 end 47 98 48 99 return testutils