Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support table tests defined in for loop #71

Merged
merged 2 commits into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 56 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 receiver_method = [[
; 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 )))) @namespace.definition
]]

local table_tests = [[
;; query for list table tests
(block
Expand Down Expand Up @@ -59,6 +70,40 @@ function M.detect_tests(file_path)
field: (field_identifier) @test.field.name1
(#eq? @test.field.name @test.field.name1))))))))

;; query for list table tests (wrapped in loop)
(for_statement
(range_clause
left: (expression_list
(identifier)
(identifier) @test.case )
right: (composite_literal
type: (slice_type
element: (struct_type
(field_declaration_list
(field_declaration
name: (field_identifier)
type: (type_identifier)))))
body: (literal_value
(literal_element
(literal_value
(keyed_element
(literal_element
(identifier)) @test.field.name
(literal_element
(interpreted_string_literal) @test.name ))
) @test.definition)
)))
body: (block
(expression_statement
(call_expression
function: (selector_expression
operand: (identifier)
field: (field_identifier))
arguments: (argument_list
(selector_expression
operand: (identifier)
field: (field_identifier) @test.field.name1) (#eq? @test.field.name @test.field.name1))))))

;; query for map table tests
(block
(short_var_declaration
Expand Down Expand Up @@ -90,7 +135,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 .. receiver_method .. table_tests
local opts = { nested_tests = true }

---@type neotest.Tree
Expand Down
71 changes: 62 additions & 9 deletions tests/go/positions_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -173,31 +173,84 @@ describe("Discovery of test positions", function()
},
{
{
id = test_filepath .. "::TestTableTestMap",
name = "TestTableTestMap",
id = test_filepath .. "::TestTableTestInlineStructLoop",
name = "TestTableTestInlineStructLoop",
path = test_filepath,
type = "test",
},
{
{
id = test_filepath
.. '::TestTableTestInlineStructLoop::"TableTest1"',
name = '"TableTest1"',
path = test_filepath,
type = "test",
},
},
{
{
id = test_filepath
.. '::TestTableTestInlineStructLoop::"TableTest2"',
name = '"TableTest2"',
path = test_filepath,
type = "test",
},
},
},
{
{
id = test_filepath .. "::TestSubTestTableTestInlineStructLoop",
name = "TestSubTestTableTestInlineStructLoop",
path = test_filepath,
type = "test",
},
{
{
id = test_filepath .. '::TestTableTestMap::"add 1+1"',
name = '"add 1+1"',
id = test_filepath
.. '::TestSubTestTableTestInlineStructLoop::"SubTest"',
name = '"SubTest"',
path = test_filepath,
type = "test",
},
{
{
id = test_filepath
.. '::TestSubTestTableTestInlineStructLoop::"SubTest"::"TableTest1"',
name = '"TableTest1"',
path = test_filepath,
type = "test",
},
},
{
{
id = test_filepath
.. '::TestSubTestTableTestInlineStructLoop::"SubTest"::"TableTest2"',
name = '"TableTest2"',
path = test_filepath,
type = "test",
},
},
},
},
{
{
id = test_filepath .. "::TestTableTestMap",
name = "TestTableTestMap",
path = test_filepath,
type = "test",
},
{
{
id = test_filepath .. '::TestTableTestMap::"add 2+2"',
name = '"add 2+2"',
id = test_filepath .. '::TestTableTestMap::"TableTest1"',
name = '"TableTest1"',
path = test_filepath,
type = "test",
},
},
{
{
id = test_filepath .. '::TestTableTestMap::"add 5+5"',
name = '"add 5+5"',
id = test_filepath .. '::TestTableTestMap::"TableTest2"',
name = '"TableTest2"',
path = test_filepath,
type = "test",
},
Expand All @@ -216,7 +269,7 @@ describe("Discovery of test positions", function()
local ignoreKeys = { range = true }
local expectedCopy, resultCopy =
compareIgnoringKeys(expected, result, ignoreKeys)
-- assert.are.same(vim.inspect(expectedCopy), vim.inspect(resultCopy))
assert.are.same(vim.inspect(expectedCopy), vim.inspect(resultCopy))
assert.are.same(expectedCopy, resultCopy)
end)
end)
45 changes: 42 additions & 3 deletions tests/go/positions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,55 @@ func TestSubTestTableTestInlineStruct(t *testing.T) {
})
}

// Table test defined as anonymous struct in loop.
func TestTableTestInlineStructLoop(t *testing.T) {
for _, tc := range []struct {
name string
x int
y int
expected int
}{
{name: "TableTest1", x: 1, y: 2, expected: 3},
{name: "TableTest2", x: 3, y: 4, expected: 7},
} {
t.Run(tc.name, func(t *testing.T) {
if Add(tc.x, tc.y) != tc.expected {
t.Fail()
}
})
}
}

// Table test defined as anonymous struct in loop (in sub-test).
func TestSubTestTableTestInlineStructLoop(t *testing.T) {
t.Run("SubTest", func(t *testing.T) {
for _, tc := range []struct {
name string
x int
y int
expected int
}{
{name: "TableTest1", x: 1, y: 2, expected: 3},
{name: "TableTest2", x: 3, y: 4, expected: 7},
} {
t.Run(tc.name, func(t *testing.T) {
if Add(tc.x, tc.y) != tc.expected {
t.Fail()
}
})
}
})
}

// Table test defined as map.
func TestTableTestMap(t *testing.T) {
tt := map[string]struct {
a int
b int
want int
}{
"add 1+1": {a: 1, b: 1, want: 2},
"add 2+2": {a: 2, b: 2, want: 4},
"add 5+5": {a: 5, b: 5, want: 10},
"TableTest1": {a: 1, b: 1, want: 2},
"TableTest2": {a: 2, b: 2, want: 4},
}
for name, tc := range tt {
t.Run(name, func(t *testing.T) {
Expand Down
Loading