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: table test inline struct loop, not keyed #156

Merged
merged 2 commits into from
Dec 7, 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
36 changes: 36 additions & 0 deletions lua/neotest-golang/query.lua
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,42 @@ 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)
(function_declaration
name: (identifier)
parameters: (parameter_list
(parameter_declaration
name: (identifier)
type: (pointer_type
(qualified_type
package: (package_identifier)
name: (type_identifier))))))
body: (block
(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
(short_var_declaration
Expand Down
52 changes: 52 additions & 0 deletions tests/go/positions_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,58 @@ 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 .. "::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",
Expand Down
38 changes: 38 additions & 0 deletions tests/go/positions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,44 @@ 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()
}
})
}
}

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) {
Expand Down
Loading