diff --git a/lua/gopher/comment.lua b/lua/gopher/comment.lua index 4886096..3b17a96 100644 --- a/lua/gopher/comment.lua +++ b/lua/gopher/comment.lua @@ -9,31 +9,28 @@ local comment = {} ---@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 - cmt = cmt .. res.name .. " " - return cmt + return cmt .. res.name .. " " end ok, res = pcall(ts.get_func_under_cursor, bufnr) if ok then - cmt = cmt .. res.name .. " " - return cmt + return cmt .. res.name .. " " end ok, res = pcall(ts.get_interface_under_cursor, bufnr) if ok then - cmt = cmt .. res.name .. " " - return cmt + return cmt .. res.name .. " " end ok, res = pcall(ts.get_package_under_cursor, bufnr) if ok then - cmt = cmt .. "Package " .. res.name .. " provides " - return cmt + return cmt .. "Package " .. res.name .. " provides " end return cmt diff --git a/spec/fixtures/comment/empty_input.go b/spec/fixtures/comment/empty_input.go new file mode 100644 index 0000000..e69de29 diff --git a/spec/fixtures/comment/empty_output.go b/spec/fixtures/comment/empty_output.go new file mode 100644 index 0000000..aa56d2c --- /dev/null +++ b/spec/fixtures/comment/empty_output.go @@ -0,0 +1,2 @@ +// + diff --git a/spec/fixtures/comment/func_input.go b/spec/fixtures/comment/func_input.go new file mode 100644 index 0000000..aec80b0 --- /dev/null +++ b/spec/fixtures/comment/func_input.go @@ -0,0 +1,5 @@ +package main + +func Test(a int) bool { + return false +} diff --git a/spec/fixtures/comment/func_output.go b/spec/fixtures/comment/func_output.go new file mode 100644 index 0000000..7dc39e5 --- /dev/null +++ b/spec/fixtures/comment/func_output.go @@ -0,0 +1,6 @@ +package main + +// Test +func Test(a int) bool { + return false +} diff --git a/spec/fixtures/comment/interface_input.go b/spec/fixtures/comment/interface_input.go new file mode 100644 index 0000000..b82f837 --- /dev/null +++ b/spec/fixtures/comment/interface_input.go @@ -0,0 +1,3 @@ +package main + +type Testinter interface{} diff --git a/spec/fixtures/comment/interface_output.go b/spec/fixtures/comment/interface_output.go new file mode 100644 index 0000000..ca9e0dd --- /dev/null +++ b/spec/fixtures/comment/interface_output.go @@ -0,0 +1,4 @@ +package main + +// Testinter +type Testinter interface{} diff --git a/spec/fixtures/comment/method_input.go b/spec/fixtures/comment/method_input.go new file mode 100644 index 0000000..39f715c --- /dev/null +++ b/spec/fixtures/comment/method_input.go @@ -0,0 +1,7 @@ +package main + +type Method struct{} + +func (Method) Run() error { + return nil +} diff --git a/spec/fixtures/comment/method_output.go b/spec/fixtures/comment/method_output.go new file mode 100644 index 0000000..2ffdf87 --- /dev/null +++ b/spec/fixtures/comment/method_output.go @@ -0,0 +1,8 @@ +package main + +type Method struct{} + +// Run +func (Method) Run() error { + return nil +} diff --git a/spec/fixtures/comment/package_output.go b/spec/fixtures/comment/package_output.go index 66d106a..3721c5c 100644 --- a/spec/fixtures/comment/package_output.go +++ b/spec/fixtures/comment/package_output.go @@ -1,2 +1,2 @@ -// Package main provides main +// Package main provides package main diff --git a/spec/fixtures/comment/struct_input.go b/spec/fixtures/comment/struct_input.go new file mode 100644 index 0000000..193b262 --- /dev/null +++ b/spec/fixtures/comment/struct_input.go @@ -0,0 +1,3 @@ +package main + +type CommentStruct struct {} diff --git a/spec/fixtures/comment/struct_output.go b/spec/fixtures/comment/struct_output.go new file mode 100644 index 0000000..db0ced0 --- /dev/null +++ b/spec/fixtures/comment/struct_output.go @@ -0,0 +1,4 @@ +package main + +// CommentStruct +type CommentStruct struct {} diff --git a/spec/integration/comment_test.lua b/spec/integration/comment_test.lua index 8276688..ca740a6 100644 --- a/spec/integration/comment_test.lua +++ b/spec/integration/comment_test.lua @@ -5,23 +5,47 @@ local T = MiniTest.new_set { hooks = { post_once = child.stop, pre_case = function() - MiniTest.skip "This module should be fixed first" child.restart { "-u", t.mininit_path } end, }, } + +local function do_the_test(fixture, pos) + local tmp = t.tmpfile() + local fixtures = t.get_fixtures("comment/" .. fixture) + t.writefile(tmp, fixtures.input) + + child.cmd("silent edit " .. tmp) + child.fn.setpos(".", { child.fn.bufnr "%", unpack(pos) }) + child.cmd "GoCmt" + child.cmd "write" + + t.eq(t.readfile(tmp), fixtures.output) +end + T["comment"] = MiniTest.new_set {} +T["comment"]["should add comment to package"] = function() + do_the_test("package", { 1, 1 }) +end -T["comment"]["should add comment to package"] = function() end +T["comment"]["should add comment to struct"] = function() + do_the_test("struct", { 4, 1 }) +end -T["comment"]["should add comment to struct"] = function() end +T["comment"]["should add comment to function"] = function() + do_the_test("func", { 3, 1 }) +end -T["comment"]["should add comment to function"] = function() end +T["comment"]["should add comment to method"] = function() + do_the_test("func", { 5, 1 }) +end -T["comment"]["should add comment to method"] = function() end +T["comment"]["should add comment to interface"] = function() + do_the_test("interface", { 3, 1 }) +end -T["comment"]["should add comment to interface"] = function() end - -T["comment"]["otherwise should add // above cursor"] = function() end +T["comment"]["otherwise should add // above cursor"] = function() + do_the_test("empty", { 1, 1 }) +end return T