Skip to content

Commit

Permalink
Add additional methods to GinkgoT() to improve compatibility with the…
Browse files Browse the repository at this point in the history
… testing.TB interface
  • Loading branch information
onsi committed Sep 9, 2020
1 parent 6d83527 commit b5fe44d
Show file tree
Hide file tree
Showing 4 changed files with 216 additions and 11 deletions.
18 changes: 14 additions & 4 deletions ginkgo_dsl.go
Expand Up @@ -93,26 +93,36 @@ func GinkgoT(optionalOffset ...int) GinkgoTInterface {
if len(optionalOffset) > 0 {
offset = optionalOffset[0]
}
return testingtproxy.New(GinkgoWriter, Fail, offset)
failedFunc := func() bool {
return CurrentGinkgoTestDescription().Failed
}
nameFunc := func() string {
return CurrentGinkgoTestDescription().FullTestText
}
return testingtproxy.New(GinkgoWriter, Fail, Skip, failedFunc, nameFunc, offset)
}

//The interface returned by GinkgoT(). This covers most of the methods
//in the testing package's T.
type GinkgoTInterface interface {
Fail()
Cleanup(func())
Error(args ...interface{})
Errorf(format string, args ...interface{})
Fail()
FailNow()
Failed() bool
Fatal(args ...interface{})
Fatalf(format string, args ...interface{})
Helper()
Log(args ...interface{})
Logf(format string, args ...interface{})
Failed() bool
Name() string
Parallel()
Skip(args ...interface{})
Skipf(format string, args ...interface{})
SkipNow()
Skipf(format string, args ...interface{})
Skipped() bool
TempDir() string
}

//Custom Ginkgo test reporters must implement the Reporter interface.
Expand Down
42 changes: 35 additions & 7 deletions internal/testingtproxy/testing_t_proxy.go
Expand Up @@ -6,21 +6,34 @@ import (
)

type failFunc func(message string, callerSkip ...int)
type skipFunc func(message string, callerSkip ...int)
type failedFunc func() bool
type nameFunc func() string

