From 7495e29d4fb057113b3fc96a0eeb1d463406c159 Mon Sep 17 00:00:00 2001 From: Oleksandr Smirnov Date: Wed, 19 Mar 2025 13:34:10 +0200 Subject: [PATCH] tests(comment): fix --- lua/gopher/comment.lua | 36 +++++++++++++++----------- spec/fixtures/comment/struct_input.go | 2 +- spec/fixtures/comment/struct_output.go | 2 +- spec/integration/comment_test.lua | 7 +++-- spec/testutils.lua | 5 ++++ 5 files changed, 33 insertions(+), 19 deletions(-) diff --git a/lua/gopher/comment.lua b/lua/gopher/comment.lua index 3b17a96..94f0b43 100644 --- a/lua/gopher/comment.lua +++ b/lua/gopher/comment.lua @@ -7,33 +7,39 @@ local ts = require "gopher._utils.ts" local log = require "gopher._utils.log" local comment = {} +---@param name string +---@return string +---@private +local function template(name) + return "// " .. (name or "") .. " " +end + ---@param bufnr integer ---@return string ---@private local function generate(bufnr) - local cmt = "// " - - local ok, res = pcall(ts.get_struct_under_cursor, bufnr) - if ok then - return cmt .. res.name .. " " + local sok, sres = pcall(ts.get_struct_under_cursor, bufnr) + vim.print(sok, sres) + if sok then + return template(sres.name) end - ok, res = pcall(ts.get_func_under_cursor, bufnr) - if ok then - return cmt .. res.name .. " " + local fok, fres = pcall(ts.get_func_under_cursor, bufnr) + if fok then + return template(fres.name) end - ok, res = pcall(ts.get_interface_under_cursor, bufnr) - if ok then - return cmt .. res.name .. " " + local iok, ires = pcall(ts.get_interface_under_cursor, bufnr) + if iok then + return template(ires.name) end - ok, res = pcall(ts.get_package_under_cursor, bufnr) - if ok then - return cmt .. "Package " .. res.name .. " provides " + local pok, pres = pcall(ts.get_package_under_cursor, bufnr) + if pok then + return "// Package " .. pres.name .. " provides " end - return cmt + return "// " end function comment.comment() diff --git a/spec/fixtures/comment/struct_input.go b/spec/fixtures/comment/struct_input.go index 193b262..98e8561 100644 --- a/spec/fixtures/comment/struct_input.go +++ b/spec/fixtures/comment/struct_input.go @@ -1,3 +1,3 @@ package main -type CommentStruct struct {} +type CommentStruct struct{} diff --git a/spec/fixtures/comment/struct_output.go b/spec/fixtures/comment/struct_output.go index db0ced0..14e279d 100644 --- a/spec/fixtures/comment/struct_output.go +++ b/spec/fixtures/comment/struct_output.go @@ -1,4 +1,4 @@ package main // CommentStruct -type CommentStruct struct {} +type CommentStruct struct{} diff --git a/spec/integration/comment_test.lua b/spec/integration/comment_test.lua index ca740a6..9cad5f7 100644 --- a/spec/integration/comment_test.lua +++ b/spec/integration/comment_test.lua @@ -21,6 +21,9 @@ local function do_the_test(fixture, pos) child.cmd "write" t.eq(t.readfile(tmp), fixtures.output) + + -- without it all other(not even from this module) tests are falling + t.deletefile(tmp) end T["comment"] = MiniTest.new_set {} @@ -37,11 +40,11 @@ T["comment"]["should add comment to function"] = function() end T["comment"]["should add comment to method"] = function() - do_the_test("func", { 5, 1 }) + do_the_test("method", { 5, 1 }) end T["comment"]["should add comment to interface"] = function() - do_the_test("interface", { 3, 1 }) + do_the_test("interface", { 3, 6 }) end T["comment"]["otherwise should add // above cursor"] = function() diff --git a/spec/testutils.lua b/spec/testutils.lua index e5ab9a2..32c19ab 100644 --- a/spec/testutils.lua +++ b/spec/testutils.lua @@ -31,6 +31,11 @@ function testutils.writefile(fpath, contents) vim.fn.writefile(vim.split(contents, "\n"), fpath) end +---@param fpath string +function testutils.deletefile(fpath) + vim.fn.delete(fpath) +end + ---@param fixture string ---@return {input: string, output: string} function testutils.get_fixtures(fixture)