From f9741edefdecfe27d0d4726035732e81b9460529 Mon Sep 17 00:00:00 2001 From: Fredrik Averpil Date: Sat, 29 Jun 2024 16:15:40 +0200 Subject: [PATCH] feat: support table tests defined as in-loop anon structs --- lua/neotest-golang/ast.lua | 35 +++++++++++++++++++-- tests/go/positions_spec.lua | 63 ++++++++++++++++++++++++++++++++++++- tests/go/positions_test.go | 40 +++++++++++++++++++++++ 3 files changed, 135 insertions(+), 3 deletions(-) diff --git a/lua/neotest-golang/ast.lua b/lua/neotest-golang/ast.lua index 5418769d..3383c719 100644 --- a/lua/neotest-golang/ast.lua +++ b/lua/neotest-golang/ast.lua @@ -70,8 +70,39 @@ function M.detect_tests(file_path) field: (field_identifier) @test.field.name1 (#eq? @test.field.name @test.field.name1)))))))) - ;; query for list-in-loop table tests - ;; TODO: add this here + ;; 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 diff --git a/tests/go/positions_spec.lua b/tests/go/positions_spec.lua index 939cf053..dd5682b4 100644 --- a/tests/go/positions_spec.lua +++ b/tests/go/positions_spec.lua @@ -171,6 +171,67 @@ describe("Discovery of test positions", function() }, }, }, + { + { + 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 + .. '::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", @@ -216,7 +277,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) diff --git a/tests/go/positions_test.go b/tests/go/positions_test.go index f4bbeb0a..d257e233 100644 --- a/tests/go/positions_test.go +++ b/tests/go/positions_test.go @@ -112,6 +112,46 @@ 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 {