Skip to content
This repository has been archived by the owner on Aug 29, 2022. It is now read-only.

Commit

Permalink
Use wrapped *testing.T
Browse files Browse the repository at this point in the history
  • Loading branch information
nhatthm committed Apr 19, 2021
1 parent cf68c92 commit e8a03be
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
18 changes: 13 additions & 5 deletions assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,28 @@ import (
"github.com/stretchr/testify/assert"
)

type testingT struct {
// TestingT is an interface wrapper around *testing.T.
type TestingT interface {
Errorf(format string, args ...interface{})
FailNow()
Log(args ...interface{})
Logf(format string, args ...interface{})
}

type tError struct {
err error
}

func (t *testingT) Errorf(format string, args ...interface{}) {
func (t *tError) Errorf(format string, args ...interface{}) {
t.err = fmt.Errorf(format, args...) // nolint: goerr113
}

func (t *testingT) LastError() error {
func (t *tError) LastError() error {
return t.err
}

func t() *testingT {
return &testingT{}
func teeError() *tError {
return &tError{}
}

// AssertState asserts console state.
Expand Down
6 changes: 3 additions & 3 deletions assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
func TestTestingT_LastError(t *testing.T) {
t.Parallel()

newT := &testingT{}
newT.Errorf("error: %s", "unknown")
tee := teeError()
tee.Errorf("error: %s", "unknown")

assert.EqualError(t, newT.LastError(), `error: unknown`)
assert.EqualError(t, tee.LastError(), `error: unknown`)
}
7 changes: 3 additions & 4 deletions manager.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package consoledog

import (
"testing"
"time"

"github.com/Netflix/go-expect"
Expand All @@ -21,7 +20,7 @@ type Option func(m *Manager)

// Manager manages console and its state.
type Manager struct {
test *testing.T
test TestingT

console *expect.Console
state *vt10x.State
Expand Down Expand Up @@ -98,7 +97,7 @@ func (m *Manager) Flush() {
func (m *Manager) isConsoleOutput(expected *godog.DocString) error {
m.Flush()

t := t()
t := teeError()
AssertState(t, m.state, expected.Content)

return t.LastError()
Expand All @@ -119,7 +118,7 @@ func (m *Manager) WithCloser(c Closer) *Manager {
}

// New initiates a new console Manager.
func New(t *testing.T, options ...Option) *Manager { // nolint: thelper
func New(t TestingT, options ...Option) *Manager {
m := &Manager{
test: t,
}
Expand Down

0 comments on commit e8a03be

Please sign in to comment.