Skip to content

Commit

Permalink
feat: test suite support
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikaverpil committed Jun 25, 2024
1 parent af16ea8 commit 22f14b4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -332,3 +332,22 @@ You can run tests, formatting and linting locally with `make all`. Install
dependencies with `make install`. Have a look at the [Makefile](Makefile) for
more details. You can also use the neotest-plenary and neotest-golang adapters
to run the tests of this repo within Neovim.

### AST and tree-sitter

To figure out new tree-sitter queries (for detecting tests), the following
commands are available in Neovim to aid you:

- `:Inspect` to show the highlight groups under the cursor.
- `:InspectTree` to show the parsed syntax tree (formerly known as
"TSPlayground").
- `:EditQuery` to open the Live Query Editor (Nvim 0.10+).

For example, open up a Go test file and then open up a new window side-by side
in which you execute `:InspectTree`. Here you can now see what the tree-sitter
query syntax representation looks like for the Go test file.

Also open up the editor with `:EditQuery`, so that you can have all three
windows opened side-by-side. In the editor, you can now start creating your
syntax query and play around. You can paste in queries from `ast.lua` in the
editor, to see how the query behaves and highlights parts of your Go test file.
33 changes: 22 additions & 11 deletions lua/neotest-golang/ast.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,36 @@ 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
local test_function = [[
; query for test function
((function_declaration
name: (identifier) @test.name)
(#match? @test.name "^(Test|Example)"))
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
; query for subtest, like t.Run()
(call_expression
function: (selector_expression
field: (field_identifier) @test.method)
(#match? @test.method "^Run$")
field: (field_identifier) @test.method) (#match? @test.method "^Run$")
arguments: (argument_list . (interpreted_string_literal) @test.name))
@test.definition
]]

local test_method = [[
; query for test method
(method_declaration
name: (field_identifier) @test.name (#match? @test.name "^(Test|Example)")) @test.definition
]]

local method_receiver = [[
; query for receiver method, to be used as test suite namespace
(method_declaration
receiver: (parameter_list
(parameter_declaration
; name: (identifier)
type: (pointer_type
(type_identifier) @namespace.name (#match? @namespace.name "(Test|Example|Suite)") )))) @namespace.definition
]]

local table_tests = [[
;; query for list table tests
(block
Expand Down Expand Up @@ -90,7 +101,7 @@ function M.detect_tests(file_path)
(#eq? @test.key.name @test.key.name1))))))))
]]

local query = functions_and_methods .. table_tests
local query = test_function .. test_method .. method_receiver .. table_tests
local opts = { nested_tests = true }

---@type neotest.Tree
Expand Down

0 comments on commit 22f14b4

Please sign in to comment.