From 0ef8f0fb0e0fd1fef8ff19b9b4fd4a25627e0f23 Mon Sep 17 00:00:00 2001 From: Fredrik Averpil Date: Tue, 25 Jun 2024 23:01:26 +0200 Subject: [PATCH] feat: add test suite support --- lua/neotest-golang/ast.lua | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/lua/neotest-golang/ast.lua b/lua/neotest-golang/ast.lua index b4b9edd8..5e1f6425 100644 --- a/lua/neotest-golang/ast.lua +++ b/lua/neotest-golang/ast.lua @@ -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 @@ -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