From 14aa09bf564d8f3056d4f73778fbabdf07df94f8 Mon Sep 17 00:00:00 2001 From: Fredrik Averpil Date: Thu, 1 Aug 2024 20:38:52 +0200 Subject: [PATCH] feat: table test inline struct loop, not keyed --- lua/neotest-golang/query.lua | 26 ++++++++++++++++++++++++++ tests/go/positions_spec.lua | 26 ++++++++++++++++++++++++++ tests/go/positions_test.go | 19 +++++++++++++++++++ 3 files changed, 71 insertions(+) diff --git a/lua/neotest-golang/query.lua b/lua/neotest-golang/query.lua index 4b51371a..b8db8026 100644 --- a/lua/neotest-golang/query.lua +++ b/lua/neotest-golang/query.lua @@ -90,6 +90,32 @@ M.table_tests = [[ operand: (identifier) 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) ;; unkeyed 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))))) + body: (block + (expression_statement + (call_expression + function: (selector_expression + operand: (identifier) + field: (field_identifier)) + arguments: (argument_list + (selector_expression + operand: (identifier) + field: (field_identifier))))))) + ;; query for map table tests (block (short_var_declaration diff --git a/tests/go/positions_spec.lua b/tests/go/positions_spec.lua index d526feae..fda5d32d 100644 --- a/tests/go/positions_spec.lua +++ b/tests/go/positions_spec.lua @@ -197,6 +197,32 @@ describe("Discovery of test positions", function() }, }, }, + { + { + id = test_filepath .. "::TestTableTestInlineStructLoopNotKeyed", + name = "TestTableTestInlineStructLoopNotKeyed", + path = test_filepath, + type = "test", + }, + { + { + id = test_filepath + .. '::TestTableTestInlineStructLoopNotKeyed::"TableTest1"', + name = '"TableTest1"', + path = test_filepath, + type = "test", + }, + }, + { + { + id = test_filepath + .. '::TestTableTestInlineStructLoopNotKeyed::"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 5b808103..d9503d2b 100644 --- a/tests/go/positions_test.go +++ b/tests/go/positions_test.go @@ -131,6 +131,25 @@ func TestTableTestInlineStructLoop(t *testing.T) { } } +// Table test defined as anonymous struct in loop without named fields. +func TestTableTestInlineStructLoopNotKeyed(t *testing.T) { + for _, tc := range []struct { + name string + x int + y int + expected int + }{ + {"TableTest1", 1, 2, 3}, // no `name:` to match on + {"TableTest2", 3, 4, 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) {