-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
t.go
50 lines (43 loc) · 946 Bytes
/
t.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package justest
import (
"fmt"
"testing"
)
type T interface {
Name() string
Cleanup(f func())
Fatalf(format string, args ...any)
Failed() bool
Log(args ...any)
Logf(format string, args ...any)
}
type HasParent interface{ GetParent() T }
type noOpHelper struct{}
//go:noinline
func (n *noOpHelper) Helper() {}
//go:noinline
func GetHelper(t T) interface{ Helper() } {
var candidate any = t
for candidate != nil {
if h, ok := candidate.(interface{ Helper() }); ok {
return h
} else if hp, ok := candidate.(HasParent); ok {
candidate = hp.GetParent()
} else {
panic(fmt.Sprintf("unsupported T instance: %+v (%T)", candidate, candidate))
}
}
return &noOpHelper{}
}
//go:noinline
func GetRoot(t T) *testing.T {
for {
if hp, ok := t.(HasParent); ok {
t = hp.GetParent()
} else if rt, ok := t.(*testing.T); ok {
return rt
} else {
panic(fmt.Sprintf("unsupported T instance: %+v (%T)", t, t))
}
}
}