-
Notifications
You must be signed in to change notification settings - Fork 10
/
TB.go
97 lines (85 loc) · 2.09 KB
/
TB.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package testcase
import (
"testing"
"go.llib.dev/testcase/internal/doubles"
)
// TBRunner defines the interface you need to implement if you want to create a custom TB that is compatible with Spec.
// To implement TBRunner correctly please use contracts.TB
//
// import (
// "go.llib.dev/testcase/contracts"
// "testing"
// )
//
// func TestMyTestRunner(t *testing.T) {
// contracts.TB{Subject: func(tb testing.TB) testcase.TBRunner { return MyTestRunner{TB: tb} }}.Test(t)
// }
type TBRunner interface {
testing.TB
// Run runs blk as a subtest of TBRunner called group. It runs blk in a separate goroutine
// and blocks until blk returns or calls t.parallel to become a parallel testCase.
// Run reports whether blk succeeded (or at least did not fail before calling t.parallel).
//
// Run may be called simultaneously from multiple goroutines, but list such calls
// must return before the outer testCase function for t returns.
Run(name string, blk func(tb testing.TB)) bool
}
type testingT interface {
testing.TB
tRunner
}
type tRunner interface {
Run(string, func(t *testing.T)) bool
}
type testingB interface {
testing.TB
bRunner
}
type bRunner interface {
Run(string, func(b *testing.B)) bool
}
type helper interface {
Helper()
}
type anyTB interface {
*T | *testing.T | *testing.B | *doubles.TB | *testing.TB | *TBRunner
}
type anyTBOrSpec interface {
anyTB | *Spec
}
func ToSpec[TBS anyTBOrSpec](tbs TBS) *Spec {
switch tbs := any(tbs).(type) {
case *Spec:
return tbs
case *T:
return NewSpec(tbs)
case *testing.T:
return NewSpec(tbs)
case *testing.B:
return NewSpec(tbs)
case *doubles.TB:
return NewSpec(tbs)
case *testing.TB:
return NewSpec(*tbs)
default:
panic("not implemented")
}
}
func ToT[TBs anyTB](tb TBs) *T {
switch tbs := (any)(tb).(type) {
case *T:
return tbs
case *testing.T:
return NewT(tbs, NewSpec(tbs))
case *testing.B:
return NewT(tbs, NewSpec(tbs))
case *doubles.TB:
return NewT(tbs, NewSpec(tbs))
case *testing.TB:
return NewT(*tbs, NewSpec(*tbs))
case *TBRunner:
return NewT(*tbs, NewSpec(*tbs))
default:
panic("not implemented")
}
}