Skip to content

Commit

Permalink
Fix false positives on structs
Browse files Browse the repository at this point in the history
  • Loading branch information
letmein committed Dec 19, 2024
1 parent 3565f31 commit 969db94
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 67 deletions.
130 changes: 78 additions & 52 deletions lua/neotest-golang/query.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,74 +59,100 @@ M.table_tests = [[
;; query for list table tests (wrapped in loop)
(for_statement
(range_clause
left: (expression_list
left: (expression_list
(identifier)
(identifier) @test.case )
right: (composite_literal
type: (slice_type
element: (struct_type
(field_declaration_list
(field_declaration
name: (field_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
(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)
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 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
(block
(short_var_declaration
left: (expression_list (identifier) @test.cases)
right: (expression_list
(composite_literal
body: (literal_value
(literal_element
(literal_value
(literal_element (interpreted_string_literal) @test.name)
(literal_element)) @test.definition)))))
(for_statement
(range_clause
left: (expression_list
(identifier)
(identifier))
right: (composite_literal
left: (expression_list
((identifier) @test.key.name)
((identifier) @test.case))
right: (identifier) @test.cases1
(#eq? @test.cases @test.cases1))
body: (block
(expression_statement
(call_expression
function: (selector_expression
field: (field_identifier) @test.method)
(#match? @test.method "^Run$")
arguments: (argument_list
(selector_expression
operand: (identifier) @test.case1
(#eq? @test.case @test.case1))))))))
;; Query for table tests with inline structs (not keyed, 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) ; unkeyed field for test name
type: (type_identifier) @field.type (#eq? @field.type "string") ))))))))
element: (struct_type
(field_declaration_list
(field_declaration
name: (field_identifier) @test.field.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$")))))
(literal_element)) @test.definition))))
body: (block
(expression_statement
(call_expression
function: (selector_expression
field: (field_identifier) @test.method)
(#match? @test.method "^Run$")
arguments: (argument_list
(selector_expression
operand: (identifier) @test.case1
(#eq? @test.case @test.case1)
field: (field_identifier) @test.field.name1
(#eq? @test.field.name @test.field.name1)))))))
;; query for map table tests
;; query for map table tests
(block
(short_var_declaration
left: (expression_list
Expand Down Expand Up @@ -166,8 +192,8 @@ function M.detect_tests(file_path)
if options.get().testify_enabled == true then
-- detect receiver types (as namespaces) and test methods.
query = query
.. testify.query.namespace_query
.. testify.query.test_method_query
.. testify.query.namespace_query
.. testify.query.test_method_query
end

---@type neotest.Tree
Expand Down
36 changes: 21 additions & 15 deletions tests/go/positions_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ describe("Discovery of test positions", function()
{
{
id = test_filepath
.. '::TestSubTestTableTestStruct::"SubTest"::"TableTest1"',
.. '::TestSubTestTableTestStruct::"SubTest"::"TableTest1"',
name = '"TableTest1"',
path = test_filepath,
type = "test",
Expand All @@ -104,7 +104,7 @@ describe("Discovery of test positions", function()
{
{
id = test_filepath
.. '::TestSubTestTableTestStruct::"SubTest"::"TableTest2"',
.. '::TestSubTestTableTestStruct::"SubTest"::"TableTest2"',
name = '"TableTest2"',
path = test_filepath,
type = "test",
Expand Down Expand Up @@ -146,15 +146,15 @@ describe("Discovery of test positions", function()
{
{
id = test_filepath
.. '::TestSubTestTableTestInlineStruct::"SubTest"',
.. '::TestSubTestTableTestInlineStruct::"SubTest"',
name = '"SubTest"',
path = test_filepath,
type = "test",
},
{
{
id = test_filepath
.. '::TestSubTestTableTestInlineStruct::"SubTest"::"TableTest1"',
.. '::TestSubTestTableTestInlineStruct::"SubTest"::"TableTest1"',
name = '"TableTest1"',
path = test_filepath,
type = "test",
Expand All @@ -163,7 +163,7 @@ describe("Discovery of test positions", function()
{
{
id = test_filepath
.. '::TestSubTestTableTestInlineStruct::"SubTest"::"TableTest2"',
.. '::TestSubTestTableTestInlineStruct::"SubTest"::"TableTest2"',
name = '"TableTest2"',
path = test_filepath,
type = "test",
Expand All @@ -181,7 +181,7 @@ describe("Discovery of test positions", function()
{
{
id = test_filepath
.. '::TestTableTestInlineStructLoop::"TableTest1"',
.. '::TestTableTestInlineStructLoop::"TableTest1"',
name = '"TableTest1"',
path = test_filepath,
type = "test",
Expand All @@ -190,7 +190,7 @@ describe("Discovery of test positions", function()
{
{
id = test_filepath
.. '::TestTableTestInlineStructLoop::"TableTest2"',
.. '::TestTableTestInlineStructLoop::"TableTest2"',
name = '"TableTest2"',
path = test_filepath,
type = "test",
Expand All @@ -207,7 +207,7 @@ describe("Discovery of test positions", function()
{
{
id = test_filepath
.. '::TestTableTestInlineStructLoopNotKeyed::"TableTest1"',
.. '::TestTableTestInlineStructLoopNotKeyed::"TableTest1"',
name = '"TableTest1"',
path = test_filepath,
type = "test",
Expand All @@ -216,7 +216,7 @@ describe("Discovery of test positions", function()
{
{
id = test_filepath
.. '::TestTableTestInlineStructLoopNotKeyed::"TableTest2"',
.. '::TestTableTestInlineStructLoopNotKeyed::"TableTest2"',
name = '"TableTest2"',
path = test_filepath,
type = "test",
Expand All @@ -233,7 +233,7 @@ describe("Discovery of test positions", function()
{
{
id = test_filepath
.. '::TestTableTestInlineStructLoopNotKeyed2::"TableTest1"',
.. '::TestTableTestInlineStructLoopNotKeyed2::"TableTest1"',
name = '"TableTest1"',
path = test_filepath,
type = "test",
Expand All @@ -242,7 +242,7 @@ describe("Discovery of test positions", function()
{
{
id = test_filepath
.. '::TestTableTestInlineStructLoopNotKeyed2::"TableTest2"',
.. '::TestTableTestInlineStructLoopNotKeyed2::"TableTest2"',
name = '"TableTest2"',
path = test_filepath,
type = "test",
Expand All @@ -259,15 +259,15 @@ describe("Discovery of test positions", function()
{
{
id = test_filepath
.. '::TestSubTestTableTestInlineStructLoop::"SubTest"',
.. '::TestSubTestTableTestInlineStructLoop::"SubTest"',
name = '"SubTest"',
path = test_filepath,
type = "test",
},
{
{
id = test_filepath
.. '::TestSubTestTableTestInlineStructLoop::"SubTest"::"TableTest1"',
.. '::TestSubTestTableTestInlineStructLoop::"SubTest"::"TableTest1"',
name = '"TableTest1"',
path = test_filepath,
type = "test",
Expand All @@ -276,7 +276,7 @@ describe("Discovery of test positions", function()
{
{
id = test_filepath
.. '::TestSubTestTableTestInlineStructLoop::"SubTest"::"TableTest2"',
.. '::TestSubTestTableTestInlineStructLoop::"SubTest"::"TableTest2"',
name = '"TableTest2"',
path = test_filepath,
type = "test",
Expand Down Expand Up @@ -308,12 +308,18 @@ describe("Discovery of test positions", function()
},
},
},
{ {
id = "/Users/yuriy/src/neotest-golang/tests/go/positions_test.go::TestStructNotTableTest",
name = "TestStructNotTableTest",
path = "/Users/yuriy/src/neotest-golang/tests/go/positions_test.go",
type = "test"
} },
}

-- Act
---@type neotest.Tree
local tree =
nio.tests.with_async_context(adapter.discover_positions, test_filepath)
nio.tests.with_async_context(adapter.discover_positions, test_filepath)

-- Assert
local result = tree:to_list()
Expand Down
18 changes: 18 additions & 0 deletions tests/go/positions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,21 @@ func TestTableTestMap(t *testing.T) {
})
}
}

// Struct which is not a table test.
func TestStructNotTableTest(t *testing.T) {
type item struct {
id string
name string
}
items := []item{
{"1", "one"},
{"2", "two"},
}

for _, i := range items {
if i.id == "" {
t.Fail()
}
}
}

0 comments on commit 969db94

Please sign in to comment.