|
| 1 | +// Licensed under the MIT license, see LICENSE file for details. |
| 2 | + |
| 3 | +//go:build go1.18 |
| 4 | +// +build go1.18 |
| 5 | + |
| 6 | +package quicktest |
| 7 | + |
| 8 | +import "testing" |
| 9 | + |
| 10 | +// fastRun implements c.Run for some known types. |
| 11 | +// It returns the result of calling c.Run and also reports |
| 12 | +// whether it was able to do so. |
| 13 | +func fastRun(c *C, name string, f func(c *C)) (bool, bool) { |
| 14 | + switch t := c.TB.(type) { |
| 15 | + case runner[*testing.T]: |
| 16 | + return fastRun1(c, name, f, t), true |
| 17 | + case runner[*testing.B]: |
| 18 | + return fastRun1(c, name, f, t), true |
| 19 | + case runner[*C]: |
| 20 | + return fastRun1(c, name, f, t), true |
| 21 | + case runner[testing.TB]: |
| 22 | + // This case is here mostly for benchmarking, because |
| 23 | + // it's hard to create a working concrete instance of *testing.T |
| 24 | + // that isn't part of the outer tests. |
| 25 | + return fastRun1(c, name, f, t), true |
| 26 | + } |
| 27 | + return false, false |
| 28 | +} |
| 29 | + |
| 30 | +type runner[T any] interface { |
| 31 | + Run(name string, f func(T)) bool |
| 32 | +} |
| 33 | + |
| 34 | +func fastRun1[T testing.TB](c *C, name string, f func(*C), t runner[T]) bool { |
| 35 | + return t.Run(name, func(t2 T) { |
| 36 | + c2 := New(t2) |
| 37 | + defer c2.Done() |
| 38 | + c2.SetFormat(c.getFormat()) |
| 39 | + f(c2) |
| 40 | + }) |
| 41 | +} |
0 commit comments