@@ -109,27 +109,44 @@ export class GoRunTestCodeLensProvider extends GoBaseCodeLensProvider {
109
109
return codelens ;
110
110
}
111
111
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 .* ?) \( / ;
113
115
114
116
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' ;
116
122
117
123
codelens . push (
118
124
new CodeLens ( f . range , {
119
125
title : 'run test' ,
120
126
command : 'go.test.cursor' ,
121
- arguments : [ { functionName } ]
127
+ arguments : [ {
128
+ functionName : functionName ,
129
+ isTestSuite : ! ! testSuiteMatch
130
+ } ]
122
131
} ) ,
123
132
new CodeLens ( f . range , {
124
133
title : 'debug test' ,
125
134
command : 'go.debug.cursor' ,
126
- arguments : [ { functionName } ]
135
+ arguments : [ {
136
+ functionName : functionName ,
137
+ isTestSuite : ! ! testSuiteMatch
138
+ } ]
127
139
} )
128
140
) ;
129
141
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
+
130
147
for ( let i = f . range . start . line ; i < f . range . end . line ; i ++ ) {
131
148
const line = document . lineAt ( i ) ;
132
- const simpleMatch = line . text . match ( simpleRunRegex ) ;
149
+ const simpleMatch = line . text . match ( testCaseRegex ) ;
133
150
134
151
// BUG: this does not handle nested subtests. This should
135
152
// be solved once codelens is handled by gopls and not by
@@ -141,12 +158,20 @@ export class GoRunTestCodeLensProvider extends GoBaseCodeLensProvider {
141
158
new CodeLens ( line . range , {
142
159
title : 'run test' ,
143
160
command : 'go.subtest.cursor' ,
144
- arguments : [ { functionName, subTestName } ]
161
+ arguments : [ {
162
+ functionName : functionName ,
163
+ subTestName : subTestName ,
164
+ isTestSuite : ! ! testSuiteMatch
165
+ } ]
145
166
} ) ,
146
167
new CodeLens ( line . range , {
147
168
title : 'debug test' ,
148
169
command : 'go.debug.subtest.cursor' ,
149
- arguments : [ { functionName, subTestName } ]
170
+ arguments : [ {
171
+ functionName : functionName ,
172
+ subTestName : subTestName ,
173
+ isTestSuite : ! ! testSuiteMatch
174
+ } ]
150
175
} )
151
176
) ;
152
177
}
0 commit comments