Skip to content

Commit

Permalink
Merge 5f7be05 into cea1e8e
Browse files Browse the repository at this point in the history
  • Loading branch information
philpennock committed Oct 30, 2016
2 parents cea1e8e + 5f7be05 commit 026bd38
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
24 changes: 23 additions & 1 deletion expect.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package testlib

import (
"fmt"
"strings"
)

Expand All @@ -30,7 +31,17 @@ func (t *T) ExpectError(err error, desc ...string) {
if len(desc) > 0 {
prefix = strings.Join(desc, " ") + ": "
}
t.Fatalf("%sExpected error was not returned.", prefix)
t.Fatalf("%sError not returned when one was expected.", prefix)
}

// ExpectErrorf checks if the given error object is non-nil and if it is
// not then it will Fatalf the test with a message formed by *f formatting.
func (t *T) ExpectErrorf(err error, spec string, args ...interface{}) {
if err != nil {
return
}
prefix := fmt.Sprintf(spec, args...) + ": "
t.Fatalf("%sError not returned when one was expected.", prefix)
}

// Checks to see that the given error object is nil. This is handy for
Expand All @@ -47,6 +58,17 @@ func (t *T) ExpectSuccess(err error, desc ...string) {
prefix, err, err.Error())
}

// ExpectSuccessf checks that the given error object is nil. If non-nil
// then report as a test failure.
func (t *T) ExpectSuccessf(err error, spec string, args ...interface{}) {
if err == nil {
return
}
prefix := fmt.Sprintf(spec, args...) + ": "
t.Fatalf("%sUnexpected error encountered: %#v (%s)",
prefix, err, err.Error())
}

// Fails if the error message does not contain the given string.
func (t *T) ExpectErrorMessage(err error, msg string, desc ...string) {
prefix := ""
Expand Down
36 changes: 36 additions & 0 deletions expect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ func TestT_ExpectError(t *testing.T) {
}
}

func TestT_ExpectErrorf(t *testing.T) {
t.Parallel()
m, T := testSetup()

// Capture the error message.
msg := ""
m.funcFatal = func(args ...interface{}) {
msg = fmt.Sprint(args...)
}
m.CheckPass(t, func() { T.ExpectErrorf(fmt.Errorf("EXPECTED"), "foo %d", 2) })
m.CheckFail(t, func() { T.ExpectErrorf(nil, "foo %d", 3) })
if msg == "" {
t.Fatalf("No error message was reported.")
} else if !strings.HasPrefix(msg, "foo 3: ") {
t.Fatalf("The prefix was not prepended to the message: '''%s'''", msg)
}
}

func TestT_ExpectSuccess(t *testing.T) {
t.Parallel()
m, T := testSetup()
Expand All @@ -56,6 +74,24 @@ func TestT_ExpectSuccess(t *testing.T) {
}
}

func TestT_ExpectSuccessf(t *testing.T) {
t.Parallel()
m, T := testSetup()

// Capture the error message.
msg := ""
m.funcFatal = func(args ...interface{}) {
msg = fmt.Sprint(args...)
}
m.CheckPass(t, func() { T.ExpectSuccessf(nil, "foo %d", 4) })
m.CheckFail(t, func() { T.ExpectSuccessf(fmt.Errorf("ERROR"), "foo %d", 5) })
if msg == "" {
t.Fatalf("No error message was reported.")
} else if !strings.HasPrefix(msg, "foo 5: ") {
t.Fatalf("The prefix was not prepended to the message: '''%s'''", msg)
}
}

func TestT_ExpectErrorMessage(t *testing.T) {
t.Parallel()
m, T := testSetup()
Expand Down

0 comments on commit 026bd38

Please sign in to comment.