Skip to content

Commit

Permalink
Merge pull request #100 from maxatome/tb
Browse files Browse the repository at this point in the history
td.TestingFT → testing.TB
  • Loading branch information
maxatome authored May 31, 2020
2 parents d18326d + bb70138 commit 5b2d586
Show file tree
Hide file tree
Showing 15 changed files with 328 additions and 152 deletions.
16 changes: 8 additions & 8 deletions helpers/tdhttp/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,21 @@ type Response struct {
Body interface{} // Body is the expected body (expected to be empty if nil)
}

func cmpMarshaledResponse(tt td.TestingFT,
func cmpMarshaledResponse(tb testing.TB,
req *http.Request,
handler func(w http.ResponseWriter, r *http.Request),
acceptEmptyBody bool,
unmarshal func([]byte, interface{}) error,
expectedResp Response,
args ...interface{},
) bool {
tt.Helper()
tb.Helper()

if testName := tdutil.BuildTestName(args...); testName != "" {
tt.Log(testName)
tb.Log(testName)
}

t := td.NewT(tt)
t := td.NewT(tb)
defer t.AnchorsPersistTemporarily()()

ta := NewTestAPI(t, http.HandlerFunc(handler)).Request(req)
Expand Down Expand Up @@ -78,7 +78,7 @@ func cmpMarshaledResponse(tt td.TestingFT,
// It returns true if the tests succeed, false otherwise.
//
// See TestAPI type and its methods for more flexible tests.
func CmpMarshaledResponse(t td.TestingFT,
func CmpMarshaledResponse(t testing.TB,
req *http.Request,
handler func(w http.ResponseWriter, r *http.Request),
unmarshal func([]byte, interface{}) error,
Expand Down Expand Up @@ -118,7 +118,7 @@ func CmpMarshaledResponse(t td.TestingFT,
// a string.
//
// See TestAPI type and its methods for more flexible tests.
func CmpResponse(t td.TestingFT,
func CmpResponse(t testing.TB,
req *http.Request,
handler func(w http.ResponseWriter, r *http.Request),
expectedResp Response,
Expand Down Expand Up @@ -187,7 +187,7 @@ func CmpResponse(t td.TestingFT,
// explicitly.
//
// See TestAPI type and its methods for more flexible tests.
func CmpJSONResponse(t td.TestingFT,
func CmpJSONResponse(t testing.TB,
req *http.Request,
handler func(w http.ResponseWriter, r *http.Request),
expectedResp Response,
Expand Down Expand Up @@ -240,7 +240,7 @@ func CmpJSONResponse(t td.TestingFT,
// explicitly.
//
// See TestAPI type and its methods for more flexible tests.
func CmpXMLResponse(t td.TestingFT,
func CmpXMLResponse(t testing.TB,
req *http.Request,
handler func(w http.ResponseWriter, r *http.Request),
expectedResp Response,
Expand Down
14 changes: 7 additions & 7 deletions helpers/tdhttp/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,12 @@ func TestCmpResponse(tt *testing.T) {
},
},
} {
t.RunT(curTest.Name,
t.Run(curTest.Name,
func(t *td.T) {
testCmpResponse(t, tdhttp.CmpResponse, "CmpResponse", curTest)
})

t.RunT(curTest.Name+" TestAPI",
t.Run(curTest.Name+" TestAPI",
func(t *td.T) {
testTestAPI(t, (*tdhttp.TestAPI).CmpBody, "CmpBody", curTest)
})
Expand Down Expand Up @@ -265,12 +265,12 @@ func TestCmpJSONResponse(tt *testing.T) {
},
},
} {
t.RunT(curTest.Name,
t.Run(curTest.Name,
func(t *td.T) {
testCmpResponse(t, tdhttp.CmpJSONResponse, "CmpJSONResponse", curTest)
})

t.RunT(curTest.Name+" TestAPI",
t.Run(curTest.Name+" TestAPI",
func(t *td.T) {
testTestAPI(t, (*tdhttp.TestAPI).CmpJSONBody, "CmpJSONBody", curTest)
})
Expand Down Expand Up @@ -409,12 +409,12 @@ func TestCmpXMLResponse(tt *testing.T) {
},
},
} {
t.RunT(curTest.Name,
t.Run(curTest.Name,
func(t *td.T) {
testCmpResponse(t, tdhttp.CmpXMLResponse, "CmpXMLResponse", curTest)
})

t.RunT(curTest.Name+" TestAPI",
t.Run(curTest.Name+" TestAPI",
func(t *td.T) {
testTestAPI(t, (*tdhttp.TestAPI).CmpXMLBody, "CmpXMLBody", curTest)
})
Expand Down Expand Up @@ -445,7 +445,7 @@ func testLogs(t *td.T, mockT *tdutil.T, curTest CmpResponseTest) {
}

func testCmpResponse(t *td.T,
cmp func(td.TestingFT, *http.Request, func(http.ResponseWriter, *http.Request), tdhttp.Response, ...interface{}) bool,
cmp func(testing.TB, *http.Request, func(http.ResponseWriter, *http.Request), tdhttp.Response, ...interface{}) bool,
cmpName string,
curTest CmpResponseTest,
) {
Expand Down
18 changes: 9 additions & 9 deletions helpers/tdhttp/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
func TestNewRequest(tt *testing.T) {
t := td.NewT(tt)

t.RunT("NewRequest", func(t *td.T) {
t.Run("NewRequest", func(t *td.T) {
req := tdhttp.NewRequest("GET", "/path", nil,
"Foo", "Bar",
"Zip", "Test")
Expand All @@ -30,7 +30,7 @@ func TestNewRequest(tt *testing.T) {
})
})

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

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

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

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

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

t.RunT("NewJSONRequest", func(t *td.T) {
t.Run("NewJSONRequest", func(t *td.T) {
req := tdhttp.NewJSONRequest("GET", "/path",
TestStruct{
Name: "Bob",
Expand All @@ -194,7 +194,7 @@ func TestNewJSONRequest(tt *testing.T) {
}
})

t.RunT("NewJSONRequest panic", func(t *td.T) {
t.Run("NewJSONRequest panic", func(t *td.T) {
t.CmpPanic(
func() { tdhttp.NewJSONRequest("GET", "/path", func() {}) },
td.NotEmpty(),
Expand Down Expand Up @@ -261,7 +261,7 @@ func TestNewJSONRequest(tt *testing.T) {
func TestNewXMLRequest(tt *testing.T) {
t := td.NewT(tt)

t.RunT("NewXMLRequest", func(t *td.T) {
t.Run("NewXMLRequest", func(t *td.T) {
req := tdhttp.NewXMLRequest("GET", "/path",
TestStruct{
Name: "Bob",
Expand All @@ -279,7 +279,7 @@ func TestNewXMLRequest(tt *testing.T) {
}
})

t.RunT("NewXMLRequest panic", func(t *td.T) {
t.Run("NewXMLRequest panic", func(t *td.T) {
t.CmpPanic(
func() { tdhttp.NewXMLRequest("GET", "/path", func() {}) },
td.NotEmpty(),
Expand Down
5 changes: 3 additions & 2 deletions helpers/tdhttp/test_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"reflect"
"runtime"
"strings"
"testing"
"time"

"github.com/maxatome/go-testdeep/helpers/tdutil"
Expand Down Expand Up @@ -57,9 +58,9 @@ type TestAPI struct {
// ta.Get("/ping").
// CmpStatus(200).
// CmpBody("pong")
func NewTestAPI(tt td.TestingFT, handler http.Handler) *TestAPI {
func NewTestAPI(tb testing.TB, handler http.Handler) *TestAPI {
return &TestAPI{
t: td.NewT(tt),
t: td.NewT(tb),
handler: handler,
}
}
Expand Down
76 changes: 51 additions & 25 deletions internal/test/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package test

import (
"fmt"
"runtime"
"testing"
)

Expand Down Expand Up @@ -43,70 +44,95 @@ func (t *TestingT) Helper() {
// Do nothing
}

// TestingFT is a type implementing td.TestingFT intended to be used
// in tests.
type TestingFT struct {
// TestingTB is a type implementing testing.TB intended to be used in
// tests.
type TestingTB struct {
TestingT
name string
testing.TB
cleanup func()
}

// NewTestingTB returns a new instance of *TestingTB.
func NewTestingTB(name string) *TestingTB {
return &TestingTB{name: name}
}

// Cleanup mocks testing.T Cleanup method. Not thread-safe but we
// don't care in tests.
func (t *TestingTB) Cleanup(fn func()) {
old := t.cleanup
t.cleanup = func() {
if old != nil {
defer old()
}
fn()
}
runtime.SetFinalizer(t, func(t *TestingTB) { t.cleanup() })
}

// NewTestingFT returns a new instance of *TestingFT.
func NewTestingFT(name string) *TestingFT {
return &TestingFT{name: name}
// Fatal mocks testing.T Error method.
func (t *TestingTB) Error(args ...interface{}) {
t.TestingT.Error(args...)
}

// Errorf mocks testing.T Errorf method.
func (t *TestingFT) Errorf(format string, args ...interface{}) {
t.Error(fmt.Sprintf(format, args...))
func (t *TestingTB) Errorf(format string, args ...interface{}) {
t.TestingT.Error(fmt.Sprintf(format, args...))
}

// Fail mocks testing.T Fail method.
func (t *TestingFT) Fail() {
func (t *TestingTB) Fail() {
t.HasFailed = true
}

// FailNow mocks testing.T FailNow method.
func (t *TestingFT) FailNow() {
func (t *TestingTB) FailNow() {
t.HasFailed = true
t.IsFatal = true
}

// Failed mocks testing.T Failed method.
func (t *TestingFT) Failed() bool {
func (t *TestingTB) Failed() bool {
return t.HasFailed
}

// Fatal mocks testing.T Fatal method.
func (t *TestingTB) Fatal(args ...interface{}) {
t.TestingT.Fatal(args...)
}

// Fatalf mocks testing.T Fatalf method.
func (t *TestingFT) Fatalf(format string, args ...interface{}) {
t.Fatal(fmt.Sprintf(format, args...))
func (t *TestingTB) Fatalf(format string, args ...interface{}) {
t.TestingT.Fatal(fmt.Sprintf(format, args...))
}

// Helper mocks testing.T Helper method.
func (t *TestingTB) Helper() {
// Do nothing
}

// Log mocks testing.T Log method.
func (t *TestingFT) Log(args ...interface{}) {}
func (t *TestingTB) Log(args ...interface{}) {}

// Logf mocks testing.T Logf method.
func (t *TestingFT) Logf(format string, args ...interface{}) {}
func (t *TestingTB) Logf(format string, args ...interface{}) {}

// Name mocks testing.T Name method.
func (t *TestingFT) Name() string {
func (t *TestingTB) Name() string {
return t.name
}

// Skip mocks testing.T Skip method.
func (t *TestingFT) Skip(args ...interface{}) {}
func (t *TestingTB) Skip(args ...interface{}) {}

// SkipNow mocks testing.T SkipNow method.
func (t *TestingFT) SkipNow() {}
func (t *TestingTB) SkipNow() {}

// Skipf mocks testing.T Skipf method.
func (t *TestingFT) Skipf(format string, args ...interface{}) {}
func (t *TestingTB) Skipf(format string, args ...interface{}) {}

// Skipped mocks testing.T Skipped method.
func (t *TestingFT) Skipped() bool {
func (t *TestingTB) Skipped() bool {
return false
}

// Run mocks testing.T Run method.
func (t *TestingFT) Run(name string, f func(t *testing.T)) bool {
return true
}
8 changes: 4 additions & 4 deletions td/cmp_deeply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,21 @@ DATA: test error message
}

func TestCmp(t *testing.T) {
tt := test.NewTestingFT(t.Name())
tt := test.NewTestingTB(t.Name())
test.IsTrue(t, Cmp(tt, 1, 1))
test.IsFalse(t, tt.Failed())

tt = test.NewTestingFT(t.Name())
tt = test.NewTestingTB(t.Name())
test.IsFalse(t, Cmp(tt, 1, 2))
test.IsTrue(t, tt.Failed())
}

func TestCmpDeeply(t *testing.T) {
tt := test.NewTestingFT(t.Name())
tt := test.NewTestingTB(t.Name())
test.IsTrue(t, CmpDeeply(tt, 1, 1))
test.IsFalse(t, tt.Failed())

tt = test.NewTestingFT(t.Name())
tt = test.NewTestingTB(t.Name())
test.IsFalse(t, CmpDeeply(tt, 1, 2))
test.IsTrue(t, tt.Failed())
}
2 changes: 1 addition & 1 deletion td/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type ContextConfig struct {
MaxErrors int
anchors *anchors.Info
// FailureIsFatal allows to Fatal() (instead of Error()) when a test
// fails. Using *testing.T instance as t.TestingFT value, FailNow()
// fails. Using *testing.T or *testing.B instance as t.TB value, FailNow()
// is called behind the scenes when Fatal() is called. See testing
// documentation for details.
FailureIsFatal bool
Expand Down
Loading

0 comments on commit 5b2d586

Please sign in to comment.