Skip to content

Commit

Permalink
feat: support table tests defined as in-loop anon structs
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikaverpil committed Jun 29, 2024
1 parent 7158e70 commit f9741ed
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 3 deletions.
35 changes: 33 additions & 2 deletions lua/neotest-golang/ast.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
63 changes: 62 additions & 1 deletion tests/go/positions_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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)
40 changes: 40 additions & 0 deletions tests/go/positions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit f9741ed

Please sign in to comment.