refactor(tests): rewrite using new thing

This commit is contained in:
Oleksandr Smirnov 2025-03-22 20:04:30 +02:00
parent c4c302ed48
commit 1cb507fa40
No known key found for this signature in database
5 changed files with 36 additions and 101 deletions

View file

@ -11,19 +11,14 @@ local T = MiniTest.new_set {
}
local function do_the_test(fixture, pos)
local tmp = t.tmpfile()
local fixtures = t.get_fixtures("comment/" .. fixture)
t.writefile(tmp, fixtures.input)
child.cmd("silent edit " .. tmp)
child.fn.setpos(".", { child.fn.bufnr "%", unpack(pos) })
local rs = t.setup("comment/" .. fixture, child, pos)
child.cmd "GoCmt"
child.cmd "write"
t.eq(t.readfile(tmp), fixtures.output)
t.eq(t.readfile(rs.tmp), rs.fixtures.output)
-- without it all other(not even from this module) tests are falling
t.deletefile(tmp)
t.deletefile(rs.tmp)
end
T["comment"] = MiniTest.new_set {}

View file

@ -21,27 +21,17 @@ local function read_testfile(fpath)
end
T["gotests"]["should add test for function under cursor"] = function()
local tmp = t.tmpfile()
local fixtures = t.get_fixtures "tests/function"
t.writefile(tmp, fixtures.input)
child.cmd("silent edit " .. tmp)
child.fn.setpos(".", { child.fn.bufnr "%", 3, 6 })
local rs = t.setup("tests/function", child, { 3, 5 })
child.cmd "GoTestAdd"
t.eq(fixtures.output, read_testfile(tmp))
t.eq(rs.fixtures.output, read_testfile(rs.tmp))
end
T["gotests"]["should add test for method under cursor"] = function()
local tmp = t.tmpfile()
local fixtures = t.get_fixtures "tests/method"
t.writefile(tmp, fixtures.input)
child.cmd("silent edit " .. tmp)
child.fn.setpos(".", { child.fn.bufnr "%", 5, 19 })
local rs = t.setup("tests/method", child, { 5, 19 })
child.cmd "GoTestAdd"
t.eq(fixtures.output, read_testfile(tmp))
t.eq(rs.fixtures.output, read_testfile(rs.tmp))
end
return T

View file

@ -11,30 +11,24 @@ local T = MiniTest.new_set {
}
T["iferr"] = MiniTest.new_set {}
T["iferr"]["works"] = function()
local tmp = t.tmpfile()
local fixtures = t.get_fixtures "iferr/iferr"
t.writefile(tmp, fixtures.input)
child.cmd("silent edit " .. tmp)
child.fn.setpos(".", { child.fn.bufnr "%", 8, 2, 0 })
local rs = t.setup("iferr/iferr", child, { 8, 2 })
child.cmd "GoIfErr"
child.cmd "write"
t.eq(t.readfile(tmp), fixtures.output)
t.eq(t.readfile(rs.tmp), rs.fixtures.output)
end
T["iferr"]["works with custom message"] = function()
local tmp = t.tmpfile()
local fixtures = t.get_fixtures "iferr/message"
t.writefile(tmp, fixtures.input)
child.lua [[
require("gopher").setup {
iferr = { message = 'fmt.Errorf("failed to %w", err)' }
} ]]
child.lua [[ require("gopher").setup { iferr = { message = 'fmt.Errorf("failed to %w", err)' } } ]]
child.cmd("silent edit " .. tmp)
child.fn.setpos(".", { child.fn.bufnr "%", 6, 2, 0 })
local rs = t.setup("iferr/message", child, { 6, 2 })
child.cmd "GoIfErr"
child.cmd "write"
t.eq(t.readfile(tmp), fixtures.output)
t.eq(t.readfile(rs.tmp), rs.fixtures.output)
end
return T

View file

@ -11,45 +11,31 @@ local T = MiniTest.new_set {
}
T["impl"] = MiniTest.new_set {}
T["impl"]["works w io.Writer"] = function()
local tmp = t.tmpfile()
local fixtures = t.get_fixtures "impl/writer"
t.writefile(tmp, fixtures.input)
child.cmd("silent edit " .. tmp)
child.fn.setpos(".", { child.fn.bufnr(tmp), 3, 0 })
local rs = t.setup("impl/writer", child, { 3, 0 })
child.cmd "GoImpl w io.Writer"
child.cmd "write"
-- NOTE: since "impl" won't implement interface if it's already implemented i went with this hack
local rhs = fixtures.output:gsub("Test2", "Test")
t.eq(t.readfile(tmp), rhs)
local rhs = rs.fixtures.output:gsub("Test2", "Test")
t.eq(t.readfile(rs.tmp), rhs)
end
T["impl"]["works r Read io.Reader"] = function()
local tmp = t.tmpfile()
local fixtures = t.get_fixtures "impl/reader"
t.writefile(tmp, fixtures.input)
child.cmd("silent edit " .. tmp)
local rs = t.setup("impl/reader", child)
child.cmd "GoImpl r Read io.Reader"
child.cmd "write"
local rhs = fixtures.output:gsub("Read2", "Read")
t.eq(t.readfile(tmp), rhs)
local rhs = rs.fixtures.output:gsub("Read2", "Read")
t.eq(t.readfile(rs.tmp), rhs)
end
T["impl"]["works io.Closer"] = function()
local tmp = t.tmpfile()
local fixtures = t.get_fixtures "impl/closer"
t.writefile(tmp, fixtures.input)
child.cmd("silent edit " .. tmp)
child.fn.setpos(".", { child.fn.bufnr(tmp), 3, 6 })
local rs = t.setup("impl/closer", child, { 3, 6})
child.cmd "GoImpl io.Closer"
child.cmd "write"
local rhs = fixtures.output:gsub("Test2", "Test")
t.eq(t.readfile(tmp), rhs)
local rhs = rs.fixtures.output:gsub("Test2", "Test")
t.eq(t.readfile(rs.tmp), rhs)
end
return T

