diff --git a/spec/fixtures/tags/add_input.go b/spec/fixtures/tags/add_input.go new file mode 100644 index 0000000..7e27a27 --- /dev/null +++ b/spec/fixtures/tags/add_input.go @@ -0,0 +1,11 @@ +package main + +type Test struct { + ID int + Name string + Num int64 + Another struct { + First int + Second string + } +} diff --git a/spec/fixtures/tags/add_output.go b/spec/fixtures/tags/add_output.go new file mode 100644 index 0000000..eaba62f --- /dev/null +++ b/spec/fixtures/tags/add_output.go @@ -0,0 +1,11 @@ +package main + +type Test struct { + ID int `json:"id"` + Name string `json:"name"` + Num int64 `json:"num"` + Another struct { + First int `json:"first"` + Second string `json:"second"` + } `json:"another"` +} diff --git a/spec/fixtures/tags/remove_input.go b/spec/fixtures/tags/remove_input.go new file mode 100644 index 0000000..eaba62f --- /dev/null +++ b/spec/fixtures/tags/remove_input.go @@ -0,0 +1,11 @@ +package main + +type Test struct { + ID int `json:"id"` + Name string `json:"name"` + Num int64 `json:"num"` + Another struct { + First int `json:"first"` + Second string `json:"second"` + } `json:"another"` +} diff --git a/spec/fixtures/tags/remove_output.go b/spec/fixtures/tags/remove_output.go new file mode 100644 index 0000000..7e27a27 --- /dev/null +++ b/spec/fixtures/tags/remove_output.go @@ -0,0 +1,11 @@ +package main + +type Test struct { + ID int + Name string + Num int64 + Another struct { + First int + Second string + } +} diff --git a/spec/gopher_struct_tags_spec.lua b/spec/gopher_struct_tags_spec.lua new file mode 100644 index 0000000..826933b --- /dev/null +++ b/spec/gopher_struct_tags_spec.lua @@ -0,0 +1,56 @@ +local cur_dir = vim.fn.expand "%:p:h" + +describe("gopher.struct_tags", function() + it("can be required", function() + require "gopher.struct_tags" + end) + + it("can add json tag to struct", function() + local add = require("gopher.struct_tags").add + local name = vim.fn.tempname() .. ".go" + local input_file = vim.fn.readfile(cur_dir .. "/spec/fixtures/tags/add_input.go") + local output_file = vim.fn.join( + vim.fn.readfile(cur_dir .. "/spec/fixtures/tags/add_output.go"), + "\n" + ) + + vim.fn.writefile(input_file, name) + vim.cmd("silent exe 'e " .. name .. "'") + + local bufn = vim.fn.bufnr "" + vim.bo.filetype = "go" + vim.fn.setpos(".", { bufn, 3, 6, 0 }) + add() + + vim.wait(100, function() end) + local fmt = vim.fn.join(vim.fn.readfile(name), "\n") + assert.are.same(output_file, fmt) + + vim.cmd("bd! " .. name) + end) + + it("can remove json tag from struct", function() + local remove = require("gopher.struct_tags").remove + local name = vim.fn.tempname() .. ".go" + local input_file = vim.fn.readfile(cur_dir .. "/spec/fixtures/tags/remove_input.go") + local output_file = vim.fn.join( + vim.fn.readfile(cur_dir .. "/spec/fixtures/tags/remove_output.go"), + "\n" + ) + + vim.fn.writefile(input_file, name) + vim.cmd("silent exe 'e " .. name .. "'") + + local bufn = vim.fn.bufnr "" + vim.bo.filetype = "go" + vim.fn.setpos(".", { bufn, 3, 6, 0 }) + remove() + + vim.wait(100, function() end) + + local fmt = vim.fn.join(vim.fn.readfile(name), "\n") + assert.are.same(output_file, fmt) + + vim.cmd("bd! " .. name) + end) +end)