Skip to content

Commit 9e3b83d

Browse files
NickHackmancodymikol
authored andcommitted
feat: support Kotest WordSpec
1 parent f81fa37 commit 9e3b83d

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

lua/neotest-kotlin/treesitter/init.lua

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,39 @@ function M.parse_positions(file)
6969
return neotest.treesitter.parse_positions(file, kotest_query, {
7070
nested_namespaces = true,
7171
nested_tests = false,
72+
---@type fun(file_path: string, source: string, captured_nodes: table<string, userdata>, metadata: table<string, vim.treesitter.query.TSMetadata>): neotest.Position|neotest.Position[]|nil Builds one or more positions from the captured nodes from a query match.
73+
build_position = function(file_path, source, captured_nodes, metadata)
74+
---@param captured_nodes table<string, userdata>
75+
local function get_match_type(captured_nodes)
76+
if captured_nodes["test.name"] then
77+
return "test"
78+
end
79+
if captured_nodes["namespace.name"] then
80+
return "namespace"
81+
end
82+
end
83+
84+
local match_type = get_match_type(captured_nodes)
85+
if match_type then
86+
local node_name = match_type .. ".name"
87+
---@type string
88+
local name =
89+
vim.treesitter.get_node_text(captured_nodes[node_name], source)
90+
91+
if metadata[node_name] ~= nil and metadata[node_name].text ~= nil then
92+
name = metadata[node_name].text
93+
end
94+
95+
local definition = captured_nodes[match_type .. ".definition"]
96+
97+
return {
98+
type = match_type,
99+
path = file_path,
100+
name = name,
101+
range = { definition:range() },
102+
}
103+
end
104+
end,
72105
})
73106
end
74107

lua/neotest-kotlin/treesitter/kotest-query.lua

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,40 @@ return [[
124124
)
125125
) @test.definition
126126
127-
;; -- todo WORD SPEC --
127+
;; --- WORD SPEC ---
128+
129+
; Matches "context" `when` { /** body **/ }
130+
; Matches "context" When { /** body **/ }
131+
132+
(infix_expression
133+
(string_literal
134+
(string_content) @namespace.name (#gsub! @namespace.name "$" " when")
135+
)
136+
(simple_identifier) @function_name (#any-of? @function_name "`when`" "When")
137+
(lambda_literal)
138+
) @namespace.definition
139+
140+
; Matches "context" should { /** body **/ }
141+
142+
(infix_expression
143+
(string_literal
144+
(string_content) @namespace.name (#gsub! @namespace.name "$" " should")
145+
)
146+
(simple_identifier) @function_name (#eq? @function_name "should")
147+
(lambda_literal)
148+
) @namespace.definition
149+
150+
; Matches "test" { /** body **/ }
151+
152+
(call_expression
153+
(string_literal
154+
(string_content) @test.name
155+
)
156+
(call_suffix
157+
(annotated_lambda)
158+
)
159+
) @test.definition
160+
128161
;; --- FEATURE SPEC ---
129162
130163
; Matches namespace feature("context") { /** body **/ }

0 commit comments

Comments
 (0)