Skip to content

Commit 35d8bb3

Browse files
committed
testing: add TB, an interface common to T and B
R=golang-dev, kevlar, rsc, adg, r CC=golang-dev https://golang.org/cl/12962043
1 parent a41a5bb commit 35d8bb3

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

src/pkg/database/sql/sql_test.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,7 @@ const fakeDBName = "foo"
3939

4040
var chrisBirthday = time.Unix(123456789, 0)
4141

42-
type testOrBench interface {
43-
Fatalf(string, ...interface{})
44-
Errorf(string, ...interface{})
45-
Fatal(...interface{})
46-
Error(...interface{})
47-
Logf(string, ...interface{})
48-
}
49-
50-
func newTestDB(t testOrBench, name string) *DB {
42+
func newTestDB(t testing.TB, name string) *DB {
5143
db, err := Open("test", fakeDBName)
5244
if err != nil {
5345
t.Fatalf("Open: %v", err)
@@ -69,14 +61,14 @@ func newTestDB(t testOrBench, name string) *DB {
6961
return db
7062
}
7163

72-
func exec(t testOrBench, db *DB, query string, args ...interface{}) {
64+
func exec(t testing.TB, db *DB, query string, args ...interface{}) {
7365
_, err := db.Exec(query, args...)
7466
if err != nil {
7567
t.Fatalf("Exec of %q: %v", query, err)
7668
}
7769
}
7870

79-
func closeDB(t testOrBench, db *DB) {
71+
func closeDB(t testing.TB, db *DB) {
8072
if e := recover(); e != nil {
8173
fmt.Printf("Panic: %v\n", e)
8274
panic(e)
@@ -1061,7 +1053,7 @@ func TestStmtCloseOrder(t *testing.T) {
10611053
}
10621054
}
10631055

1064-
func manyConcurrentQueries(t testOrBench) {
1056+
func manyConcurrentQueries(t testing.TB) {
10651057
maxProcs, numReqs := 16, 500
10661058
if testing.Short() {
10671059
maxProcs, numReqs = 4, 50

src/pkg/testing/testing.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,31 @@ func decorate(s string) string {
196196
return buf.String()
197197
}
198198

199+
// TB is the interface common to T and B.
200+
type TB interface {
201+
Error(args ...interface{})
202+
Errorf(format string, args ...interface{})
203+
Fail()
204+
FailNow()
205+
Failed() bool
206+
Fatal(args ...interface{})
207+
Fatalf(format string, args ...interface{})
208+
Log(args ...interface{})
209+
Logf(format string, args ...interface{})
210+
Skip(args ...interface{})
211+
SkipNow()
212+
Skipf(format string, args ...interface{})
213+
Skipped() bool
214+
215+
// A private method to prevent users implementing the
216+
// interface and so future additions to it will not
217+
// violate Go 1 compatibility.
218+
private()
219+
}
220+
221+
var _ TB = (*T)(nil)
222+
var _ TB = (*B)(nil)
223+
199224
// T is a type passed to Test functions to manage test state and support formatted test logs.
200225
// Logs are accumulated during execution and dumped to standard error when done.
201226
type T struct {
@@ -204,6 +229,8 @@ type T struct {
204229
startParallel chan bool // Parallel tests will wait on this.
205230
}
206231

232+
func (c *common) private() {}
233+
207234
// Fail marks the function as having failed but continues execution.
208235
func (c *common) Fail() {
209236
c.mu.Lock()

0 commit comments

Comments
 (0)