@@ -109,27 +109,44 @@ export class GoRunTestCodeLensProvider extends GoBaseCodeLensProvider {
109109 return codelens ;
110110 }
111111
112- const simpleRunRegex = / t .R u n \( " ( [ ^ " ] + ) " , / ;
112+ // Captures receiver name and function name.
113+ // reason is: function name coming from f.name is not compatible with struct methods.
114+ const testSuiteRegex = / f u n c \( ( .* ?) .* ?\) ( T e s t .* ?) \( / ;
113115
114116 for ( const f of testFunctions ) {
115- const functionName = f . name ;
117+ let functionName = f . name ;
118+ const line = document . lineAt ( f . range . start . line ) ;
119+ const testSuiteMatch = line . text . match ( testSuiteRegex ) ;
120+ if ( testSuiteMatch ) functionName = testSuiteMatch [ 2 ]
121+ var receiverName = testSuiteMatch ? testSuiteMatch [ 1 ] : 't' ;
116122
117123 codelens . push (
118124 new CodeLens ( f . range , {
119125 title : 'run test' ,
120126 command : 'go.test.cursor' ,
121- arguments : [ { functionName } ]
127+ arguments : [ {
128+ functionName : functionName ,
129+ isTestSuite : ! ! testSuiteMatch
130+ } ]
122131 } ) ,
123132 new CodeLens ( f . range , {
124133 title : 'debug test' ,
125134 command : 'go.debug.cursor' ,
126- arguments : [ { functionName } ]
135+ arguments : [ {
136+ functionName : functionName ,
137+ isTestSuite : ! ! testSuiteMatch
138+ } ]
127139 } )
128140 ) ;
129141
142+ // Dynamic regex for capturing receiverName.Run("testName", ...)
143+ // receiver name is either t for normal test functions or the receiver of a test suite.
144+ // example: func (s *testSuite) TestFunc() // Returns 's'
145+ let testCaseRegex = new RegExp ( receiverName + "\.Run\\(\"([^\"]+)\"," ) ;
146+
130147 for ( let i = f . range . start . line ; i < f . range . end . line ; i ++ ) {
131148 const line = document . lineAt ( i ) ;
132- const simpleMatch = line . text . match ( simpleRunRegex ) ;
149+ const simpleMatch = line . text . match ( testCaseRegex ) ;
133150
134151 // BUG: this does not handle nested subtests. This should
135152 // be solved once codelens is handled by gopls and not by
@@ -141,12 +158,20 @@ export class GoRunTestCodeLensProvider extends GoBaseCodeLensProvider {
141158 new CodeLens ( line . range , {
142159 title : 'run test' ,
143160 command : 'go.subtest.cursor' ,
144- arguments : [ { functionName, subTestName } ]
161+ arguments : [ {
162+ functionName : functionName ,
163+ subTestName : subTestName ,
164+ isTestSuite : ! ! testSuiteMatch
165+ } ]
145166 } ) ,
146167 new CodeLens ( line . range , {
147168 title : 'debug test' ,
148169 command : 'go.debug.subtest.cursor' ,
149- arguments : [ { functionName, subTestName } ]
170+ arguments : [ {
171+ functionName : functionName ,
172+ subTestName : subTestName ,
173+ isTestSuite : ! ! testSuiteMatch
174+ } ]
150175 } )
151176 ) ;
152177 }
0 commit comments