refactor(spec): struct_tags

This commit is contained in:
Smirnov Oleksandr 2022-12-19 12:23:23 +02:00
parent 88d8848111
commit 8749051b96

View file

@ -6,47 +6,44 @@ describe("gopher.struct_tags", function()
end) end)
it("can add json tag to struct", function() it("can add json tag to struct", function()
local add = require("gopher.struct_tags").add local tag = require "gopher.struct_tags"
local name = vim.fn.tempname() .. ".go" local temp_file = vim.fn.tempname() .. ".go"
local input_file = vim.fn.readfile(cur_dir .. "/spec/fixtures/tags/add_input.go") local input_file = vim.fn.readfile(cur_dir .. "/spec/fixtures/tags/add_input.go")
local output_file = local output_file =
vim.fn.join(vim.fn.readfile(cur_dir .. "/spec/fixtures/tags/add_output.go"), "\n") vim.fn.join(vim.fn.readfile(cur_dir .. "/spec/fixtures/tags/add_output.go"), "\n")
vim.fn.writefile(input_file, name) vim.fn.writefile(input_file, temp_file)
vim.cmd("silent exe 'e " .. name .. "'") vim.cmd("silent exe 'e " .. temp_file .. "'")
local bufn = vim.fn.bufnr ""
vim.bo.filetype = "go" vim.bo.filetype = "go"
local bufn = vim.fn.bufnr(0)
vim.fn.setpos(".", { bufn, 3, 6, 0 }) vim.fn.setpos(".", { bufn, 3, 6, 0 })
add() tag.add()
vim.wait(100, function() end) vim.wait(100)
local fmt = vim.fn.join(vim.fn.readfile(name), "\n") assert.are.same(output_file, vim.fn.join(vim.fn.readfile(temp_file), "\n"))
assert.are.same(output_file, fmt)
vim.cmd("bd! " .. name) vim.cmd("bd! " .. temp_file)
end) end)
it("can remove json tag from struct", function() it("can remove json tag from struct", function()
local remove = require("gopher.struct_tags").remove local tag = require "gopher.struct_tags"
local name = vim.fn.tempname() .. ".go" local temp_file = vim.fn.tempname() .. ".go"
local input_file = vim.fn.readfile(cur_dir .. "/spec/fixtures/tags/remove_input.go") local input_file = vim.fn.readfile(cur_dir .. "/spec/fixtures/tags/remove_input.go")
local output_file = local output_file =
vim.fn.join(vim.fn.readfile(cur_dir .. "/spec/fixtures/tags/remove_output.go"), "\n") vim.fn.join(vim.fn.readfile(cur_dir .. "/spec/fixtures/tags/remove_output.go"), "\n")
vim.fn.writefile(input_file, name) vim.fn.writefile(input_file, temp_file)
vim.cmd("silent exe 'e " .. name .. "'") vim.cmd("silent exe 'e " .. temp_file .. "'")
local bufn = vim.fn.bufnr ""
vim.bo.filetype = "go" vim.bo.filetype = "go"
local bufn = vim.fn.bufnr()
vim.fn.setpos(".", { bufn, 3, 6, 0 }) vim.fn.setpos(".", { bufn, 3, 6, 0 })
remove() tag.remove()
vim.wait(100, function() end) vim.wait(100)
assert.are.same(output_file, vim.fn.join(vim.fn.readfile(temp_file), "\n"))
local fmt = vim.fn.join(vim.fn.readfile(name), "\n") vim.cmd("bd! " .. temp_file)
assert.are.same(output_file, fmt)
vim.cmd("bd! " .. name)
end) end)
end) end)