Skip to content

Commit 5b2d586

Browse files
authored
Merge pull request #100 from maxatome/tb
td.TestingFT → testing.TB
2 parents d18326d + bb70138 commit 5b2d586

15 files changed

+328
-152
lines changed

helpers/tdhttp/http.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,21 @@ type Response struct {
2727
Body interface{} // Body is the expected body (expected to be empty if nil)
2828
}
2929

30-
func cmpMarshaledResponse(tt td.TestingFT,
30+
func cmpMarshaledResponse(tb testing.TB,
3131
req *http.Request,
3232
handler func(w http.ResponseWriter, r *http.Request),
3333
acceptEmptyBody bool,
3434
unmarshal func([]byte, interface{}) error,
3535
expectedResp Response,
3636
args ...interface{},
3737
) bool {
38-
tt.Helper()
38+
tb.Helper()
3939

4040
if testName := tdutil.BuildTestName(args...); testName != "" {
41-
tt.Log(testName)
41+
tb.Log(testName)
4242
}
4343

44-
t := td.NewT(tt)
44+
t := td.NewT(tb)
4545
defer t.AnchorsPersistTemporarily()()
4646

4747
ta := NewTestAPI(t, http.HandlerFunc(handler)).Request(req)
@@ -78,7 +78,7 @@ func cmpMarshaledResponse(tt td.TestingFT,
7878
// It returns true if the tests succeed, false otherwise.
7979
//
8080
// See TestAPI type and its methods for more flexible tests.
81-
func CmpMarshaledResponse(t td.TestingFT,
81+
func CmpMarshaledResponse(t testing.TB,
8282
req *http.Request,
8383
handler func(w http.ResponseWriter, r *http.Request),
8484
unmarshal func([]byte, interface{}) error,
@@ -118,7 +118,7 @@ func CmpMarshaledResponse(t td.TestingFT,
118118
// a string.
119119
//
120120
// See TestAPI type and its methods for more flexible tests.
121-
func CmpResponse(t td.TestingFT,
121+
func CmpResponse(t testing.TB,
122122
req *http.Request,
123123
handler func(w http.ResponseWriter, r *http.Request),
124124
expectedResp Response,
@@ -187,7 +187,7 @@ func CmpResponse(t td.TestingFT,
187187
// explicitly.
188188
//
189189
// See TestAPI type and its methods for more flexible tests.
190-
func CmpJSONResponse(t td.TestingFT,
190+
func CmpJSONResponse(t testing.TB,
191191
req *http.Request,
192192
handler func(w http.ResponseWriter, r *http.Request),
193193
expectedResp Response,
@@ -240,7 +240,7 @@ func CmpJSONResponse(t td.TestingFT,
240240
// explicitly.
241241
//
242242
// See TestAPI type and its methods for more flexible tests.
243-
func CmpXMLResponse(t td.TestingFT,
243+
func CmpXMLResponse(t testing.TB,
244244
req *http.Request,
245245
handler func(w http.ResponseWriter, r *http.Request),
246246
expectedResp Response,

helpers/tdhttp/http_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,12 @@ func TestCmpResponse(tt *testing.T) {
201201
},
202202
},
203203
} {
204-
t.RunT(curTest.Name,
204+
t.Run(curTest.Name,
205205
func(t *td.T) {
206206
testCmpResponse(t, tdhttp.CmpResponse, "CmpResponse", curTest)
207207
})
208208

209-
t.RunT(curTest.Name+" TestAPI",
209+
t.Run(curTest.Name+" TestAPI",
210210
func(t *td.T) {
211211
testTestAPI(t, (*tdhttp.TestAPI).CmpBody, "CmpBody", curTest)
212212
})
@@ -265,12 +265,12 @@ func TestCmpJSONResponse(tt *testing.T) {
265265
},
266266
},
267267
} {
268-
t.RunT(curTest.Name,
268+
t.Run(curTest.Name,
269269
func(t *td.T) {
270270
testCmpResponse(t, tdhttp.CmpJSONResponse, "CmpJSONResponse", curTest)
271271
})
272272

273-
t.RunT(curTest.Name+" TestAPI",
273+
t.Run(curTest.Name+" TestAPI",
274274
func(t *td.T) {
275275
testTestAPI(t, (*tdhttp.TestAPI).CmpJSONBody, "CmpJSONBody", curTest)
276276
})
@@ -409,12 +409,12 @@ func TestCmpXMLResponse(tt *testing.T) {
409409
},
410410
},
411411
} {
412-
t.RunT(curTest.Name,
412+
t.Run(curTest.Name,
413413
func(t *td.T) {
414414
testCmpResponse(t, tdhttp.CmpXMLResponse, "CmpXMLResponse", curTest)
415415
})
416416

417-
t.RunT(curTest.Name+" TestAPI",
417+
t.Run(curTest.Name+" TestAPI",
418418
func(t *td.T) {
419419
testTestAPI(t, (*tdhttp.TestAPI).CmpXMLBody, "CmpXMLBody", curTest)
420420
})
@@ -445,7 +445,7 @@ func testLogs(t *td.T, mockT *tdutil.T, curTest CmpResponseTest) {
445445
}
446446

447447
func testCmpResponse(t *td.T,
448-
cmp func(td.TestingFT, *http.Request, func(http.ResponseWriter, *http.Request), tdhttp.Response, ...interface{}) bool,
448+
cmp func(testing.TB, *http.Request, func(http.ResponseWriter, *http.Request), tdhttp.Response, ...interface{}) bool,
449449
cmpName string,
450450
curTest CmpResponseTest,
451451
) {

helpers/tdhttp/request_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
func TestNewRequest(tt *testing.T) {
2020
t := td.NewT(tt)
2121

22-
t.RunT("NewRequest", func(t *td.T) {
22+
t.Run("NewRequest", func(t *td.T) {
2323
req := tdhttp.NewRequest("GET", "/path", nil,
2424
"Foo", "Bar",
2525
"Zip", "Test")
@@ -30,7 +30,7 @@ func TestNewRequest(tt *testing.T) {
3030
})
3131
})
3232

33-
t.RunT("NewRequest last header value less", func(t *td.T) {
33+
t.Run("NewRequest last header value less", func(t *td.T) {
3434
req := tdhttp.NewRequest("GET", "/path", nil,
3535
"Foo", "Bar",
3636
"Zip")
@@ -41,7 +41,7 @@ func TestNewRequest(tt *testing.T) {
4141
})
4242
})
4343

44-
t.RunT("NewRequest header http.Header", func(t *td.T) {
44+
t.Run("NewRequest header http.Header", func(t *td.T) {
4545
req := tdhttp.NewRequest("GET", "/path", nil,
4646
http.Header{
4747
"Foo": []string{"Bar"},
@@ -54,7 +54,7 @@ func TestNewRequest(tt *testing.T) {
5454
})
5555
})
5656

57-
t.RunT("NewRequest header combined", func(t *td.T) {
57+
t.Run("NewRequest header combined", func(t *td.T) {
5858
req := tdhttp.NewRequest("GET", "/path", nil,
5959
"H1", "V1",
6060
http.Header{
@@ -70,7 +70,7 @@ func TestNewRequest(tt *testing.T) {
7070
})
7171
})
7272

73-
t.RunT("NewRequest header panic", func(t *td.T) {
73+
t.Run("NewRequest header panic", func(t *td.T) {
7474
t.CmpPanic(func() { tdhttp.NewRequest("GET", "/path", nil, "H", "V", true) },
7575
"headers... can only contains string and http.Header, not bool (@ headers[2])")
7676

@@ -176,7 +176,7 @@ type TestStruct struct {
176176
func TestNewJSONRequest(tt *testing.T) {
177177
t := td.NewT(tt)
178178

179-
t.RunT("NewJSONRequest", func(t *td.T) {
179+
t.Run("NewJSONRequest", func(t *td.T) {
180180
req := tdhttp.NewJSONRequest("GET", "/path",
181181
TestStruct{
182182
Name: "Bob",
@@ -194,7 +194,7 @@ func TestNewJSONRequest(tt *testing.T) {
194194
}
195195
})
196196

197-
t.RunT("NewJSONRequest panic", func(t *td.T) {
197+
t.Run("NewJSONRequest panic", func(t *td.T) {
198198
t.CmpPanic(
199199
func() { tdhttp.NewJSONRequest("GET", "/path", func() {}) },
200200
td.NotEmpty(),
@@ -261,7 +261,7 @@ func TestNewJSONRequest(tt *testing.T) {
261261
func TestNewXMLRequest(tt *testing.T) {
262262
t := td.NewT(tt)
263263

264-
t.RunT("NewXMLRequest", func(t *td.T) {
264+
t.Run("NewXMLRequest", func(t *td.T) {
265265
req := tdhttp.NewXMLRequest("GET", "/path",
266266
TestStruct{
267267
Name: "Bob",
@@ -279,7 +279,7 @@ func TestNewXMLRequest(tt *testing.T) {
279279
}
280280
})
281281

282-
t.RunT("NewXMLRequest panic", func(t *td.T) {
282+
t.Run("NewXMLRequest panic", func(t *td.T) {
283283
t.CmpPanic(
284284
func() { tdhttp.NewXMLRequest("GET", "/path", func() {}) },
285285
td.NotEmpty(),

helpers/tdhttp/test_api.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"reflect"
1818
"runtime"
1919
"strings"
20+
"testing"
2021
"time"
2122

2223
"github.com/maxatome/go-testdeep/helpers/tdutil"
@@ -57,9 +58,9 @@ type TestAPI struct {
5758
// ta.Get("/ping").
5859
// CmpStatus(200).
5960
// CmpBody("pong")
60-
func NewTestAPI(tt td.TestingFT, handler http.Handler) *TestAPI {
61+
func NewTestAPI(tb testing.TB, handler http.Handler) *TestAPI {
6162
return &TestAPI{
62-
t: td.NewT(tt),
63+
t: td.NewT(tb),
6364
handler: handler,
6465
}
6566
}

internal/test/types.go

Lines changed: 51 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package test
88

99
import (
1010
"fmt"
11+
"runtime"
1112
"testing"
1213
)
1314

@@ -43,70 +44,95 @@ func (t *TestingT) Helper() {
4344
// Do nothing
4445
}
4546

46-
// TestingFT is a type implementing td.TestingFT intended to be used
47-
// in tests.
48-
type TestingFT struct {
47+
// TestingTB is a type implementing testing.TB intended to be used in
48+
// tests.
49+
type TestingTB struct {
4950
TestingT
5051
name string
52+
testing.TB
53+
cleanup func()
54+
}
55+
56+
// NewTestingTB returns a new instance of *TestingTB.
57+
func NewTestingTB(name string) *TestingTB {
58+
return &TestingTB{name: name}
59+
}
60+
61+
// Cleanup mocks testing.T Cleanup method. Not thread-safe but we
62+
// don't care in tests.
63+
func (t *TestingTB) Cleanup(fn func()) {
64+
old := t.cleanup
65+
t.cleanup = func() {
66+
if old != nil {
67+
defer old()
68+
}
69+
fn()
70+
}
71+
runtime.SetFinalizer(t, func(t *TestingTB) { t.cleanup() })
5172
}
5273

53-
// NewTestingFT returns a new instance of *TestingFT.
54-
func NewTestingFT(name string) *TestingFT {
55-
return &TestingFT{name: name}
74+
// Fatal mocks testing.T Error method.
75+
func (t *TestingTB) Error(args ...interface{}) {
76+
t.TestingT.Error(args...)
5677
}
5778

5879
// Errorf mocks testing.T Errorf method.
59-
func (t *TestingFT) Errorf(format string, args ...interface{}) {
60-
t.Error(fmt.Sprintf(format, args...))
80+
func (t *TestingTB) Errorf(format string, args ...interface{}) {
81+
t.TestingT.Error(fmt.Sprintf(format, args...))
6182
}
6283

6384
// Fail mocks testing.T Fail method.
64-
func (t *TestingFT) Fail() {
85+
func (t *TestingTB) Fail() {
6586
t.HasFailed = true
6687
}
6788

6889
// FailNow mocks testing.T FailNow method.
69-
func (t *TestingFT) FailNow() {
90+
func (t *TestingTB) FailNow() {
7091
t.HasFailed = true
7192
t.IsFatal = true
7293
}
7394

7495
// Failed mocks testing.T Failed method.
75-
func (t *TestingFT) Failed() bool {
96+
func (t *TestingTB) Failed() bool {
7697
return t.HasFailed
7798
}
7899

100+
// Fatal mocks testing.T Fatal method.
101+
func (t *TestingTB) Fatal(args ...interface{}) {
102+
t.TestingT.Fatal(args...)
103+
}
104+
79105
// Fatalf mocks testing.T Fatalf method.
80-
func (t *TestingFT) Fatalf(format string, args ...interface{}) {
81-
t.Fatal(fmt.Sprintf(format, args...))
106+
func (t *TestingTB) Fatalf(format string, args ...interface{}) {
107+
t.TestingT.Fatal(fmt.Sprintf(format, args...))
108+
}
109+
110+
// Helper mocks testing.T Helper method.
111+
func (t *TestingTB) Helper() {
112+
// Do nothing
82113
}
83114

84115
// Log mocks testing.T Log method.
85-
func (t *TestingFT) Log(args ...interface{}) {}
116+
func (t *TestingTB) Log(args ...interface{}) {}
86117

87118
// Logf mocks testing.T Logf method.
88-
func (t *TestingFT) Logf(format string, args ...interface{}) {}
119+
func (t *TestingTB) Logf(format string, args ...interface{}) {}
89120

90121
// Name mocks testing.T Name method.
91-
func (t *TestingFT) Name() string {
122+
func (t *TestingTB) Name() string {
92123
return t.name
93124
}
94125

95126
// Skip mocks testing.T Skip method.
96-
func (t *TestingFT) Skip(args ...interface{}) {}
127+
func (t *TestingTB) Skip(args ...interface{}) {}
97128

98129
// SkipNow mocks testing.T SkipNow method.
99-
func (t *TestingFT) SkipNow() {}
130+
func (t *TestingTB) SkipNow() {}
100131

101132
// Skipf mocks testing.T Skipf method.
102-
func (t *TestingFT) Skipf(format string, args ...interface{}) {}
133+
func (t *TestingTB) Skipf(format string, args ...interface{}) {}
103134

104135
// Skipped mocks testing.T Skipped method.
105-
func (t *TestingFT) Skipped() bool {
136+
func (t *TestingTB) Skipped() bool {
106137
return false
107138
}
108-
109-
// Run mocks testing.T Run method.
110-
func (t *TestingFT) Run(name string, f func(t *testing.T)) bool {
111-
return true
112-
}

td/cmp_deeply_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,21 +89,21 @@ DATA: test error message
8989
}
9090

9191
func TestCmp(t *testing.T) {
92-
tt := test.NewTestingFT(t.Name())
92+
tt := test.NewTestingTB(t.Name())
9393
test.IsTrue(t, Cmp(tt, 1, 1))
9494
test.IsFalse(t, tt.Failed())
9595

96-
tt = test.NewTestingFT(t.Name())
96+
tt = test.NewTestingTB(t.Name())
9797
test.IsFalse(t, Cmp(tt, 1, 2))
9898
test.IsTrue(t, tt.Failed())
9999
}
100100

101101
func TestCmpDeeply(t *testing.T) {
102-
tt := test.NewTestingFT(t.Name())
102+
tt := test.NewTestingTB(t.Name())
103103
test.IsTrue(t, CmpDeeply(tt, 1, 1))
104104
test.IsFalse(t, tt.Failed())
105105

106-
tt = test.NewTestingFT(t.Name())
106+
tt = test.NewTestingTB(t.Name())
107107
test.IsFalse(t, CmpDeeply(tt, 1, 2))
108108
test.IsTrue(t, tt.Failed())
109109
}

td/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ type ContextConfig struct {
3838
MaxErrors int
3939
anchors *anchors.Info
4040
// FailureIsFatal allows to Fatal() (instead of Error()) when a test
41-
// fails. Using *testing.T instance as t.TestingFT value, FailNow()
41+
// fails. Using *testing.T or *testing.B instance as t.TB value, FailNow()
4242
// is called behind the scenes when Fatal() is called. See testing
4343
// documentation for details.
4444
FailureIsFatal bool

0 commit comments

Comments
 (0)