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 0fa5421
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 50 deletions.
126 changes: 76 additions & 50 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
6 changes: 6 additions & 0 deletions tests/go/positions_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,12 @@ 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
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 0fa5421

Please sign in to comment.