+4
-21
spec/integration/comment_test.lua
+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
+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
+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
+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
+52
-1
spec/testutils.lua
+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