Skip to content

Commit

Permalink
Rename the TestLib structure to T
Browse files Browse the repository at this point in the history
This matches the testing.T library a little closer and makes it easier to
pass arguments around withing helper functions.
  • Loading branch information
Brady Catherman committed Aug 29, 2014
1 parent 5642964 commit 56e763c
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 34 deletions.
4 changes: 2 additions & 2 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
// Examples
//
// Note that the traditional Go Examples functionality is hindered since
// calls to TestLib assume that you are starting in a unit test (and therefor
// calls to T assume that you are starting in a unit test (and therefor
// have access to a *testing.T object). What follows are some simple pasted
// examples showing how the code can look.
//
Expand All @@ -71,7 +71,7 @@
// returned an error, and Equal() will Fatalf if the read value does not match
// what we write.
// func TestIoReadAll(t *testing.T) {
// T := testlib.NewTestLib(t)
// T := testlib.NewT(t)
// defer T.Finish()
// tempFile := T.WriteTempFile("contents")
// contents, err := ioutil.ReadFile(tempFile)
Expand Down
8 changes: 4 additions & 4 deletions equal.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
// Compares two values to ensure that they are equal to each other. This will
// deep inspect both values to ensure that the full structure tree is equal.
// It also walks through pointers ensuring that everything is equal.
func (t *TestLib) Equal(have, want interface{}, desc ...string) {
func (t *T) Equal(have, want interface{}, desc ...string) {
prefix := ""
if len(desc) > 0 {
prefix = strings.Join(desc, " ") + ": "
Expand Down Expand Up @@ -56,7 +56,7 @@ func (t *TestLib) Equal(have, want interface{}, desc ...string) {

// Like Equal() except that it asserts that the two values are not equal
// to each other.
func (t *TestLib) NotEqual(have, want interface{}, desc ...string) {
func (t *T) NotEqual(have, want interface{}, desc ...string) {
prefix := ""
if len(desc) > 0 {
prefix = strings.Join(desc, " ") + ": "
Expand Down Expand Up @@ -91,7 +91,7 @@ type visitedNode struct {
}

// Returns true if the underlying object is nil.
func (t *TestLib) isNil(obj interface{}) bool {
func (t *T) isNil(obj interface{}) bool {
if obj == nil {
return true
}
Expand All @@ -109,7 +109,7 @@ func (t *TestLib) isNil(obj interface{}) bool {
}

// Deep comparison. This is based on golang 1.2's reflect.Equal functionality.
func (t *TestLib) deepEqual(
func (t *T) deepEqual(
desc string, have, want reflect.Value, visited map[uintptr]*visitedNode,
) (diffs []string) {
if !want.IsValid() && !have.IsValid() {
Expand Down
8 changes: 4 additions & 4 deletions equal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import (

type testEqualCutomType string

func TestTestLibEqual(t *testing.T) {
func TestTEqual(t *testing.T) {
m := &mockT{}
T := NewTestLib(m)
T := NewT(m)

var nilPtr *mockT
strSlice1 := []string{"A", "B", "C"}
Expand Down Expand Up @@ -101,9 +101,9 @@ func TestTestLibEqual(t *testing.T) {
m.CheckFail(t, func() { T.Equal(strMap1, strMap3) })
}

func TestTestLibNotEqual(t *testing.T) {
func TestTNotEqual(t *testing.T) {
m := &mockT{}
T := NewTestLib(m)
T := NewT(m)

var nilPtr *mockT
strSlice1 := []string{"A", "B", "C"}
Expand Down
4 changes: 2 additions & 2 deletions expect.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

// This call will check that the given error object is non nil and if it is
// not it will automatically Fatalf the test with a message.
func (t *TestLib) ExpectError(err error, desc ...string) {
func (t *T) ExpectError(err error, desc ...string) {
if err != nil {
return
}
Expand All @@ -35,7 +35,7 @@ func (t *TestLib) ExpectError(err error, desc ...string) {

// Checks to see that the given error object is nil. This is handy for
// performing quick checks on calls that are expected to work.
func (t *TestLib) ExpectSuccess(err error, desc ...string) {
func (t *T) ExpectSuccess(err error, desc ...string) {
if err == nil {
return
}
Expand Down
14 changes: 7 additions & 7 deletions files.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
//
// Files in this directory are cleaned up by a child process that is forked
// from the running process so that nothing can stop them from being cleaned.
func (t *TestLib) RootTempDir() string {
func (t *T) RootTempDir() string {
testLibRootDirOnce.Do(func() {
var err error
var reader *os.File
Expand All @@ -58,7 +58,7 @@ func (t *TestLib) RootTempDir() string {
// Creates a temporary directory for this specific test which will be cleaned
// once the test has finished executing. This calls RootTempDir() to create the
// base directory.
func (t *TestLib) TempDirMode(mode os.FileMode) string {
func (t *T) TempDirMode(mode os.FileMode) string {
f, err := ioutil.TempDir(t.RootTempDir(), t.Name())
t.ExpectSuccess(err)
t.NotEqual(f, "")
Expand All @@ -70,13 +70,13 @@ func (t *TestLib) TempDirMode(mode os.FileMode) string {
}

// Like TempDirMode except this sets the default mode to 0755.
func (t *TestLib) TempDir() string {
func (t *T) TempDir() string {
return t.TempDirMode(os.FileMode(0755))
}

// Creates a temporary file in a temporary directory with a specific mode
// set on it. This will return the file descriptor of the open file.
func (t *TestLib) TempFileMode(mode os.FileMode) *os.File {
func (t *T) TempFileMode(mode os.FileMode) *os.File {
f, err := ioutil.TempFile(t.RootTempDir(), t.Name())
t.ExpectSuccess(err)
t.ExpectSuccess(os.Chmod(f.Name(), mode))
Expand All @@ -88,13 +88,13 @@ func (t *TestLib) TempFileMode(mode os.FileMode) *os.File {
}

// Like TempFileMode except that it uses a default mode of 0644.
func (t *TestLib) TempFile() *os.File {
func (t *T) TempFile() *os.File {
return t.TempFileMode(os.FileMode(0644))
}

// Makes a temporary file with the given string as contents. This returns
// the name of the created file.
func (t *TestLib) WriteTempFileMode(contents string, mode os.FileMode) string {
func (t *T) WriteTempFileMode(contents string, mode os.FileMode) string {
f := t.TempFileMode(mode)
name := f.Name()
_, err := io.WriteString(f, contents)
Expand All @@ -104,7 +104,7 @@ func (t *TestLib) WriteTempFileMode(contents string, mode os.FileMode) string {
}

// Like WriteTempFileMode except this uses the default temp file mode.
func (t *TestLib) WriteTempFile(contents string) string {
func (t *T) WriteTempFile(contents string) string {
return t.WriteTempFileMode(contents, 0644)
}

Expand Down
28 changes: 14 additions & 14 deletions testlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ type testingTB interface {
// This is the tracking object used to store various variables and bits needed
// for tracking the test. Each test should create this object and then destroy
// it when the test finishes by using a defer() call.
type TestLib struct {
type T struct {
// This stores a reference to the testing.T or testing.B object used to
// create the initial test.
t testingTB

// This is the name of the test (function name) that created this TestLib
// This is the name of the test (function name) that created this T
// instance.
name string

Expand All @@ -61,14 +61,14 @@ type TestLib struct {
}

// This should be called when the test is started. It will initialize a
// TestLib instance for the specific test.
func NewTestLib(t testingTB) *TestLib {
return &TestLib{t: t}
// T instance for the specific test.
func NewT(t testingTB) *T {
return &T{t: t}
}

// This function should be immediately added as a defer after initializing
// the TestLib structure. This will clean up after the test.
func (t *TestLib) Finish() {
// the T structure. This will clean up after the test.
func (t *T) Finish() {
for i := len(t.finalizers) - 1; i >= 0; i-- {
t.finalizers[i]()
}
Expand All @@ -79,14 +79,14 @@ func (t *TestLib) Finish() {
// This adds a function that will be called once the test completes. The
// functions are called when the test finishes in reverse order from how
// they were added.
func (t *TestLib) AddFinalizer(f func()) {
func (t *T) AddFinalizer(f func()) {
t.finalizers = append(t.finalizers, f)
}

// This call will make a stack trace message for the Fatal/Fatalf and
// Error/Errorf function calls. This will insert "msg" at the top of the
// stack and return a string.
func (t *TestLib) makeStack(msg string) string {
func (t *T) makeStack(msg string) string {
lines := make([]string, 0, 100)
lines = append(lines, msg)

Expand Down Expand Up @@ -114,33 +114,33 @@ func (t *TestLib) makeStack(msg string) string {

// Wraps the testing.T.Error function call in order to provide full stack
// traces around the error being reported rather than just the calling line.
func (t *TestLib) Error(args ...interface{}) {
func (t *T) Error(args ...interface{}) {
t.t.Error(t.makeStack(fmt.Sprint(args...)))
}

// Like Error() but for formatted strings.
func (t *TestLib) Errorf(format string, args ...interface{}) {
func (t *T) Errorf(format string, args ...interface{}) {
t.t.Error(t.makeStack(fmt.Sprintf(format, args...)))
}

// This is a wrapper around Fatal in order to provide much nicer output.
// Specifically this will output a full stack trace rather than just the
// failing line. This is optimal because it makes debugging when loops
// or helper functions are used far easier.
func (t *TestLib) Fatal(args ...interface{}) {
func (t *T) Fatal(args ...interface{}) {
// TODO: Add pre-failure helper functions.
t.t.Fatal(t.makeStack(fmt.Sprint(args...)))
}

// Like Fatal() except for formatted strings.
func (t *TestLib) Fatalf(format string, args ...interface{}) {
func (t *T) Fatalf(format string, args ...interface{}) {
// TODO: Add pre-failure helper functions.
t.t.Fatal(t.makeStack(fmt.Sprintf(format, args...)))
}

// Gets the function name of the running test. This is useful since there is
// no other programatic way of finding out which test is running.
func (t *TestLib) Name() string {
func (t *T) Name() string {
// If we already calculated this then just return the cached value.
if t.name != "" {
return t.name
Expand Down
2 changes: 1 addition & 1 deletion timeout.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
// has elapsed. If 'f' returns true this will return, otherwise if 'f' returns
// false for the whole period then this will automatically call Fatal to
// terminate the test.
func (t *TestLib) TryUntil(
func (t *T) TryUntil(
f func() bool, timeout time.Duration, desc ...string,
) {
prefix := ""
Expand Down

0 comments on commit 56e763c

Please sign in to comment.