View file

@ -11,55 +11,35 @@ local T = MiniTest.new_set {
}
T["struct_tags"] = MiniTest.new_set {}
T["struct_tags"]["should add tag"] = function()
local tmp = t.tmpfile()
local fixtures = t.get_fixtures "tags/add"
t.writefile(tmp, fixtures.input)
child.cmd("silent edit " .. tmp)
child.fn.setpos(".", { child.fn.bufnr(tmp), 3, 6, 0 })
local rs = t.setup("tags/add", child, { 3, 6 })
child.cmd "GoTagAdd json"
child.cmd "write"
t.eq(t.readfile(tmp), fixtures.output)
t.eq(t.readfile(rs.tmp), rs.fixtures.output)
end
T["struct_tags"]["should remove tag"] = function()
local tmp = t.tmpfile()
local fixtures = t.get_fixtures "tags/remove"
t.writefile(tmp, fixtures.input)
child.cmd("silent edit " .. tmp)
child.fn.setpos(".", { child.fn.bufnr(tmp), 4, 6, 0 })
local rs = t.setup("tags/remove", child, { 4, 6 })
child.cmd "GoTagRm json"
child.cmd "write"
t.eq(t.readfile(tmp), fixtures.output)
t.eq(t.readfile(rs.tmp), rs.fixtures.output)
end
T["struct_tags"]["should be able to handle many structs"] = function()
local tmp = t.tmpfile()
local fixtures = t.get_fixtures "tags/many"
t.writefile(tmp, fixtures.input)
child.cmd("silent edit " .. tmp)
child.fn.setpos(".", { child.fn.bufnr(tmp), 10, 3, 0 })
local rs = t.setup("tags/many", child, { 10, 3 })
child.cmd "GoTagAdd testing"
child.cmd "write"
t.eq(t.readfile(tmp), fixtures.output)
t.eq(t.readfile(rs.tmp), rs.fixtures.output)
end
T["struct_tags"]["should clear struct"] = function()
local tmp = t.tmpfile()
local fixtures = t.get_fixtures "tags/clear"
t.writefile(tmp, fixtures.input)
child.cmd("silent edit " .. tmp)
child.fn.setpos(".", { child.fn.bufnr(tmp), 3, 1, 0 })
local rs = t.setup("tags/clear", child, { 3, 1 })
child.cmd "GoTagClear"
child.cmd "write"
t.eq(t.readfile(tmp), fixtures.output)
t.eq(t.readfile(rs.tmp), rs.fixtures.output)
end
T["struct_tags"]["should add more than one tag"] = function()
@ -83,29 +63,19 @@ T["struct_tags"]["should add more than one tag"] = function()
end
T["struct_tags"]["should add tags on var"] = function()
local tmp = t.tmpfile()
local fixtures = t.get_fixtures "tags/var"
t.writefile(tmp, fixtures.input)
child.cmd("silent edit " .. tmp)
child.fn.setpos(".", { child.fn.bufnr(tmp), 5, 3 })
local rs = t.setup("tags/var", child, { 5, 6 })
child.cmd "GoTagAdd yaml"
child.cmd "write"
t.eq(t.readfile(tmp), fixtures.output)
t.eq(t.readfile(rs.tmp), rs.fixtures.output)
end
T["struct_tags"]["should add tags on short declr var"] = function()
local tmp = t.tmpfile()
local fixtures = t.get_fixtures "tags/svar"
t.writefile(tmp, fixtures.input)
child.cmd("silent edit " .. tmp)
child.fn.setpos(".", { child.fn.bufnr(tmp), 4, 3 })
local rs = t.setup("tags/svar", child, { 4, 3 })
child.cmd "GoTagAdd xml"
child.cmd "write"
t.eq(t.readfile(tmp), fixtures.output)
t.eq(t.readfile(rs.tmp), rs.fixtures.output)
end
return T