go.nvim/lua/tests/go_fmt_spec.lua

165 lines
5.5 KiB
Lua
Raw Normal View History

2021-03-10 12:15:06 +00:00
local eq = assert.are.same
local cur_dir = vim.fn.expand("%:p:h")
2021-08-24 04:53:37 +00:00
local busted = require("plenary/busted")
2021-03-10 12:15:06 +00:00
2021-08-24 04:53:37 +00:00
describe("should run gofmt", function()
-- vim.fn.readfile('minimal.vim')
-- vim.fn.writefile(vim.fn.readfile('fixtures/fmt/hello.go'), name)
2022-07-26 16:21:58 +00:00
require("plenary.reload").reload_module("go.nvim")
2021-09-02 16:35:54 +00:00
it("should run fmt", function()
local name = vim.fn.tempname() .. ".go"
print("tmp:" .. name)
--
local path = cur_dir .. "/lua/tests/fixtures/fmt/hello.go" -- %:p:h ? %:p
print("test:" .. path)
local lines = vim.fn.readfile(path)
vim.fn.writefile(lines, name)
2021-12-02 06:44:22 +00:00
local expected = vim.fn.join(vim.fn.readfile(cur_dir .. "/lua/tests/fixtures/fmt/hello_golden.go"), "\n")
2021-09-02 16:35:54 +00:00
local cmd = " silent exe 'e " .. name .. "'"
vim.cmd(cmd)
local l = vim.api.nvim_buf_get_lines(0, 0, -1, true)
print("buf read: " .. vim.inspect(l))
vim.bo.filetype = "go"
print("exp:" .. vim.inspect(expected))
print("tmp" .. name)
local gofmt = require("go.format")
gofmt.gofmt()
-- enable the channel response
2021-12-07 01:01:22 +00:00
vim.wait(400, function() end)
2021-09-02 16:35:54 +00:00
local fmt = vim.fn.join(vim.fn.readfile(name), "\n")
print("fmt" .. fmt)
vim.fn.assert_equal(fmt, expected)
eq(expected, fmt)
2022-07-26 16:21:58 +00:00
cmd = "bd! " .. name
2021-09-02 16:35:54 +00:00
vim.cmd(cmd)
end)
it("should run fmt sending from buffer", function()
local name = vim.fn.tempname() .. ".go"
print("tmp:" .. name)
--
local path = cur_dir .. "/lua/tests/fixtures/fmt/hello.go" -- %:p:h ? %:p
print("test:" .. path)
local lines = vim.fn.readfile(path)
vim.fn.writefile(lines, name)
2021-12-02 06:44:22 +00:00
local expected = vim.fn.join(vim.fn.readfile(cur_dir .. "/lua/tests/fixtures/fmt/hello_golden.go"), "\n")
2021-09-02 16:35:54 +00:00
local cmd = " silent exe 'e " .. name .. "'"
vim.cmd(cmd)
local l = vim.api.nvim_buf_get_lines(0, 0, -1, true)
print("buf read: " .. vim.inspect(l))
vim.bo.filetype = "go"
print("exp:" .. vim.inspect(expected))
print("tmp" .. name)
local gofmt = require("go.format")
gofmt.gofmt()
2021-09-02 16:35:54 +00:00
-- enable the channel response
2021-12-07 01:01:22 +00:00
vim.wait(400, function() end)
2021-09-02 16:35:54 +00:00
local fmt = vim.fn.join(vim.fn.readfile(name), "\n")
print("fmt" .. fmt)
vim.fn.assert_equal(fmt, expected)
eq(expected, fmt)
2022-07-26 16:21:58 +00:00
cmd = "bd! " .. name
2021-09-02 16:35:54 +00:00
vim.cmd(cmd)
end)
it("should run import from file", function()
local path = cur_dir .. "/lua/tests/fixtures/fmt/goimports.go" -- %:p:h ? %:p
2021-12-02 06:44:22 +00:00
local expected = vim.fn.join(vim.fn.readfile(cur_dir .. "/lua/tests/fixtures/fmt/goimports_golden.go"), "\n")
2021-09-02 16:35:54 +00:00
local name = vim.fn.tempname() .. ".go"
print(name)
local lines = vim.fn.readfile(path)
vim.fn.writefile(lines, name)
local cmd = " silent exe 'e " .. name .. "'"
vim.cmd(cmd)
2021-12-07 01:01:22 +00:00
require("go").setup({ goimport = "goimports" })
2021-09-02 16:35:54 +00:00
vim.cmd([[cd %:p:h]])
require("go.format").goimport()
print("workspaces:", vim.inspect(vim.lsp.buf.list_workspace_folders()))
2021-12-07 01:01:22 +00:00
vim.wait(400, function() end)
2021-09-02 16:35:54 +00:00
local fmt = vim.fn.join(vim.fn.readfile(name), "\n")
eq(expected, fmt)
cmd = "bd! " .. name
vim.cmd(cmd)
end)
it("should run import from file with goimport with package name", function()
2021-09-02 16:35:54 +00:00
local path = cur_dir .. "/lua/tests/fixtures/fmt/goimports.go" -- %:p:h ? %:p
2021-12-02 06:44:22 +00:00
local expected = vim.fn.join(vim.fn.readfile(cur_dir .. "/lua/tests/fixtures/fmt/goimports_golden.go"), "\n")
2021-09-02 16:35:54 +00:00
local name = vim.fn.tempname() .. ".go"
print(name)
local lines = vim.fn.readfile(path)
local cmd = " silent exe 'e " .. name .. "'"
vim.fn.writefile(lines, name)
vim.cmd(cmd)
vim.cmd([[cd %:p:h]])
print("code write to " .. name)
require("go").setup({ goimport = "goimports", gofmt = "gofmt" })
2021-09-02 16:35:54 +00:00
local gofmt = require("go.format")
gofmt.goimport("fmt")
2021-09-02 16:35:54 +00:00
2021-12-07 01:01:22 +00:00
vim.wait(400, function() end)
2021-09-02 16:35:54 +00:00
vim.cmd([[w]])
local fmt = vim.fn.join(vim.fn.readfile(name), "\n")
print(fmt)
eq(expected, fmt)
end)
2021-09-02 15:11:09 +00:00
2021-08-24 07:34:33 +00:00
it("should run import from file with gopls", function()
2021-08-24 11:05:14 +00:00
local path = cur_dir .. "/lua/tests/fixtures/fmt/goimports2.go" -- %:p:h ? %:p
2021-12-02 06:44:22 +00:00
local expected = vim.fn.join(vim.fn.readfile(cur_dir .. "/lua/tests/fixtures/fmt/goimports2_golden.go"), "\n")
2021-08-24 07:34:33 +00:00
2021-12-07 01:01:22 +00:00
_GO_NVIM_CFG.goimport = "gopls"
2021-08-24 11:05:14 +00:00
local cmd = " silent exe 'e " .. path .. "'"
2021-08-24 07:34:33 +00:00
vim.cmd(cmd)
2021-09-02 10:42:22 +00:00
2021-09-02 16:33:34 +00:00
vim.cmd([[cd %:p:h]])
2021-09-02 10:53:44 +00:00
vim.cmd([[packadd go.nvim]])
2021-12-07 01:01:22 +00:00
require("go").setup({ goimport = "gopls", lsp_cfg = true })
vim.wait(2000, function() end)
2021-08-24 07:34:33 +00:00
require("go.format").goimport()
2021-08-24 11:05:14 +00:00
2022-05-29 04:55:22 +00:00
print("workspaces:", vim.inspect(vim.lsp.buf.list_workspace_folders()))
vim.wait(500, function() end)
vim.cmd([[w]])
local fmt = vim.fn.join(vim.fn.readfile(path), "\n")
print(vim.inspect(fmt))
eq(expected, fmt)
eq(1, 1) -- still not working
cmd = "bd! " .. path
vim.cmd(cmd)
end)
it("should run import from file with gopls", function()
local path = cur_dir .. "/lua/tests/fixtures/fmt/goimports3.go" -- %:p:h ? %:p
local expected = vim.fn.join(vim.fn.readfile(cur_dir .. "/lua/tests/fixtures/fmt/goimports3_golden.go"), "\n")
_GO_NVIM_CFG.goimport = "gopls"
local cmd = " silent exe 'e " .. path .. "'"
vim.cmd(cmd)
vim.cmd([[cd %:p:h]])
vim.cmd([[packadd go.nvim]])
require("go").setup({ goimport = "gopls", lsp_cfg = true })
vim.wait(2000, function() end)
require("go.format").goimport()
2021-08-24 07:34:33 +00:00
print("workspaces:", vim.inspect(vim.lsp.buf.list_workspace_folders()))
vim.wait(500, function() end)
2021-09-02 16:33:34 +00:00
vim.cmd([[w]])
2021-08-24 11:05:14 +00:00
local fmt = vim.fn.join(vim.fn.readfile(path), "\n")
2021-09-02 10:34:57 +00:00
print(vim.inspect(fmt))
2021-09-02 11:02:01 +00:00
eq(expected, fmt)
2021-08-24 07:42:03 +00:00
eq(1, 1) -- still not working
2021-08-24 11:05:14 +00:00
cmd = "bd! " .. path
2021-08-24 04:53:37 +00:00
vim.cmd(cmd)
end)
end)