From d11545d32eaa8cbfc11e64ddee4d5c130d554060 Mon Sep 17 00:00:00 2001 From: Fredrik Averpil Date: Mon, 9 Sep 2024 22:20:22 +0200 Subject: [PATCH] fix: update query for more unkeyed support --- lua/neotest-golang/query.lua | 56 +++++++++++++++++++++--------------- tests/go/positions_spec.lua | 26 +++++++++++++++++ tests/go/positions_test.go | 19 ++++++++++++ 3 files changed, 78 insertions(+), 23 deletions(-) diff --git a/lua/neotest-golang/query.lua b/lua/neotest-golang/query.lua index 9cb21b52..0e5b48cf 100644 --- a/lua/neotest-golang/query.lua +++ b/lua/neotest-golang/query.lua @@ -91,30 +91,40 @@ M.table_tests = [[ field: (field_identifier) @test.field.name1) (#eq? @test.field.name @test.field.name1)))))) ;; Query for table tests with inline structs (not keyed) - (for_statement - (range_clause - right: (composite_literal - type: (slice_type - element: (struct_type - (field_declaration_list - (field_declaration - name: (field_identifier) ;; the key of the struct's test name - type: (type_identifier) @field.type (#eq? @field.type "string"))))) - body: (literal_value - (literal_element - (literal_value - (literal_element - (interpreted_string_literal) @test.name) @test.definition))))) + (function_declaration + name: (identifier) + parameters: (parameter_list + (parameter_declaration + name: (identifier) + type: (pointer_type + (qualified_type + package: (package_identifier) + name: (type_identifier)))))) body: (block - (expression_statement - (call_expression - function: (selector_expression - operand: (identifier) - field: (field_identifier) @test.method (#match? @test.method "^Run$")) - arguments: (argument_list - (selector_expression - operand: (identifier) - field: (field_identifier))))))) + (for_statement + (range_clause + left: (expression_list + (identifier) + (identifier)) + 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") )))))))) + 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$"))))) ;; query for map table tests (block diff --git a/tests/go/positions_spec.lua b/tests/go/positions_spec.lua index fda5d32d..58f63849 100644 --- a/tests/go/positions_spec.lua +++ b/tests/go/positions_spec.lua @@ -223,6 +223,32 @@ describe("Discovery of test positions", function() }, }, }, + { + { + id = test_filepath .. "::TestTableTestInlineStructLoopNotKeyed2", + name = "TestTableTestInlineStructLoopNotKeyed2", + path = test_filepath, + type = "test", + }, + { + { + id = test_filepath + .. '::TestTableTestInlineStructLoopNotKeyed2::"TableTest1"', + name = '"TableTest1"', + path = test_filepath, + type = "test", + }, + }, + { + { + id = test_filepath + .. '::TestTableTestInlineStructLoopNotKeyed2::"TableTest2"', + name = '"TableTest2"', + path = test_filepath, + type = "test", + }, + }, + }, { { id = test_filepath .. "::TestSubTestTableTestInlineStructLoop", diff --git a/tests/go/positions_test.go b/tests/go/positions_test.go index 0d935b89..a20835ff 100644 --- a/tests/go/positions_test.go +++ b/tests/go/positions_test.go @@ -157,6 +157,25 @@ func TestTableTestInlineStructLoopNotKeyed(t *testing.T) { } } +func TestTableTestInlineStructLoopNotKeyed2(t *testing.T) { + testcases := []struct { + name string + x int + y int + expected int + }{ + {"TableTest1", 1, 2, 3}, // no `name:` to match on + {"TableTest2", 3, 4, 7}, + } + for _, tc := range testcases { + 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) {