@@ -276,6 +276,13 @@ func getRunFuncSignature(t reflect.Type) (reflect.Type, error) {
276276// implement a Run method with a correct signature.
277277func (c * C ) Run (name string , f func (c * C )) bool {
278278 cFormat := c .getFormat ()
279+ // A wrapper for f that prepares its *C for the subtest
280+ callF := func (tb testing.TB ) {
281+ cSub := New (tb )
282+ defer cSub .Done ()
283+ cSub .SetFormat (cFormat )
284+ f (cSub )
285+ }
279286
280287 // Handle the various signatures of the Run method of c.TB
281288 switch tb := c .TB .(type ) {
@@ -286,30 +293,21 @@ func (c *C) Run(name string, f func(c *C)) bool {
286293 }:
287294 return tb .Run (name , func (t * testing.T ) {
288295 t .Helper ()
289- cSub := New (t )
290- defer cSub .Done ()
291- cSub .SetFormat (cFormat )
292- f (cSub )
296+ callF (t )
293297 })
294298
295299 // *testing.B
296300 case interface {
297301 Run (string , func (* testing.B )) bool
298302 }:
299303 return tb .Run (name , func (b * testing.B ) {
300- cSub := New (b )
301- defer cSub .Done ()
302- cSub .SetFormat (cFormat )
303- f (cSub )
304+ callF (b )
304305 })
305306
306307 // *quicktest.C
307308 case interface { Run (string , func (* C )) bool }:
308309 return tb .Run (name , func (c * C ) {
309- cSub := New (c )
310- defer cSub .Done ()
311- cSub .SetFormat (cFormat )
312- f (cSub )
310+ callF (c )
313311 })
314312
315313 // any testing.TB, by using reflect
@@ -320,10 +318,7 @@ func (c *C) Run(name string, f func(c *C)) bool {
320318 }
321319
322320 fv := reflect .MakeFunc (farg , func (args []reflect.Value ) []reflect.Value {
323- cSub := New (args [0 ].Interface ().(testing.TB ))
324- defer cSub .Done ()
325- cSub .SetFormat (cFormat )
326- f (cSub )
321+ callF (args [0 ].Interface ().(testing.TB ))
327322 return nil
328323 })
329324
0 commit comments