From e4789b2adc563c38cb4bb817604eaaf9741c7af6 Mon Sep 17 00:00:00 2001 From: Fredrik Averpil Date: Fri, 24 Jan 2025 12:25:33 +0100 Subject: [PATCH] fix: testify operand not recognized --- lua/neotest-golang/features/testify/query.lua | 9 ++++++ tests/go/internal/testify/positions_spec.lua | 32 +++++++++++++++++++ tests/go/internal/testify/positions_test.go | 18 +++++++++++ 3 files changed, 59 insertions(+) diff --git a/lua/neotest-golang/features/testify/query.lua b/lua/neotest-golang/features/testify/query.lua index 4ab9b353..896a4cd2 100644 --- a/lua/neotest-golang/features/testify/query.lua +++ b/lua/neotest-golang/features/testify/query.lua @@ -17,6 +17,15 @@ M.namespace_query = [[ type: (pointer_type (type_identifier) @namespace.name )))) @namespace.definition name: (field_identifier) @test_function (#match? @test_function "^(Test|Example)") (#not-match? @test.name "^TestMain$") + + ; query for subtest, like t.Run() + (call_expression + function: (selector_expression + operand: (identifier) @test.operand (#match? @test.operand "^(s|suite)$") + field: (field_identifier) @test.method) (#match? @test.method "^Run$") + arguments: (argument_list . (interpreted_string_literal) @test.name)) + @test.definition + ]] M.test_method_query = [[ diff --git a/tests/go/internal/testify/positions_spec.lua b/tests/go/internal/testify/positions_spec.lua index 7e9c5c0b..97215237 100644 --- a/tests/go/internal/testify/positions_spec.lua +++ b/tests/go/internal/testify/positions_spec.lua @@ -124,6 +124,38 @@ describe("With testify_enabled=true", function() type = "test", }, }, + { + { + id = test_filepath .. "::TestExampleTestSuite::TestSubTestOperand1", + name = "TestSubTestOperand1", + path = test_filepath .. "", + type = "test", + }, + { + { + id = '/Users/fredrik/code/public/neotest-golang/tests/go/internal/testify/positions_test.go::TestExampleTestSuite::TestSubTestOperand1::"subtest"', + name = '"subtest"', + path = test_filepath .. "", + type = "test", + }, + }, + }, + { + { + id = test_filepath .. "::TestExampleTestSuite::TestSubTestOperand2", + name = "TestSubTestOperand2", + path = test_filepath .. "", + type = "test", + }, + { + { + id = '/Users/fredrik/code/public/neotest-golang/tests/go/internal/testify/positions_test.go::TestExampleTestSuite::TestSubTestOperand2::"subtest"', + name = '"subtest"', + path = test_filepath .. "", + type = "test", + }, + }, + }, }, { { diff --git a/tests/go/internal/testify/positions_test.go b/tests/go/internal/testify/positions_test.go index 1433aca1..5da534af 100644 --- a/tests/go/internal/testify/positions_test.go +++ b/tests/go/internal/testify/positions_test.go @@ -76,3 +76,21 @@ func TestTrivial(t *testing.T) { func (suite *OtherTestSuite) TestOther() { assert.Equal(suite.T(), 5, suite.VariableThatShouldStartAtFive) } + +// -------------------------------------------------------------------- + +// // A test method with a subttest, using operand suite. +func (suite *ExampleTestSuite) TestSubTestOperand1() { + suite.Run("subtest", func() { + suite.VariableThatShouldStartAtFive = 10 + assert.Equal(suite.T(), 10, suite.VariableThatShouldStartAtFive) + }) +} + +// // A test method with a subttest, using operand s. +func (s *ExampleTestSuite) TestSubTestOperand2() { + s.Run("subtest", func() { + s.VariableThatShouldStartAtFive = 10 + assert.Equal(s.T(), 10, s.VariableThatShouldStartAtFive) + }) +}