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 ae76e62
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 14 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
71 changes: 62 additions & 9 deletions tests/go/positions_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -173,31 +173,84 @@ describe("Discovery of test positions", function()
},
{
{
id = test_filepath .. "::TestTableTestMap",
name = "TestTableTestMap",
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 .. '::TestTableTestMap::"add 1+1"',
name = '"add 1+1"',
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",
name = "TestTableTestMap",
path = test_filepath,
type = "test",
},
{
{
id = test_filepath .. '::TestTableTestMap::"add 2+2"',
name = '"add 2+2"',
id = test_filepath .. '::TestTableTestMap::"TableTest1"',
name = '"TableTest1"',
path = test_filepath,
type = "test",
},
},
{
{
id = test_filepath .. '::TestTableTestMap::"add 5+5"',
name = '"add 5+5"',
id = test_filepath .. '::TestTableTestMap::"TableTest2"',
name = '"TableTest2"',
path = test_filepath,
type = "test",
},
Expand All @@ -216,7 +269,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)
45 changes: 42 additions & 3 deletions tests/go/positions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,55 @@ 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 {
a int
b int
want int
}{
"add 1+1": {a: 1, b: 1, want: 2},
"add 2+2": {a: 2, b: 2, want: 4},
"add 5+5": {a: 5, b: 5, want: 10},
"TableTest1": {a: 1, b: 1, want: 2},
"TableTest2": {a: 2, b: 2, want: 4},
}
for name, tc := range tt {
t.Run(name, func(t *testing.T) {
Expand Down

0 comments on commit ae76e62

Please sign in to comment.