test: test it and fix it

This commit is contained in:
Oleksandr Smirnov 2025-03-22 13:06:02 +02:00
parent 46c093dbae
commit d629fe9282
No known key found for this signature in database
6 changed files with 76 additions and 4 deletions

View file

@ -3,8 +3,9 @@ local queries = {
struct = [[
[(type_spec name: (type_identifier) @_name
type: (struct_type))
(var_spec name: (identifier) @_name
type: (struct_type))
(var_declaration (var_spec
name: (identifier) @_name @_var
type: (struct_type)))
(short_var_declaration
left: (expression_list (identifier) @_name @_var)
right: (expression_list (composite_literal
@ -46,7 +47,7 @@ end
---@param query vim.treesitter.Query
---@param node TSNode
---@param bufnr integer
---@return {name:string}
---@return {name:string, is_varstruct:boolean}
local function get_captures(query, node, bufnr)
local res = {}
for _, match, _ in query:iter_matches(node, bufnr) do
@ -110,7 +111,7 @@ function ts.get_struct_under_cursor(bufnr)
return do_stuff(bufnr, {
"type_spec",
"type_declaration",
"var_spec",
"var_declaration",
"short_var_declaration",
}, queries.struct)
end

11
spec/fixtures/tags/svar_input.go vendored Normal file
View file

@ -0,0 +1,11 @@
package main
func main() {
s := struct {
API string
Key string
}{
API: "api.com",
Key: "key",
}
}

11
spec/fixtures/tags/svar_output.go vendored Normal file
View file

@ -0,0 +1,11 @@
package main
func main() {
s := struct {
API string `xml:"api"`
Key string `xml:"key"`
}{
API: "api.com",
Key: "key",
}
}

8
spec/fixtures/tags/var_input.go vendored Normal file
View file

@ -0,0 +1,8 @@
package main
func main() {
var a struct {
TestField1 string
TestField2 string
}
}

8
spec/fixtures/tags/var_output.go vendored Normal file
View file

@ -0,0 +1,8 @@
package main
func main() {
var a struct {
TestField1 string `yaml:"test_field_1"`
TestField2 string `yaml:"test_field_2"`
}
}

View file

@ -21,6 +21,7 @@ T["struct_tags"]["should add tag"] = function()
child.cmd "write"
t.eq(t.readfile(tmp), fixtures.output)
t.deletefile(tmp)
end
T["struct_tags"]["should remove tag"] = function()
@ -34,6 +35,7 @@ T["struct_tags"]["should remove tag"] = function()
child.cmd "write"
t.eq(t.readfile(tmp), fixtures.output)
t.deletefile(tmp)
end
T["struct_tags"]["should be able to handle many structs"] = function()
@ -47,6 +49,7 @@ T["struct_tags"]["should be able to handle many structs"] = function()
child.cmd "write"
t.eq(t.readfile(tmp), fixtures.output)
t.deletefile(tmp)
end
T["struct_tags"]["should clear struct"] = function()
@ -60,6 +63,7 @@ T["struct_tags"]["should clear struct"] = function()
child.cmd "write"
t.eq(t.readfile(tmp), fixtures.output)
t.deletefile(tmp)
end
T["struct_tags"]["should add more than one tag"] = function()
@ -80,6 +84,35 @@ T["struct_tags"]["should add more than one tag"] = function()
child.cmd "write"
t.eq(t.readfile(tmp), fixtures.output)
t.deletefile(tmp)
end
T["struct_tags"]["should add tags on var"] = function()
-- local tmp = t.tmpfile()
local tmp = "/tmp/test.go"
local fixtures = t.get_fixtures "tags/var"
t.writefile(tmp, fixtures.input)
child.cmd("silent edit " .. tmp)
child.fn.setpos(".", { child.fn.bufnr(tmp), 5, 3 })
child.cmd "GoTagAdd yaml"
child.cmd "write"
t.eq(t.readfile(tmp), fixtures.output)
end
T["struct_tags"]["should add tags on short declr var"] = function()
local tmp = t.tmpfile()
local fixtures = t.get_fixtures "tags/svar"
t.writefile(tmp, fixtures.input)
child.cmd("silent edit " .. tmp)
child.fn.setpos(".", { child.fn.bufnr(tmp), 4, 3 })
child.cmd "GoTagAdd xml"
child.cmd "write"
t.eq(t.readfile(tmp), fixtures.output)
t.deletefile(tmp)
end
return T