Skip to content

Commit

Permalink
refactor: split up files and functions
Browse files Browse the repository at this point in the history
This prepares for supporting better testing and future features
  • Loading branch information
fredrikaverpil committed Jun 16, 2024
1 parent 2649202 commit ee6cd4c
Show file tree
Hide file tree
Showing 11 changed files with 508 additions and 394 deletions.
102 changes: 102 additions & 0 deletions lua/neotest-golang/ast.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
local lib = require("neotest.lib")

local M = {}

--- Detect test names in Go *._test.go files.
--- @param file_path string
function M.detect_tests(file_path)
local functions_and_methods = [[
;;query
((function_declaration
name: (identifier) @test.name)
(#match? @test.name "^(Test|Example)"))
@test.definition
(method_declaration
name: (field_identifier) @test.name
(#match? @test.name "^(Test|Example)")) @test.definition
(call_expression
function: (selector_expression
field: (field_identifier) @test.method)
(#match? @test.method "^Run$")
arguments: (argument_list . (interpreted_string_literal) @test.name))
@test.definition
]]

local table_tests = [[
;; query for list table tests
(block
(short_var_declaration
left: (expression_list
(identifier) @test.cases)
right: (expression_list
(composite_literal
(literal_value
(literal_element
(literal_value
(keyed_element
(literal_element
(identifier) @test.field.name)
(literal_element
(interpreted_string_literal) @test.name)))) @test.definition))))
(for_statement
(range_clause
left: (expression_list
(identifier) @test.case)
right: (identifier) @test.cases1
(#eq? @test.cases @test.cases1))
body: (block
(expression_statement
(call_expression
function: (selector_expression
field: (field_identifier) @test.method)
(#match? @test.method "^Run$")
arguments: (argument_list
(selector_expression
operand: (identifier) @test.case1
(#eq? @test.case @test.case1)
field: (field_identifier) @test.field.name1
(#eq? @test.field.name @test.field.name1))))))))
;; query for map table tests
(block
(short_var_declaration
left: (expression_list
(identifier) @test.cases)
right: (expression_list
(composite_literal
(literal_value
(keyed_element
(literal_element
(interpreted_string_literal) @test.name)
(literal_element
(literal_value) @test.definition))))))
(for_statement
(range_clause
left: (expression_list
((identifier) @test.key.name)
((identifier) @test.case))
right: (identifier) @test.cases1
(#eq? @test.cases @test.cases1))
body: (block
(expression_statement
(call_expression
function: (selector_expression
field: (field_identifier) @test.method)
(#match? @test.method "^Run$")
arguments: (argument_list
((identifier) @test.key.name1
(#eq? @test.key.name @test.key.name1))))))))
]]

local query = functions_and_methods .. table_tests
local opts = { nested_tests = true }

---@type neotest.Tree
local positions = lib.treesitter.parse_positions(file_path, query, opts)

return positions
end

return M
3 changes: 2 additions & 1 deletion lua/neotest-golang/convert.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local M = {}

-- Converts the AST-detected test name into the 'go test' command test name format.
-- Converts the AST-detected Neotest node test name into the 'go test' command
-- test name format.
---@param pos_id string
---@return string
function M.to_gotest_test_name(pos_id)
Expand Down
Loading

0 comments on commit ee6cd4c

Please sign in to comment.