diff --git a/lua/neotest-golang/query.lua b/lua/neotest-golang/query.lua index 0e5b48cf..d3375cfb 100644 --- a/lua/neotest-golang/query.lua +++ b/lua/neotest-golang/query.lua @@ -59,74 +59,100 @@ M.table_tests = [[ ;; query for list table tests (wrapped in loop) (for_statement (range_clause - left: (expression_list + left: (expression_list (identifier) - (identifier) @test.case ) - right: (composite_literal - type: (slice_type - element: (struct_type - (field_declaration_list - (field_declaration - name: (field_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 + (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) + 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 table tests with inline structs (not keyed) - (function_declaration - name: (identifier) - parameters: (parameter_list - (parameter_declaration - name: (identifier) - type: (pointer_type - (qualified_type - package: (package_identifier) - name: (type_identifier)))))) - body: (block + (block + (short_var_declaration + left: (expression_list (identifier) @test.cases) + right: (expression_list + (composite_literal + body: (literal_value + (literal_element + (literal_value + (literal_element (interpreted_string_literal) @test.name) + (literal_element)) @test.definition))))) (for_statement (range_clause - left: (expression_list - (identifier) - (identifier)) - right: (composite_literal + 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 + (selector_expression + operand: (identifier) @test.case1 + (#eq? @test.case @test.case1)))))))) + + ;; Query for table tests with inline structs (not keyed, 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) ; unkeyed field for test name - type: (type_identifier) @field.type (#eq? @field.type "string") )))))))) + element: (struct_type + (field_declaration_list + (field_declaration + name: (field_identifier) @test.field.name + type: (type_identifier) @field.type (#eq? @field.type "string"))))) + body: (literal_value (literal_element (literal_value (literal_element (interpreted_string_literal) @test.name) - (literal_element)) @test.definition)) - body: (block - (expression_statement - (call_expression - function: (selector_expression - operand: (identifier) - field: (field_identifier) @test.method (#match? @test.method "^Run$"))))) + (literal_element)) @test.definition)))) + 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 + ;; query for map table tests (block (short_var_declaration left: (expression_list @@ -166,8 +192,8 @@ function M.detect_tests(file_path) if options.get().testify_enabled == true then -- detect receiver types (as namespaces) and test methods. query = query - .. testify.query.namespace_query - .. testify.query.test_method_query + .. testify.query.namespace_query + .. testify.query.test_method_query end ---@type neotest.Tree diff --git a/tests/go/positions_spec.lua b/tests/go/positions_spec.lua index 58f63849..b5306041 100644 --- a/tests/go/positions_spec.lua +++ b/tests/go/positions_spec.lua @@ -95,7 +95,7 @@ describe("Discovery of test positions", function() { { id = test_filepath - .. '::TestSubTestTableTestStruct::"SubTest"::"TableTest1"', + .. '::TestSubTestTableTestStruct::"SubTest"::"TableTest1"', name = '"TableTest1"', path = test_filepath, type = "test", @@ -104,7 +104,7 @@ describe("Discovery of test positions", function() { { id = test_filepath - .. '::TestSubTestTableTestStruct::"SubTest"::"TableTest2"', + .. '::TestSubTestTableTestStruct::"SubTest"::"TableTest2"', name = '"TableTest2"', path = test_filepath, type = "test", @@ -146,7 +146,7 @@ describe("Discovery of test positions", function() { { id = test_filepath - .. '::TestSubTestTableTestInlineStruct::"SubTest"', + .. '::TestSubTestTableTestInlineStruct::"SubTest"', name = '"SubTest"', path = test_filepath, type = "test", @@ -154,7 +154,7 @@ describe("Discovery of test positions", function() { { id = test_filepath - .. '::TestSubTestTableTestInlineStruct::"SubTest"::"TableTest1"', + .. '::TestSubTestTableTestInlineStruct::"SubTest"::"TableTest1"', name = '"TableTest1"', path = test_filepath, type = "test", @@ -163,7 +163,7 @@ describe("Discovery of test positions", function() { { id = test_filepath - .. '::TestSubTestTableTestInlineStruct::"SubTest"::"TableTest2"', + .. '::TestSubTestTableTestInlineStruct::"SubTest"::"TableTest2"', name = '"TableTest2"', path = test_filepath, type = "test", @@ -181,7 +181,7 @@ describe("Discovery of test positions", function() { { id = test_filepath - .. '::TestTableTestInlineStructLoop::"TableTest1"', + .. '::TestTableTestInlineStructLoop::"TableTest1"', name = '"TableTest1"', path = test_filepath, type = "test", @@ -190,7 +190,7 @@ describe("Discovery of test positions", function() { { id = test_filepath - .. '::TestTableTestInlineStructLoop::"TableTest2"', + .. '::TestTableTestInlineStructLoop::"TableTest2"', name = '"TableTest2"', path = test_filepath, type = "test", @@ -207,7 +207,7 @@ describe("Discovery of test positions", function() { { id = test_filepath - .. '::TestTableTestInlineStructLoopNotKeyed::"TableTest1"', + .. '::TestTableTestInlineStructLoopNotKeyed::"TableTest1"', name = '"TableTest1"', path = test_filepath, type = "test", @@ -216,7 +216,7 @@ describe("Discovery of test positions", function() { { id = test_filepath - .. '::TestTableTestInlineStructLoopNotKeyed::"TableTest2"', + .. '::TestTableTestInlineStructLoopNotKeyed::"TableTest2"', name = '"TableTest2"', path = test_filepath, type = "test", @@ -233,7 +233,7 @@ describe("Discovery of test positions", function() { { id = test_filepath - .. '::TestTableTestInlineStructLoopNotKeyed2::"TableTest1"', + .. '::TestTableTestInlineStructLoopNotKeyed2::"TableTest1"', name = '"TableTest1"', path = test_filepath, type = "test", @@ -242,7 +242,7 @@ describe("Discovery of test positions", function() { { id = test_filepath - .. '::TestTableTestInlineStructLoopNotKeyed2::"TableTest2"', + .. '::TestTableTestInlineStructLoopNotKeyed2::"TableTest2"', name = '"TableTest2"', path = test_filepath, type = "test", @@ -259,7 +259,7 @@ describe("Discovery of test positions", function() { { id = test_filepath - .. '::TestSubTestTableTestInlineStructLoop::"SubTest"', + .. '::TestSubTestTableTestInlineStructLoop::"SubTest"', name = '"SubTest"', path = test_filepath, type = "test", @@ -267,7 +267,7 @@ describe("Discovery of test positions", function() { { id = test_filepath - .. '::TestSubTestTableTestInlineStructLoop::"SubTest"::"TableTest1"', + .. '::TestSubTestTableTestInlineStructLoop::"SubTest"::"TableTest1"', name = '"TableTest1"', path = test_filepath, type = "test", @@ -276,7 +276,7 @@ describe("Discovery of test positions", function() { { id = test_filepath - .. '::TestSubTestTableTestInlineStructLoop::"SubTest"::"TableTest2"', + .. '::TestSubTestTableTestInlineStructLoop::"SubTest"::"TableTest2"', name = '"TableTest2"', path = test_filepath, type = "test", @@ -308,12 +308,18 @@ describe("Discovery of test positions", function() }, }, }, + { { + id = "/Users/yuriy/src/neotest-golang/tests/go/positions_test.go::TestStructNotTableTest", + name = "TestStructNotTableTest", + path = "/Users/yuriy/src/neotest-golang/tests/go/positions_test.go", + type = "test" + } }, } -- Act ---@type neotest.Tree local tree = - nio.tests.with_async_context(adapter.discover_positions, test_filepath) + nio.tests.with_async_context(adapter.discover_positions, test_filepath) -- Assert local result = tree:to_list() diff --git a/tests/go/positions_test.go b/tests/go/positions_test.go index a20835ff..d5222fe9 100644 --- a/tests/go/positions_test.go +++ b/tests/go/positions_test.go @@ -216,3 +216,21 @@ func TestTableTestMap(t *testing.T) { }) } } + +// Struct which is not a table test. +func TestStructNotTableTest(t *testing.T) { + type item struct { + id string + name string + } + items := []item{ + {"1", "one"}, + {"2", "two"}, + } + + for _, i := range items { + if i.id == "" { + t.Fail() + } + } +}