From ab874c6c4f1f1d31a39837b53fecfef0c64751bd Mon Sep 17 00:00:00 2001 From: Smirnov Oleksandr Date: Sat, 4 Feb 2023 17:04:12 +0200 Subject: [PATCH] some tests refactoring and sep by type --- lua/gopher/struct_tags.lua | 20 ++------- spec/gopher_config_spec.lua | 41 ------------------- spec/gopher_spec.lua | 5 --- spec/gopher_utils_spec.lua | 19 --------- .../struct_tags_spec.lua} | 6 +-- spec/minimal_init.lua | 1 - spec/unit/config_spec.lua | 22 ++++++++++ spec/unit/utils_spec.lua | 19 +++++++++ 8 files changed, 46 insertions(+), 87 deletions(-) delete mode 100644 spec/gopher_config_spec.lua delete mode 100644 spec/gopher_spec.lua delete mode 100644 spec/gopher_utils_spec.lua rename spec/{gopher_struct_tags_spec.lua => integration/struct_tags_spec.lua} (85%) create mode 100644 spec/unit/config_spec.lua create mode 100644 spec/unit/utils_spec.lua diff --git a/lua/gopher/struct_tags.lua b/lua/gopher/struct_tags.lua index a91901a..a0844a0 100644 --- a/lua/gopher/struct_tags.lua +++ b/lua/gopher/struct_tags.lua @@ -47,10 +47,7 @@ local function modify(...) args = cmd_args, on_exit = function(data, retval) if retval ~= 0 then - u.notify( - "command 'gomodifytags " .. unpack(cmd_args) .. "' exited with code " .. retval, - "error" - ) + u.notify("command 'gomodifytags " .. unpack(cmd_args) .. "' exited with code " .. retval, "error") return end @@ -60,12 +57,7 @@ local function modify(...) -- decode goted value local tagged = vim.json.decode(table.concat(res_data)) - if - tagged.errors ~= nil - or tagged.lines == nil - or tagged["start"] == nil - or tagged["start"] == 0 - then + if tagged.errors ~= nil or tagged.lines == nil or tagged["start"] == nil or tagged["start"] == 0 then u.notify("failed to set tags " .. vim.inspect(tagged), "error") end @@ -74,13 +66,7 @@ local function modify(...) end -- write goted tags - vim.api.nvim_buf_set_lines( - 0, - tagged.start - 1, - tagged.start - 1 + #tagged.lines, - false, - tagged.lines - ) + vim.api.nvim_buf_set_lines(0, tagged.start - 1, tagged.start - 1 + #tagged.lines, false, tagged.lines) vim.cmd "write" end diff --git a/spec/gopher_config_spec.lua b/spec/gopher_config_spec.lua deleted file mode 100644 index 1d033c0..0000000 --- a/spec/gopher_config_spec.lua +++ /dev/null @@ -1,41 +0,0 @@ -describe("gopher.config", function() - it("can be required", function() - require "gopher.config" - end) - - it(".setup() when gets empty table not edit config", function() - local c = require "gopher.config" - c.setup {} - - assert.are.same(c.config.commands.go, "go") - assert.are.same(c.config.commands.gomodifytags, "gomodifytags") - assert.are.same(c.config.commands.gotests, "gotests") - assert.are.same(c.config.commands.impl, "impl") - end) - - it(".setup() when get one custom value sets that", function() - local c = require "gopher.config" - c.setup { commands = { - go = "custom_go", - } } - - assert.are.same(c.config.commands.go, "custom_go") - end) - - it(".setup() when get all custom values sets it", function() - local c = require "gopher.config" - c.setup { - commands = { - go = "go1.18", - gomodifytags = "user-gomodifytags", - gotests = "gotests", - impl = "goimpl", - }, - } - - assert.are.same(c.config.commands.go, "go1.18") - assert.are.same(c.config.commands.gomodifytags, "user-gomodifytags") - assert.are.same(c.config.commands.gotests, "gotests") - assert.are.same(c.config.commands.impl, "goimpl") - end) -end) diff --git a/spec/gopher_spec.lua b/spec/gopher_spec.lua deleted file mode 100644 index b50b5ea..0000000 --- a/spec/gopher_spec.lua +++ /dev/null @@ -1,5 +0,0 @@ -describe("gopher", function() - it("can be required", function() - require "gopher" - end) -end) diff --git a/spec/gopher_utils_spec.lua b/spec/gopher_utils_spec.lua deleted file mode 100644 index f052cff..0000000 --- a/spec/gopher_utils_spec.lua +++ /dev/null @@ -1,19 +0,0 @@ -describe("gopher._utils", function() - it("can be requried", function() - require "gopher._utils" - end) - - it(".empty() with non-empty talbe", function() - local empty = require("gopher._utils").empty - local res = empty { first = "1", second = 2 } - - assert.are.same(res, false) - end) - - it(".empty() with empty talbe", function() - local empty = require("gopher._utils").empty - local res = empty {} - - assert.are.same(res, true) - end) -end) diff --git a/spec/gopher_struct_tags_spec.lua b/spec/integration/struct_tags_spec.lua similarity index 85% rename from spec/gopher_struct_tags_spec.lua rename to spec/integration/struct_tags_spec.lua index ad8dd6e..03a97ae 100644 --- a/spec/gopher_struct_tags_spec.lua +++ b/spec/integration/struct_tags_spec.lua @@ -9,8 +9,7 @@ describe("gopher.struct_tags", function() local tag = require "gopher.struct_tags" local temp_file = 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") + local output_file = vim.fn.join(vim.fn.readfile(cur_dir .. "/spec/fixtures/tags/add_output.go"), "\n") vim.fn.writefile(input_file, temp_file) vim.cmd("silent exe 'e " .. temp_file .. "'") @@ -30,8 +29,7 @@ describe("gopher.struct_tags", function() local tag = require "gopher.struct_tags" local temp_file = 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") + local output_file = vim.fn.join(vim.fn.readfile(cur_dir .. "/spec/fixtures/tags/remove_output.go"), "\n") vim.fn.writefile(input_file, temp_file) vim.cmd("silent exe 'e " .. temp_file .. "'") diff --git a/spec/minimal_init.lua b/spec/minimal_init.lua index feda336..2a24076 100644 --- a/spec/minimal_init.lua +++ b/spec/minimal_init.lua @@ -1,4 +1,3 @@ - local function root(p) local f = debug.getinfo(1, "S").source:sub(2) return vim.fn.fnamemodify(f, ":p:h:h") .. "/" .. (p or "") diff --git a/spec/unit/config_spec.lua b/spec/unit/config_spec.lua new file mode 100644 index 0000000..2787b47 --- /dev/null +++ b/spec/unit/config_spec.lua @@ -0,0 +1,22 @@ +describe("unit.gopher.config", function() + it("can be required", function() + require "gopher.config" + end) + + describe(".setup()", function() + local c = require "gopher.config" + + it("returns defaults when gets empty table", function() + c.setup {} + assert.are.same(c.config.commands.go, "go") + assert.are.same(c.config.commands.gomodifytags, "gomodifytags") + assert.are.same(c.config.commands.gotests, "gotests") + assert.are.same(c.config.commands.impl, "impl") + end) + + it("can set user opts", function() + c.setup { commands = { go = "custom_go" } } + assert.are.same(c.config.commands.go, "custom_go") + end) + end) +end) diff --git a/spec/unit/utils_spec.lua b/spec/unit/utils_spec.lua new file mode 100644 index 0000000..c36fce2 --- /dev/null +++ b/spec/unit/utils_spec.lua @@ -0,0 +1,19 @@ +describe("gopher._utils", function() + it("can be requried", function() + require "gopher._utils" + end) + + describe(".empty()", function() + local empty = require("gopher._utils").empty + + it("with non-empty talbe", function() + local res = empty { first = "1", second = 2 } + assert.are.same(res, false) + end) + + it("with empty talbe", function() + local res = empty {} + assert.are.same(res, true) + end) + end) +end)