func New(writer io.Writer, fail failFunc, offset int) *ginkgoTestingTProxy {
func New(writer io.Writer, fail failFunc, skip skipFunc, failed failedFunc, name nameFunc, offset int) *ginkgoTestingTProxy {
return &ginkgoTestingTProxy{
fail: fail,
offset: offset,
writer: writer,
skip: skip,
failed: failed,
name: name,
}
}

type ginkgoTestingTProxy struct {
fail failFunc
skip skipFunc
failed failedFunc
name nameFunc
offset int
writer io.Writer
}

func (t *ginkgoTestingTProxy) Cleanup(func()) {
// No-op
}

func (t *ginkgoTestingTProxy) Error(args ...interface{}) {
t.fail(fmt.Sprintln(args...), t.offset)
}
Expand All @@ -37,6 +50,10 @@ func (t *ginkgoTestingTProxy) FailNow() {
t.fail("failed", t.offset)
}

func (t *ginkgoTestingTProxy) Failed() bool {
return t.failed()
}

func (t *ginkgoTestingTProxy) Fatal(args ...interface{}) {
t.fail(fmt.Sprintln(args...), t.offset)
}
Expand All @@ -45,6 +62,10 @@ func (t *ginkgoTestingTProxy) Fatalf(format string, args ...interface{}) {
t.fail(fmt.Sprintf(format, args...), t.offset)
}

func (t *ginkgoTestingTProxy) Helper() {
// No-op
}

func (t *ginkgoTestingTProxy) Log(args ...interface{}) {
fmt.Fprintln(t.writer, args...)
}
Expand All @@ -53,24 +74,31 @@ func (t *ginkgoTestingTProxy) Logf(format string, args ...interface{}) {
t.Log(fmt.Sprintf(format, args...))
}

func (t *ginkgoTestingTProxy) Failed() bool {
return false
func (t *ginkgoTestingTProxy) Name() string {
return t.name()
}

func (t *ginkgoTestingTProxy) Parallel() {
// No-op
}

func (t *ginkgoTestingTProxy) Skip(args ...interface{}) {
fmt.Println(args...)
t.skip(fmt.Sprintln(args...), t.offset)
}

func (t *ginkgoTestingTProxy) Skipf(format string, args ...interface{}) {
t.Skip(fmt.Sprintf(format, args...))
func (t *ginkgoTestingTProxy) SkipNow() {
t.skip("skip", t.offset)
}

func (t *ginkgoTestingTProxy) SkipNow() {
func (t *ginkgoTestingTProxy) Skipf(format string, args ...interface{}) {
t.skip(fmt.Sprintf(format, args...), t.offset)
}

func (t *ginkgoTestingTProxy) Skipped() bool {
return false
}

func (t *ginkgoTestingTProxy) TempDir() string {
// No-op
return ""
}
13 changes: 13 additions & 0 deletions internal/testingtproxy/testingtproxy_suite_test.go
@@ -0,0 +1,13 @@
package testingtproxy_test

import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func TestTestingtproxy(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Testingtproxy Suite")
}
154 changes: 154 additions & 0 deletions internal/testingtproxy/testingtproxy_test.go
@@ -0,0 +1,154 @@
package testingtproxy_test

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"github.com/onsi/gomega/gbytes"

"github.com/onsi/ginkgo/internal/testingtproxy"
)

type messagedCall struct {
message string
callerSkip []int
}

var _ = Describe("Testingtproxy", func() {
var t GinkgoTInterface

var failFunc func(message string, callerSkip ...int)
var skipFunc func(message string, callerSkip ...int)
var failedFunc func() bool
var nameFunc func() string

var nameToReturn string
var failedToReturn bool
var failFuncCall messagedCall
var skipFuncCall messagedCall
var offset int
var buf *gbytes.Buffer

BeforeEach(func() {
failFuncCall = messagedCall{}
skipFuncCall = messagedCall{}
nameToReturn = ""
failedToReturn = false
offset = 3

failFunc = func(message string, callerSkip ...int) {
failFuncCall.message = message
failFuncCall.callerSkip = callerSkip
}

skipFunc = func(message string, callerSkip ...int) {
skipFuncCall.message = message
skipFuncCall.callerSkip = callerSkip
}

failedFunc = func() bool {
return failedToReturn
}

nameFunc = func() string {
return nameToReturn
}

buf = gbytes.NewBuffer()

t = testingtproxy.New(buf, failFunc, skipFunc, failedFunc, nameFunc, offset)
})

It("ignores Cleanup", func() {
GinkgoT().Cleanup(func() {
panic("bam!")
}) //is a no-op
})

It("supports Error", func() {
t.Error("a", 17)
Ω(failFuncCall.message).Should(Equal("a 17\n"))
Ω(failFuncCall.callerSkip).Should(Equal([]int{offset}))
})

It("supports Errorf", func() {
t.Errorf("%s %d!", "a", 17)
Ω(failFuncCall.message).Should(Equal("a 17!"))
Ω(failFuncCall.callerSkip).Should(Equal([]int{offset}))
})

It("supports Fail", func() {
t.Fail()
Ω(failFuncCall.message).Should(Equal("failed"))
Ω(failFuncCall.callerSkip).Should(Equal([]int{offset}))
})

It("supports FailNow", func() {
t.Fail()
Ω(failFuncCall.message).Should(Equal("failed"))
Ω(failFuncCall.callerSkip).Should(Equal([]int{offset}))
})

It("supports Fatal", func() {
t.Fatal("a", 17)
Ω(failFuncCall.message).Should(Equal("a 17\n"))
Ω(failFuncCall.callerSkip).Should(Equal([]int{offset}))
})

It("supports Fatalf", func() {
t.Fatalf("%s %d!", "a", 17)
Ω(failFuncCall.message).Should(Equal("a 17!"))
Ω(failFuncCall.callerSkip).Should(Equal([]int{offset}))
})

It("ignores Helper", func() {
GinkgoT().Helper() //is a no-op
})

It("supports Log", func() {
t.Log("a", 17)
Ω(string(buf.Contents())).Should(Equal("a 17\n"))
})

It("supports Logf", func() {
t.Logf("%s %d!", "a", 17)
Ω(string(buf.Contents())).Should(Equal("a 17!\n"))
})

It("supports Name", func() {
nameToReturn = "C.S. Lewis"
Ω(t.Name()).Should(Equal("C.S. Lewis"))

Ω(GinkgoT().Name()).Should(ContainSubstring("supports Name"))
})

It("ignores Parallel", func() {
GinkgoT().Parallel() //is a no-op
})

It("supports Skip", func() {
t.Skip("a", 17)
Ω(skipFuncCall.message).Should(Equal("a 17\n"))
Ω(skipFuncCall.callerSkip).Should(Equal([]int{offset}))
})

It("supports SkipNow", func() {
t.SkipNow()
Ω(skipFuncCall.message).Should(Equal("skip"))
Ω(skipFuncCall.callerSkip).Should(Equal([]int{offset}))
})

It("supports Skipf", func() {
t.Skipf("%s %d!", "a", 17)
Ω(skipFuncCall.message).Should(Equal("a 17!"))
Ω(skipFuncCall.callerSkip).Should(Equal([]int{offset}))
})

It("always returns false for Skipped", func() {
Ω(GinkgoT().Skipped()).Should(BeFalse())
})

It("returns empty string for TempDir", func() {
Ω(GinkgoT().TempDir()).Should(Equal(""))
})
})

0 comments on commit b5fe44d

Please sign in to comment.