-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: split up files and functions
This prepares for supporting better testing and future features
- Loading branch information
1 parent
2649202
commit ee6cd4c
Showing
11 changed files
with
508 additions
and
394 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.